예제 #1
0
 // ISourceRange getNameRange();
 @Test
 public void getNameRange() throws Exception {
   module.open(null);
   final IErlElement element = module.getElementAt(1);
   final IErlAttribute attribute = (IErlAttribute) element;
   final ISourceRange nameRange = attribute.getNameRange();
   assertEquals(0, nameRange.getOffset());
   assertEquals(12, nameRange.getLength());
 }
예제 #2
0
 // void setNameRange(int offset, int length);
 @Test
 public void setNameRange() throws Exception {
   module.open(null);
   final IErlElement element = module.getElementAt(1);
   final IErlAttribute attribute = (IErlAttribute) element;
   final int offset = 1, length = 2;
   attribute.setNameRange(offset, length);
   final ISourceRange nameRange = attribute.getNameRange();
   assertEquals(offset, nameRange.getOffset());
   assertEquals(length, nameRange.getLength());
 }
예제 #3
0
 /** @see ISourceReference */
 public String getSource() throws JavaModelException {
   IOpenable openable = this.parent.getOpenableParent();
   IBuffer buffer = openable.getBuffer();
   if (buffer == null) {
     return null;
   }
   ISourceRange range = getSourceRange();
   int offset = range.getOffset();
   int length = range.getLength();
   if (offset == -1 || length == 0) {
     return null;
   }
   try {
     return buffer.getText(offset, length);
   } catch (RuntimeException e) {
     return null;
   }
 }
  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()]);
  }
예제 #5
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);
          }
        }
      }
    }
  }