Java Code Examples for com.intellij.openapi.editor.highlighter.HighlighterIterator#getTokenType()

The following examples show how to use com.intellij.openapi.editor.highlighter.HighlighterIterator#getTokenType() . 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: SimpleTokenSetQuoteHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) {
  int start = iterator.getStart();
  try {
    Document doc = editor.getDocument();
    CharSequence chars = doc.getCharsSequence();
    int lineEnd = doc.getLineEndOffset(doc.getLineNumber(offset));

    while (!iterator.atEnd() && iterator.getStart() < lineEnd) {
      IElementType tokenType = iterator.getTokenType();

      if (myLiteralTokenSet.contains(tokenType)) {
        if (isNonClosedLiteral(iterator, chars)) return true;
      }
      iterator.advance();
    }
  }
  finally {
    while(iterator.atEnd() || iterator.getStart() != start) iterator.retreat();
  }

  return false;
}
 
Example 2
Source File: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  IElementType tokenType = it.getTokenType();
  Language language = tokenType.getLanguage();
  TokenSet comments = myComments.get(language);
  if (comments == null) {
    ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    if (definition != null) {
      comments = definition.getCommentTokens();
    }
    if (comments == null) {
      return false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 3
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findLeftLParen(HighlighterIterator iterator,
                                 IElementType lparenTokenType,
                                 CharSequence fileText,
                                 FileType fileType) {
  int lastLbraceOffset = -1;

  Stack<IElementType> braceStack = new Stack<IElementType>();
  for (; !iterator.atEnd(); iterator.retreat()) {
    final IElementType tokenType = iterator.getTokenType();

    if (isLBraceToken(iterator, fileText, fileType)) {
      if (!braceStack.isEmpty()) {
        IElementType topToken = braceStack.pop();
        if (!isPairBraces(tokenType, topToken, fileType)) {
          break; // unmatched braces
        }
      }
      else {
        if (tokenType == lparenTokenType) {
          return iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isRBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastLbraceOffset;
}
 
Example 4
Source File: XQueryQuoteHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isOpeningQuote(HighlighterIterator iterator, int offset) {
    if (OPENINGQUOT == iterator.getTokenType() || OPENINGAPOS == iterator.getTokenType()){
        int start = iterator.getStart();
        return offset == start;
    }

    return false;
}
 
Example 5
Source File: CommentByBlockCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean breaksExistingComment(int offset, boolean includingAfterLineComment) {
  if (!(myCommenter instanceof CodeDocumentationAwareCommenter) || !(myEditor instanceof EditorEx) || offset == 0) return false;
  CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)myCommenter;
  HighlighterIterator it = ((EditorEx)myEditor).getHighlighter().createIterator(offset - 1);
  IElementType tokenType = it.getTokenType();
  return (tokenType != null &&
          (it.getEnd() > offset &&
           (tokenType == commenter.getLineCommentTokenType() || tokenType == commenter.getBlockCommentTokenType() || tokenType == commenter.getDocumentationCommentTokenType()) ||
           includingAfterLineComment && it.getEnd() == offset && tokenType == commenter.getLineCommentTokenType() && !(commenter instanceof CommenterWithLineSuffix)));
}
 
Example 6
Source File: BashAnnotatorHighlightingTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private List<TextAttributes> collectComments() {
    List<TextAttributes> result = Lists.newArrayList();

    HighlighterIterator iterator = ((EditorEx) myFixture.getEditor()).getHighlighter().createIterator(0);
    while (!iterator.atEnd()) {
        if (iterator.getTokenType() == BashTokenTypes.COMMENT) {
            result.add(iterator.getTextAttributes());
        }
        iterator.advance();
    }
    return result;
}
 
Example 7
Source File: PairedBraceMatcherAdapter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isStructuralBrace(HighlighterIterator iterator, CharSequence text, FileType fileType) {
  final IElementType tokenType = iterator.getTokenType();
  final BracePair[] pairs = myMatcher.getPairs();
  for (BracePair pair : pairs) {
    if (tokenType == pair.getRightBraceType() || tokenType == pair.getLeftBraceType()) return pair.isStructural();
  }
  return false;
}
 
Example 8
Source File: CustomFileTypeQuoteHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isClosingQuote(HighlighterIterator iterator, int offset) {
  final IElementType tokenType = iterator.getTokenType();

  if (tokenType == CustomHighlighterTokenType.STRING ||
      tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING ||
      tokenType == CustomHighlighterTokenType.CHARACTER){
    int start = iterator.getStart();
    int end = iterator.getEnd();
    return end - start >= 1 && offset == end - 1;
  }
  return false;
}
 
Example 9
Source File: CSharpDocBraceMatcher.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLBraceToken(HighlighterIterator iterator, CharSequence fileText, FileType fileType)
{
	final IElementType tokenType = iterator.getTokenType();

	return tokenType == CSharpDocTokenType.XML_START_TAG_START ||
			tokenType == CSharpDocTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER;
}
 
Example 10
Source File: BaseIndentEnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected IElementType getNonWhitespaceElementType(final HighlighterIterator iterator, int currentLineStartOffset, final int prevLineStartOffset) {
  while (!iterator.atEnd() && iterator.getEnd() >= currentLineStartOffset && iterator.getStart() >= prevLineStartOffset) {
    final IElementType tokenType = iterator.getTokenType();
    if (!myWhitespaceTokens.contains(tokenType)) {
      return tokenType;
    }
    iterator.retreat();
  }
  return null;
}
 
Example 11
Source File: BraceHighlightingHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void doHighlight(int offset, int originalOffset, @Nonnull FileType fileType) {
  if (myEditor.getFoldingModel().isOffsetCollapsed(offset)) return;

  HighlighterIterator iterator = getEditorHighlighter().createIterator(offset);
  final CharSequence chars = myDocument.getCharsSequence();

  if (BraceMatchingUtil.isLBraceToken(iterator, chars, fileType)) {
    IElementType tokenType = iterator.getTokenType();

    iterator.advance();
    if (!iterator.atEnd() && BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
      if (BraceMatchingUtil.isPairBraces(tokenType, iterator.getTokenType(), fileType) &&
          originalOffset == iterator.getStart()) return;
    }

    iterator.retreat();
    highlightLeftBrace(iterator, false, fileType);

    if (offset > 0) {
      iterator = getEditorHighlighter().createIterator(offset - 1);
      if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
        highlightRightBrace(iterator, fileType);
      }
    }
  }
  else if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
    highlightRightBrace(iterator, fileType);
  }
}
 
Example 12
Source File: CustomFileTypeCompletionContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean inCommentOrLiteral(CompletionParameters parameters) {
  HighlighterIterator iterator = ((EditorEx)parameters.getEditor()).getHighlighter().createIterator(parameters.getOffset());
  IElementType elementType = iterator.getTokenType();
  if (elementType == CustomHighlighterTokenType.WHITESPACE) {
    iterator.retreat();
    elementType = iterator.getTokenType();
  }
  return elementType == CustomHighlighterTokenType.LINE_COMMENT ||
         elementType == CustomHighlighterTokenType.MULTI_LINE_COMMENT ||
         elementType == CustomHighlighterTokenType.STRING ||
         elementType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING;
}
 
Example 13
Source File: FluidQuoteHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public boolean isClosingQuote(HighlighterIterator iterator, int offset) {
    if (this.isInsideLiteral(iterator)) {
        int start = iterator.getStart();
        int end = iterator.getEnd();
        return end - start >= 1 && offset == end - 1;
    } else {
        IElementType tokenType = iterator.getTokenType();
        return tokenType == FluidTypes.DOUBLE_QUOTE || tokenType == FluidTypes.SINGLE_QUOTE;
    }
}
 
Example 14
Source File: CustomFileTypeQuoteHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isOpeningQuote(HighlighterIterator iterator, int offset) {
  final IElementType tokenType = iterator.getTokenType();

  if (tokenType == CustomHighlighterTokenType.STRING ||
      tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING ||
      tokenType == CustomHighlighterTokenType.CHARACTER){
    int start = iterator.getStart();
    return offset == start;
  }
  return false;
}
 
Example 15
Source File: BraceMatchingUtilAdapter.java    From HighlightBracketPair with Apache License 2.0 5 votes vote down vote up
/**
 * find the right closest brace offset position
 *
 * @param iterator highlight iterator
 * @param rparenTokenType right token type to paired
 * @param fileText file text
 * @param fileType file type
 * @return offset
 */
public static int findRightRParen(HighlighterIterator iterator,
                                  IElementType rparenTokenType,
                                  CharSequence fileText,
                                  FileType fileType, boolean isBlockCaret) {
    int lastRbraceOffset = -1;
    int initOffset = iterator.atEnd() ? -1 : iterator.getStart();
    Stack<IElementType> braceStack = new Stack<>();
    for (; !iterator.atEnd(); iterator.advance()) {
        final IElementType tokenType = iterator.getTokenType();

        if (isRBraceToken(iterator, fileText, fileType)) {
            if (!braceStack.isEmpty()) {
                IElementType topToken = braceStack.pop();
                if (!isPairBraces(tokenType, topToken, fileType)) {
                    break; // unmatched braces
                }
            } else {
                if (tokenType == rparenTokenType) {
                    return iterator.getStart();
                } else {
                    break;
                }
            }
        } else if (isLBraceToken(iterator, fileText, fileType)) {
            if (isBlockCaret && initOffset == iterator.getStart())
                continue;
            else
                braceStack.push(iterator.getTokenType());
        }
    }

    return lastRbraceOffset;
}
 
Example 16
Source File: BraceMatchingUtilAdapter.java    From HighlightBracketPair with Apache License 2.0 5 votes vote down vote up
/**
 * Find the left closest brace offset position.
 *
 * @param iterator highlighter iterator
 * @param lparenTokenType left token type to be paired
 * @param fileText file text
 * @param fileType file type
 * @return offset
 */
public static int findLeftLParen(HighlighterIterator iterator,
                                 IElementType lparenTokenType,
                                 CharSequence fileText,
                                 FileType fileType, boolean isBlockCaret) {
    int lastLbraceOffset = -1;
    int initOffset = iterator.atEnd() ? -1 : iterator.getStart();
    Stack<IElementType> braceStack = new Stack<>();
    for (; !iterator.atEnd(); iterator.retreat()) {
        final IElementType tokenType = iterator.getTokenType();

        if (isLBraceToken(iterator, fileText, fileType)) {
            if (!isBlockCaret && initOffset == iterator.getStart())
                continue;
            if (!braceStack.isEmpty()) {
                IElementType topToken = braceStack.pop();
                if (!isPairBraces(tokenType, topToken, fileType)) {
                    break; // unmatched braces
                }
            } else {
                if (tokenType == lparenTokenType) {
                    return iterator.getStart();
                } else {
                    break;
                }
            }
        } else if (isRBraceToken(iterator, fileText, fileType)) {
            if (initOffset == iterator.getStart())
                continue;
            braceStack.push(iterator.getTokenType());
        }
    }

    return lastLbraceOffset;
}
 
Example 17
Source File: CSharpDocBraceMatcher.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isStructuralBrace(HighlighterIterator iterator, CharSequence text, FileType fileType)
{
	IElementType tokenType = iterator.getTokenType();
	return isXmlStructuralBrace(iterator, text, fileType, tokenType);
}
 
Example 18
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean findStructuralLeftBrace(@Nonnull FileType fileType, @Nonnull HighlighterIterator iterator, @Nonnull CharSequence fileText) {
  final Stack<IElementType> braceStack = new Stack<IElementType>();
  final Stack<String> tagNameStack = new Stack<String>();

  BraceMatcher matcher = getBraceMatcher(fileType, iterator);

  while (!iterator.atEnd()) {
    if (isStructuralBraceToken(fileType, iterator, fileText)) {
      if (isRBraceToken(iterator, fileText, fileType)) {
        braceStack.push(iterator.getTokenType());
        tagNameStack.push(getTagName(matcher, fileText, iterator));
      }
      if (isLBraceToken(iterator, fileText, fileType)) {
        if (braceStack.isEmpty()) return true;

        final int group = matcher.getBraceTokenGroupId(iterator.getTokenType());

        final IElementType topTokenType = braceStack.pop();
        final IElementType tokenType = iterator.getTokenType();

        boolean isStrict = isStrictTagMatching(matcher, fileType, group);
        boolean isCaseSensitive = areTagsCaseSensitive(matcher, fileType, group);

        String topTagName = null;
        String tagName = null;
        if (isStrict) {
          topTagName = tagNameStack.pop();
          tagName = getTagName(matcher, fileText, iterator);
        }

        if (!isPairBraces(topTokenType, tokenType, fileType)
            || isStrict && !Comparing.equal(topTagName, tagName, isCaseSensitive)) {
          return false;
        }
      }
    }

    iterator.retreat();
  }

  return false;
}
 
Example 19
Source File: GraphQLQuoteHandler.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
public boolean isClosingQuote(final HighlighterIterator iterator, final int offset) {
    return iterator.getTokenType() == GraphQLElementTypes.CLOSING_QUOTE;
}
 
Example 20
Source File: GraphQLQuoteHandler.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
public boolean isOpeningQuote(final HighlighterIterator iterator, final int offset) {
    return iterator.getTokenType() == GraphQLElementTypes.OPEN_QUOTE;
}