protected void getGoalFromStaticDeclaration(
      String variableName, final List<IGoal> subGoals, final IType declaringType, IType realType)
      throws ModelException {
    ISourceModule sourceModule = declaringType.getSourceModule();
    ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule);
    TypeDeclaration typeDeclaration =
        PHPModelUtils.getNodeByClass(moduleDeclaration, declaringType);

    // try to search declarations of type "self::$var =" or
    // "$this->var ="
    ClassDeclarationSearcher searcher;
    if (realType != null) {
      searcher =
          new ClassDeclarationSearcher(
              sourceModule, typeDeclaration, 0, 0, variableName, realType, declaringType);
    } else {
      searcher = new ClassDeclarationSearcher(sourceModule, typeDeclaration, 0, 0, variableName);
    }
    try {
      moduleDeclaration.traverse(searcher);
      Map<ASTNode, IContext> staticDeclarations = searcher.getStaticDeclarations();
      for (ASTNode node : staticDeclarations.keySet()) {
        IContext context = staticDeclarations.get(node);
        if (context instanceof MethodContext) {
          MethodContext methodContext = (MethodContext) context;
          methodContext.setCurrentType(realType);
        }
        subGoals.add(new ExpressionTypeGoal(context, node));
      }
    } catch (Exception e) {
      if (DLTKCore.DEBUG) {
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 2
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);
      }
    }
  }
  private ModuleDeclaration getModuleDeclaration(IBuildContext context) {

    ISourceModule sourceModule = context.getSourceModule();
    ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule);

    return moduleDeclaration;
  }
  public IGoal[] init() {
    ClassVariableDeclarationGoal typedGoal = (ClassVariableDeclarationGoal) goal;
    IType[] types = typedGoal.getTypes();

    if (types == null) {
      TypeContext context = (TypeContext) typedGoal.getContext();
      types = PHPTypeInferenceUtils.getModelElements(context.getInstanceType(), context);
    }
    if (types == null) {
      return null;
    }

    IContext context = typedGoal.getContext();
    IModelAccessCache cache = null;
    if (context instanceof IModelCacheContext) {
      cache = ((IModelCacheContext) context).getCache();
    }

    String variableName = typedGoal.getVariableName();

    final List<IGoal> subGoals = new LinkedList<IGoal>();
    for (final IType type : types) {
      try {
        ITypeHierarchy hierarchy = null;
        if (cache != null) {
          hierarchy = cache.getSuperTypeHierarchy(type, null);
        }
        IField[] fields =
            PHPModelUtils.getTypeHierarchyField(type, hierarchy, variableName, true, null);
        Map<IType, IType> fieldDeclaringTypeSet = new HashMap<IType, IType>();
        for (IField field : fields) {
          IType declaringType = field.getDeclaringType();
          if (declaringType != null) {
            fieldDeclaringTypeSet.put(declaringType, type);
            ISourceModule sourceModule = declaringType.getSourceModule();
            ModuleDeclaration moduleDeclaration =
                SourceParserUtil.getModuleDeclaration(sourceModule);
            TypeDeclaration typeDeclaration =
                PHPModelUtils.getNodeByClass(moduleDeclaration, declaringType);

            if (typeDeclaration != null && field instanceof SourceRefElement) {
              SourceRefElement sourceRefElement = (SourceRefElement) field;
              ISourceRange sourceRange = sourceRefElement.getSourceRange();

              ClassDeclarationSearcher searcher =
                  new ClassDeclarationSearcher(
                      sourceModule,
                      typeDeclaration,
                      sourceRange.getOffset(),
                      sourceRange.getLength(),
                      null,
                      type,
                      declaringType);
              try {
                moduleDeclaration.traverse(searcher);
                if (searcher.getResult() != null) {
                  subGoals.add(new ExpressionTypeGoal(searcher.getContext(), searcher.getResult()));
                }
              } catch (Exception e) {
                if (DLTKCore.DEBUG) {
                  e.printStackTrace();
                }
              }
            }
          }
        }

        if (subGoals.size() == 0) {
          getGoalFromStaticDeclaration(variableName, subGoals, type, null);
        }
        fieldDeclaringTypeSet.remove(type);
        if (subGoals.size() == 0 && !fieldDeclaringTypeSet.isEmpty()) {
          for (Iterator iterator = fieldDeclaringTypeSet.keySet().iterator();
              iterator.hasNext(); ) {
            IType fieldDeclaringType = (IType) iterator.next();
            getGoalFromStaticDeclaration(
                variableName,
                subGoals,
                fieldDeclaringType,
                fieldDeclaringTypeSet.get(fieldDeclaringType));
          }
        }
      } catch (CoreException e) {
        if (DLTKCore.DEBUG) {
          e.printStackTrace();
        }
      }
    }

    resolveMagicClassVariableDeclaration(types, variableName, cache);

    return subGoals.toArray(new IGoal[subGoals.size()]);
  }
 /**
  * Get LuaSourceRoot from ISourceModule <br>
  * DLTK Model => AST
  */
 public static LuaSourceRoot getLuaSourceRoot(ISourceModule module) {
   ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(module);
   if (moduleDeclaration instanceof LuaSourceRoot) return (LuaSourceRoot) moduleDeclaration;
   return null;
 }
  @Override
  public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {

    IDocument document = viewer.getDocument();
    ITextEditor textEditor = ((PHPStructuredTextViewer) viewer).getTextEditor();

    if (textEditor instanceof PHPStructuredEditor) {
      IModelElement editorElement = ((PHPStructuredEditor) textEditor).getModelElement();
      if (editorElement != null) {

        ISourceModule sourceModule = ((ModelElement) editorElement).getSourceModule();

        try {

          if (sourceModule.getTypes().length != 1) {
            return;
          }
          ModuleDeclaration module = SourceParserUtil.getModuleDeclaration(sourceModule);
          ImplementationValidator validator = new ImplementationValidator(sourceModule);
          String code = "";
          try {
            module.traverse(validator);
          } catch (Exception e) {
            e.getClass();
            e.printStackTrace();
          }
          char indentChar = FormatPreferencesSupport.getInstance().getIndentationChar(document);
          String indent = String.valueOf(indentChar);

          for (MissingMethodImplementation miss : validator.getMissing()) {

            for (IMethod method : miss.getMisses()) {
              code +=
                  MethodStub.getMethodStub(
                      method.getParent().getElementName(),
                      method,
                      method,
                      indent,
                      TextUtilities.getDefaultLineDelimiter(document),
                      true);
            }

            document.replace(miss.getInjectionOffset(), 0, code);

            UseStatementInjector injector = new UseStatementInjector(this);
            injector.inject(document, getTextViewer(), offset);

            // TODO: format code using new PDT formatter
            /*
            Formatter formatter = new Formatter();
            formatter.format(document);
            */

          }

        } catch (Exception e) {
          Logger.logException(e);
        }
      }
    }
  }