Search in sources :

Example 36 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlTagImpl method getAttribute.

@Override
@Nullable
public XmlAttribute getAttribute(String qname) {
    if (qname == null)
        return null;
    final XmlAttribute[] attributes = getAttributes();
    final boolean caseSensitive = isCaseSensitive();
    for (final XmlAttribute attribute : attributes) {
        final ASTNode child = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(attribute.getNode());
        if (child instanceof LeafElement) {
            final LeafElement attrNameElement = (LeafElement) child;
            if ((caseSensitive && Comparing.equal(attrNameElement.getChars(), qname) || !caseSensitive && Comparing.equal(attrNameElement.getChars(), qname, false))) {
                return attribute;
            }
        }
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GroovyCompletionData method isControlStructure.

private static boolean isControlStructure(PsiElement context) {
    final int offset = context.getTextRange().getStartOffset();
    PsiElement prevSibling = context.getPrevSibling();
    if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
        ASTNode node = prevSibling.getNode();
        return !TokenSets.DOTS.contains(node.getElementType());
    }
    if (GroovyCompletionUtil.isNewStatement(context, true)) {
        final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
        if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
            return true;
        }
    }
    if (context.getParent() != null) {
        PsiElement parent = context.getParent();
        if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
            return true;
        }
        if (parent instanceof GrReferenceExpression) {
            PsiElement superParent = parent.getParent();
            if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
                return true;
            }
        }
        return false;
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 38 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 39 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GroovySpacingProcessor method visitClosure.

@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
    ASTNode rBraceAtTheEnd = GeeseUtil.getClosureRBraceAtTheEnd(myChild1);
    if (myGroovySettings.USE_FLYING_GEESE_BRACES && myType2 == GroovyTokenTypes.mRCURLY && rBraceAtTheEnd != null) {
        String text = rBraceAtTheEnd.getTreeParent().getText();
        if (text.indexOf('\n') < 0) {
            /* the case:
       foo {
         bar {print x}<we are here>}
        */
            myResult = Spacing.createSpacing(1, 1, 1, false, 1);
        } else {
            myResult = Spacing.createSpacing(0, 0, 0, true, 100, 0);
        }
    } else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 == GroovyTokenTypes.mRCURLY) {
        //empty closure
        createSpaceInCode(false);
    } else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 != GroovyElementTypes.PARAMETERS_LIST && myType2 != GroovyTokenTypes.mCLOSABLE_BLOCK_OP || myType2 == GroovyTokenTypes.mRCURLY) {
        //spaces between statements
        boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
        createDependentLFSpacing(true, spacesWithinBraces, closure.getTextRange());
    } else if (myType1 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP) {
        myResult = GroovySpacingProcessorBasic.createDependentSpacingForClosure(mySettings, myGroovySettings, closure, true);
    } else if (myType1 == GroovyTokenTypes.mLCURLY && (myType2 == GroovyElementTypes.PARAMETERS_LIST || myType2 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP)) {
        boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
        createSpaceInCode(spacesWithinBraces);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 40 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class GroovySpacingProcessor method _init.

private void _init(@Nullable final ASTNode child) {
    if (child == null)
        return;
    ASTNode treePrev = child.getTreePrev();
    while (treePrev != null && isWhiteSpace(treePrev)) {
        treePrev = treePrev.getTreePrev();
    }
    if (treePrev == null) {
        _init(child.getTreeParent());
    } else {
        myChild2 = child;
        myType2 = myChild2.getElementType();
        myChild1 = treePrev;
        myType1 = myChild1.getElementType();
        final CompositeElement parent = (CompositeElement) treePrev.getTreeParent();
        myParent = SourceTreeToPsiMap.treeElementToPsi(parent);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

Aggregations

ASTNode (com.intellij.lang.ASTNode)839 NotNull (org.jetbrains.annotations.NotNull)154 PsiElement (com.intellij.psi.PsiElement)152 IElementType (com.intellij.psi.tree.IElementType)152 Nullable (org.jetbrains.annotations.Nullable)113 TextRange (com.intellij.openapi.util.TextRange)97 ArrayList (java.util.ArrayList)60 PsiFile (com.intellij.psi.PsiFile)36 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 Annotation (com.intellij.lang.annotation.Annotation)25 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)22 CharTable (com.intellij.util.CharTable)22 FileASTNode (com.intellij.lang.FileASTNode)20 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)20 Document (com.intellij.openapi.editor.Document)20 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)18 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)18 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)18 Project (com.intellij.openapi.project.Project)17 XmlTag (com.intellij.psi.xml.XmlTag)17