com.intellij.lang.LanguageCommenters Java Examples

The following examples show how to use com.intellij.lang.LanguageCommenters. 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: 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 #4
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 #5
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 #6
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 #7
Source File: CommentByLineCommentAction.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;
  }

  if (LanguageCommenters.INSTANCE.forLanguage(file.getLanguage()) != null || LanguageCommenters.INSTANCE.forLanguage(file.getViewProvider().getBaseLanguage()) != null) return true;
  PsiElement host = InjectedLanguageManager.getInstance(project).getInjectionHost(file);
  return host != null && LanguageCommenters.INSTANCE.forLanguage(host.getLanguage()) != null;
}
 
Example #8
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 #9
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 #10
Source File: ArrangementSectionRuleManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private ArrangementSectionRuleManager(@Nonnull Language language,
                                      @Nonnull ArrangementStandardSettingsManager settingsManager,
                                      @Nonnull ArrangementColorsProvider colorsProvider,
                                      @Nonnull ArrangementMatchingRulesControl control) {
  myCommenter = LanguageCommenters.INSTANCE.forLanguage(language);
  myControl = control;
  final List<CompositeArrangementSettingsToken> tokens = ContainerUtil.newArrayList();
  tokens.add(new CompositeArrangementSettingsToken(TYPE, ContainerUtil.newArrayList(START_SECTION, END_SECTION)));
  tokens.add(new CompositeArrangementSettingsToken(TEXT));
  myEditor = new ArrangementMatchingRuleEditor(settingsManager, tokens, colorsProvider, control);
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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);
}