Search in sources :

Example 46 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoBreakStatementExitPointHandler method computeUsages.

@Override
public void computeUsages(List<PsiElement> targets) {
    PsiElement breakStmtOwner = findBreakStatementOwner();
    GoRecursiveVisitor visitor = new GoRecursiveVisitor() {

        @Override
        public void visitLabelDefinition(@NotNull GoLabelDefinition o) {
            if (o == breakStmtOwner) {
                addOccurrence(o);
            }
            super.visitLabelDefinition(o);
        }

        @Override
        public void visitBreakStatement(@NotNull GoBreakStatement o) {
            if (o == myBreakStatement || getBreakStatementOwnerOrResolve(o) == breakStmtOwner) {
                addOccurrence(o);
            }
            super.visitBreakStatement(o);
        }

        @Override
        public void visitSwitchStatement(@NotNull GoSwitchStatement o) {
            if (o == breakStmtOwner) {
                GoSwitchStart switchStart = o.getSwitchStart();
                if (switchStart != null) {
                    addOccurrence(switchStart.getSwitch());
                }
            }
            super.visitSwitchStatement(o);
        }

        @Override
        public void visitForStatement(@NotNull GoForStatement o) {
            if (o == breakStmtOwner) {
                addOccurrence(o.getFor());
            }
            super.visitForStatement(o);
        }

        @Override
        public void visitSelectStatement(@NotNull GoSelectStatement o) {
            if (o == breakStmtOwner) {
                addOccurrence(o.getSelect());
            }
            super.visitSelectStatement(o);
        }
    };
    if (breakStmtOwner != null) {
        PsiElement parent = breakStmtOwner.getParent();
        if (parent instanceof GoCompositeElement) {
            visitor.visitCompositeElement((GoCompositeElement) parent);
        }
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement)

Example 47 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoFoldingBuilder method buildLanguageFoldRegions.

@Override
protected void buildLanguageFoldRegions(@NotNull List<FoldingDescriptor> result, @NotNull PsiElement root, @NotNull Document document, boolean quick) {
    if (!(root instanceof GoFile))
        return;
    GoFile file = (GoFile) root;
    if (!file.isContentsLoaded())
        return;
    GoImportList importList = ((GoFile) root).getImportList();
    if (importList != null) {
        GoImportDeclaration firstImport = ContainerUtil.getFirstItem(importList.getImportDeclarationList());
        if (firstImport != null) {
            PsiElement importKeyword = firstImport.getImport();
            int offset = importKeyword.getTextRange().getEndOffset();
            int startOffset = importKeyword.getNextSibling() instanceof PsiWhiteSpace ? offset + 1 : offset;
            int endOffset = importList.getTextRange().getEndOffset();
            if (endOffset - startOffset > 3) {
                result.add(new NamedFoldingDescriptor(importList, startOffset, endOffset, null, "..."));
            }
        }
    }
    for (GoBlock block : PsiTreeUtil.findChildrenOfType(file, GoBlock.class)) {
        if (block.getTextRange().getLength() > 1) {
            result.add(new NamedFoldingDescriptor(block.getNode(), block.getTextRange(), null, "{...}"));
        }
    }
    for (GoExprSwitchStatement switchStatement : PsiTreeUtil.findChildrenOfType(file, GoExprSwitchStatement.class)) {
        fold(switchStatement, switchStatement.getLbrace(), switchStatement.getRbrace(), "{...}", result);
    }
    for (GoSelectStatement selectStatement : PsiTreeUtil.findChildrenOfType(file, GoSelectStatement.class)) {
        fold(selectStatement, selectStatement.getLbrace(), selectStatement.getRbrace(), "{...}", result);
    }
    for (GoTypeSpec type : file.getTypes()) {
        foldTypes(type.getSpecType().getType(), result);
    }
    for (GoCaseClause caseClause : PsiTreeUtil.findChildrenOfType(file, GoCaseClause.class)) {
        PsiElement colon = caseClause.getColon();
        if (colon != null && !caseClause.getStatementList().isEmpty()) {
            fold(caseClause, colon.getNextSibling(), caseClause, "...", result);
        }
    }
    for (GoCommClause commClause : PsiTreeUtil.findChildrenOfType(file, GoCommClause.class)) {
        PsiElement colon = commClause.getColon();
        if (colon != null && !commClause.getStatementList().isEmpty()) {
            fold(commClause, colon.getNextSibling(), commClause, "...", result);
        }
    }
    for (GoVarDeclaration varDeclaration : PsiTreeUtil.findChildrenOfType(file, GoVarDeclaration.class)) {
        if (varDeclaration.getVarSpecList().size() > 1) {
            fold(varDeclaration, varDeclaration.getLparen(), varDeclaration.getRparen(), "(...)", result);
        }
    }
    for (GoConstDeclaration constDeclaration : PsiTreeUtil.findChildrenOfType(file, GoConstDeclaration.class)) {
        if (constDeclaration.getConstSpecList().size() > 1) {
            fold(constDeclaration, constDeclaration.getLparen(), constDeclaration.getRparen(), "(...)", result);
        }
    }
    for (GoTypeDeclaration typeDeclaration : PsiTreeUtil.findChildrenOfType(file, GoTypeDeclaration.class)) {
        if (typeDeclaration.getTypeSpecList().size() > 1) {
            fold(typeDeclaration, typeDeclaration.getLparen(), typeDeclaration.getRparen(), "(...)", result);
        }
    }
    for (GoCompositeLit compositeLit : PsiTreeUtil.findChildrenOfType(file, GoCompositeLit.class)) {
        GoLiteralValue literalValue = compositeLit.getLiteralValue();
        if (literalValue != null && literalValue.getElementList().size() > 1) {
            fold(literalValue, literalValue.getLbrace(), literalValue.getRbrace(), "{...}", result);
        }
    }
    if (!quick) {
        Set<PsiElement> processedComments = ContainerUtil.newHashSet();
        PsiTreeUtil.processElements(file, element -> {
            ASTNode node = element.getNode();
            IElementType type = node.getElementType();
            TextRange range = element.getTextRange();
            if (type == GoParserDefinition.MULTILINE_COMMENT && range.getLength() > 2) {
                result.add(new NamedFoldingDescriptor(node, range, null, "/*...*/"));
            }
            if (type == GoParserDefinition.LINE_COMMENT) {
                addCommentFolds(element, processedComments, result);
            }
            foldTypes(element, result);
            return true;
        });
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) IElementType(com.intellij.psi.tree.IElementType) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 48 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStatementMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    if (!(file instanceof GoFile && super.checkAvailable(editor, file, info, down)))
        return false;
    Couple<PsiElement> primeElementRange = getElementRange(editor, file);
    if (primeElementRange == null)
        return false;
    PsiElement commonParent = primeElementRange.first.isEquivalentTo(primeElementRange.second) ? primeElementRange.first.getParent() : PsiTreeUtil.findCommonParent(primeElementRange.first, primeElementRange.second);
    if (commonParent == null)
        return false;
    Couple<PsiElement> elementRange = getTopmostElementRange(primeElementRange, commonParent);
    if (elementRange == null)
        return false;
    if (commonParent == elementRange.first)
        commonParent = commonParent.getParent();
    info.toMove = new LineRange(elementRange.first, elementRange.second);
    if (elementRange.first instanceof GoTopLevelDeclaration && commonParent instanceof GoFile) {
        PsiElement toMove2 = getNeighborOfType(elementRange, GoTopLevelDeclaration.class, down);
        info.toMove2 = toMove2 != null ? new LineRange(toMove2) : null;
        return true;
    }
    if (commonParent instanceof GoImportList) {
        PsiElement toMove2 = getNeighborOfType(elementRange, GoImportDeclaration.class, down);
        info.toMove2 = toMove2 != null ? new LineRange(toMove2) : null;
        return true;
    }
    return setUpInfo(info, elementRange, commonParent, down);
}
Also used : LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

Example 49 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStatementMover method getElementRange.

private static Couple<PsiElement> getElementRange(@NotNull Editor editor, @NotNull PsiFile file) {
    Pair<PsiElement, PsiElement> primeElementRangePair = getElementRange(editor, file, getLineRangeFromSelection(editor));
    if (primeElementRangePair == null)
        return null;
    ASTNode firstNode = TreeUtil.findFirstLeaf(primeElementRangePair.first.getNode());
    ASTNode lastNode = TreeUtil.findLastLeaf(primeElementRangePair.second.getNode());
    if (firstNode == null || lastNode == null)
        return null;
    return Couple.of(firstNode.getPsi(), lastNode.getPsi());
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement)

Example 50 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoStatementMover method setUpInfo.

private static boolean setUpInfo(@NotNull MoveInfo info, @NotNull Couple<PsiElement> range, @NotNull PsiElement commonParent, boolean down) {
    info.toMove = new LineRange(range.first, range.second);
    info.toMove2 = null;
    if (range.first instanceof GoPackageClause)
        return true;
    PsiElement topLevelElement = PsiTreeUtil.findPrevParent(commonParent.getContainingFile(), commonParent);
    int nearLine = down ? info.toMove.endLine : info.toMove.startLine - 1;
    LineRange lineRange = new LineRange(topLevelElement);
    if (!lineRange.containsLine(down ? info.toMove.endLine + 1 : info.toMove.startLine - 2)) {
        return true;
    }
    info.toMove2 = lineRange.containsLine(down ? info.toMove.endLine + 1 : info.toMove.startLine - 2) ? new LineRange(nearLine, nearLine + 1) : null;
    return true;
}
Also used : LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3198 Nullable (org.jetbrains.annotations.Nullable)493 PsiFile (com.intellij.psi.PsiFile)474 NotNull (org.jetbrains.annotations.NotNull)442 TextRange (com.intellij.openapi.util.TextRange)239 PsiReference (com.intellij.psi.PsiReference)227 Project (com.intellij.openapi.project.Project)222 VirtualFile (com.intellij.openapi.vfs.VirtualFile)210 ArrayList (java.util.ArrayList)195 ASTNode (com.intellij.lang.ASTNode)142 XmlTag (com.intellij.psi.xml.XmlTag)134 PsiClass (com.intellij.psi.PsiClass)115 Editor (com.intellij.openapi.editor.Editor)112 Document (com.intellij.openapi.editor.Document)109 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)85 PsiDirectory (com.intellij.psi.PsiDirectory)80 IElementType (com.intellij.psi.tree.IElementType)78 Module (com.intellij.openapi.module.Module)77 PsiMethod (com.intellij.psi.PsiMethod)73 UsageInfo (com.intellij.usageView.UsageInfo)70