use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class CommentByLineCommentHandler method getBlockSuitableCommenter.
private static Commenter getBlockSuitableCommenter(final PsiFile file, int offset, int endOffset) {
final Language languageSuitableForCompleteFragment;
if (offset >= endOffset) {
// we are on empty line
PsiElement element = file.findElementAt(offset);
if (element != null)
languageSuitableForCompleteFragment = element.getParent().getLanguage();
else
languageSuitableForCompleteFragment = null;
} else {
languageSuitableForCompleteFragment = PsiUtilBase.reallyEvaluateLanguageInRange(offset, endOffset, file);
}
Commenter blockSuitableCommenter = languageSuitableForCompleteFragment == null ? LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()) : null;
if (blockSuitableCommenter == null && file.getFileType() instanceof CustomSyntaxTableFileType) {
blockSuitableCommenter = new Commenter() {
final SyntaxTable mySyntaxTable = ((CustomSyntaxTableFileType) file.getFileType()).getSyntaxTable();
@Override
@Nullable
public String getLineCommentPrefix() {
return mySyntaxTable.getLineComment();
}
@Override
@Nullable
public String getBlockCommentPrefix() {
return mySyntaxTable.getStartComment();
}
@Override
@Nullable
public String getBlockCommentSuffix() {
return mySyntaxTable.getEndComment();
}
@Override
public String getCommentedBlockCommentPrefix() {
return null;
}
@Override
public String getCommentedBlockCommentSuffix() {
return null;
}
};
}
return blockSuitableCommenter;
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class CommentByLineCommentHandler method uncommentLine.
private static void uncommentLine(Block block, int line, boolean removeSpace) {
Document document = block.editor.getDocument();
Commenter commenter = block.commenters[line - block.startLine];
if (commenter == null)
commenter = findCommenter(block.editor, block.psiFile, line);
if (commenter == null)
return;
final int startOffset = block.startOffsets[line - block.startLine];
if (commenter instanceof SelfManagingCommenter) {
final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter) commenter;
selfManagingCommenter.uncommentLine(line, startOffset, document, block.commenterStateMap.get(selfManagingCommenter));
return;
}
final int endOffset = block.endOffsets[line - block.startLine];
if (startOffset == endOffset) {
return;
}
RangeMarker marker = endOffset > startOffset ? block.editor.getDocument().createRangeMarker(startOffset, endOffset) : null;
try {
if (doUncommentLine(line, document, commenter, startOffset, endOffset, removeSpace))
return;
if (marker != null) {
CommentByBlockCommentHandler.processDocument(document, marker, commenter, false);
}
} finally {
if (marker != null) {
marker.dispose();
}
}
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class CommentByBlockCommentAction method isValidFor.
@Override
protected boolean isValidFor(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull final PsiFile file) {
final FileType fileType = file.getFileType();
if (fileType instanceof AbstractFileType) {
return ((AbstractFileType) fileType).getCommenter() != null;
}
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage());
if (commenter == null)
commenter = LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage());
if (commenter == null)
return false;
return commenter.getBlockCommentPrefix() != null && commenter.getBlockCommentSuffix() != null;
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class BlockMarkerCommentsInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new PsiElementVisitor() {
@Override
public void visitComment(final PsiComment element) {
final IElementType tokenType = element.getTokenType();
if (!(tokenType.equals(JavaTokenType.END_OF_LINE_COMMENT))) {
return;
}
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(element.getLanguage());
String rawCommentText = element.getText();
final String prefix = commenter.getLineCommentPrefix();
if (prefix != null && rawCommentText.startsWith(prefix)) {
rawCommentText = rawCommentText.substring(prefix.length());
}
final String commentText = rawCommentText.trim().toLowerCase();
if (!commentText.startsWith(END_WORD) || StringUtil.split(commentText, " ").size() > 3) {
return;
}
if (MARKER_PATTERN.accepts(element)) {
holder.registerProblem(element, "Redundant block marker", new LocalQuickFix() {
@NotNull
@Override
public String getFamilyName() {
return "Remove block marker comments";
}
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
descriptor.getPsiElement().delete();
}
});
}
}
};
}
use of com.intellij.lang.Commenter in project intellij-community by JetBrains.
the class PsiParserFacadeImpl method createBlockCommentFromText.
@NotNull
@Override
public PsiComment createBlockCommentFromText(@NotNull Language language, @NotNull String text) throws IncorrectOperationException {
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
assert commenter != null : language;
final String blockCommentPrefix = commenter.getBlockCommentPrefix();
final String blockCommentSuffix = commenter.getBlockCommentSuffix();
PsiFile aFile = PsiFileFactory.getInstance(myManager.getProject()).createFileFromText("_Dummy_", language, (blockCommentPrefix + text + blockCommentSuffix));
return findPsiCommentChild(aFile);
}
Aggregations