Search in sources :

Example 21 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 22 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 23 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)

Example 24 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 25 with LeafPsiElement

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

the class PyElementGeneratorImpl method insertItemIntoListRemoveRedundantCommas.

@Override
@NotNull
public PsiElement insertItemIntoListRemoveRedundantCommas(@NotNull final PyElement list, @Nullable final PyExpression afterThis, @NotNull final PyExpression toInsert) {
    // TODO: #insertItemIntoList is probably buggy. In such case, fix it and get rid of this method
    final PsiElement result = insertItemIntoList(list, afterThis, toInsert);
    final LeafPsiElement[] leafs = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class);
    if (leafs != null) {
        final Deque<LeafPsiElement> commas = Queues.newArrayDeque(Collections2.filter(Arrays.asList(leafs), COMMAS_ONLY));
        if (!commas.isEmpty()) {
            final LeafPsiElement lastComma = commas.getLast();
            if (PsiTreeUtil.getNextSiblingOfType(lastComma, PyExpression.class) == null) {
                //Comma has no expression after it
                lastComma.delete();
            }
        }
    }
    return result;
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)27 PsiElement (com.intellij.psi.PsiElement)13 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 Test (org.junit.Test)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