@Override
  public void replaceDotToken(PsiElement newDot) {
    if (newDot == null) return;
    if (!TokenSets.DOTS.contains(newDot.getNode().getElementType())) return;
    final PsiElement oldDot = getDotToken();
    if (oldDot == null) return;

    getNode().replaceChild(oldDot.getNode(), newDot.getNode());
  }
Esempio n. 2
0
 @Override
 public PsiElement qualifyClassReferences(@NotNull PsiElement element) {
   final ReferenceAdjuster adjuster =
       ReferenceAdjuster.Extension.getReferenceAdjuster(element.getLanguage());
   if (adjuster != null) {
     final ASTNode reference = adjuster.process(element.getNode(), false, false, true, true);
     return SourceTreeToPsiMap.treeToPsiNotNull(reference);
   }
   return element;
 }
Esempio n. 3
0
 public static void deleteClass(@NotNull JetClassOrObject clazz) {
   CheckUtil.checkWritable(clazz);
   JetFile file = (JetFile) clazz.getContainingFile();
   List<JetDeclaration> declarations = file.getDeclarations();
   if (declarations.size() == 1) {
     file.delete();
   } else {
     PsiElement parent = clazz.getParent();
     CodeEditUtil.removeChild(parent.getNode(), clazz.getNode());
   }
 }
Esempio n. 4
0
 @Override
 public void shortenClassReferences(@NotNull PsiElement element, int startOffset, int endOffset)
     throws IncorrectOperationException {
   CheckUtil.checkWritable(element);
   if (SourceTreeToPsiMap.hasTreeElement(element)) {
     final ReferenceAdjuster adjuster =
         ReferenceAdjuster.Extension.getReferenceAdjuster(element.getLanguage());
     if (adjuster != null) {
       adjuster.processRange(element.getNode(), startOffset, endOffset, myProject);
     }
   }
 }
 @NotNull
 private static PsiElement originalElement(@NotNull PsiClass aClass) {
   final PsiElement originalElement = aClass.getOriginalElement();
   ASTNode node = originalElement.getNode();
   if (node != null) {
     final PsiCompiledElement compiled = node.getUserData(ClsElementImpl.COMPILED_ELEMENT);
     if (compiled != null) {
       return compiled;
     }
   }
   return originalElement;
 }
Esempio n. 6
0
  @Nullable
  public static Name getConventionName(@NotNull JetSimpleNameExpression simpleNameExpression) {
    if (simpleNameExpression.getIdentifier() != null) {
      return simpleNameExpression.getReferencedNameAsName();
    }

    PsiElement firstChild = simpleNameExpression.getFirstChild();
    if (firstChild != null) {
      IElementType elementType = firstChild.getNode().getElementType();
      if (elementType instanceof JetToken) {
        JetToken jetToken = (JetToken) elementType;
        return OperatorConventions.getNameForOperationSymbol(jetToken);
      }
    }

    return null;
  }
  @Override
  @Nullable
  public String getReferenceName() {
    PsiElement nameElement = getReferenceNameElement();
    if (nameElement != null) {
      IElementType nodeType = nameElement.getNode().getElementType();
      if (TokenSets.STRING_LITERAL_SET.contains(nodeType)) {
        final Object value = GrLiteralImpl.getLiteralValue(nameElement);
        if (value instanceof String) {
          return (String) value;
        }
      }

      return nameElement.getText();
    }
    return null;
  }
Esempio n. 8
0
  @Override
  public PsiElement shortenClassReferences(@NotNull PsiElement element, int flags)
      throws IncorrectOperationException {
    CheckUtil.checkWritable(element);
    if (!SourceTreeToPsiMap.hasTreeElement(element)) return element;

    final boolean addImports = (flags & DO_NOT_ADD_IMPORTS) == 0;
    final boolean incompleteCode = (flags & UNCOMPLETE_CODE) != 0;

    final ReferenceAdjuster adjuster =
        ReferenceAdjuster.Extension.getReferenceAdjuster(element.getLanguage());
    if (adjuster != null) {
      final ASTNode reference =
          adjuster.process(element.getNode(), addImports, incompleteCode, myProject);
      return SourceTreeToPsiMap.treeToPsiNotNull(reference);
    } else {
      return element;
    }
  }
Esempio n. 9
0
  @NotNull
  public XmlTagValue getValue() {
    XmlTagValue tagValue = myValue;
    if (tagValue == null) {
      final PsiElement[] elements = getElements();
      final List<XmlTagChild> bodyElements = new ArrayList<XmlTagChild>(elements.length);

      boolean insideBody = false;
      for (final PsiElement element : elements) {
        final ASTNode treeElement = element.getNode();
        if (insideBody) {
          if (treeElement.getElementType() == XmlTokenType.XML_END_TAG_START) break;
          if (!(element instanceof XmlTagChild)) continue;
          bodyElements.add((XmlTagChild) element);
        } else if (treeElement.getElementType() == XmlTokenType.XML_TAG_END) insideBody = true;
      }

      XmlTagChild[] tagChildren =
          ContainerUtil.toArray(bodyElements, new XmlTagChild[bodyElements.size()]);
      myValue = tagValue = new XmlTagValueImpl(tagChildren, this);
    }
    return tagValue;
  }
 @Override
 public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
   return typeChar == ':' && JavaTokenType.COLON == position.getNode().getElementType();
 }
Esempio n. 11
0
 @Nullable
 public static PsiElement findChildByType(
     @NotNull PsiElement element, @NotNull IElementType type) {
   ASTNode node = element.getNode().findChildByType(type);
   return node == null ? null : node.getPsi();
 }
  @NotNull
  private GroovyResolveResult[] resolveTypeOrPropertyInner() {
    PsiElement nameElement = getReferenceNameElement();
    String name = getReferenceName();

    if (name == null || nameElement == null) return GroovyResolveResult.EMPTY_ARRAY;

    IElementType nameType = nameElement.getNode().getElementType();
    if (nameType == GroovyTokenTypes.kTHIS) {
      GroovyResolveResult[] results = GrThisReferenceResolver.resolveThisExpression(this);
      if (results != null) {
        return results;
      }
    } else if (nameType == GroovyTokenTypes.kSUPER) {
      GroovyResolveResult[] results = GrSuperReferenceResolver.resolveSuperExpression(this);
      if (results != null) {
        return results;
      }
    }

    EnumSet<ClassHint.ResolveKind> kinds =
        getParent() instanceof GrReferenceExpression
            ? ClassHint.RESOLVE_KINDS_CLASS_PACKAGE
            : ClassHint.RESOLVE_KINDS_CLASS;

    GroovyResolveResult[] classCandidates = null;

    GrReferenceResolveRunner resolveRunner = new GrReferenceResolveRunner(this);

    ResolverProcessor processor = new PropertyResolverProcessor(name, this);
    resolveRunner.resolveImpl(processor);
    final GroovyResolveResult[] fieldCandidates = processor.getCandidates();

    if (hasAt()) {
      return fieldCandidates;
    }

    boolean canBeClassOrPackage = ResolveUtil.canBeClassOrPackage(this);

    if (canBeClassOrPackage && findClassOrPackageAtFirst()) {
      ResolverProcessor classProcessor = new ClassResolverProcessor(name, this, kinds);
      resolveRunner.resolveImpl(classProcessor);
      classCandidates = classProcessor.getCandidates();
      if (classCandidates.length > 0 && containsPackage(classCandidates)) {
        final PsiElement firstNonReferenceExprParent =
            PsiTreeUtil.skipParentsOfType(this, GrReferenceExpressionImpl.class);
        final GrReferenceExpressionImpl topRef =
            (GrReferenceExpressionImpl)
                PsiTreeUtil.findFirstParent(
                    this,
                    new Condition<PsiElement>() {
                      @Override
                      public boolean value(PsiElement parent) {
                        return parent.getParent() == firstNonReferenceExprParent
                            && parent instanceof GrReferenceExpressionImpl;
                      }
                    });
        if (topRef != null) {
          final String fqn = topRef.getTextSkipWhiteSpaceAndComments();
          if (JavaPsiFacade.getInstance(getProject()).findClass(fqn, getResolveScope()) != null) {
            return classCandidates;
          }
        }
      }
    }

    // if reference expression is in class we need to return field instead of accessor method
    for (GroovyResolveResult candidate : fieldCandidates) {
      final PsiElement element = candidate.getElement();
      if (element instanceof PsiField) {
        final PsiClass containingClass = ((PsiField) element).getContainingClass();
        if (containingClass != null && PsiUtil.getContextClass(this) == containingClass)
          return fieldCandidates;
      } else if (!(element instanceof GrBindingVariable)) {
        return fieldCandidates;
      }
    }

    if (classCandidates != null && classCandidates.length > 0) return classCandidates;

    final boolean isLValue = PsiUtil.isLValue(this);
    String[] accessorNames =
        isLValue
            ? GroovyPropertyUtils.suggestSettersName(name)
            : GroovyPropertyUtils.suggestGettersName(name);
    List<GroovyResolveResult> accessorResults = new ArrayList<GroovyResolveResult>();
    for (String accessorName : accessorNames) {
      AccessorResolverProcessor accessorResolver =
          new AccessorResolverProcessor(
              accessorName,
              name,
              this,
              !isLValue,
              false,
              PsiImplUtil.getQualifierType(this),
              getTypeArguments());
      resolveRunner.resolveImpl(accessorResolver);
      final GroovyResolveResult[] candidates = accessorResolver.getCandidates();

      // can be only one correct candidate or some incorrect
      if (candidates.length == 1 && candidates[0].isStaticsOK() && candidates[0].isAccessible()) {
        return candidates;
      } else {
        ContainerUtil.addAll(accessorResults, candidates);
      }
    }

    final ArrayList<GroovyResolveResult> fieldList = ContainerUtil.newArrayList(fieldCandidates);
    filterOutBindings(fieldList);
    if (!fieldList.isEmpty()) {
      return fieldList.toArray(new GroovyResolveResult[fieldList.size()]);
    }

    if (classCandidates == null && canBeClassOrPackage) {
      ResolverProcessor classProcessor = new ClassResolverProcessor(name, this, kinds);
      resolveRunner.resolveImpl(classProcessor);
      classCandidates = classProcessor.getCandidates();
    }

    if (classCandidates != null && classCandidates.length > 0) return classCandidates;
    if (!accessorResults.isEmpty()) return new GroovyResolveResult[] {accessorResults.get(0)};
    return GroovyResolveResult.EMPTY_ARRAY;
  }
 @Override
 @Nullable
 public IElementType getDotTokenType() {
   PsiElement dot = getDotToken();
   return dot == null ? null : dot.getNode().getElementType();
 }