Ejemplo n.º 1
0
  protected void addAlias(ICompletionReporter reporter, String suffix) throws BadLocationException {
    if (aliasAdded) {
      return;
    }
    aliasAdded = true;
    ICompletionContext context = getContext();
    AbstractCompletionContext abstractContext = (AbstractCompletionContext) context;
    if (!abstractContext.getCompletionRequestor().isContextInformationMode()) {
      // get types for alias
      String prefix = abstractContext.getPrefixWithoutProcessing();
      boolean exactMatch = false;
      if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) == 0) {
        return;
      } else if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
        prefix = prefix.substring(0, prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR));
        exactMatch = true;
      } else {

      }

      if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) < 0) {
        IModuleSource module = reporter.getModule();
        org.eclipse.dltk.core.ISourceModule sourceModule =
            (org.eclipse.dltk.core.ISourceModule) module.getModelElement();
        ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule);
        final int offset = abstractContext.getOffset();
        IType namespace = PHPModelUtils.getCurrentNamespace(sourceModule, offset);

        final Map<String, UsePart> result =
            PHPModelUtils.getAliasToNSMap(prefix, moduleDeclaration, offset, namespace, exactMatch);
        reportAlias(reporter, suffix, abstractContext, module, result);
      }
    }
  }
Ejemplo n.º 2
0
  public void apply(ICompletionReporter reporter) throws BadLocationException {

    ICompletionContext context = getContext();
    AbstractCompletionContext abstractContext = (AbstractCompletionContext) context;
    if (abstractContext.getPrefixWithoutProcessing().trim().length() == 0) {
      return;
    }
    SourceRange replacementRange = getReplacementRange(abstractContext);

    IType[] types = getTypes(abstractContext);
    // now we compute type suffix in PHPCompletionProposalCollector
    String suffix = ""; // $NON-NLS-1$
    String nsSuffix = getNSSuffix(abstractContext);

    for (IType type : types) {
      try {
        int flags = type.getFlags();
        reporter.reportType(
            type,
            PHPFlags.isNamespace(flags) ? nsSuffix : suffix,
            replacementRange,
            getExtraInfo());
      } catch (ModelException e) {
        PHPCorePlugin.log(e);
      }
    }
    addAlias(reporter, suffix);
  }
Ejemplo n.º 3
0
 public String getSuffix(AbstractCompletionContext abstractContext) {
   String nextWord = null;
   try {
     nextWord = abstractContext.getNextWord();
   } catch (BadLocationException e) {
     PHPCorePlugin.log(e);
   }
   return "::".equals(nextWord) ? "" : "::"; // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 }
Ejemplo n.º 4
0
  /**
   * Runs the query to retrieve all global types
   *
   * @param context
   * @return
   * @throws BadLocationException
   */
  protected IType[] getTypes(AbstractCompletionContext context) throws BadLocationException {

    String prefix = context.getPrefix();
    if (prefix.startsWith("$")) {
      return EMPTY;
    }

    IDLTKSearchScope scope = createSearchScope();
    if (context.getCompletionRequestor().isContextInformationMode()) {
      return PhpModelAccess.getDefault()
          .findTypes(prefix, MatchRule.EXACT, trueFlag, falseFlag, scope, null);
    }

    List<IType> result = new LinkedList<IType>();
    if (prefix.length() > 1 && prefix.toUpperCase().equals(prefix)) {
      // Search by camel-case
      IType[] types =
          PhpModelAccess.getDefault()
              .findTypes(prefix, MatchRule.CAMEL_CASE, trueFlag, falseFlag, scope, null);
      result.addAll(Arrays.asList(types));
    }
    IType[] types =
        PhpModelAccess.getDefault()
            .findTypes(null, prefix, MatchRule.PREFIX, trueFlag, falseFlag, scope, null);
    if (context instanceof NamespaceMemberContext) {
      for (IType type : types) {
        if (PHPModelUtils.getFullName(type).startsWith(prefix)) {
          result.add(type);
        }
      }
    } else {
      result.addAll(Arrays.asList(types));
    }

    return (IType[]) result.toArray(new IType[result.size()]);
  }
 public String getSuffix(AbstractCompletionContext context) {
   return context.hasWhitespaceBeforeCursor() ? " " : ""; // $NON-NLS-1$ //$NON-NLS-2$
 }
Ejemplo n.º 6
0
  /**
   * Adds the self function with the relevant data to the proposals array
   *
   * @param context
   * @param reporter
   * @throws BadLocationException
   */
  protected void addSelf(AbstractCompletionContext context, ICompletionReporter reporter)
      throws BadLocationException {

    String prefix = context.getPrefix();
    SourceRange replaceRange = getReplacementRange(context);

    if (CodeAssistUtils.startsWithIgnoreCase("self", prefix)) {
      if (!context.getCompletionRequestor().isContextInformationMode()
          || prefix.length() == 4) { // "self".length()

        String suffix = getSuffix(context);

        // get the class data for "self". In case of null, the self
        // function will not be added
        IType selfClassData =
            CodeAssistUtils.getSelfClassData(context.getSourceModule(), context.getOffset());
        if (selfClassData != null) {
          try {
            IMethod ctor = null;
            for (IMethod method : selfClassData.getMethods()) {
              if (method.isConstructor()) {
                ctor = method;
                break;
              }
            }
            if (ctor != null) {
              ISourceRange sourceRange = selfClassData.getSourceRange();
              FakeMethod ctorMethod =
                  new FakeMethod(
                      (ModelElement) selfClassData,
                      "self",
                      sourceRange.getOffset(),
                      sourceRange.getLength(),
                      sourceRange.getOffset(),
                      sourceRange.getLength()) {
                    public boolean isConstructor() throws ModelException {
                      return true;
                    }
                  };
              ctorMethod.setParameters(ctor.getParameters());
              reporter.reportMethod(ctorMethod, suffix, replaceRange);
            } else {
              ISourceRange sourceRange = selfClassData.getSourceRange();
              reporter.reportMethod(
                  new FakeMethod(
                      (ModelElement) selfClassData,
                      "self",
                      sourceRange.getOffset(),
                      sourceRange.getLength(),
                      sourceRange.getOffset(),
                      sourceRange.getLength()),
                  "()",
                  replaceRange);
            }
          } catch (ModelException e) {
            PHPCorePlugin.log(e);
          }
        }
      }
    }
  }
Ejemplo n.º 7
0
  protected void reportAlias(
      ICompletionReporter reporter,
      String suffix,
      AbstractCompletionContext abstractContext,
      IModuleSource module,
      final Map<String, UsePart> result)
      throws BadLocationException {
    SourceRange replacementRange = getReplacementRange(abstractContext);
    String prefix = abstractContext.getPrefixWithoutProcessing();
    IDLTKSearchScope scope = createSearchScope();
    for (Iterator iterator = result.keySet().iterator(); iterator.hasNext(); ) {
      String name = (String) iterator.next();
      String fullName = result.get(name).getNamespace().getFullyQualifiedName();
      if (fullName.startsWith("\\")) {
        fullName = fullName.substring(1);
      }
      IType[] elements =
          PhpModelAccess.getDefault()
              .findTypes(null, fullName, MatchRule.PREFIX, 0, 0, scope, null);
      try {
        for (int i = 0; i < elements.length; i++) {
          String elementName = elements[i].getElementName();
          if (!PHPFlags.isNamespace(elements[i].getFlags())) {
            reportAlias(
                reporter,
                scope,
                module,
                replacementRange,
                elements[i],
                elementName,
                elementName.replace(fullName, name),
                suffix);
          } else {
            String nsname = prefix.replace(name, fullName);
            if (nsname.startsWith(elementName + SPLASH)
                && nsname.lastIndexOf(SPLASH) == elementName.length()) {
              // namespace strategy will handle this case
              continue;
            }
            IType[] typesOfNS = elements[i].getTypes();

            for (int j = 0; j < typesOfNS.length; j++) {
              reportAlias(
                  reporter,
                  scope,
                  module,
                  replacementRange,
                  typesOfNS[j],
                  elementName + SPLASH + typesOfNS[j].getElementName(),
                  (elementName + SPLASH + typesOfNS[j].getElementName()).replace(fullName, name),
                  suffix);
            }
          }
        }

        elements =
            PhpModelAccess.getDefault().findTypes(fullName, MatchRule.EXACT, 0, 0, scope, null);

        for (int i = 0; i < elements.length; i++) {
          String elementName = elements[i].getElementName();
          if (!PHPFlags.isNamespace(elements[i].getFlags())) {
            reportAlias(
                reporter, scope, module, replacementRange, elements[i], elementName, name, suffix);
          } else {
            String nsname = prefix.replace(name, fullName);
            if (nsname.startsWith(elementName + SPLASH)
                && nsname.lastIndexOf(SPLASH) == elementName.length()) {
              // namespace strategy will handle this case
              continue;
            }
            IType[] typesOfNS = elements[i].getTypes();

            for (int j = 0; j < typesOfNS.length; j++) {
              reportAlias(
                  reporter,
                  scope,
                  module,
                  replacementRange,
                  typesOfNS[j],
                  elementName + SPLASH + typesOfNS[j].getElementName(),
                  (elementName + SPLASH + typesOfNS[j].getElementName()).replace(fullName, name),
                  suffix);
            }
          }
        }
      } catch (ModelException e) {
        e.printStackTrace();
      }
    }
  }