Search in sources :

Example 51 with PsiWhiteSpace

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

the class DartDocUtil method getMultilineDocCommentText.

@NotNull
private static String getMultilineDocCommentText(@NotNull final DartDocComment docComment) {
    final StringBuilder buf = new StringBuilder();
    boolean afterAsterisk = false;
    for (PsiElement child = docComment.getFirstChild(); child != null; child = child.getNextSibling()) {
        final IElementType elementType = child.getNode().getElementType();
        final String text = child.getText();
        if (elementType != DartTokenTypesSets.MULTI_LINE_DOC_COMMENT_START && elementType != DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK && elementType != DartTokenTypesSets.MULTI_LINE_COMMENT_END) {
            int newLinesCount;
            if (child instanceof PsiWhiteSpace && (newLinesCount = StringUtil.countNewLines(text)) > 0) {
                buf.append(StringUtil.repeatSymbol('\n', newLinesCount));
            } else {
                if (afterAsterisk && text.startsWith(" ")) {
                    buf.append(text.substring(1));
                } else {
                    buf.append(text);
                }
            }
        }
        afterAsterisk = elementType == DartTokenTypesSets.DOC_COMMENT_LEADING_ASTERISK;
    }
    return buf.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with PsiWhiteSpace

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

the class DartRefactoringUtil method findExpressionInRange.

@Nullable
public static DartExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset) {
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 instanceof PsiWhiteSpace) {
        startOffset = element1.getTextRange().getEndOffset();
    }
    if (element2 instanceof PsiWhiteSpace) {
        endOffset = element2.getTextRange().getStartOffset();
    }
    DartExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, DartExpression.class);
    if (expression == null || expression.getTextRange().getEndOffset() != endOffset)
        return null;
    if (expression instanceof DartReference && expression.getParent() instanceof DartCallExpression)
        return null;
    return expression;
}
Also used : PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 53 with PsiWhiteSpace

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

the class AngularJSIndexingHandler method processJSDocComment.

@Override
public JSElementIndexingData processJSDocComment(@NotNull final JSDocComment comment, @Nullable JSElementIndexingData outData) {
    JSDocTag ngdocTag = null;
    JSDocTag nameTag = null;
    for (JSDocTag tag : comment.getTags()) {
        if ("ngdoc".equals(tag.getName()))
            ngdocTag = tag;
        else if ("name".equals(tag.getName()))
            nameTag = tag;
    }
    if (ngdocTag != null && nameTag != null) {
        final JSDocTagValue nameValue = nameTag.getValue();
        String name = nameValue != null ? nameValue.getText() : null;
        if (name != null)
            name = name.substring(name.indexOf(':') + 1);
        String ngdocValue = null;
        PsiElement nextSibling = ngdocTag.getNextSibling();
        if (nextSibling instanceof PsiWhiteSpace)
            nextSibling = nextSibling.getNextSibling();
        if (nextSibling != null && nextSibling.getNode().getElementType() == JSDocTokenTypes.DOC_COMMENT_DATA) {
            ngdocValue = nextSibling.getText();
        }
        if (ngdocValue != null && name != null) {
            final String[] commentLines = StringUtil.splitByLines(comment.getText());
            final boolean directive = ngdocValue.contains(DIRECTIVE);
            final boolean component = ngdocValue.contains(COMPONENT);
            if (directive || component) {
                final String restrictions = calculateRestrictions(commentLines, directive ? DEFAULT_RESTRICTIONS : "E");
                if (outData == null)
                    outData = new JSElementIndexingDataImpl();
                addImplicitElements(comment, directive ? DIRECTIVE : COMPONENT, AngularDirectivesDocIndex.KEY, name, restrictions, outData);
            } else if (ngdocValue.contains(FILTER)) {
                if (outData == null)
                    outData = new JSElementIndexingDataImpl();
                addImplicitElements(comment, FILTER, AngularFilterIndex.KEY, name, null, outData);
            }
        }
    }
    return outData;
}
Also used : JSDocTagValue(com.intellij.lang.javascript.psi.jsdoc.JSDocTagValue) JSElementIndexingDataImpl(com.intellij.lang.javascript.psi.stubs.impl.JSElementIndexingDataImpl) JSDocTag(com.intellij.lang.javascript.psi.jsdoc.JSDocTag) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 54 with PsiWhiteSpace

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

the class DartRefactoringUtil method findStatementsInRange.

@NotNull
public static PsiElement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    if (element1 instanceof PsiWhiteSpace) {
        startOffset = element1.getTextRange().getEndOffset();
        element1 = file.findElementAt(startOffset);
    }
    if (element2 instanceof PsiWhiteSpace) {
        endOffset = element2.getTextRange().getStartOffset();
        element2 = file.findElementAt(endOffset - 1);
    }
    if (element1 != null && element2 != null) {
        PsiElement commonParent = PsiTreeUtil.findCommonParent(element1, element2);
        if (commonParent instanceof DartExpression) {
            return new PsiElement[] { commonParent };
        }
    }
    final DartStatements statements = PsiTreeUtil.getParentOfType(element1, DartStatements.class);
    if (statements == null || element2 == null || !PsiTreeUtil.isAncestor(statements, element2, true)) {
        return PsiElement.EMPTY_ARRAY;
    }
    // don't forget about leafs (ex. ';')
    final ASTNode[] astResult = UsefulPsiTreeUtil.findChildrenRange(statements.getNode().getChildren(null), startOffset, endOffset);
    return ContainerUtil.map2Array(astResult, PsiElement.class, ASTNode::getPsi);
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 55 with PsiWhiteSpace

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

the class SMTRunnerConsoleProperties method findSuitableNavigatableForLine.

@Nullable
protected Navigatable findSuitableNavigatableForLine(@NotNull Project project, @NotNull VirtualFile file, int line) {
    // lets find first non-ws psi element
    final Document doc = FileDocumentManager.getInstance().getDocument(file);
    final PsiFile psi = doc == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(doc);
    if (psi == null) {
        return null;
    }
    int offset = doc.getLineStartOffset(line);
    int endOffset = doc.getLineEndOffset(line);
    for (int i = offset + 1; i < endOffset; i++) {
        PsiElement el = psi.findElementAt(i);
        if (el != null && !(el instanceof PsiWhiteSpace)) {
            offset = el.getTextOffset();
            break;
        }
    }
    return new OpenFileDescriptor(project, file, offset);
}
Also used : PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

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