use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class XmlDeclareIdInCommentAction method getUncommentedText.
@Nullable
private static String getUncommentedText(@NotNull final PsiComment comment) {
final PsiFile psiFile = comment.getContainingFile();
final Language language = psiFile.getViewProvider().getBaseLanguage();
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
if (commenter != null) {
String text = comment.getText();
final String prefix = commenter.getBlockCommentPrefix();
if (prefix != null && text.startsWith(prefix)) {
text = text.substring(prefix.length());
final String suffix = commenter.getBlockCommentSuffix();
if (suffix != null && text.length() > suffix.length()) {
return text.substring(0, text.length() - suffix.length()).trim();
}
}
}
return null;
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class BlockCommentSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(e.getLanguage());
if (commenter == null)
return null;
String blockStart = commenter.getBlockCommentPrefix();
String blockEnd = commenter.getBlockCommentSuffix();
if (blockStart == null || blockEnd == null)
return null;
String elementText = e.getText();
if (elementText == null || !elementText.startsWith(blockStart) || !elementText.endsWith(blockEnd))
return null;
TextRange elementRange = e.getTextRange();
return Collections.singletonList(new TextRange(elementRange.getStartOffset() + blockStart.length(), elementRange.getEndOffset() - blockEnd.length()));
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class CommentByLineCommentHandler method postInvoke.
@Override
public void postInvoke() {
FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.comment.line");
myCodeStyleManager = CodeStyleManager.getInstance(myProject);
CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(myProject);
// second pass - determining whether we need to comment or to uncomment
boolean allLinesCommented = true;
for (Block block : myBlocks) {
int startLine = block.startLine;
int endLine = block.endLine;
Document document = block.editor.getDocument();
PsiFile psiFile = block.psiFile;
CommonCodeStyleSettings languageSettings = codeStyleSettings.getCommonSettings(psiFile.getLanguage());
block.startOffsets = new int[endLine - startLine + 1];
block.endOffsets = new int[endLine - startLine + 1];
block.commenters = new Commenter[endLine - startLine + 1];
block.commenterStateMap = new THashMap<>();
CharSequence chars = document.getCharsSequence();
boolean singleline = startLine == endLine;
int offset = document.getLineStartOffset(startLine);
offset = CharArrayUtil.shiftForward(chars, offset, " \t");
int endOffset = CharArrayUtil.shiftBackward(chars, document.getLineEndOffset(endLine), " \t\n");
block.blockSuitableCommenter = getBlockSuitableCommenter(psiFile, offset, endOffset);
block.commentWithIndent = !languageSettings.LINE_COMMENT_AT_FIRST_COLUMN;
block.addSpace = languageSettings.LINE_COMMENT_ADD_SPACE;
for (int line = startLine; line <= endLine; line++) {
Commenter commenter = block.blockSuitableCommenter != null ? block.blockSuitableCommenter : findCommenter(block.editor, psiFile, line);
if (commenter == null || commenter.getLineCommentPrefix() == null && (commenter.getBlockCommentPrefix() == null || commenter.getBlockCommentSuffix() == null)) {
block.skip = true;
break;
}
if (commenter instanceof SelfManagingCommenter && block.commenterStateMap.get(commenter) == null) {
final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
CommenterDataHolder state = selfManagingCommenter.createLineCommentingState(startLine, endLine, document, psiFile);
if (state == null)
state = SelfManagingCommenter.EMPTY_STATE;
block.commenterStateMap.put(selfManagingCommenter, state);
}
block.commenters[line - startLine] = commenter;
if (!isLineCommented(block, line, commenter) && (singleline || !isLineEmpty(document, line))) {
allLinesCommented = false;
if (commenter instanceof IndentedCommenter) {
final Boolean value = ((IndentedCommenter) commenter).forceIndentedLineComment();
if (value != null) {
block.commentWithIndent = value;
}
}
break;
}
}
}
boolean moveCarets = true;
for (Block block : myBlocks) {
if (block.carets.size() > 1 && block.startLine != block.endLine) {
moveCarets = false;
break;
}
}
// third pass - actual change
Collections.reverse(myBlocks);
for (Block block : myBlocks) {
if (!block.skip) {
if (!allLinesCommented) {
if (!block.commentWithIndent) {
doDefaultCommenting(block);
} else {
doIndentCommenting(block);
}
} else {
for (int line = block.endLine; line >= block.startLine; line--) {
uncommentLine(block, line, block.addSpace);
}
}
}
if (!moveCarets || block.caretUpdate == null) {
continue;
}
Document document = block.editor.getDocument();
for (Caret caret : block.carets) {
switch(block.caretUpdate) {
case PUT_AT_COMMENT_START:
final Commenter commenter = block.commenters[0];
if (commenter != null) {
String prefix;
if (commenter instanceof SelfManagingCommenter) {
prefix = ((SelfManagingCommenter) commenter).getCommentPrefix(block.startLine, document, block.commenterStateMap.get((SelfManagingCommenter) commenter));
// TODO
if (prefix == null)
prefix = "";
} else {
prefix = commenter.getLineCommentPrefix();
if (prefix == null)
prefix = commenter.getBlockCommentPrefix();
}
int lineStart = document.getLineStartOffset(block.startLine);
lineStart = CharArrayUtil.shiftForward(document.getCharsSequence(), lineStart, " \t");
lineStart += prefix.length();
lineStart = CharArrayUtil.shiftForward(document.getCharsSequence(), lineStart, " \t");
if (lineStart > document.getTextLength())
lineStart = document.getTextLength();
caret.moveToOffset(lineStart);
}
break;
case SHIFT_DOWN:
// Don't tweak caret position if we're already located on the last document line.
LogicalPosition position = caret.getLogicalPosition();
if (position.line < document.getLineCount() - 1) {
int verticalShift = 1 + block.editor.getSoftWrapModel().getSoftWrapsForLine(position.line).size() - EditorUtil.getSoftWrapCountAfterLineStart(block.editor, position);
caret.moveCaretRelatively(0, verticalShift, false, true);
}
break;
case RESTORE_SELECTION:
caret.setSelection(document.getLineStartOffset(document.getLineNumber(caret.getSelectionStart())), caret.getSelectionEnd());
}
}
}
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class CommentByLineCommentHandler method getCommentStart.
private static int getCommentStart(Editor editor, PsiFile psiFile, int line) {
int offset = editor.getDocument().getLineStartOffset(line);
CharSequence chars = editor.getDocument().getCharsSequence();
offset = CharArrayUtil.shiftForward(chars, offset, " \t");
final Commenter commenter = findCommenter(editor, psiFile, line);
if (commenter == null)
return -1;
String prefix = commenter.getLineCommentPrefix();
if (prefix == null)
prefix = commenter.getBlockCommentPrefix();
if (prefix == null)
return -1;
return CharArrayUtil.regionMatches(chars, offset, prefix) ? offset : -1;
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class JoinLinesHandler method tryConvertEndOfLineComment.
private static void tryConvertEndOfLineComment(Document doc, PsiElement commentElement) {
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(commentElement.getLanguage());
if (commenter instanceof CodeDocumentationAwareCommenter) {
CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter) commenter;
String lineCommentPrefix = commenter.getLineCommentPrefix();
String blockCommentPrefix = commenter.getBlockCommentPrefix();
String blockCommentSuffix = commenter.getBlockCommentSuffix();
if (commentElement.getNode().getElementType() == docCommenter.getLineCommentTokenType() && blockCommentPrefix != null && blockCommentSuffix != null && lineCommentPrefix != null) {
String commentText = StringUtil.trimStart(commentElement.getText(), lineCommentPrefix);
try {
Project project = commentElement.getProject();
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiComment newComment = parserFacade.createBlockCommentFromText(commentElement.getLanguage(), commentText);
commentElement.replace(newComment);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(doc);
} catch (IncorrectOperationException e) {
LOG.info("Failed to replace line comment with block comment", e);
}
}
}
}
Aggregations