Search in sources :

Example 1 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace 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 2 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class HtmlLocalInspectionTool method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlToken(final XmlToken token) {
            IElementType tokenType = token.getTokenType();
            if (tokenType == XmlTokenType.XML_NAME || tokenType == XmlTokenType.XML_TAG_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkTag(tag, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            checkAttribute(attribute, holder, isOnTheFly);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) IElementType(com.intellij.psi.tree.IElementType) OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class RemoveUnnecessaryBackslashQuickFix method removeBackSlash.

public static void removeBackSlash(PsiElement parent) {
    if (parent != null) {
        Stack<PsiElement> stack = new Stack<>();
        if (parent instanceof PyParenthesizedExpression)
            stack.push(((PyParenthesizedExpression) parent).getContainedExpression());
        else
            stack.push(parent);
        while (!stack.isEmpty()) {
            PsiElement el = stack.pop();
            PsiWhiteSpace[] children = PsiTreeUtil.getChildrenOfType(el, PsiWhiteSpace.class);
            if (children != null) {
                for (PsiWhiteSpace ws : children) {
                    if (ws.getText().contains("\\")) {
                        ws.delete();
                    }
                }
            }
            for (PsiElement psiElement : el.getChildren()) {
                stack.push(psiElement);
            }
        }
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement) Stack(com.intellij.util.containers.Stack) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 4 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class ReplaceExceptPartQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement exceptPart = descriptor.getPsiElement();
    if (exceptPart instanceof PyExceptPart) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        PsiElement element = ((PyExceptPart) exceptPart).getExceptClass().getNextSibling();
        while (element instanceof PsiWhiteSpace) {
            element = element.getNextSibling();
        }
        assert element != null;
        PyTryExceptStatement newElement = elementGenerator.createFromText(LanguageLevel.forElement(exceptPart), PyTryExceptStatement.class, "try:  pass except a as b:  pass");
        ASTNode node = newElement.getExceptParts()[0].getNode().findChildByType(PyTokenTypes.AS_KEYWORD);
        assert node != null;
        element.replace(node.getPsi());
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PyExceptPart(com.jetbrains.python.psi.PyExceptPart) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement) PyTryExceptStatement(com.jetbrains.python.psi.PyTryExceptStatement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 5 with PsiWhiteSpace

use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.

the class CompatibilityVisitor method visitPyExceptBlock.

@Override
public void visitPyExceptBlock(PyExceptPart node) {
    super.visitPyExceptBlock(node);
    final PyExpression exceptClass = node.getExceptClass();
    if (exceptClass != null) {
        PsiElement element = exceptClass.getNextSibling();
        while (element instanceof PsiWhiteSpace) {
            element = element.getNextSibling();
        }
        if (element != null && "as".equals(element.getText())) {
            registerOnFirstMatchingVersion(level -> level.isOlderThan(LanguageLevel.PYTHON26), "Python versions < 2.6 do not support this syntax.", node);
        }
        if (element != null && ",".equals(element.getText())) {
            registerForAllMatchingVersions(LanguageLevel::isPy3K, " not support this syntax.", node, new ReplaceExceptPartQuickFix());
        }
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)90 PsiElement (com.intellij.psi.PsiElement)82 TextRange (com.intellij.openapi.util.TextRange)23 ASTNode (com.intellij.lang.ASTNode)15 PsiFile (com.intellij.psi.PsiFile)15 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 PsiComment (com.intellij.psi.PsiComment)13 IElementType (com.intellij.psi.tree.IElementType)13 Document (com.intellij.openapi.editor.Document)12 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 Editor (com.intellij.openapi.editor.Editor)5 Language (com.intellij.lang.Language)4 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)4 XmlTag (com.intellij.psi.xml.XmlTag)4 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)3 XmlFile (com.intellij.psi.xml.XmlFile)3