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);
}
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;
}
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));
}
}
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());
}
}
}
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();
}
}
Aggregations