Search in sources :

Example 11 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class UpdatePsiFileCopyright method getLineCopyrightComments.

private static CommentRange getLineCopyrightComments(List<PsiComment> comments, Document doc, int i, PsiComment comment) {
    PsiElement firstComment = comment;
    PsiElement lastComment = comment;
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilCore.findLanguageFromElement(comment));
    if (isLineComment(commenter, comment, doc)) {
        int sline = doc.getLineNumber(comment.getTextRange().getStartOffset());
        int eline = doc.getLineNumber(comment.getTextRange().getEndOffset());
        for (int j = i - 1; j >= 0; j--) {
            PsiComment cmt = comments.get(j);
            if (isLineComment(commenter, cmt, doc) && doc.getLineNumber(cmt.getTextRange().getEndOffset()) == sline - 1) {
                firstComment = cmt;
                sline = doc.getLineNumber(cmt.getTextRange().getStartOffset());
            } else {
                break;
            }
        }
        for (int j = i + 1; j < comments.size(); j++) {
            PsiComment cmt = comments.get(j);
            if (isLineComment(commenter, cmt, doc) && doc.getLineNumber(cmt.getTextRange().getStartOffset()) == eline + 1) {
                lastComment = cmt;
                eline = doc.getLineNumber(cmt.getTextRange().getEndOffset());
            } else {
                break;
            }
        }
    }
    return new CommentRange(firstComment, lastComment);
}
Also used : Commenter(com.intellij.lang.Commenter)

Example 12 with Commenter

use of com.intellij.lang.Commenter in project ideavim by JetBrains.

the class SearchHelper method findMatchingBlockCommentPair.

private static int findMatchingBlockCommentPair(@NotNull PsiElement element, int pos) {
    final Language language = element.getLanguage();
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
    final PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
    if (comment != null) {
        final int ret = findMatchingBlockCommentPair(comment, pos, commenter.getBlockCommentPrefix(), commenter.getBlockCommentSuffix());
        if (ret >= 0) {
            return ret;
        }
        if (commenter instanceof CodeDocumentationAwareCommenter) {
            final CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter;
            return findMatchingBlockCommentPair(comment, pos, docCommenter.getDocumentationCommentPrefix(), docCommenter.getDocumentationCommentSuffix());
        }
    }
    return -1;
}
Also used : CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) PsiComment(com.intellij.psi.PsiComment) Language(com.intellij.lang.Language) CodeDocumentationAwareCommenter(com.intellij.lang.CodeDocumentationAwareCommenter) Commenter(com.intellij.lang.Commenter)

Example 13 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class ActionHint method parse.

/**
   * Parse given file with given contents extracting ActionHint of it.
   * <p>
   * Currently the following syntax is supported:
   * </p>
   * {@code // "quick-fix name or intention text" "true|false|<ProblemHighlightType>"}
   * <p>
   * (replace // with line comment prefix in the corresponding language if necessary).
   * If {@link ProblemHighlightType} enum value is specified instead of true/false
   * (e.g. {@code "INFORMATION"}), then
   * it's expected that the action is present and it's a quick-fix with given highlight type.
   * </p>
   *
   * @param file PsiFile associated with contents (used to determine the language)
   * @param contents file contents
   * @return ActionHint object
   * @throws AssertionError if action hint is absent or has invalid format
   */
@NotNull
public static ActionHint parse(@NotNull PsiFile file, @NotNull String contents) {
    PsiFile hostFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(hostFile.getLanguage());
    String comment = commenter.getLineCommentPrefix();
    if (comment == null) {
        comment = commenter.getBlockCommentPrefix();
    }
    assert comment != null : commenter;
    // "quick fix action text to perform" "should be available"
    Pattern pattern = Pattern.compile("^" + Pattern.quote(comment) + " \"(.*)\" \"(\\w+)\".*", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(contents);
    TestCase.assertTrue("No comment found in " + file.getVirtualFile(), matcher.matches());
    final String text = matcher.group(1);
    String state = matcher.group(2);
    if (state.equals("true") || state.equals("false")) {
        return new ActionHint(text, Boolean.parseBoolean(state), null);
    } else {
        return new ActionHint(text, true, ProblemHighlightType.valueOf(state));
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Commenter(com.intellij.lang.Commenter) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class CommentByBlockCommentHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull PsiFile file) {
    myProject = project;
    myEditor = editor;
    myCaret = caret;
    myFile = file;
    myDocument = editor.getDocument();
    FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.comment.block");
    final Commenter commenter = findCommenter(myFile, myEditor, caret);
    if (commenter == null)
        return;
    final String prefix;
    final String suffix;
    if (commenter instanceof SelfManagingCommenter) {
        final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
        mySelfManagedCommenterData = selfManagingCommenter.createBlockCommentingState(caret.getSelectionStart(), caret.getSelectionEnd(), myDocument, myFile);
        if (mySelfManagedCommenterData == null) {
            mySelfManagedCommenterData = SelfManagingCommenter.EMPTY_STATE;
        }
        prefix = selfManagingCommenter.getBlockCommentPrefix(caret.getSelectionStart(), myDocument, mySelfManagedCommenterData);
        suffix = selfManagingCommenter.getBlockCommentSuffix(caret.getSelectionEnd(), myDocument, mySelfManagedCommenterData);
    } else {
        prefix = commenter.getBlockCommentPrefix();
        suffix = commenter.getBlockCommentSuffix();
    }
    if (prefix == null || suffix == null)
        return;
    TextRange commentedRange = findCommentedRange(commenter);
    if (commentedRange != null) {
        final int commentStart = commentedRange.getStartOffset();
        final int commentEnd = commentedRange.getEndOffset();
        int selectionStart = commentStart;
        int selectionEnd = commentEnd;
        if (myCaret.hasSelection()) {
            selectionStart = myCaret.getSelectionStart();
            selectionEnd = myCaret.getSelectionEnd();
        }
        if ((commentStart < selectionStart || commentStart >= selectionEnd) && (commentEnd <= selectionStart || commentEnd > selectionEnd)) {
            commentRange(selectionStart, selectionEnd, prefix, suffix, commenter);
        } else {
            uncommentRange(commentedRange, trim(prefix), trim(suffix), commenter);
        }
    } else {
        if (myCaret.hasSelection()) {
            int selectionStart = myCaret.getSelectionStart();
            int selectionEnd = myCaret.getSelectionEnd();
            if (commenter instanceof IndentedCommenter) {
                final Boolean value = ((IndentedCommenter) commenter).forceIndentedLineComment();
                if (value != null && value == Boolean.TRUE) {
                    selectionStart = myDocument.getLineStartOffset(myDocument.getLineNumber(selectionStart));
                    selectionEnd = myDocument.getLineEndOffset(myDocument.getLineNumber(selectionEnd));
                }
            }
            commentRange(selectionStart, selectionEnd, prefix, suffix, commenter);
        } else {
            EditorUtil.fillVirtualSpaceUntilCaret(editor);
            int caretOffset = myCaret.getOffset();
            if (commenter instanceof IndentedCommenter) {
                final Boolean value = ((IndentedCommenter) commenter).forceIndentedLineComment();
                if (value != null && value == Boolean.TRUE) {
                    final int lineNumber = myDocument.getLineNumber(caretOffset);
                    final int start = myDocument.getLineStartOffset(lineNumber);
                    final int end = myDocument.getLineEndOffset(lineNumber);
                    commentRange(start, end, prefix, suffix, commenter);
                    return;
                }
            }
            myDocument.insertString(caretOffset, prefix + suffix);
            myCaret.moveToOffset(caretOffset + prefix.length());
        }
    }
}
Also used : Commenter(com.intellij.lang.Commenter) TextRange(com.intellij.openapi.util.TextRange)

Example 15 with Commenter

use of com.intellij.lang.Commenter in project intellij-community by JetBrains.

the class CommentByLineCommentHandler method commentLine.

private static void commentLine(Block block, int line, int offset) {
    Commenter commenter = block.blockSuitableCommenter;
    Document document = block.editor.getDocument();
    if (commenter == null)
        commenter = findCommenter(block.editor, block.psiFile, line);
    if (commenter == null)
        return;
    if (commenter instanceof SelfManagingCommenter) {
        final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
        selfManagingCommenter.commentLine(line, offset, document, block.commenterStateMap.get(selfManagingCommenter));
        return;
    }
    int endOffset = document.getLineEndOffset(line);
    RangeMarker marker = document.createRangeMarker(offset, endOffset);
    marker.setGreedyToLeft(true);
    marker.setGreedyToRight(true);
    try {
        if (doCommentLine(block, line, offset, endOffset, commenter, document))
            return;
        CommentByBlockCommentHandler.processDocument(document, marker, commenter, true);
    } finally {
        marker.dispose();
    }
}
Also used : Commenter(com.intellij.lang.Commenter)

Aggregations

Commenter (com.intellij.lang.Commenter)22 NotNull (org.jetbrains.annotations.NotNull)6 Language (com.intellij.lang.Language)4 PsiFile (com.intellij.psi.PsiFile)4 Nullable (org.jetbrains.annotations.Nullable)3 CodeDocumentationAwareCommenter (com.intellij.lang.CodeDocumentationAwareCommenter)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiComment (com.intellij.psi.PsiComment)2 PsiElement (com.intellij.psi.PsiElement)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 SyntaxTable (com.intellij.ide.highlighter.custom.SyntaxTable)1 InjectedCaret (com.intellij.injected.editor.InjectedCaret)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 FileType (com.intellij.openapi.fileTypes.FileType)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)1 IElementType (com.intellij.psi.tree.IElementType)1 XmlTag (com.intellij.psi.xml.XmlTag)1