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

The following examples show how to use com.intellij.openapi.editor.highlighter.HighlighterIterator#getStart() . 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: BraceHighlighter.java    From HighlightBracketPair with Apache License 2.0 6 votes vote down vote up
public BracePair findClosetBracePairInStringSymbols(int offset) {
    if (offset < 0 || this.fileText == null || this.fileText.length() == 0)
        return EMPTY_BRACE_PAIR;
    EditorHighlighter editorHighlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = editorHighlighter.createIterator(offset);
    IElementType type = iterator.getTokenType();
    boolean isBlockCaret = this.isBlockCaret();
    if (!BraceMatchingUtilAdapter.isStringToken(type))
        return EMPTY_BRACE_PAIR;

    int leftOffset = iterator.getStart();
    int rightOffset = iterator.getEnd() - 1;
    if (!isBlockCaret && leftOffset == offset)
        return EMPTY_BRACE_PAIR;
    return new BracePair.BracePairBuilder().
            leftType(DOUBLE_QUOTE).
            rightType(DOUBLE_QUOTE).
            leftOffset(leftOffset).
            rightOffset(rightOffset).build();
}
 
Example 2
Source File: BuildQuoteHandler.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isOpeningQuote(HighlighterIterator iterator, int offset) {
  if (!myLiteralTokenSet.contains(iterator.getTokenType())) {
    return false;
  }
  int start = iterator.getStart();
  if (offset == start) {
    return true;
  }
  final Document document = iterator.getDocument();
  if (document == null) {
    return false;
  }
  CharSequence text = document.getCharsSequence();
  char theQuote = text.charAt(offset);
  if (offset >= 2
      && text.charAt(offset - 1) == theQuote
      && text.charAt(offset - 2) == theQuote
      && (offset < 3 || text.charAt(offset - 3) != theQuote)) {
    if (super.isOpeningQuote(iterator, offset)) {
      return true;
    }
  }
  return false;
}
 
Example 3
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 4
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 5
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int findRightmostRParen(HighlighterIterator iterator,
                                      IElementType rparenTokenType,
                                      CharSequence fileText,
                                      FileType fileType) {
  int lastRbraceOffset = -1;

  Stack<IElementType> braceStack = new Stack<IElementType>();
  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) {
          lastRbraceOffset = iterator.getStart();
        }
        else {
          break;
        }
      }
    }
    else if (isLBraceToken(iterator, fileText, fileType)) {
      braceStack.push(iterator.getTokenType());
    }
  }

  return lastRbraceOffset;
}
 
Example 6
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 7
Source File: BraceMatcherBasedSelectioner.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
  final VirtualFile file = e.getContainingFile().getVirtualFile();
  final FileType fileType = file == null? null : file.getFileType();
  if (fileType == null) return super.select(e, editorText, cursorOffset, editor);
  final int textLength = editorText.length();
  final TextRange totalRange = e.getTextRange();
  final HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(totalRange.getStartOffset());
  final BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);

  final ArrayList<TextRange> result = new ArrayList<TextRange>();
  final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<Trinity<Integer, Integer, IElementType>>();
  while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
    final Trinity<Integer, Integer, IElementType> last;
    if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
      stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
    }
    else if (braceMatcher.isRBraceToken(iterator, editorText, fileType)
        && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
      stack.removeLast();
      result.addAll(expandToWholeLine(editorText, new TextRange(last.first, iterator.getEnd())));
      int bodyStart = last.second;
      int bodyEnd = iterator.getStart();
      while (bodyStart < textLength && Character.isWhitespace(editorText.charAt(bodyStart))) bodyStart ++;
      while (bodyEnd > 0 && Character.isWhitespace(editorText.charAt(bodyEnd - 1))) bodyEnd --;
      result.addAll(expandToWholeLine(editorText, new TextRange(bodyStart, bodyEnd)));
    }
    iterator.advance();
  }
  result.add(e.getTextRange());
  return result;
}
 
Example 8
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 9
Source File: XQueryQuoteHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
protected boolean isNonClosedLiteral(HighlighterIterator iterator, CharSequence chars) {
    if (iterator.getStart() >= iterator.getEnd() - 1 ||
            chars.charAt(iterator.getEnd() - 1) != '\"' && chars.charAt(iterator.getEnd() - 1) != '\'') {
        return true;
    }
    return false;
}
 
Example 10
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 11
Source File: CommentByBlockCommentHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private TextRange getBlockCommentAt(int offset) {
  CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)myCommenter;
  HighlighterIterator it = ((EditorEx)myEditor).getHighlighter().createIterator(offset);
  if (it.getTokenType() == commenter.getBlockCommentTokenType()) {
    return new TextRange(it.getStart(), it.getEnd());
  }
  if (docCommentIsBlockComment(commenter)) {
    PsiComment comment = getCommentAtOffset(offset);
    if (comment != null && commenter.isDocumentationComment(comment)) {
      return comment.getTextRange();
    }
  }
  return null;
}
 
Example 12
Source File: BuildQuoteHandler.java    From intellij 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 (!myLiteralTokenSet.contains(tokenType)) {
    return false;
  }
  int start = iterator.getStart();
  int end = iterator.getEnd();
  if (end - start >= 1 && offset == end - 1) {
    return true; // single quote
  }
  if (end - start < 3 || offset < end - 3) {
    return false;
  }
  // check for triple quote
  Document doc = iterator.getDocument();
  if (doc == null) {
    return false;
  }
  CharSequence chars = doc.getCharsSequence();
  char quote = chars.charAt(start);
  boolean tripleQuote = quote == chars.charAt(start + 1) && quote == chars.charAt(start + 2);
  if (!tripleQuote) {
    return false;
  }
  for (int i = offset; i < Math.min(offset + 2, end); i++) {
    if (quote != chars.charAt(i)) {
      return false;
    }
  }
  return true;
}
 
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: FluidQuoteHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public boolean isOpeningQuote(HighlighterIterator iterator, int offset) {
    if (this.isInsideLiteral(iterator)) {
        int start = iterator.getStart();
        return offset == start;
    } else {
        IElementType tokenType = iterator.getTokenType();
        return tokenType == FluidTypes.DOUBLE_QUOTE || tokenType == FluidTypes.SINGLE_QUOTE;
    }
}
 
Example 15
Source File: Brace.java    From HighlightBracketPair with Apache License 2.0 5 votes vote down vote up
public Brace(IElementType elementType, HighlighterIterator iterator) {
    this.elementType = elementType;
    this.offset = iterator.getStart();

    Document document = iterator.getDocument();
    this.text = document.getText(new TextRange(iterator.getStart(),
            iterator.getEnd()));
}
 
Example 16
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 17
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 18
Source File: UnifiedEditorHighlighter.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void init(@Nonnull HighlighterIterator it1,
                  @Nonnull HighlighterIterator it2,
                  @Nonnull List<HighlightRange> ranges,
                  int textLength) {
  ApplicationManager.getApplication().assertReadAccessAllowed();

  int offset = 0;

  for (HighlightRange range : ranges) {
    TextRange base = range.getBase();
    TextRange changed = range.getChanged();

    if (base.isEmpty()) continue;

    if (base.getStartOffset() > offset) {
      addElement(new Element(offset, base.getStartOffset(), null, TextAttributes.ERASE_MARKER));
      offset = base.getStartOffset();
    }

    HighlighterIterator it = range.getSide().select(it1, it2);
    while (!it.atEnd() && changed.getStartOffset() >= it.getEnd()) {
      it.advance();
    }

    if (it.atEnd()) {
      LOG.error("Unexpected end of highlighter");
      break;
    }

    if (changed.getEndOffset() <= it.getStart()) {
      continue;
    }

    while (true) {
      int relativeStart = Math.max(it.getStart() - changed.getStartOffset(), 0);
      int relativeEnd = Math.min(it.getEnd() - changed.getStartOffset(), changed.getLength() + 1);

      addElement(new Element(offset + relativeStart,
                             offset + relativeEnd,
                             it.getTokenType(),
                             it.getTextAttributes()));

      if (changed.getEndOffset() <= it.getEnd()) {
        offset += changed.getLength();
        break;
      }

      it.advance();
      if (it.atEnd()) {
        LOG.error("Unexpected end of highlighter");
        break;
      }
    }
  }

  if (offset < textLength) {
    addElement(new Element(offset, textLength, null, TextAttributes.ERASE_MARKER));
  }
}
 
Example 19
Source File: CodeBlockUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static int calcBlockEndOffset(Editor editor, PsiFile file) {

    Document document = editor.getDocument();
    int offset = editor.getCaretModel().getOffset();
    final FileType fileType = file.getFileType();
    HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(offset);
    if (iterator.atEnd()) return -1;

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

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

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

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

      moved = true;
      iterator.advance();
    }

    return isBeforeLBrace? iterator.getEnd() : iterator.getStart();
  }
 
Example 20
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;
}