Search in sources :

Example 51 with PsiComment

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

the class FormatterBasedLineIndentInfoBuilder method getBlocksStartingNewLine.

@NotNull
private List<Block> getBlocksStartingNewLine() {
    NewLineBlocksIterator newLineBlocksIterator = new NewLineBlocksIterator(myRootBlock, myDocument, myProgressIndicator);
    List<Block> newLineBlocks = new ArrayList<>();
    int currentLine = 0;
    while (newLineBlocksIterator.hasNext() && currentLine < MAX_NEW_LINE_BLOCKS_TO_PROCESS) {
        Block next = newLineBlocksIterator.next();
        if (next instanceof ASTBlock && ((ASTBlock) next).getNode() instanceof PsiComment) {
            continue;
        }
        newLineBlocks.add(next);
        currentLine++;
    }
    return newLineBlocks;
}
Also used : PsiComment(com.intellij.psi.PsiComment) ASTBlock(com.intellij.formatting.ASTBlock) ArrayList(java.util.ArrayList) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) ASTBlock(com.intellij.formatting.ASTBlock) Block(com.intellij.formatting.Block) NewLineBlocksIterator(com.intellij.psi.formatter.common.NewLineBlocksIterator) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with PsiComment

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

the class UpdateJavaScriptFileCopyright method scanFile.

protected void scanFile() {
    PsiElement first = getFile().getFirstChild();
    if (first != null) {
        final PsiElement child = first.getFirstChild();
        if (child instanceof PsiComment) {
            first = child;
        }
    }
    PsiElement last = first;
    PsiElement next = first;
    while (next != null) {
        if (next instanceof PsiComment || next instanceof PsiWhiteSpace) {
            next = getNextSibling(next);
        } else {
            break;
        }
        last = next;
    }
    if (first != null) {
        checkComments(first, last, true);
    } else {
        checkComments(null, null, true);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 53 with PsiComment

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

the class UpdateJspxFileCopyright method scanFile.

protected void scanFile() {
    logger.debug("updating " + getFile().getVirtualFile());
    XmlDocument doc = ((XmlFile) getFile()).getDocument();
    XmlProlog xmlProlog = doc.getProlog();
    if (xmlProlog == null) {
        return;
    }
    PsiElement elem = xmlProlog.getFirstChild();
    PsiElement docTypeStart = null;
    while (elem != null) {
        if (elem instanceof XmlDoctype) {
            docTypeStart = elem;
            break;
        }
        elem = getNextSibling(elem);
    }
    PsiElement first = xmlProlog.getFirstChild();
    int location = getLanguageOptions().getFileLocation();
    if (docTypeStart != null) {
        final ArrayList<PsiComment> comments = new ArrayList<>();
        collectComments(doc.getFirstChild(), xmlProlog, comments);
        collectComments(first, docTypeStart, comments);
        checkComments(first, location == XmlOptions.LOCATION_BEFORE_DOCTYPE, comments);
        checkComments(docTypeStart, doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG);
        return;
    } else if (location == XmlOptions.LOCATION_BEFORE_DOCTYPE) {
        location = XmlOptions.LOCATION_BEFORE_ROOTTAG;
    }
    final ArrayList<PsiComment> comments = new ArrayList<>();
    collectComments(doc.getFirstChild(), xmlProlog, comments);
    collectComments(first, doc.getRootTag(), comments);
    checkComments(doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG, comments);
}
Also used : PsiComment(com.intellij.psi.PsiComment) XmlFile(com.intellij.psi.xml.XmlFile) XmlDoctype(com.intellij.psi.xml.XmlDoctype) ArrayList(java.util.ArrayList) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement)

Example 54 with PsiComment

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

the class TopLevelMatchingHandler method match.

@Override
public boolean match(final PsiElement patternNode, final PsiElement matchedNode, final MatchContext matchContext) {
    final boolean matched = delegate.match(patternNode, matchedNode, matchContext);
    if (matched) {
        List<PsiElement> matchedNodes = matchContext.getMatchedNodes();
        if (matchedNodes == null) {
            matchedNodes = new ArrayList<>();
            matchContext.setMatchedNodes(matchedNodes);
        }
        PsiElement elementToAdd = matchedNode;
        if (patternNode instanceof PsiComment && StructuralSearchUtil.isDocCommentOwner(matchedNode)) {
            // psicomment and psidoccomment are placed inside the psimember next to them so
            // simple topdown matching should do additional "dances" to cover this case.
            elementToAdd = matchedNode.getFirstChild();
            assert elementToAdd instanceof PsiComment;
        }
        matchedNodes.add(elementToAdd);
    }
    if ((!matched || matchContext.getOptions().isRecursiveSearch()) && matchContext.getPattern().getStrategy().continueMatching(matchedNode) && matchContext.shouldRecursivelyMatch()) {
        matchContext.getMatcher().matchContext(new SsrFilteringNodeIterator(new SiblingNodeIterator(matchedNode.getFirstChild())));
    }
    return matched;
}
Also used : PsiComment(com.intellij.psi.PsiComment) SiblingNodeIterator(com.intellij.dupLocator.iterators.SiblingNodeIterator) SsrFilteringNodeIterator(com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator) PsiElement(com.intellij.psi.PsiElement)

Example 55 with PsiComment

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

the class JsonLineMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    myShouldAddComma = false;
    if (!(file instanceof JsonFile) || !super.checkAvailable(editor, file, info, down)) {
        return false;
    }
    Pair<PsiElement, PsiElement> movedElementRange = getElementRange(editor, file, info.toMove);
    if (!isValidElementRange(movedElementRange)) {
        return false;
    }
    // Tweak range to move if it's necessary
    movedElementRange = expandCommentsInRange(movedElementRange);
    info.toMove = new LineRange(movedElementRange.getFirst(), movedElementRange.getSecond());
    // Adjust destination range to prevent illegal offsets
    final int lineCount = editor.getDocument().getLineCount();
    if (down) {
        info.toMove2 = new LineRange(info.toMove.endLine, Math.min(info.toMove.endLine + 1, lineCount));
    } else {
        info.toMove2 = new LineRange(Math.max(info.toMove.startLine - 1, 0), info.toMove.startLine);
    }
    if (movedElementRange.getFirst() instanceof PsiComment && movedElementRange.getSecond() instanceof PsiComment) {
        return true;
    }
    // Check whether additional comma is needed
    final Pair<PsiElement, PsiElement> destElementRange = getElementRange(editor, file, info.toMove2);
    if (isValidElementRange(destElementRange) && movedElementRange.getFirst().getParent() == destElementRange.getSecond().getParent()) {
        final PsiElement commonParent = movedElementRange.getFirst().getParent();
        final PsiElement lowerRightElement = down ? destElementRange.getSecond() : movedElementRange.getSecond();
        // Destination rightmost element is not closing brace or bracket
        if (lowerRightElement instanceof JsonElement) {
            if (commonParent instanceof JsonArray && notFollowedByNextElementOrComma(lowerRightElement, JsonValue.class) || commonParent instanceof JsonObject && notFollowedByNextElementOrComma(lowerRightElement, JsonProperty.class)) {
                myShouldAddComma = true;
            }
        }
    }
    return true;
}
Also used : PsiComment(com.intellij.psi.PsiComment) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiComment (com.intellij.psi.PsiComment)68 PsiElement (com.intellij.psi.PsiElement)47 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)21 Nullable (org.jetbrains.annotations.Nullable)19 IElementType (com.intellij.psi.tree.IElementType)11 ArrayList (java.util.ArrayList)11 TextRange (com.intellij.openapi.util.TextRange)9 PsiFile (com.intellij.psi.PsiFile)8 NotNull (org.jetbrains.annotations.NotNull)8 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)7 ASTNode (com.intellij.lang.ASTNode)6 Matcher (java.util.regex.Matcher)6 Document (com.intellij.openapi.editor.Document)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)3 XmlTag (com.intellij.psi.xml.XmlTag)3 Commenter (com.intellij.lang.Commenter)2 Language (com.intellij.lang.Language)2 Editor (com.intellij.openapi.editor.Editor)2 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)2