Search in sources :

Example 11 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-plugins by JetBrains.

the class MarkdownHighlightingAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    final IElementType type = element.getNode().getElementType();
    if (type == MarkdownTokenTypes.EMPH) {
        final PsiElement parent = element.getParent();
        if (parent == null) {
            return;
        }
        final IElementType parentType = parent.getNode().getElementType();
        if (parentType == MarkdownElementTypes.EMPH || parentType == MarkdownElementTypes.STRONG) {
            final Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(parentType == MarkdownElementTypes.EMPH ? MarkdownHighlighterColors.ITALIC_MARKER_ATTR_KEY : MarkdownHighlighterColors.BOLD_MARKER_ATTR_KEY);
        }
        return;
    }
    if (element instanceof LeafPsiElement) {
        return;
    }
    final TextAttributesKey[] tokenHighlights = SYNTAX_HIGHLIGHTER.getTokenHighlights(type);
    if (tokenHighlights.length > 0 && !MarkdownHighlighterColors.TEXT_ATTR_KEY.equals(tokenHighlights[0])) {
        final Annotation annotation = holder.createInfoAnnotation(element, null);
        annotation.setTextAttributes(tokenHighlights[0]);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Example 12 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class GroovyRefactoringUtil method findStatementsInRange.

@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset, boolean strict) {
    if (!(file instanceof GroovyFileBase))
        return PsiElement.EMPTY_ARRAY;
    Language language = GroovyLanguage.INSTANCE;
    PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
    PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
    if (element1 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element1)) {
        startOffset = element1.getTextRange().getEndOffset();
        element1 = file.findElementAt(startOffset);
    }
    if (element2 instanceof PsiWhiteSpace || org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(element2)) {
        endOffset = element2.getTextRange().getStartOffset();
        element2 = file.findElementAt(endOffset - 1);
    }
    if (element1 == null || element2 == null)
        return PsiElement.EMPTY_ARRAY;
    PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
    if (parent == null)
        return PsiElement.EMPTY_ARRAY;
    while (true) {
        if (parent instanceof GrCodeBlock)
            break;
        if (parent instanceof GroovyFileBase)
            break;
        if (parent instanceof GrCaseSection)
            break;
        if (parent instanceof GrStatement) {
            parent = parent.getParent();
            break;
        }
        if (parent == null)
            return PsiElement.EMPTY_ARRAY;
        final PsiElement prev = parent;
        parent = parent.getParent();
        if (parent instanceof GrCodeBlock && prev instanceof LeafPsiElement) {
            //braces
            parent = parent.getParent();
        }
    }
    if (!parent.equals(element1)) {
        while (!parent.equals(element1.getParent())) {
            element1 = element1.getParent();
        }
    }
    if (startOffset != element1.getTextRange().getStartOffset() && strict)
        return PsiElement.EMPTY_ARRAY;
    if (!parent.equals(element2)) {
        while (!parent.equals(element2.getParent())) {
            element2 = element2.getParent();
        }
    }
    if (endOffset != element2.getTextRange().getEndOffset() && strict)
        return PsiElement.EMPTY_ARRAY;
    if (parent instanceof GrCodeBlock && parent.getParent() instanceof GrBlockStatement && element1 == ((GrCodeBlock) parent).getLBrace() && element2 == ((GrCodeBlock) parent).getRBrace()) {
        return new PsiElement[] { parent.getParent() };
    }
    // calculate children
    PsiElement[] children = PsiElement.EMPTY_ARRAY;
    PsiElement psiChild = parent.getFirstChild();
    if (psiChild != null) {
        List<PsiElement> result = new ArrayList<>();
        while (psiChild != null) {
            result.add(psiChild);
            psiChild = psiChild.getNextSibling();
        }
        children = PsiUtilCore.toPsiElementArray(result);
    }
    ArrayList<PsiElement> possibleStatements = new ArrayList<>();
    boolean flag = false;
    for (PsiElement child : children) {
        if (child == element1) {
            flag = true;
        }
        if (flag) {
            possibleStatements.add(child);
        }
        if (child == element2) {
            break;
        }
    }
    for (PsiElement element : possibleStatements) {
        if (!(element instanceof GrStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment || TokenSets.SEPARATORS.contains(element.getNode().getElementType()))) {
            return PsiElement.EMPTY_ARRAY;
        }
    }
    return PsiUtilCore.toPsiElementArray(possibleStatements);
}
Also used : Language(com.intellij.lang.Language) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class GrTypeDefinitionImpl method getDefaultAnchor.

@Nullable
private PsiElement getDefaultAnchor(GrTypeDefinitionBody body, PsiMember member) {
    GroovyCodeStyleSettingsFacade settings = GroovyCodeStyleSettingsFacade.getInstance(getProject());
    int order = getMemberOrderWeight(member, settings);
    if (order < 0)
        return null;
    PsiElement lastMember = null;
    for (PsiElement child = body.getFirstChild(); child != null; child = child.getNextSibling()) {
        int order1 = getMemberOrderWeight(getAnyMember(child), settings);
        if (order1 < 0)
            continue;
        if (order1 > order) {
            final PsiElement lBrace = body.getLBrace();
            if (lastMember != null) {
                PsiElement nextSibling = lastMember.getNextSibling();
                while (nextSibling instanceof LeafPsiElement && (nextSibling.getText().equals(",") || nextSibling.getText().equals(";"))) {
                    nextSibling = nextSibling.getNextSibling();
                }
                return nextSibling == null && lBrace != null ? PsiUtil.skipWhitespacesAndComments(lBrace.getNextSibling(), true) : nextSibling;
            } else if (lBrace != null) {
                return PsiUtil.skipWhitespacesAndComments(lBrace.getNextSibling(), true);
            }
        }
        lastMember = child;
    }
    return body.getRBrace();
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyCodeStyleSettingsFacade(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyCodeStyleSettingsFacade) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class GroovyCompletionUtil method isInPossibleClosureParameter.

public static boolean isInPossibleClosureParameter(PsiElement position) {
    //Closure cl={String x, <caret>...
    if (position == null)
        return false;
    if (position instanceof PsiWhiteSpace || position.getNode().getElementType() == GroovyTokenTypes.mNLS) {
        position = FilterPositionUtil.searchNonSpaceNonCommentBack(position);
    }
    boolean hasCommas = false;
    while (position != null) {
        PsiElement parent = position.getParent();
        if (parent instanceof GrVariable) {
            PsiElement prev = FilterPositionUtil.searchNonSpaceNonCommentBack(parent);
            hasCommas = prev != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA;
        }
        if (parent instanceof GrClosableBlock) {
            PsiElement sibling = position.getPrevSibling();
            while (sibling != null) {
                if (sibling instanceof GrParameterList) {
                    return hasCommas;
                }
                boolean isComma = sibling instanceof LeafPsiElement && GroovyTokenTypes.mCOMMA == ((LeafPsiElement) sibling).getElementType();
                hasCommas |= isComma;
                if (isComma || sibling instanceof PsiWhiteSpace || sibling instanceof PsiErrorElement || sibling instanceof GrVariableDeclaration || sibling instanceof GrReferenceExpression && !((GrReferenceExpression) sibling).isQualified()) {
                    sibling = sibling.getPrevSibling();
                } else {
                    return false;
                }
            }
            return false;
        }
        position = parent;
    }
    return false;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 15 with LeafPsiElement

use of com.intellij.psi.impl.source.tree.LeafPsiElement in project intellij-community by JetBrains.

the class GrListOrMapImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    final PsiElement psi = child.getPsi();
    if (psi instanceof GrExpression || psi instanceof GrNamedArgument) {
        PsiElement prev = PsiUtil.getPrevNonSpace(psi);
        PsiElement next = PsiUtil.getNextNonSpace(psi);
        if (prev != null && prev.getNode() != null && prev.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
            super.deleteChildInternal(prev.getNode());
        } else if (next instanceof LeafPsiElement && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mCOMMA) {
            super.deleteChildInternal(next.getNode());
        }
    }
    super.deleteChildInternal(child);
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)25 PsiElement (com.intellij.psi.PsiElement)11 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)4 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)1 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 Document (com.intellij.openapi.editor.Document)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1