com.intellij.openapi.editor.highlighter.EditorHighlighter Java Examples

The following examples show how to use com.intellij.openapi.editor.highlighter.EditorHighlighter. 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: TodoItemNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void collectHighlights(@Nonnull List<? super HighlightedRegion> highlights, @Nonnull EditorHighlighter highlighter, int startOffset, int endOffset, int highlightOffsetShift) {
  HighlighterIterator iterator = highlighter.createIterator(startOffset);
  while (!iterator.atEnd()) {
    int start = Math.max(iterator.getStart(), startOffset);
    int end = Math.min(iterator.getEnd(), endOffset);
    if (start >= endOffset) break;

    TextAttributes attributes = iterator.getTextAttributes();
    int fontType = attributes.getFontType();
    if ((fontType & Font.BOLD) != 0) { // suppress bold attribute
      attributes = attributes.clone();
      attributes.setFontType(fontType & ~Font.BOLD);
    }
    HighlightedRegion region = new HighlightedRegion(highlightOffsetShift + start - startOffset, highlightOffsetShift + end - startOffset, attributes);
    highlights.add(region);
    iterator.advance();
  }
}
 
Example #2
Source File: LatteIndexPatternBuilder.java    From intellij-latte with MIT License 6 votes vote down vote up
@Nullable
@Override
public Lexer getIndexingLexer(@NotNull PsiFile file) {
	if (!(file instanceof LatteFile)) {
		return null;
	}
	VirtualFile virtualFile = file.getVirtualFile();
	if (virtualFile == null) {
		virtualFile = file.getViewProvider().getVirtualFile();
	}

	try {
		LayeredLexer.ourDisableLayersFlag.set(Boolean.TRUE);
		EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(file.getProject(), virtualFile);
		return new LexerEditorHighlighterLexer(highlighter, false);
	} finally {
		LayeredLexer.ourDisableLayersFlag.set(Boolean.FALSE);
	}
}
 
Example #3
Source File: UnifiedDiffViewer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private EditorHighlighter buildHighlighter(@Nullable Project project,
                                           @Nonnull DocumentContent content1,
                                           @Nonnull DocumentContent content2,
                                           @Nonnull CharSequence text1,
                                           @Nonnull CharSequence text2,
                                           @Nonnull List<HighlightRange> ranges,
                                           int textLength) {
  EditorHighlighter highlighter1 = DiffUtil.initEditorHighlighter(project, content1, text1);
  EditorHighlighter highlighter2 = DiffUtil.initEditorHighlighter(project, content2, text2);

  if (highlighter1 == null && highlighter2 == null) return null;
  if (highlighter1 == null) highlighter1 = DiffUtil.initEmptyEditorHighlighter(text1);
  if (highlighter2 == null) highlighter2 = DiffUtil.initEmptyEditorHighlighter(text2);

  return new UnifiedEditorHighlighter(myDocument, highlighter1, highlighter2, ranges, textLength);
}
 
Example #4
Source File: BraceHighlighter.java    From HighlightBracketPair with Apache License 2.0 6 votes vote down vote up
public BracePair findClosetBracePairInBraceTokens(int offset) {
    EditorHighlighter editorHighlighter = ((EditorEx) editor).getHighlighter();
    boolean isBlockCaret = this.isBlockCaret();
    List<Pair<IElementType, IElementType>> braceTokens = this.getSupportedBraceToken();
    for (Pair<IElementType, IElementType> braceTokenPair :
            braceTokens) {
        HighlighterIterator leftTraverseIterator = editorHighlighter.createIterator(offset);
        HighlighterIterator rightTraverseIterator = editorHighlighter.createIterator(offset);
        int leftBraceOffset = BraceMatchingUtilAdapter.findLeftLParen(
                leftTraverseIterator, braceTokenPair.getLeft(), this.fileText, this.fileType, isBlockCaret);
        int rightBraceOffset = BraceMatchingUtilAdapter.findRightRParen(
                rightTraverseIterator, braceTokenPair.getRight(), this.fileText, this.fileType, isBlockCaret);
        if (leftBraceOffset != NON_OFFSET && rightBraceOffset != NON_OFFSET) {
            return new BracePair.BracePairBuilder().
                    leftType(braceTokenPair.getLeft()).
                    rightType(braceTokenPair.getRight()).
                    leftIterator(leftTraverseIterator).
                    rightIterator(rightTraverseIterator).build();


        }
    }
    return EMPTY_BRACE_PAIR;
}
 
Example #5
Source File: DiffUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static EditorHighlighter createEditorHighlighter(@Nullable Project project, @Nonnull DocumentContent content) {
  FileType type = content.getContentType();
  VirtualFile file = content.getHighlightFile();
  Language language = content.getUserData(DiffUserDataKeys.LANGUAGE);

  EditorHighlighterFactory highlighterFactory = EditorHighlighterFactory.getInstance();
  if (language != null) {
    SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, file);
    return highlighterFactory.createEditorHighlighter(syntaxHighlighter, EditorColorsManager.getInstance().getGlobalScheme());
  }
  if (file != null) {
    if ((type == null || type == PlainTextFileType.INSTANCE) || file.getFileType() == type || file instanceof LightVirtualFile) {
      return highlighterFactory.createEditorHighlighter(project, file);
    }
  }
  if (type != null) {
    return highlighterFactory.createEditorHighlighter(project, type);
  }
  return null;
}
 
Example #6
Source File: SyntaxHighlighterOverEditorHighlighter.java    From consulo with Apache License 2.0 6 votes vote down vote up
public SyntaxHighlighterOverEditorHighlighter(SyntaxHighlighter _highlighter, VirtualFile file, Project project) {
  if (file.getFileType() == PlainTextFileType.INSTANCE) { // optimization for large files, PlainTextSyntaxHighlighterFactory is slow
    highlighter = new PlainSyntaxHighlighter();
    lexer = highlighter.getHighlightingLexer();
  } else {
    highlighter = _highlighter;
    LayeredLexer.ourDisableLayersFlag.set(Boolean.TRUE);
    EditorHighlighter editorHighlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(project, file);

    try {
      if (editorHighlighter instanceof LayeredLexerEditorHighlighter) {
        lexer = new LexerEditorHighlighterLexer(editorHighlighter, false);
      }
      else {
        lexer = highlighter.getHighlightingLexer();
      }
    }
    finally {
      LayeredLexer.ourDisableLayersFlag.set(null);
    }
  }
}
 
Example #7
Source File: FileTypeEditorHighlighterProviders.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected List<EditorHighlighterProvider> buildExtensions(String stringKey, final FileType key) {
  List<EditorHighlighterProvider> fromEP = super.buildExtensions(stringKey, key);
  if (fromEP.isEmpty()) {
    EditorHighlighterProvider defaultProvider = new EditorHighlighterProvider() {
      @Override
      public EditorHighlighter getEditorHighlighter(@Nullable Project project,
                                                    @Nonnull FileType fileType,
                                                    @Nullable VirtualFile virtualFile,
                                                    @Nonnull EditorColorsScheme colors) {
        return EditorHighlighterFactory.getInstance().createEditorHighlighter(
          SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, project, virtualFile), colors);
      }
    };
    return Collections.singletonList(defaultProvider);
  }
  return fromEP;
}
 
Example #8
Source File: TodoTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void validateCache() {
  ApplicationManager.getApplication().assertIsDispatchThread();
  TodoTreeStructure treeStructure = getTodoTreeStructure();
  // First of all we need to update "dirty" file set.
  for (Iterator<VirtualFile> i = myDirtyFileSet.iterator(); i.hasNext(); ) {
    VirtualFile file = i.next();
    PsiFile psiFile = file.isValid() ? PsiManager.getInstance(myProject).findFile(file) : null;
    if (psiFile == null || !treeStructure.accept(psiFile)) {
      if (myFileTree.contains(file)) {
        myFileTree.removeFile(file);
        myFile2Highlighter.remove(file);
      }
    }
    else { // file is valid and contains T.O.D.O items
      myFileTree.removeFile(file);
      myFileTree.add(file); // file can be moved. remove/add calls move it to another place
      EditorHighlighter highlighter = myFile2Highlighter.get(file);
      if (highlighter != null) { // update highlighter text
        highlighter.setText(PsiDocumentManager.getInstance(myProject).getDocument(psiFile).getCharsSequence());
      }
    }
    i.remove();
  }
  LOG.assertTrue(myDirtyFileSet.isEmpty());
  // Now myDirtyFileSet should be empty
}
 
Example #9
Source File: MergePanel2.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void setupHighlighterSettings(Editor left, Editor base, Editor right) {
  Editor[] editors = new Editor[]{left, base, right};
  DiffContent[] contents = myData.getContents();
  FileType[] types = DiffUtil.chooseContentTypes(contents);

  VirtualFile fallbackFile = contents[1].getFile();
  FileType fallbackType = contents[1].getContentType();

  for (int i = 0; i < 3; i++) {
    Editor editor = editors[i];
    DiffContent content = contents[i];

    EditorHighlighter highlighter =
            createHighlighter(types[i], content.getFile(), fallbackFile, fallbackType, myData.getProject()).createHighlighter();
    if (highlighter != null) {
      ((EditorEx)editor).setHighlighter(highlighter);
    }
  }
}
 
Example #10
Source File: ToggleCaseAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static String toCase(Editor editor, int startOffset, int endOffset, final boolean lower) {
  CharSequence text = editor.getDocument().getImmutableCharSequence();
  EditorHighlighter highlighter;
  if (editor instanceof EditorEx) {
    highlighter = ((EditorEx)editor).getHighlighter();
  }
  else {
    highlighter = new EmptyEditorHighlighter(null);
    highlighter.setText(text);
  }
  HighlighterIterator iterator = highlighter.createIterator(startOffset);
  StringBuilder builder = new StringBuilder(endOffset - startOffset);
  while (!iterator.atEnd()) {
    int start = trim(iterator.getStart(), startOffset, endOffset);
    int end = trim(iterator.getEnd(), startOffset, endOffset);
    CharSequence fragment = text.subSequence(start, end);

    builder.append(iterator.getTokenType() == VALID_STRING_ESCAPE_TOKEN ? fragment :
                   lower ? fragment.toString().toLowerCase(Locale.getDefault()) :
                   fragment.toString().toUpperCase(Locale.getDefault()));

    if (end == endOffset) break;
    iterator.advance();
  }
  return builder.toString();
}
 
Example #11
Source File: EditorHighlighterCache.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
          ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
 
Example #12
Source File: EditorHighlighterCache.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Lexer getLexerBasedOnLexerHighlighter(CharSequence text, VirtualFile virtualFile, Project project) {
  EditorHighlighter highlighter = null;

  PsiFile psiFile = virtualFile != null ? PsiManager.getInstance(project).findFile(virtualFile) : null;
  final Document document = psiFile != null ? PsiDocumentManager.getInstance(project).getDocument(psiFile) : null;
  final EditorHighlighter cachedEditorHighlighter;
  boolean alreadyInitializedHighlighter = false;

  if (document != null &&
      (cachedEditorHighlighter = getEditorHighlighterForCachesBuilding(document)) != null &&
      PlatformIdTableBuilding.checkCanUseCachedEditorHighlighter(text, cachedEditorHighlighter)) {
    highlighter = cachedEditorHighlighter;
    alreadyInitializedHighlighter = true;
  }
  else if (virtualFile != null) {
    highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(project, virtualFile);
  }

  if (highlighter != null) {
    return new LexerEditorHighlighterLexer(highlighter, alreadyInitializedHighlighter);
  }
  return null;
}
 
Example #13
Source File: EnterInStringLiteralHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isInStringLiteral(@Nonnull Editor editor, @Nonnull DataContext dataContext, int offset) {
  Language language = EnterHandler.getLanguage(dataContext);
  if (offset > 0 && language != null) {
    QuoteHandler quoteHandler = TypedHandler.getLanguageQuoteHandler(language);
    if (quoteHandler == null) {
      FileType fileType = language.getAssociatedFileType();
      quoteHandler = fileType != null ? TypedHandler.getQuoteHandlerForType(fileType) : null;
    }
    if (quoteHandler != null) {
      EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
      HighlighterIterator iterator = highlighter.createIterator(offset - 1);
      return StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()) || quoteHandler.isInsideLiteral(iterator);
    }
  }
  return false;
}
 
Example #14
Source File: TemplateEditorUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static Editor createEditor(boolean isReadOnly, final Document document, final Project project) {
  EditorFactory editorFactory = EditorFactory.getInstance();
  Editor editor = (isReadOnly ? editorFactory.createViewer(document, project) : editorFactory.createEditor(document, project));

  EditorSettings editorSettings = editor.getSettings();
  editorSettings.setVirtualSpace(false);
  editorSettings.setLineMarkerAreaShown(false);
  editorSettings.setIndentGuidesShown(false);
  editorSettings.setLineNumbersShown(false);
  editorSettings.setFoldingOutlineShown(false);

  EditorColorsScheme scheme = editor.getColorsScheme();
  scheme.setColor(EditorColors.CARET_ROW_COLOR, null);

  VirtualFile file = FileDocumentManager.getInstance().getFile(document);
  if (file != null) {
    EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(file, scheme, project);
    ((EditorEx) editor).setHighlighter(highlighter);
  }

  return editor;
}
 
Example #15
Source File: SimpleEditorPreview.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void updateView() {
  EditorColorsScheme scheme = myOptions.getSelectedScheme();

  myEditor.setColorsScheme(scheme);

  EditorHighlighter highlighter = null;
  if (myPage instanceof EditorHighlightingProvidingColorSettingsPage) {

    highlighter = ((EditorHighlightingProvidingColorSettingsPage)myPage).createEditorHighlighter(scheme);
  }
  if (highlighter == null) {
    final SyntaxHighlighter pageHighlighter = myPage.getHighlighter();
    highlighter = HighlighterFactory.createHighlighter(pageHighlighter, scheme);
  }
  myEditor.setHighlighter(highlighter);
  updateHighlighters();

  myEditor.reinitSettings();
}
 
Example #16
Source File: DesktopEditorImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void setHighlighter(@Nonnull final EditorHighlighter highlighter) {
  if (isReleased) return; // do not set highlighter to the released editor
  assertIsDispatchThread();
  final Document document = getDocument();
  Disposer.dispose(myHighlighterDisposable);

  document.addDocumentListener(highlighter);
  myHighlighterDisposable = () -> document.removeDocumentListener(highlighter);
  Disposer.register(myDisposable, myHighlighterDisposable);
  highlighter.setEditor(this);
  highlighter.setText(document.getImmutableCharSequence());
  if (!(highlighter instanceof EmptyEditorHighlighter)) {
    EditorHighlighterCache.rememberEditorHighlighterForCachesOptimization(document, highlighter);
  }
  myHighlighter = highlighter;

  if (myPanel != null) {
    reinitSettings();
  }
}
 
Example #17
Source File: TextPainter.java    From consulo with Apache License 2.0 6 votes vote down vote up
public TextPainter(DocumentEx editorDocument,
                   EditorHighlighter highlighter,
                   String fileName,
                   final Project project,
                   final FileType fileType, final List<LineMarkerInfo> separators) {
  myCodeStyleSettings = CodeStyleSettingsManager.getSettings(project);
  myDocument = editorDocument;
  myPrintSettings = PrintSettings.getInstance();
  String fontName = myPrintSettings.FONT_NAME;
  int fontSize = myPrintSettings.FONT_SIZE;
  myPlainFont = new Font(fontName, Font.PLAIN, fontSize);
  myBoldFont = new Font(fontName, Font.BOLD, fontSize);
  myItalicFont = new Font(fontName, Font.ITALIC, fontSize);
  myBoldItalicFont = new Font(fontName, Font.BOLD | Font.ITALIC, fontSize);
  myHighlighter = highlighter;
  myHeaderFont = new Font(myPrintSettings.FOOTER_HEADER_FONT_NAME, Font.PLAIN,
                          myPrintSettings.FOOTER_HEADER_FONT_SIZE);
  myFileName = fileName;
  mySegmentEnd = myDocument.getTextLength();
  myFileType = fileType;
  myMethodSeparators = separators != null ? separators.toArray(new LineMarkerInfo[separators.size()]) : new LineMarkerInfo[0];
  myCurrentMethodSeparator = 0;
}
 
Example #18
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 #19
Source File: DetailViewImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
protected Editor createEditor(@Nullable Project project, Document document, VirtualFile file) {
  EditorEx editor = (EditorEx)EditorFactory.getInstance().createViewer(document, project);

  final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(file, scheme, project);

  editor.setFile(file);
  editor.setHighlighter(highlighter);

  EditorSettings settings = editor.getSettings();
  settings.setAnimatedScrolling(false);
  settings.setRefrainFromScrolling(false);
  settings.setLineNumbersShown(true);
  settings.setFoldingOutlineShown(false);
  if (settings instanceof SettingsImpl) {
    ((SettingsImpl)settings).setSoftWrapAppliancePlace(SoftWrapAppliancePlaces.PREVIEW);
  }
  editor.getFoldingModel().setFoldingEnabled(false);

  return editor;
}
 
Example #20
Source File: LanguageConsoleImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String printWithHighlighting(@Nonnull LanguageConsoleView console, @Nonnull Editor inputEditor, @Nonnull TextRange textRange) {
  String text;
  EditorHighlighter highlighter;
  if (inputEditor instanceof EditorWindow) {
    PsiFile file = ((EditorWindow)inputEditor).getInjectedFile();
    highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
    String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
    highlighter.setText(fullText);
    text = textRange.substring(fullText);
  }
  else {
    text = inputEditor.getDocument().getText(textRange);
    highlighter = ((EditorEx)inputEditor).getHighlighter();
  }
  SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter)highlighter).getSyntaxHighlighter() : null;
  LanguageConsoleImpl consoleImpl = (LanguageConsoleImpl)console;
  consoleImpl.doAddPromptToHistory();
  if (syntax != null) {
    ConsoleViewUtil.printWithHighlighting(console, text, syntax, () -> {
      String identPrompt = consoleImpl.myConsoleExecutionEditor.getConsolePromptDecorator().getIndentPrompt();
      if (StringUtil.isNotEmpty(identPrompt)) {
        consoleImpl.addPromptToHistoryImpl(identPrompt);
      }
    });
  }
  else {
    console.print(text, ConsoleViewContentType.USER_INPUT);
  }
  console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
  return text;
}
 
Example #21
Source File: PrintManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static TextPainter doInitTextPainter(final PsiFile psiFile, final Editor editor) {
  final String fileName = psiFile.getVirtualFile().getPresentableUrl();
  DocumentEx doc = (DocumentEx)PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
  if (doc == null) return null;
  EditorHighlighter highlighter = HighlighterFactory.createHighlighter(psiFile.getProject(), psiFile.getVirtualFile());
  highlighter.setText(doc.getCharsSequence());
  return new TextPainter(doc, highlighter, fileName, psiFile, psiFile.getFileType(), editor);
}
 
Example #22
Source File: FluidFileType.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
protected FluidFileType() {
    super(FluidLanguage.INSTANCE);

    FileTypeEditorHighlighterProviders.INSTANCE.addExplicitExtension(this, new EditorHighlighterProvider() {
        public EditorHighlighter getEditorHighlighter(@Nullable Project project, @NotNull FileType fileType, @Nullable VirtualFile virtualFile, @NotNull EditorColorsScheme colors) {

            return new FluidTemplateHighlighter(project, virtualFile, colors);
        }
    });
}
 
Example #23
Source File: BraceHighlightingHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static EditorHighlighter getLazyParsableHighlighterIfAny(Project project, Editor editor, PsiFile psiFile) {
  if (!PsiDocumentManager.getInstance(project).isCommitted(editor.getDocument())) {
    return ((EditorEx)editor).getHighlighter();
  }
  PsiElement elementAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
  for (PsiElement e : SyntaxTraverser.psiApi().parents(elementAt).takeWhile(Conditions.notEqualTo(psiFile))) {
    if (!(PsiUtilCore.getElementType(e) instanceof ILazyParseableElementType)) continue;
    Language language = ILazyParseableElementType.LANGUAGE_KEY.get(e.getNode());
    if (language == null) continue;
    TextRange range = e.getTextRange();
    final int offset = range.getStartOffset();
    SyntaxHighlighter syntaxHighlighter =
            SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, psiFile.getVirtualFile());
    LexerEditorHighlighter highlighter = new LexerEditorHighlighter(syntaxHighlighter, editor.getColorsScheme()) {
      @Nonnull
      @Override
      public HighlighterIterator createIterator(int startOffset) {
        return new HighlighterIteratorWrapper(super.createIterator(Math.max(startOffset - offset, 0))) {
          @Override
          public int getStart() {
            return super.getStart() + offset;
          }

          @Override
          public int getEnd() {
            return super.getEnd() + offset;
          }
        };
      }
    };
    highlighter.setText(editor.getDocument().getText(range));
    return highlighter;
  }
  return ((EditorEx)editor).getHighlighter();
}
 
Example #24
Source File: RecentLocationsDataModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void applySyntaxHighlighting(Project project, EditorEx editor, Document document, EditorColorsScheme colorsScheme, TextRange textRange, IdeDocumentHistoryImpl.PlaceInfo placeInfo) {
  EditorHighlighter editorHighlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(placeInfo.getFile(), colorsScheme, project);
  editorHighlighter.setEditor(new LightHighlighterClient(document, project));
  editorHighlighter.setText(document.getText(TextRange.create(0, textRange.getEndOffset())));
  int startOffset = textRange.getStartOffset();

  for (HighlighterIterator iterator = editorHighlighter.createIterator(startOffset); !iterator.atEnd() && iterator.getEnd() <= textRange.getEndOffset(); iterator.advance()) {
    if (iterator.getStart() >= startOffset) {
      editor.getMarkupModel().addRangeHighlighter(iterator.getStart() - startOffset, iterator.getEnd() - startOffset, 999, iterator.getTextAttributes(), HighlighterTargetArea.EXACT_RANGE);
    }
  }
}
 
Example #25
Source File: TodoTreeBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@code SelectInEditorManager} for the specified {@code psiFile}. Highlighters are
 * lazy created and initialized.
 */
public EditorHighlighter getHighlighter(PsiFile psiFile, Document document) {
  VirtualFile file = psiFile.getVirtualFile();
  EditorHighlighter highlighter = myFile2Highlighter.get(file);
  if (highlighter == null) {
    highlighter = HighlighterFactory.createHighlighter(UsageTreeColorsScheme.getInstance().getScheme(), file.getName(), myProject);
    highlighter.setText(document.getCharsSequence());
    myFile2Highlighter.put(file, highlighter);
  }
  return highlighter;
}
 
Example #26
Source File: EditorPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void paintBorderEffect(EditorHighlighter highlighter) {
  HighlighterIterator it = highlighter.createIterator(myStartOffset);
  while (!it.atEnd() && it.getStart() < myEndOffset) {
    TextAttributes attributes = it.getTextAttributes();
    EffectDescriptor borderDescriptor = getBorderDescriptor(attributes);
    if (borderDescriptor != null) {
      paintBorderEffect(it.getStart(), it.getEnd(), borderDescriptor);
    }
    it.advance();
  }
}
 
Example #27
Source File: UnifiedDiffViewer.java    From consulo with Apache License 2.0 5 votes vote down vote up
public TwosideDocumentData(@Nonnull UnifiedFragmentBuilder builder,
                           @Nullable EditorHighlighter highlighter,
                           @Nullable UnifiedEditorRangeHighlighter rangeHighlighter) {
  myBuilder = builder;
  myHighlighter = highlighter;
  myRangeHighlighter = rangeHighlighter;
}
 
Example #28
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 #29
Source File: QueryPanel.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
private EditorHighlighter createHighlighter(EditorColorsScheme settings) {
    Language language = Language.findLanguageByID("JSON");
    if (language == null) {
        language = Language.ANY;
    }
    return new LexerEditorHighlighter(PlainTextSyntaxHighlighterFactory.getSyntaxHighlighter(language, null, null), settings);
}
 
Example #30
Source File: DiffHighlighterFactoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public EditorHighlighter createHighlighter() {
  if (myFileType == null || myProject == null) return null;
  if ((myFile != null && myFile.getFileType() == myFileType) || myFile instanceof LightVirtualFile) {
    return EditorHighlighterFactory.getInstance().createEditorHighlighter(myProject, myFile);
  }
  return EditorHighlighterFactory.getInstance().createEditorHighlighter(myProject, myFileType);
}