com.intellij.lang.Commenter Java Examples

The following examples show how to use com.intellij.lang.Commenter. 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: HaxeCommentSelectioner.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private boolean isCommentToken(PsiElement e, CharSequence token) {
  final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(HaxeLanguage.INSTANCE);
  assert(commenter instanceof HaxeCommenter);
  final IElementType tokenType = e.getNode().getElementType();

  if (tokenType == HaxeTokenTypeSets.DOC_COMMENT) {

    // XXX: Should we be checking that the token is at the beginning or end of the element?
    //      Or, that the line prefix is actually the first thing on the line?
    return  ((HaxeCommenter)commenter).getDocumentationCommentLinePrefix().contentEquals(token)
            || ((HaxeCommenter)commenter).getDocumentationCommentPrefix().contentEquals(token)
            || ((HaxeCommenter)commenter).getDocumentationCommentSuffix().contentEquals(token)
            // A lot of folks don't use the proper doc comment terminator "**/", and the compiler
            // accepts a normal block comment terminator "*/".
            || commenter.getBlockCommentSuffix().contentEquals(token);

  } else if (tokenType == HaxeTokenTypeSets.MML_COMMENT) {

    return  commenter.getBlockCommentPrefix().contentEquals(token)
            || commenter.getBlockCommentSuffix().contentEquals(token);

  }
  return commenter.getLineCommentPrefix().contentEquals(token);
}
 
Example #2
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 #3
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
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();
  }
}
 
Example #4
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void uncommentRange(Document document, int startOffset, int endOffset, @Nonnull Commenter commenter) {
  final String commentedSuffix = commenter.getCommentedBlockCommentSuffix();
  final String commentedPrefix = commenter.getCommentedBlockCommentPrefix();
  final String prefix = commenter.getBlockCommentPrefix();
  final String suffix = commenter.getBlockCommentSuffix();
  if (prefix == null || suffix == null) {
    return;
  }
  if (endOffset >= suffix.length() && CharArrayUtil.regionMatches(document.getCharsSequence(), endOffset - suffix.length(), suffix)) {
    document.deleteString(endOffset - suffix.length(), endOffset);
    endOffset -= suffix.length();
  }
  if (commentedPrefix != null && commentedSuffix != null) {
    CommentByBlockCommentHandler.commentNestedComments(document, new TextRange(startOffset, endOffset), commenter);
  }
  document.deleteString(startOffset, startOffset + prefix.length());
}
 
Example #5
Source File: CommentLiteralEscaper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isOneLine() {
  final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(myHost.getLanguage());
  if (commenter instanceof CodeDocumentationAwareCommenter) {
    return myHost.getTokenType() == ((CodeDocumentationAwareCommenter) commenter).getLineCommentTokenType();
  }
  return false;
}
 
Example #6
Source File: SoyMultiLangCommentProvider.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Commenter getLineCommenter(
    PsiFile file, Editor editor, Language lineStartLanguage, Language lineEndLanguage) {
  if (lineStartLanguage != null && lineStartLanguage == lineEndLanguage) {
    return LanguageCommenters.INSTANCE.forLanguage(lineStartLanguage);
  }
  return null;
}
 
Example #7
Source File: EditorSmartKeysConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean hasAnyDocAwareCommenters() {
  final Collection<Language> languages = Language.getRegisteredLanguages();
  for (Language language : languages) {
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
    if (commenter instanceof CodeDocumentationAwareCommenter) {
      final CodeDocumentationAwareCommenter docCommenter = (CodeDocumentationAwareCommenter)commenter;
      if (docCommenter.getDocumentationCommentLinePrefix() != null) {
        return true;
      }
    }
  }
  return false;
}
 
Example #8
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 #9
Source File: LineCommentSelectioner.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canSelect(PsiElement e) {
  if (e instanceof PsiComment) {
    final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(e.getLanguage());
    if (!(commenter instanceof CodeDocumentationAwareCommenter)) return true;
    return !((CodeDocumentationAwareCommenter) commenter).isDocumentationComment((PsiComment)e);
  }
  return false;
}
 
Example #10
Source File: CodeDocumentationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
static CommentContext tryParseCommentContext(@Nullable Commenter langCommenter, @Nonnull CharSequence chars, int lineStartOffset) {
  final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter;
  if (!isInsideCommentLikeCode) {
    return new CommentContext();
  }
  final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)langCommenter;
  int commentStartOffset = CharArrayUtil.shiftForward(chars, lineStartOffset, " \t");

  boolean docStart = commenter.getDocumentationCommentPrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentPrefix());
  boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null && CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix());
  return new CommentContext(commenter, docStart, docAsterisk, commentStartOffset);
}
 
Example #11
Source File: CommentByBlockCommentAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValidFor(@Nonnull Project project, @Nonnull Editor editor, @Nonnull Caret caret, @Nonnull 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;
}
 
Example #12
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
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];
  final int endOffset = block.endOffsets[line - block.startLine];
  if (startOffset == endOffset) {
    return;
  }

  if (commenter instanceof SelfManagingCommenter) {
    final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter;
    selfManagingCommenter.uncommentLine(line, startOffset, document, block.commenterStateMap.get(selfManagingCommenter));
    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();
    }
  }
}
 
Example #13
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 #14
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Commenter findCommenter(@Nonnull Editor editor, @Nonnull PsiFile file, final int line) {
  final FileType fileType = file.getFileType();
  if (fileType instanceof AbstractFileType) {
    return ((AbstractFileType)fileType).getCommenter();
  }
  final Language lineStartLanguage = getLineStartLanguage(editor, file, line);
  final Language lineEndLanguage = getLineEndLanguage(file, editor, line);
  return CommentByBlockCommentHandler.getCommenter(file, editor, lineStartLanguage, lineEndLanguage);
}
 
Example #15
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 #16
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 #17
Source File: SuppressionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
public static Couple<String> getBlockPrefixSuffixPair(PsiElement comment) {
  final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(comment.getLanguage());
  if (commenter != null) {
    final String prefix = commenter.getBlockCommentPrefix();
    final String suffix = commenter.getBlockCommentSuffix();
    if (prefix != null || suffix != null) {
      return Couple.of(StringUtil.notNullize(prefix), StringUtil.notNullize(suffix));
    }
  }
  return null;
}
 
Example #18
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;
}
 
Example #19
Source File: FileTypeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static Commenter getCommenter(FileType fileType) {
  if (fileType instanceof LanguageFileType) {
    return LanguageCommenters.INSTANCE.forLanguage(((LanguageFileType)fileType).getLanguage());
  }
  return null;
}
 
Example #20
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 #21
Source File: FileTypeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean hasBlockComment(FileType fileType) {
  Commenter commenter = getCommenter(fileType);

  return commenter != null && commenter.getBlockCommentPrefix() != null;
}
 
Example #22
Source File: CommentByLineCommentHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean doUncommentLine(int line, Document document, Commenter commenter, int startOffset, int endOffset, boolean removeSpace) {
  String prefix = commenter.getLineCommentPrefix();
  if (prefix != null) {
    if (removeSpace) prefix += ' ';
    CharSequence chars = document.getCharsSequence();

    if (commenter instanceof CommenterWithLineSuffix) {
      CommenterWithLineSuffix commenterWithLineSuffix = (CommenterWithLineSuffix)commenter;
      String suffix = commenterWithLineSuffix.getLineCommentSuffix();


      int theEnd = endOffset > 0 ? endOffset : document.getLineEndOffset(line);
      while (theEnd > startOffset && Character.isWhitespace(chars.charAt(theEnd - 1))) {
        theEnd--;
      }


      String lineText = document.getText(new TextRange(startOffset, theEnd));
      if (lineText.indexOf(suffix) != -1) {
        int start = startOffset + lineText.indexOf(suffix);
        document.deleteString(start, start + suffix.length());
      }
    }

    boolean matchesTrimmed = false;
    boolean commented = CharArrayUtil.regionMatches(chars, startOffset, prefix) || (matchesTrimmed = prefix.endsWith(" ") && CharArrayUtil.regionMatches(chars, startOffset, prefix.trim()));
    assert commented;

    int charsToDelete = matchesTrimmed ? prefix.trim().length() : prefix.length();
    document.deleteString(startOffset, startOffset + charsToDelete);

    // delete whitespace on line if that's all that left after uncommenting
    int lineStartOffset = document.getLineStartOffset(line);
    int lineEndOffset = document.getLineEndOffset(line);
    if (CharArrayUtil.isEmptyOrSpaces(chars, lineStartOffset, lineEndOffset)) document.deleteString(lineStartOffset, lineEndOffset);

    return true;
  }
  String text = document.getCharsSequence().subSequence(startOffset, endOffset).toString();

  prefix = commenter.getBlockCommentPrefix();
  final String suffix = commenter.getBlockCommentSuffix();
  if (prefix == null || suffix == null) {
    return true;
  }

  IntArrayList prefixes = new IntArrayList();
  IntArrayList suffixes = new IntArrayList();
  for (int position = 0; position < text.length(); ) {
    int prefixPos = text.indexOf(prefix, position);
    if (prefixPos == -1) {
      break;
    }
    prefixes.add(prefixPos);
    position = prefixPos + prefix.length();
    int suffixPos = text.indexOf(suffix, position);
    if (suffixPos == -1) {
      suffixPos = text.length() - suffix.length();
    }
    suffixes.add(suffixPos);
    position = suffixPos + suffix.length();
  }

  assert prefixes.size() == suffixes.size();

  for (int i = prefixes.size() - 1; i >= 0; i--) {
    uncommentRange(document, startOffset + prefixes.get(i), Math.min(startOffset + suffixes.get(i) + suffix.length(), endOffset), commenter);
  }
  return false;
}
 
Example #23
Source File: FileTypeUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static String buildComment(@Nonnull FileType type, @Nonnull String template, @Nonnull CopyrightFileConfig options) {
  Commenter commenter = getCommenter(type);
  UpdateCopyrightsProvider updateCopyrightsProvider = CopyrightUpdaters.INSTANCE.forFileType(type);
  return buildComment(commenter, updateCopyrightsProvider.isAllowSeparator(), template, options);
}
 
Example #24
Source File: AbstractFileType.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setCommenter(final Commenter commenter) {
  myCommenter = commenter;
}
 
Example #25
Source File: AbstractFileType.java    From consulo with Apache License 2.0 4 votes vote down vote up
public Commenter getCommenter() {
  return myCommenter;
}
 
Example #26
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 #27
Source File: MultipleLangCommentProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
Commenter getLineCommenter(PsiFile file, Editor editor,
                           Language lineStartLanguage, Language lineEndLanguage);
 
Example #28
Source File: CodeDocumentationUtil.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Analyzes position at the given offset at the given text and returns information about comments presence and kind there if any.
 *
 * @param file            target file being edited (necessary for language recognition at target offset. Language is necessary
 *                        to get information about specific comment syntax)
 * @param chars           target text
 * @param offset          target offset at the given text
 * @param lineStartOffset start offset of the line that contains given offset
 * @return object that encapsulates information about comments at the given offset at the given text
 */
@Nonnull
public static CommentContext tryParseCommentContext(@Nonnull PsiFile file, @Nonnull CharSequence chars, int offset, int lineStartOffset) {
  Commenter langCommenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilCore.getLanguageAtOffset(file, offset));
  return tryParseCommentContext(langCommenter, chars, lineStartOffset);
}