Search in sources :

Example 6 with PsiWhiteSpace

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

the class PyCommaSelectionHandler method addNextComma.

/**
   * add next comma and whitespace to selection
   * @param e is crrent element
   * @param cursorOffset is current cursor offset
   * @return result selection TextRange
   */
private static List<TextRange> addNextComma(PsiElement e, int cursorOffset) {
    PsiElement nextCommaSibling = e.getNextSibling();
    if (nextCommaSibling != null) {
        ASTNode node = nextCommaSibling.getNode();
        if (node != null) {
            IElementType commaType = node.getElementType();
            if (commaType == PyTokenTypes.COMMA) {
                PsiElement nextSpaceSibling = nextCommaSibling.getNextSibling();
                if (nextSpaceSibling != null) {
                    TextRange textRange = e.getTextRange();
                    TextRange offsetRange;
                    if (nextSpaceSibling instanceof PsiWhiteSpace) {
                        offsetRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() + 2);
                    } else {
                        offsetRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() + 1);
                    }
                    if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) {
                        return Collections.singletonList(offsetRange);
                    }
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 7 with PsiWhiteSpace

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

the class PyStatementSelectionHandler method select.

public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
    List<TextRange> result = new ArrayList<>();
    PsiElement endElement = e;
    while (endElement.getLastChild() != null) {
        endElement = endElement.getLastChild();
    }
    if (endElement instanceof PsiWhiteSpace) {
        final PsiElement prevSibling = endElement.getPrevSibling();
        if (prevSibling != null) {
            endElement = prevSibling;
        }
    }
    result.addAll(expandToWholeLine(editorText, new TextRange(e.getTextRange().getStartOffset(), endElement.getTextRange().getEndOffset())));
    return result;
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 8 with PsiWhiteSpace

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

the class PyDocstringGenerator method buildAndInsert.

@NotNull
public PyDocStringOwner buildAndInsert() {
    Preconditions.checkNotNull(myDocStringOwner, "For this action docstring owner must be supplied");
    final String replacementText = buildDocString();
    final Project project = myDocStringOwner.getProject();
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    final PyExpressionStatement replacement = elementGenerator.createDocstring(replacementText);
    final PyStringLiteralExpression docStringExpression = getDocStringExpression();
    if (docStringExpression != null) {
        docStringExpression.replace(replacement.getExpression());
    } else {
        PyStatementListContainer container = PyUtil.as(myDocStringOwner, PyStatementListContainer.class);
        if (container == null) {
            throw new IllegalStateException("Should be a function or class");
        }
        final PyStatementList statements = container.getStatementList();
        final String indentation = PyIndentUtil.getElementIndent(statements);
        final Document document = PsiDocumentManager.getInstance(project).getDocument(myDocStringOwner.getContainingFile());
        if (document != null) {
            final PsiElement beforeStatements = statements.getPrevSibling();
            final boolean onSameLine = !(beforeStatements instanceof PsiWhiteSpace) || !beforeStatements.textContains('\n');
            if (onSameLine || statements.getStatements().length == 0) {
                String replacementWithLineBreaks = "\n" + indentation + replacementText;
                if (statements.getStatements().length > 0) {
                    replacementWithLineBreaks += "\n" + indentation;
                }
                final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
                documentManager.doPostponedOperationsAndUnblockDocument(document);
                final TextRange range = beforeStatements.getTextRange();
                try {
                    if (beforeStatements instanceof PsiWhiteSpace) {
                        if (statements.getStatements().length > 0) {
                            document.replaceString(range.getStartOffset(), range.getEndOffset(), replacementWithLineBreaks);
                        } else {
                            // preserve original spacing, since it probably separates function and other declarations
                            document.insertString(range.getStartOffset(), replacementWithLineBreaks);
                        }
                    } else {
                        document.insertString(range.getEndOffset(), replacementWithLineBreaks);
                    }
                } finally {
                    documentManager.commitDocument(document);
                }
            } else {
                statements.addBefore(replacement, statements.getStatements()[0]);
            }
        }
    }
    myDocStringOwner = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(myDocStringOwner);
    return myDocStringOwner;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PsiWhiteSpace

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

the class RncFoldingBuilder method checkNodeAndSiblings.

@Nullable
private static ASTNode checkNodeAndSiblings(@Nullable ASTNode node, TokenSet tokens, ArrayList<FoldingDescriptor> regions, Document document) {
    if (node != null && tokens.contains(node.getElementType())) {
        final ASTNode start = node;
        ASTNode end = start;
        node = node.getTreeNext();
        if (node != null) {
            do {
                end = node;
                node = node.getTreeNext();
            } while (node != null && tokens.contains(node.getElementType()));
        }
        if (end != start) {
            while (end.getPsi() instanceof PsiWhiteSpace) {
                end = end.getTreePrev();
            }
            if (isOnDifferentLine(start, end, document)) {
                regions.add(new FoldingDescriptor(start, new TextRange(start.getStartOffset(), end.getTextRange().getEndOffset())));
            }
        }
    }
    return node;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PsiWhiteSpace

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

the class PropertiesRootBlock method buildChildren.

@Override
protected List<Block> buildChildren() {
    final List<Block> result = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (!(child instanceof PsiWhiteSpace)) {
            if (child.getElementType() instanceof PropertyListStubElementType) {
                ASTNode propertyNode = child.getFirstChildNode();
                while (propertyNode != null) {
                    if (propertyNode.getElementType() instanceof PropertyStubElementType) {
                        collectPropertyBlock(propertyNode, result);
                    } else if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(propertyNode.getElementType()) || PropertiesTokenTypes.BAD_CHARACTER.equals(propertyNode.getElementType())) {
                        result.add(new PropertyBlock(propertyNode, null));
                    }
                    propertyNode = propertyNode.getTreeNext();
                }
            } else if (PropertiesTokenTypes.BAD_CHARACTER.equals(child.getElementType())) {
                result.add(new PropertyBlock(child, null));
            }
        }
        if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(child.getElementType())) {
            result.add(new PropertyBlock(child, null));
        }
        child = child.getTreeNext();
    }
    return result;
}
Also used : PropertyListStubElementType(com.intellij.lang.properties.parsing.PropertyListStubElementType) PropertyStubElementType(com.intellij.lang.properties.parsing.PropertyStubElementType) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) 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