Search in sources :

Example 31 with LeafPsiElement

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

the class GroovyBlockGenerator method alignSpockTable.

private void alignSpockTable(List<GrStatement> group) {
    if (group.size() < 2) {
        return;
    }
    GrStatement inner = group.get(0);
    boolean embedded = inner != null && isTablePart(inner);
    GrStatement first = embedded ? inner : group.get(1);
    List<AlignmentProvider.Aligner> alignments = ContainerUtil.map2List(getSpockTable(first), leaf -> myAlignmentProvider.createAligner(leaf, true, Alignment.Anchor.RIGHT));
    int second = embedded ? 1 : 2;
    for (int i = second; i < group.size(); i++) {
        List<LeafPsiElement> table = getSpockTable(group.get(i));
        for (int j = 0; j < Math.min(table.size(), alignments.size()); j++) {
            alignments.get(j).append(table.get(j));
        }
    }
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 32 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 33 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 34 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 35 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)

Aggregations

LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)50 PsiElement (com.intellij.psi.PsiElement)29 Nullable (org.jetbrains.annotations.Nullable)8 IElementType (com.intellij.psi.tree.IElementType)7 Test (org.junit.Test)7 PsiFile (com.intellij.psi.PsiFile)6 TextRange (com.intellij.openapi.util.TextRange)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 NotNull (org.jetbrains.annotations.NotNull)5 Objects (com.google.common.base.Objects)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 PsiUtils (com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 BlazeRunConfigurationProducerTestCase (com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase)4 Info (com.intellij.execution.lineMarker.RunLineMarkerContributor.Info)4 AllIcons (com.intellij.icons.AllIcons)4 PsiReference (com.intellij.psi.PsiReference)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)3