Java源码示例:com.intellij.codeInsight.highlighting.HighlightManager

示例1
private static void addHighlights(List<TextRange> ranges, Editor editor, ArrayList<RangeHighlighter> highlighters) {
    EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
    HighlightManager highlightManager = HighlightManager.getInstance(editor.getProject());
    Iterator iterator = ranges.iterator();

    while (iterator.hasNext()) {
        TextRange range = (TextRange) iterator.next();
        //highlightManager.addOccurrenceHighlight(editor, range.getStartOffset() + 1, range.getEndOffset() - 1, attributes, 0, highlighters, (Color) null);
        highlightManager.addRangeHighlight(editor, range.getStartOffset() + 1, range.getEndOffset() - 1, attributes, false, highlighters);
    }

    iterator = highlighters.iterator();

    while (iterator.hasNext()) {
        RangeHighlighter highlighter = (RangeHighlighter) iterator.next();
        highlighter.setGreedyToLeft(true);
        highlighter.setGreedyToRight(true);
    }

}
 
示例2
public static void highlightElement(Editor editor, @NotNull com.intellij.openapi.project.Project project, @NotNull PsiElement[] elements)
{
    final HighlightManager highlightManager =
            HighlightManager.getInstance(project);
    final EditorColorsManager editorColorsManager =
            EditorColorsManager.getInstance();
    final EditorColorsScheme globalScheme =
            editorColorsManager.getGlobalScheme();
    final TextAttributes textattributes =
            globalScheme.getAttributes(
                    EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);

    highlightManager.addOccurrenceHighlights(
            editor, elements, textattributes, true, null);
    final WindowManager windowManager = WindowManager.getInstance();
    final StatusBar statusBar = windowManager.getStatusBar(project);
    statusBar.setInfo("Press Esc to remove highlighting");
}
 
示例3
private boolean findNextUsageInFile(@Nonnull FileEditor fileEditor, @Nonnull SearchResults.Direction direction) {
  if (fileEditor instanceof TextEditor) {
    TextEditor textEditor = (TextEditor)fileEditor;
    Editor editor = textEditor.getEditor();
    editor.getCaretModel().removeSecondaryCarets();
    if (tryToFindNextUsageViaEditorSearchComponent(editor, direction)) {
      return true;
    }

    RangeHighlighter[] highlighters = ((HighlightManagerImpl)HighlightManager.getInstance(myProject)).getHighlighters(editor);
    if (highlighters.length > 0) {
      return highlightNextHighlighter(highlighters, editor, editor.getCaretModel().getOffset(), direction == SearchResults.Direction.DOWN, false);
    }
  }

  if (direction == SearchResults.Direction.DOWN) {
    return myFindUsagesManager.findNextUsageInFile(fileEditor);
  }
  return myFindUsagesManager.findPreviousUsageInFile(fileEditor);
}
 
示例4
private void highlightTemplateVariables(Template template, Editor topLevelEditor) {
  //add highlights
  if (myHighlighters != null) { // can be null if finish is called during testing
    Map<TextRange, TextAttributes> rangesToHighlight = new HashMap<TextRange, TextAttributes>();
    final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
    if (templateState != null) {
      EditorColorsManager colorsManager = EditorColorsManager.getInstance();
      for (int i = 0; i < templateState.getSegmentsCount(); i++) {
        final TextRange segmentOffset = templateState.getSegmentRange(i);
        final String name = template.getSegmentName(i);
        TextAttributes attributes = null;
        if (name.equals(PRIMARY_VARIABLE_NAME)) {
          attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
        }
        else if (name.equals(OTHER_VARIABLE_NAME)) {
          attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
        }
        if (attributes == null) continue;
        rangesToHighlight.put(segmentOffset, attributes);
      }
    }
    addHighlights(rangesToHighlight, topLevelEditor, myHighlighters, HighlightManager.getInstance(myProject));
  }
}
 
示例5
public void finish(boolean success) {
  if (!ourRenamersStack.isEmpty() && ourRenamersStack.peek() == this) {
    ourRenamersStack.pop();
  }
  if (myHighlighters != null) {
    if (!myProject.isDisposed()) {
      final HighlightManager highlightManager = HighlightManager.getInstance(myProject);
      for (RangeHighlighter highlighter : myHighlighters) {
        highlightManager.removeSegmentHighlighter(myEditor, highlighter);
      }
    }

    myHighlighters = null;
    myEditor.putUserData(INPLACE_RENAMER, null);
  }
  if (myBalloon != null) {
    if (!isRestart()) {
      myBalloon.hide();
    }
  }
}
 
示例6
private void finish() {
    ourRenamersStack.pop();
    if (this.myHighlighters != null) {
        Project project = this.myEditor.getProject();
        if (project != null && !project.isDisposed()) {
            HighlightManager highlightManager = HighlightManager.getInstance(project);
            Iterator var3 = this.myHighlighters.iterator();

            while (var3.hasNext()) {
                RangeHighlighter highlighter = (RangeHighlighter) var3.next();
                highlightManager.removeSegmentHighlighter(this.myEditor, highlighter);
            }
        }
    }
}
 
示例7
public static void applyHighlightRange(ArrayList<TextRange> ranges, Project project, Editor editor) {
    if (project == null) {
        return;
    }

    EditorColorsScheme scheme = editor.getColorsScheme();
    TextAttributes attributes = scheme.getAttributes(TemplateColors.TEMPLATE_VARIABLE_ATTRIBUTES);

    for (TextRange textRange : ranges) {
        HighlightManager.getInstance(project).addRangeHighlight(
                editor, textRange.getBegin(), textRange.getEnd(), attributes, true, null);
    }
}
 
示例8
/**
 * Uses a range highlighter to show a range of unused text as dimmed
 */
private static void highlightUnusedRange(Editor editor, TextAttributes unusedTextAttributes, TextRange textRange) {
    final Project project = editor.getProject();
    if (project != null) {
        HighlightManager.getInstance(project).addRangeHighlight(
                editor,
                textRange.getStartOffset(),
                textRange.getEndOffset(),
                unusedTextAttributes, true, true, null);
    }
}
 
示例9
private static void highlightWord(final CompletionVariant variant, final Project project, CompletionData data) {
  int delta = data.startOffset < variant.offset ? variant.variant.length() - data.myWordUnderCursor.length() : 0;

  HighlightManager highlightManager = HighlightManager.getInstance(project);
  EditorColorsManager colorManager = EditorColorsManager.getInstance();
  TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
  highlightManager.addOccurrenceHighlight(variant.editor, variant.offset + delta, variant.offset + variant.variant.length() + delta, attributes,
                                          HighlightManager.HIDE_BY_ANY_KEY, null, null);
}
 
示例10
private void highlightExtractedElements(final List<PsiElement> extractedElements) {
  for (PsiElement each : extractedElements) {
    HighlightManager.getInstance(myProject).addRangeHighlight(
        myEditor,
        each.getTextOffset(),
        each.getTextOffset() + each.getTextLength(),
        getTestAttributesForExtract(),
        false,
        true,
        null);
  }
}
 
示例11
private void itemHovered(Crumb crumb, @SuppressWarnings("unused") InputEvent event) {
  if (!Registry.is("editor.breadcrumbs.highlight.on.hover")) {
    return;
  }

  HighlightManager hm = HighlightManager.getInstance(myProject);
  if (myHighlighed != null) {
    for (RangeHighlighter highlighter : myHighlighed) {
      hm.removeSegmentHighlighter(myEditor, highlighter);
    }
    myHighlighed = null;
  }
  if (crumb instanceof NavigatableCrumb) {
    final TextRange range = ((NavigatableCrumb)crumb).getHighlightRange();
    if (range == null) return;
    final TextAttributes attributes = new TextAttributes();
    final CrumbPresentation p = PsiCrumb.getPresentation(crumb);
    Color color = p == null ? null : p.getBackgroundColor(false, false, false);
    if (color == null) color = BreadcrumbsComponent.ButtonSettings.getBackgroundColor(false, false, false, false);
    if (color == null) color = UIUtil.getLabelBackground();
    final Color background = EditorColorsManager.getInstance().getGlobalScheme().getColor(EditorColors.CARET_ROW_COLOR);
    attributes.setBackgroundColor(makeTransparent(color, background != null ? background : Gray._200, 0.3));
    myHighlighed = new ArrayList<>(1);
    int flags = HighlightManager.HIDE_BY_ESCAPE | HighlightManager.HIDE_BY_TEXT_CHANGE | HighlightManager.HIDE_BY_ANY_KEY;
    hm.addOccurrenceHighlight(myEditor, range.getStartOffset(), range.getEndOffset(), attributes, flags, myHighlighed, null);
  }
}
 
示例12
private RangeHighlighter addHighlighter(int startOffset, int endOffset, @Nonnull TextAttributes attributes) {
  Project project = mySearchResults.getProject();
  if (project == null || project.isDisposed()) return null;
  List<RangeHighlighter> sink = new ArrayList<>();
  HighlightManager.getInstance(project).addRangeHighlight(mySearchResults.getEditor(), startOffset, endOffset, attributes, false, sink);
  RangeHighlighter result = ContainerUtil.getFirstItem(sink);
  if (result instanceof RangeHighlighterEx) ((RangeHighlighterEx)result).setVisibleIfFolded(true);
  return result;
}
 
示例13
@RequiredUIAccess
@Override
public void actionPerformed(@Nonnull AnActionEvent e) {
  DataContext dataContext = e.getDataContext();
  Editor editor = dataContext.getData(CommonDataKeys.EDITOR);
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  List<PsiElement> elements = getElementsToCopy(editor, dataContext);

  if (!doCopy(elements, project, editor) && editor != null && project != null) {
    Document document = editor.getDocument();
    PsiFile file = PsiDocumentManager.getInstance(project).getCachedPsiFile(document);
    if (file != null) {
      String toCopy = getFileFqn(file) + ":" + (editor.getCaretModel().getLogicalPosition().line + 1);
      CopyPasteManager.getInstance().setContents(new StringSelection(toCopy));
      setStatusBarText(project, toCopy + " has been copied");
    }
    return;
  }

  HighlightManager highlightManager = HighlightManager.getInstance(project);
  EditorColorsManager manager = EditorColorsManager.getInstance();
  TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
  if (elements.size() == 1 && editor != null && project != null) {
    PsiElement element = elements.get(0);
    PsiElement nameIdentifier = IdentifierUtil.getNameIdentifier(element);
    if (nameIdentifier != null) {
      highlightManager.addOccurrenceHighlights(editor, new PsiElement[]{nameIdentifier}, attributes, true, null);
    } else {
      PsiReference reference = TargetElementUtil.findReference(editor, editor.getCaretModel().getOffset());
      if (reference != null) {
        highlightManager.addOccurrenceHighlights(editor, new PsiReference[]{reference}, attributes, true, null);
      } else if (element != PsiDocumentManager.getInstance(project).getCachedPsiFile(editor.getDocument())) {
        highlightManager.addOccurrenceHighlights(editor, new PsiElement[]{element}, attributes, true, null);
      }
    }
  }
}
 
示例14
protected void addHighlights(@Nonnull Map<TextRange, TextAttributes> ranges,
                             @Nonnull Editor editor,
                             @Nonnull Collection<RangeHighlighter> highlighters,
                             @Nonnull HighlightManager highlightManager) {
  for (Map.Entry<TextRange, TextAttributes> entry : ranges.entrySet()) {
    TextRange range = entry.getKey();
    TextAttributes attributes = entry.getValue();
    highlightManager.addOccurrenceHighlight(editor, range.getStartOffset(), range.getEndOffset(), attributes, 0, highlighters, null);
  }

  for (RangeHighlighter highlighter : highlighters) {
    highlighter.setGreedyToLeft(true);
    highlighter.setGreedyToRight(true);
  }
}
 
示例15
private void updateEditorTexts(final MethodNodeBase<M> node) {
  final MethodNodeBase<M> parentNode = (MethodNodeBase)node.getParent();
  final String callerText = node != myRoot ? getText(node.getMethod()) : "";
  final Document callerDocument = myCallerEditor.getDocument();
  final String calleeText = node != myRoot ? getText(parentNode.getMethod()) : "";
  final Document calleeDocument = myCalleeEditor.getDocument();

  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    @Override
    public void run() {
      callerDocument.setText(callerText);
      calleeDocument.setText(calleeText);
    }
  });

  final M caller = node.getMethod();
  final PsiElement callee = parentNode != null ? parentNode.getElementToSearch() : null;
  if (caller != null && caller.isPhysical() && callee != null) {
    HighlightManager highlighter = HighlightManager.getInstance(myProject);
    EditorColorsManager colorManager = EditorColorsManager.getInstance();
    TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
    int start = getStartOffset(caller);
    for (PsiElement element : findElementsToHighlight(caller, callee)) {
      highlighter.addRangeHighlight(myCallerEditor, element.getTextRange().getStartOffset() - start,
                                    element.getTextRange().getEndOffset() - start, attributes, false, null);
    }
  }
}
 
示例16
public InplaceChangeSignature(Project project, Editor editor, @Nonnull PsiElement element) {
  myDocumentManager = PsiDocumentManager.getInstance(project);
  myProject = project;
  try {
    myMarkAction = StartMarkAction.start(editor, project, ChangeSignatureHandler.REFACTORING_NAME);
  }
  catch (StartMarkAction.AlreadyStartedException e) {
    final int exitCode = Messages.showYesNoDialog(myProject, e.getMessage(), ChangeSignatureHandler.REFACTORING_NAME, "Navigate to Started", "Cancel", Messages.getErrorIcon());
    if (exitCode == Messages.CANCEL) return;
    PsiElement method = myStableChange.getMethod();
    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(method);
    new OpenFileDescriptor(project, virtualFile, method.getTextOffset()).navigate(true);
    return;
  }


  myEditor = editor;
  myDetector = LanguageChangeSignatureDetectors.INSTANCE.forLanguage(element.getLanguage());
  myStableChange = myDetector.createInitialChangeInfo(element);
  myInitialSignature = myDetector.extractSignature(myStableChange);
  myInitialName = DescriptiveNameUtil.getDescriptiveName(myStableChange.getMethod());
  TextRange highlightingRange = myDetector.getHighlightingRange(myStableChange);

  HighlightManager highlightManager = HighlightManager.getInstance(myProject);
  TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.LIVE_TEMPLATE_ATTRIBUTES);
  highlightManager.addRangeHighlight(editor, highlightingRange.getStartOffset(), highlightingRange.getEndOffset(), attributes, false, myHighlighters);
  for (RangeHighlighter highlighter : myHighlighters) {
    highlighter.setGreedyToRight(true);
    highlighter.setGreedyToLeft(true);
  }
  myEditor.getDocument().addDocumentListener(this);
  myEditor.putUserData(INPLACE_CHANGE_SIGNATURE, this);
  myPreview = InplaceRefactoring.createPreviewComponent(project, myDetector.getFileType());
  showBalloon();
}
 
示例17
public void detach() {
  myEditor.getDocument().removeDocumentListener(this);
  HighlightManager highlightManager = HighlightManager.getInstance(myProject);
  for (RangeHighlighter highlighter : myHighlighters) {
    highlightManager.removeSegmentHighlighter(myEditor, highlighter);
  }
  myHighlighters.clear();
  myBalloon.hide();
  myDetector = null;
  FinishMarkAction.finish(myProject, myEditor, myMarkAction);
  myEditor.putUserData(INPLACE_CHANGE_SIGNATURE, null);
}
 
示例18
private static void highlightInEditor(@Nonnull final Project project, @Nonnull final SimpleMatch match,
                                      @Nonnull final Editor editor, Map<SimpleMatch, RangeHighlighter> highlighterMap) {
  final List<RangeHighlighter> highlighters = new ArrayList<>();
  final HighlightManager highlightManager = HighlightManager.getInstance(project);
  final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
  final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
  final int startOffset = match.getStartElement().getTextRange().getStartOffset();
  final int endOffset = match.getEndElement().getTextRange().getEndOffset();
  highlightManager.addRangeHighlight(editor, startOffset, endOffset, attributes, true, highlighters);
  highlighterMap.put(match, highlighters.get(0));
  final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(startOffset);
  editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.MAKE_VISIBLE);
}
 
示例19
private static void highlightInEditor(final Project project, final IncludeDuplicate pair, final Editor editor) {
  final HighlightManager highlightManager = HighlightManager.getInstance(project);
  EditorColorsManager colorsManager = EditorColorsManager.getInstance();
  TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
  final int startOffset = pair.getStart().getTextRange().getStartOffset();
  final int endOffset = pair.getEnd().getTextRange().getEndOffset();
  highlightManager.addRangeHighlight(editor, startOffset, endOffset, attributes, true, null);
  final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(startOffset);
  editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.MAKE_VISIBLE);
}
 
示例20
private static void removeHighlights(Editor editor, Project project) {
    HighlightManagerImpl highlightManager = (HighlightManagerImpl) HighlightManager.getInstance(project);
    for (RangeHighlighter rangeHighlighter : highlightManager.getHighlighters(editor)) {
        highlightManager.removeSegmentHighlighter(editor, rangeHighlighter);
    }
}
 
示例21
private void removeHighlighter(@Nonnull RangeHighlighter highlighter) {
  Project project = mySearchResults.getProject();
  if (project == null || project.isDisposed()) return;
  HighlightManager.getInstance(project).removeSegmentHighlighter(mySearchResults.getEditor(), highlighter);
}
 
示例22
/**
 * Notifies user about found duplicates and then highlights each of them in the editor and asks user how to proceed.
 *
 * @param callElement generated expression or statement that contains invocation of the new method
 * @param editor      instance of editor where refactoring is performed
 * @param replacer    strategy of substituting each duplicate occurence with the replacement fragment
 * @param duplicates  discovered duplicates of extracted code fragment
 * @see #collectDuplicates(SimpleDuplicatesFinder, List, PsiElement)
 */
public static void replaceDuplicates(@Nonnull PsiElement callElement,
                                     @Nonnull Editor editor,
                                     @Nonnull Consumer<Pair<SimpleMatch, PsiElement>> replacer,
                                     @Nonnull List<SimpleMatch> duplicates) {
  if (!duplicates.isEmpty()) {
    final String message = RefactoringBundle
            .message("0.has.detected.1.code.fragments.in.this.file.that.can.be.replaced.with.a.call.to.extracted.method",
                     ApplicationNamesInfo.getInstance().getProductName(), duplicates.size());
    final boolean isUnittest = ApplicationManager.getApplication().isUnitTestMode();
    final Project project = callElement.getProject();
    final int exitCode = !isUnittest ? Messages.showYesNoDialog(project, message,
                                                                RefactoringBundle.message("refactoring.extract.method.dialog.title"),
                                                                Messages.getInformationIcon()) :
                         Messages.YES;
    if (exitCode == Messages.YES) {
      boolean replaceAll = false;
      final Map<SimpleMatch, RangeHighlighter> highlighterMap = new HashMap<>();
      for (SimpleMatch match : duplicates) {
        if (!match.getStartElement().isValid() || !match.getEndElement().isValid()) continue;
        final Pair<SimpleMatch, PsiElement> replacement = Pair.create(match, callElement);
        if (!replaceAll) {
          highlightInEditor(project, match, editor, highlighterMap);

          int promptResult = FindManager.PromptResult.ALL;
          //noinspection ConstantConditions
          if (!isUnittest) {
            ReplacePromptDialog promptDialog =
                    new ReplacePromptDialog(false, RefactoringBundle.message("replace.fragment"), project);
            promptDialog.show();
            promptResult = promptDialog.getExitCode();
          }
          if (promptResult == FindManager.PromptResult.SKIP) {
            final HighlightManager highlightManager = HighlightManager.getInstance(project);
            final RangeHighlighter highlighter = highlighterMap.get(match);
            if (highlighter != null) highlightManager.removeSegmentHighlighter(editor, highlighter);
            continue;
          }
          if (promptResult == FindManager.PromptResult.CANCEL) break;

          if (promptResult == FindManager.PromptResult.OK) {
            replaceDuplicate(project, replacer, replacement);
          }
          else if (promptResult == FindManager.PromptResult.ALL) {
            replaceDuplicate(project, replacer, replacement);
            replaceAll = true;
          }
        }
        else {
          replaceDuplicate(project, replacer, replacement);
        }
      }
    }
  }
}