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

The following examples show how to use com.intellij.openapi.editor.highlighter.HighlighterIterator#retreat() . 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: CSharpQuoteHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isOpeningQuote(HighlighterIterator iterator, int offset)
{
	boolean openingQuote = super.isOpeningQuote(iterator, offset);

	if(openingQuote)
	{
		// check escape next
		if(!iterator.atEnd())
		{
			iterator.retreat();

			if(!iterator.atEnd() && StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()))
			{
				openingQuote = false;
			}
			iterator.advance();
		}
	}
	return openingQuote;
}
 
Example 3
Source File: CSharpQuoteHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isClosingQuote(HighlighterIterator iterator, int offset)
{
	boolean closingQuote = super.isClosingQuote(iterator, offset);

	if(closingQuote)
	{
		// check escape next
		if(!iterator.atEnd())
		{
			iterator.advance();

			if(!iterator.atEnd() && StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()))
			{
				closingQuote = false;
			}
			iterator.retreat();
		}
	}
	return closingQuote;
}
 
Example 4
Source File: XQueryQuoteHandler.java    From intellij-xquery 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 5
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findLeftmostLParen(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) {
          lastLbraceOffset = iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isRBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastLbraceOffset;
}
 
Example 6
Source File: CustomFileTypeQuoteHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) {
  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 (tokenType == CustomHighlighterTokenType.STRING ||
          tokenType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING ||
          tokenType == CustomHighlighterTokenType.CHARACTER) {

        if (iterator.getStart() >= iterator.getEnd() - 1 ||
            chars.charAt(iterator.getEnd() - 1) != '\"' && chars.charAt(iterator.getEnd() - 1) != '\'') {
          return true;
        }
      }
      iterator.advance();
    }
  } finally {
    while (!iterator.atEnd() && iterator.getStart() != offset) iterator.retreat();
  }

  return false;
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
Source File: EditorActionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Finds out whether there's a boundary between two lexemes of different type at given offset.
 */
public static boolean isLexemeBoundary(@Nonnull Editor editor, int offset) {
  if (!(editor instanceof EditorEx) || offset <= 0 || offset >= editor.getDocument().getTextLength()) return false;
  if (CharArrayUtil.isEmptyOrSpaces(editor.getDocument().getImmutableCharSequence(), offset - 1, offset + 1)) return false;
  EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
  HighlighterIterator it = highlighter.createIterator(offset);
  if (it.getStart() != offset) {
    return false;
  }
  IElementType rightToken = it.getTokenType();
  it.retreat();
  IElementType leftToken = it.getTokenType();
  return !Comparing.equal(leftToken, rightToken);
}
 
Example 13
Source File: CSharpDocBraceMatcher.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
private static boolean findEndTagStart(HighlighterIterator iterator)
{
	IElementType tokenType = iterator.getTokenType();
	int balance = 0;
	int count = 0;
	while(balance >= 0)
	{
		iterator.retreat();
		count++;
		if(iterator.atEnd())
		{
			break;
		}
		tokenType = iterator.getTokenType();
		if(tokenType == CSharpDocTokenType.XML_TAG_END || tokenType == CSharpDocTokenType.XML_EMPTY_ELEMENT_END)
		{
			balance++;
		}
		else if(tokenType == CSharpDocTokenType.XML_END_TAG_START || tokenType == CSharpDocTokenType.XML_START_TAG_START)
		{
			balance--;
		}
	}
	while(count-- > 0)
	{
		iterator.advance();
	}
	return tokenType == CSharpDocTokenType.XML_END_TAG_START;
}
 
Example 14
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 15
Source File: TypedHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void handleAfterLParen(@Nonnull Editor editor, @Nonnull FileType fileType, char lparenChar) {
  int offset = editor.getCaretModel().getOffset();
  HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(offset);
  boolean atEndOfDocument = offset == editor.getDocument().getTextLength();

  if (!atEndOfDocument) iterator.retreat();
  if (iterator.atEnd()) return;
  BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
  if (iterator.atEnd()) return;
  IElementType braceTokenType = iterator.getTokenType();
  final CharSequence fileText = editor.getDocument().getCharsSequence();
  if (!braceMatcher.isLBraceToken(iterator, fileText, fileType)) return;

  if (!iterator.atEnd()) {
    iterator.advance();

    if (!iterator.atEnd() && !BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType(braceTokenType, iterator.getTokenType(), fileType)) {
      return;
    }

    iterator.retreat();
  }

  int lparenOffset = BraceMatchingUtil.findLeftmostLParen(iterator, braceTokenType, fileText, fileType);
  if (lparenOffset < 0) lparenOffset = 0;

  iterator = ((EditorEx)editor).getHighlighter().createIterator(lparenOffset);
  boolean matched = BraceMatchingUtil.matchBrace(fileText, fileType, iterator, true, true);

  if (!matched) {
    String text;
    if (lparenChar == '(') {
      text = ")";
    }
    else if (lparenChar == '[') {
      text = "]";
    }
    else if (lparenChar == '<') {
      text = ">";
    }
    else if (lparenChar == '{') {
      text = "}";
    }
    else {
      throw new AssertionError("Unknown char " + lparenChar);
    }
    editor.getDocument().insertString(offset, text);
    TabOutScopesTracker.getInstance().registerEmptyScope(editor, offset);
  }
}
 
Example 16
Source File: TypedHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean handleRParen(@Nonnull Editor editor, @Nonnull FileType fileType, char charTyped) {
  if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) return false;

  int offset = editor.getCaretModel().getOffset();

  if (offset == editor.getDocument().getTextLength()) return false;

  HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(offset);
  if (iterator.atEnd()) return false;

  if (iterator.getEnd() - iterator.getStart() != 1 || editor.getDocument().getCharsSequence().charAt(iterator.getStart()) != charTyped) {
    return false;
  }

  BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
  CharSequence text = editor.getDocument().getCharsSequence();
  if (!braceMatcher.isRBraceToken(iterator, text, fileType)) {
    return false;
  }

  IElementType tokenType = iterator.getTokenType();

  iterator.retreat();

  IElementType lparenTokenType = braceMatcher.getOppositeBraceTokenType(tokenType);
  int lparenthOffset = BraceMatchingUtil.findLeftmostLParen(iterator, lparenTokenType, text, fileType);

  if (lparenthOffset < 0) {
    if (braceMatcher instanceof NontrivialBraceMatcher) {
      for (IElementType t : ((NontrivialBraceMatcher)braceMatcher).getOppositeBraceTokenTypes(tokenType)) {
        if (t == lparenTokenType) continue;
        lparenthOffset = BraceMatchingUtil.findLeftmostLParen(iterator, t, text, fileType);
        if (lparenthOffset >= 0) break;
      }
    }
    if (lparenthOffset < 0) return false;
  }

  iterator = ((EditorEx)editor).getHighlighter().createIterator(lparenthOffset);
  boolean matched = BraceMatchingUtil.matchBrace(text, fileType, iterator, true, true);

  if (!matched) return false;

  EditorModificationUtil.moveCaretRelatively(editor, 1);
  return true;
}
 
Example 17
Source File: CodeBlockUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static int calcBlockStartOffset(Editor editor, PsiFile file) {
  int offset = editor.getCaretModel().getOffset() - 1;
  if (offset < 0) return -1;

  Document document = editor.getDocument();
  final FileType fileType = file.getFileType();
  HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(offset);

  int depth = 0;
  Language braceType;
  boolean isAfterRBrace = false;
  if (isRStructuralBrace(fileType, iterator, document.getCharsSequence())) {
    isAfterRBrace = true;
    depth = -1;
    braceType = getBraceType(iterator);
  }
  else {
    braceType = null;
  }

  boolean moved = false;
  while (true) {
    if (iterator.atEnd()) return -1;

    if (isLStructuralBrace(fileType, iterator, document.getCharsSequence()) &&
        (braceType == getBraceType(iterator) || braceType == null)) {
      if (braceType == null) {
        braceType = getBraceType(iterator);
      }

      if (moved) {
        if (depth == 0) break;
        depth--;
      }
    }
    else if (isRStructuralBrace(fileType, iterator, document.getCharsSequence()) &&
             (braceType == getBraceType(iterator) || braceType == null)) {
      if (braceType == null) {
        braceType = getBraceType(iterator);
      }
      depth++;
    }

    moved = true;
    iterator.retreat();
  }

  return isAfterRBrace ? iterator.getStart() : iterator.getEnd();
}
 
Example 18
Source File: HaskellTypedHandler.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
/**
 * this is almost complete c'n'p from TypedHandler,
 * This code should be generalized into BraceMatchingUtil to support custom matching braces for plugin developers
 *
 * @see com.intellij.codeInsight.editorActions.TypedHandler
 * @see BraceMatchingUtil
 */
private static void insertMatchedEndComment(Project project, Editor editor, PsiFile file, char c) {
    if (!(file instanceof HaskellFile)) return;

    PsiDocumentManager.getInstance(project).commitAllDocuments();

    FileType fileType = file.getFileType();
    int offset = editor.getCaretModel().getOffset();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    boolean atEndOfDocument = offset == editor.getDocument().getTextLength();

    if (!atEndOfDocument) iterator.retreat();
    if (iterator.atEnd()) return;
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    if (iterator.atEnd()) return;
    IElementType braceTokenType = iterator.getTokenType();
    final CharSequence fileText = editor.getDocument().getCharsSequence();
    if (!braceMatcher.isLBraceToken(iterator, fileText, fileType)) return;

    if (!iterator.atEnd()) {
        iterator.advance();

        if (!iterator.atEnd()) {
            if (!BraceMatchingUtil.isPairedBracesAllowedBeforeTypeInFileType(braceTokenType, iterator.getTokenType(), fileType)) {
                return;
            }
            if (BraceMatchingUtil.isLBraceToken(iterator, fileText, fileType)) {
                return;
            }
        }

        iterator.retreat();
    }

    int lparenOffset = BraceMatchingUtil.findLeftmostLParen(iterator, braceTokenType, fileText, fileType);
    if (lparenOffset < 0) lparenOffset = 0;

    iterator = ((EditorEx) editor).getHighlighter().createIterator(lparenOffset);

    if (!BraceMatchingUtil.matchBrace(fileText, fileType, iterator, true, true)) {
        // Some other mechanism has put the closing '}' in the document already.
        editor.getDocument().insertString(offset, new CharArrayCharSequence(c));
    }
}