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

The following examples show how to use com.intellij.openapi.editor.highlighter.HighlighterIterator#getEnd() . 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: BuildQuoteHandler.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isNonClosedLiteral(HighlighterIterator iterator, CharSequence chars) {
  int end = iterator.getEnd();
  if (getLiteralStartOffset(chars, iterator.getStart()) >= end - 1) {
    return true;
  }
  char endSymbol = chars.charAt(end - 1);
  if (endSymbol != '"' && endSymbol != '\'') {
    return true;
  }

  //for triple quoted string
  if (end >= 3
      && (endSymbol == chars.charAt(end - 2))
      && (chars.charAt(end - 2) == chars.charAt(end - 3))
      && (end < 4 || chars.charAt(end - 4) != endSymbol)) {
    return true;
  }

  return false;
}
 
Example 2
Source File: SelectWordAtCaretAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void selectWithGuide(Editor editor, IndentGuideDescriptor guide) {
  final Document doc = editor.getDocument();
  int startOffset = editor.logicalPositionToOffset(new LogicalPosition(guide.startLine, 0));
  int endOffset = guide.endLine >= doc.getLineCount() ? doc.getTextLength() : doc.getLineStartOffset(guide.endLine);

  final VirtualFile file = ((EditorEx)editor).getVirtualFile();
  if (file != null) {
    // Make sure selection contains closing matching brace.

    final CharSequence chars = doc.getCharsSequence();
    int nonWhitespaceOffset = CharArrayUtil.shiftForward(chars, endOffset, " \t\n");
    HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(nonWhitespaceOffset);
    if (BraceMatchingUtil.isRBraceToken(iterator, chars, file.getFileType())) {
      if (editor.offsetToLogicalPosition(iterator.getStart()).column == guide.indentLevel) {
        endOffset = iterator.getEnd();
        endOffset = CharArrayUtil.shiftForward(chars, endOffset, " \t");
        if (endOffset < chars.length() && chars.charAt(endOffset) == '\n') endOffset++;
      }
    }
  }

  editor.getSelectionModel().setSelection(startOffset, endOffset);
}
 
Example 3
Source File: HippieWordCompletionHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void processWords(Editor editor, int startOffset, TokenProcessor processor) {
  CharSequence chars = editor.getDocument().getCharsSequence();
  HighlighterIterator iterator = ((EditorEx)editor).getHighlighter().createIterator(startOffset);
  while (!iterator.atEnd()) {
    int start = iterator.getStart();
    int end = iterator.getEnd();

    while (start < end) {
      int wordStart = start;
      while (wordStart < end && !isWordPart(chars.charAt(wordStart))) wordStart++;

      int wordEnd = wordStart;
      while (wordEnd < end && isWordPart(chars.charAt(wordEnd))) wordEnd++;

      if (wordEnd > wordStart && containsLetters(chars, wordStart, wordEnd) && !processor.processToken(wordStart, wordEnd)) {
        return;
      }
      start = wordEnd + 1;
    }
    iterator.advance();
  }
}
 
Example 4
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 5
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 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: 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 8
Source File: SimpleTokenSetQuoteHandler.java    From consulo 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 9
Source File: SimpleTokenSetQuoteHandler.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 (myLiteralTokenSet.contains(tokenType)){
    int start = iterator.getStart();
    int end = iterator.getEnd();
    return end - start >= 1 && offset == end - 1;
  }

  return false;
}
 
Example 10
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 11
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 12
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 13
Source File: XQueryQuoteHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isClosingQuote(HighlighterIterator iterator, int offset) {
    IElementType tokenType = iterator.getTokenType();
    if (tokenType == CLOSINGQUOT || tokenType == CLOSINGAPOS){
        int start = iterator.getStart();
        int end = iterator.getEnd();
        return end - start >= 1 && offset == end - 1;
    }

    return false;
}
 
Example 14
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 15
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 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 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 18
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 19
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 20
Source File: InjectionRegistrarImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static List<InjectedLanguageUtil.TokenInfo> obtainHighlightTokensFromLexer(@Nonnull LanguageVersion languageVersion,
                                                                                   @Nonnull CharSequence outChars,
                                                                                   @Nonnull VirtualFileWindow virtualFile,
                                                                                   @Nonnull Project project,
                                                                                   @Nonnull List<? extends PlaceInfo> placeInfos) {
  VirtualFile file = (VirtualFile)virtualFile;
  FileType fileType = file.getFileType();
  EditorHighlighterProvider provider = FileTypeEditorHighlighterProviders.INSTANCE.forFileType(fileType);
  EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  EditorHighlighter highlighter = provider.getEditorHighlighter(project, fileType, file, scheme);
  highlighter.setText(outChars);
  HighlighterIterator iterator = highlighter.createIterator(0);
  int hostNum = -1;
  int prevHostEndOffset = 0;
  LiteralTextEscaper escaper = null;
  int prefixLength = 0;
  int suffixLength = 0;
  TextRange rangeInsideHost = null;
  int shredEndOffset = -1;
  List<InjectedLanguageUtil.TokenInfo> tokens = new ArrayList<>(outChars.length() / 5); // avg. token per 5 chars
  while (!iterator.atEnd()) {
    IElementType tokenType = iterator.getTokenType();
    TextRange range = new ProperTextRange(iterator.getStart(), iterator.getEnd());
    while (range != null && !range.isEmpty()) {
      if (range.getStartOffset() >= shredEndOffset) {
        hostNum++;
        PlaceInfo info = placeInfos.get(hostNum);
        shredEndOffset = info.rangeInDecodedPSI.getEndOffset();
        prevHostEndOffset = range.getStartOffset();
        escaper = info.myEscaper;
        rangeInsideHost = info.rangeInHostElement;
        prefixLength = info.prefix.length();
        suffixLength = info.suffix.length();
      }
      //in prefix/suffix or spills over to next fragment
      if (range.getStartOffset() < prevHostEndOffset + prefixLength) {
        range = new UnfairTextRange(prevHostEndOffset + prefixLength, range.getEndOffset());
      }
      TextRange spilled = null;
      if (range.getEndOffset() > shredEndOffset - suffixLength) {
        spilled = new UnfairTextRange(shredEndOffset, range.getEndOffset());
        range = new UnfairTextRange(range.getStartOffset(), shredEndOffset - suffixLength);
      }
      if (!range.isEmpty()) {
        int start = escaper.getOffsetInHost(range.getStartOffset() - prevHostEndOffset - prefixLength, rangeInsideHost);
        if (start == -1) start = rangeInsideHost.getStartOffset();
        int end = escaper.getOffsetInHost(range.getEndOffset() - prevHostEndOffset - prefixLength, rangeInsideHost);
        if (end == -1) {
          end = rangeInsideHost.getEndOffset();
          prevHostEndOffset = shredEndOffset;
        }
        ProperTextRange rangeInHost = new ProperTextRange(start, end);
        tokens.add(new InjectedLanguageUtil.TokenInfo(tokenType, rangeInHost, hostNum, iterator.getTextAttributes()));
      }
      range = spilled;
    }
    iterator.advance();
  }
  return tokens;
}