Java Code Examples for com.intellij.lang.Commenter#getLineCommentPrefix()

The following examples show how to use com.intellij.lang.Commenter#getLineCommentPrefix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomFoldingSurroundDescriptor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
  if (startOffset >= endOffset - 1) return PsiElement.EMPTY_ARRAY;
  Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(file.getLanguage());
  if (commenter == null || commenter.getLineCommentPrefix() == null) return PsiElement.EMPTY_ARRAY;
  PsiElement startElement = file.findElementAt(startOffset);
  if (startElement instanceof PsiWhiteSpace) startElement = startElement.getNextSibling();
  PsiElement endElement = file.findElementAt(endOffset - 1);
  if (endElement instanceof PsiWhiteSpace) endElement = endElement.getPrevSibling();
  if (startElement != null && endElement != null) {
    if (startElement.getTextRange().getStartOffset() > endElement.getTextRange().getStartOffset()) return PsiElement.EMPTY_ARRAY;
    startElement = findClosestParentAfterLineBreak(startElement);
    if (startElement != null) {
      endElement = findClosestParentBeforeLineBreak(endElement);
      if (endElement != null) {
        PsiElement commonParent = startElement.getParent();
        if (endElement.getParent() == commonParent) {
          if (startElement == endElement) return new PsiElement[] {startElement};
          return new PsiElement[] {startElement, endElement};
        }
      }
    }
  }
  return PsiElement.EMPTY_ARRAY;
}
 
Example 2
Source File: LineLayout.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static String getLineCommentPrefix(IElementType token) {
  if (token == null) return null;
  Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(token.getLanguage());
  if (!(commenter instanceof CodeDocumentationAwareCommenter) || !token.equals(((CodeDocumentationAwareCommenter)commenter).getLineCommentTokenType())) return null;
  String prefix = commenter.getLineCommentPrefix();
  return prefix == null ? null : StringUtil.trimTrailing(prefix); // some commenters (e.g. for Python) include space in comment prefix
}
 
Example 3
Source File: UpdatePsiFileCopyright.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isLineComment(Commenter commenter, PsiComment comment, Document doc) {
  final String lineCommentPrefix = commenter.getLineCommentPrefix();
  if (lineCommentPrefix != null) {
    return comment.getText().startsWith(lineCommentPrefix);
  }
  final TextRange textRange = comment.getTextRange();
  return doc.getLineNumber(textRange.getStartOffset()) == doc.getLineNumber(textRange.getEndOffset());
}
 
Example 4
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: JoinLinesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean tryConvertEndOfLineComment(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);
      String suffix = docCommenter.getBlockCommentSuffix();
      if (suffix != null && suffix.length() > 1) {
        String fixedSuffix = suffix.charAt(0) + " " + suffix.substring(1);
        commentText = commentText.replace(suffix, fixedSuffix);
      }
      try {
        Project project = commentElement.getProject();
        PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
        PsiComment newComment = parserFacade.createBlockCommentFromText(commentElement.getLanguage(), commentText);
        commentElement.replace(newComment);
        return true;
      }
      catch (IncorrectOperationException e) {
        LOG.info("Failed to replace line comment with block comment", e);
      }
    }
  }
  return false;
}
 
Example 6
Source File: SuppressionUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static String getLineCommentPrefix(@Nonnull final PsiElement comment) {
  final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage());
  return commenter == null ? null : commenter.getLineCommentPrefix();
}
 
Example 7
Source File: FileTypeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean hasLineComment(FileType fileType) {
  Commenter commenter = getCommenter(fileType);

  return commenter != null && commenter.getLineCommentPrefix() != null;
}
 
Example 8
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean isLineCommented(Block block, final int line, final Commenter commenter) {
  boolean commented;
  int lineEndForBlockCommenting = -1;
  Document document = block.editor.getDocument();
  int lineStart = document.getLineStartOffset(line);
  CharSequence chars = document.getCharsSequence();
  lineStart = CharArrayUtil.shiftForward(chars, lineStart, " \t");

  if (commenter instanceof SelfManagingCommenter) {
    final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter;
    commented = selfManagingCommenter.isLineCommented(line, lineStart, document, block.commenterStateMap.get(selfManagingCommenter));
  }
  else {
    String prefix = commenter.getLineCommentPrefix();

    if (prefix != null) {
      commented = CharArrayUtil.regionMatches(chars, lineStart, StringUtil.trimTrailing(prefix));
    }
    else {
      prefix = commenter.getBlockCommentPrefix();
      String suffix = commenter.getBlockCommentSuffix();
      final int textLength = document.getTextLength();
      lineEndForBlockCommenting = document.getLineEndOffset(line);
      if (lineEndForBlockCommenting == textLength) {
        final int shifted = CharArrayUtil.shiftBackward(chars, textLength - 1, " \t");
        if (shifted < textLength - 1) lineEndForBlockCommenting = shifted;
      }
      else {
        lineEndForBlockCommenting = CharArrayUtil.shiftBackward(chars, lineEndForBlockCommenting, " \t");
      }
      commented = lineStart == lineEndForBlockCommenting && block.startLine != block.endLine ||
                  CharArrayUtil.regionMatches(chars, lineStart, prefix) && CharArrayUtil.regionMatches(chars, lineEndForBlockCommenting - suffix.length(), suffix);
    }
  }

  if (commented) {
    block.startOffsets[line - block.startLine] = lineStart;
    block.endOffsets[line - block.startLine] = lineEndForBlockCommenting;
  }

  return commented;
}