Java Code Examples for com.intellij.openapi.editor.highlighter.EditorHighlighter#setText()

The following examples show how to use com.intellij.openapi.editor.highlighter.EditorHighlighter#setText() . 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: 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 2
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 3
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 4
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static EditorHighlighter initEditorHighlighter(@Nullable Project project,
                                                      @Nonnull DocumentContent content,
                                                      @Nonnull CharSequence text) {
  EditorHighlighter highlighter = createEditorHighlighter(project, content);
  if (highlighter == null) return null;
  highlighter.setText(text);
  return highlighter;
}
 
Example 5
Source File: EditorHighlighterUpdater.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected EditorHighlighter createHighlighter() {
  EditorHighlighter highlighter = myFile != null
                                  ? EditorHighlighterFactory.getInstance().createEditorHighlighter(myProject, myFile)
                                  : new EmptyEditorHighlighter(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(HighlighterColors.TEXT));
  highlighter.setText(myEditor.getDocument().getImmutableCharSequence());
  return highlighter;
}
 
Example 6
Source File: DesktopTextEditorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected Runnable loadEditorInBackground() {
  EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(myFile, scheme, myProject);
  EditorEx editor = (EditorEx)getEditor();
  highlighter.setText(editor.getDocument().getImmutableCharSequence());
  return () -> editor.setHighlighter(highlighter);
}
 
Example 7
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 8
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 9
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 10
Source File: EditorWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public EditorHighlighter getHighlighter() {
  EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(myInjectedFile.getLanguage(), getProject(), myInjectedFile.getVirtualFile());
  EditorHighlighter highlighter = HighlighterFactory.createHighlighter(syntaxHighlighter, scheme);
  highlighter.setText(getDocument().getText());
  highlighter.setEditor(new LightHighlighterClient(getDocument(), getProject()));
  return highlighter;
}
 
Example 11
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 12
Source File: DiffUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static EditorHighlighter initEmptyEditorHighlighter(@Nonnull CharSequence text) {
  EditorHighlighter highlighter = createEmptyEditorHighlighter();
  highlighter.setText(text);
  return highlighter;
}
 
Example 13
Source File: PrintManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static TextPainter doInitTextPainter(@Nonnull final DocumentEx doc, Project project) {
  EditorHighlighter highlighter = HighlighterFactory.createHighlighter(project, "unknown");
  highlighter.setText(doc.getCharsSequence());
  return new TextPainter(doc, highlighter, "unknown", project, PlainTextFileType.INSTANCE, null);
}
 
Example 14
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;
}