Search in sources :

Example 36 with PsiComment

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

the class CfmlFoldingBuilder method buildFoldRegions.

@NotNull
public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
    final PsiElement element = node.getPsi();
    if (element instanceof CfmlFile) {
        final CfmlFile file = (CfmlFile) element;
        final PsiElement[] children = file.getChildren();
        Collection<FoldingDescriptor> result = new LinkedList<>();
        for (PsiElement child : children) {
            if (child != null && (child instanceof CfmlCompositeElement || child instanceof PsiComment)) {
                List<FoldingDescriptor> descriptors = new ArrayList<>();
                addFoldingDescriptors(descriptors, child, document);
                result.addAll(descriptors);
            }
        }
        return result.toArray(FoldingDescriptor.EMPTY);
    }
    return FoldingDescriptor.EMPTY;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiComment(com.intellij.psi.PsiComment) CfmlFile(com.intellij.coldFusion.model.files.CfmlFile) ArrayList(java.util.ArrayList) CfmlCompositeElement(com.intellij.coldFusion.model.psi.CfmlCompositeElement) PsiElement(com.intellij.psi.PsiElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with PsiComment

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

the class DartFoldingBuilder method foldFileHeader.

@Nullable
private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Document document) {
    PsiElement firstComment = dartFile.getFirstChild();
    if (firstComment instanceof PsiWhiteSpace)
        firstComment = firstComment.getNextSibling();
    if (!(firstComment instanceof PsiComment))
        return null;
    boolean containsCustomRegionMarker = false;
    PsiElement nextAfterComments = firstComment;
    while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) {
        containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments);
        nextAfterComments = nextAfterComments.getNextSibling();
    }
    if (nextAfterComments == null)
        return null;
    if (nextAfterComments instanceof DartLibraryStatement || nextAfterComments instanceof DartPartStatement || nextAfterComments instanceof DartPartOfStatement || nextAfterComments instanceof DartImportOrExportStatement) {
        if (nextAfterComments.getPrevSibling() instanceof PsiWhiteSpace)
            nextAfterComments = nextAfterComments.getPrevSibling();
        if (nextAfterComments.equals(firstComment))
            return null;
        final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextRange().getStartOffset(), nextAfterComments.getTextRange().getStartOffset());
        if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) {
            if (!containsCustomRegionMarker) {
                descriptors.add(new FoldingDescriptor(dartFile, fileHeaderCommentsRange));
            }
            return fileHeaderCommentsRange;
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with PsiComment

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

the class DartFoldingBuilder method foldConsequentStatements.

private static <T extends PsiElement> void foldConsequentStatements(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Class<T> aClass) {
    final T firstStatement = PsiTreeUtil.getChildOfType(dartFile, aClass);
    if (firstStatement == null)
        return;
    PsiElement lastStatement = firstStatement;
    PsiElement nextElement = firstStatement;
    while (aClass.isInstance(nextElement) || nextElement instanceof PsiComment || nextElement instanceof PsiWhiteSpace) {
        if (aClass.isInstance(nextElement)) {
            lastStatement = nextElement;
        }
        nextElement = nextElement.getNextSibling();
    }
    if (lastStatement != firstStatement) {
        // after "import " or "export " or "part "
        final int startOffset = firstStatement.getTextRange().getStartOffset() + firstStatement.getFirstChild().getTextLength() + 1;
        final int endOffset = lastStatement.getTextRange().getEndOffset();
        final FoldingDescriptor descriptor = new FoldingDescriptor(firstStatement, TextRange.create(startOffset, endOffset));
        if (aClass == DartImportOrExportStatement.class) {
            // imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
            descriptor.setCanBeRemovedWhenCollapsed(true);
        }
        descriptors.add(descriptor);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 39 with PsiComment

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

the class JstdFixtureHtmlLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
    if (host instanceof PsiComment) {
        PsiComment comment = (PsiComment) host;
        if (comment.getTokenType() == JSTokenTypes.C_STYLE_COMMENT) {
            String commentStr = comment.getText();
            TextRange htmlTextRange = findTextRange(commentStr);
            if (htmlTextRange != null && HTML_LANGUAGE != null) {
                injectionPlacesRegistrar.addPlace(HTML_LANGUAGE, htmlTextRange, null, null);
            }
        }
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) TextRange(com.intellij.openapi.util.TextRange)

Example 40 with PsiComment

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

the class FileHeaderChecker method checkFileHeader.

static ProblemDescriptor checkFileHeader(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean onTheFly) {
    TIntObjectHashMap<String> offsetToProperty = new TIntObjectHashMap<>();
    FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
    Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
    Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
    if (!matcher.matches()) {
        return null;
    }
    PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
    if (element == null) {
        return null;
    }
    LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly);
    String description = InspectionsBundle.message("default.file.template.description");
    return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
Also used : Pattern(java.util.regex.Pattern) PsiComment(com.intellij.psi.PsiComment) Matcher(java.util.regex.Matcher) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Aggregations

PsiComment (com.intellij.psi.PsiComment)60 PsiElement (com.intellij.psi.PsiElement)39 Nullable (org.jetbrains.annotations.Nullable)18 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)14 ArrayList (java.util.ArrayList)10 IElementType (com.intellij.psi.tree.IElementType)8 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 Matcher (java.util.regex.Matcher)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 PsiFile (com.intellij.psi.PsiFile)5 ASTNode (com.intellij.lang.ASTNode)4 Document (com.intellij.openapi.editor.Document)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)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