use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.
the class ExtractMethodHelper method highlightInEditor.
private static void highlightInEditor(@NotNull final Project project, @NotNull final SimpleMatch match, @NotNull 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);
}
use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.
the class InplaceChangeSignature method detach.
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);
}
use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.
the class ExtractMethodHelper method replaceDuplicates.
/**
* 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(@NotNull PsiElement callElement, @NotNull Editor editor, @NotNull Consumer<Pair<SimpleMatch, PsiElement>> replacer, @NotNull 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);
}
}
}
}
}
use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.
the class ThreadDumpPanel method highlightOccurrences.
private static void highlightOccurrences(String filter, Project project, Editor editor) {
final HighlightManager highlightManager = HighlightManager.getInstance(project);
EditorColorsManager colorManager = EditorColorsManager.getInstance();
final TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
String documentText = editor.getDocument().getText();
int i = -1;
while (true) {
int nextOccurrence = StringUtil.indexOfIgnoreCase(documentText, filter, i + 1);
if (nextOccurrence < 0) {
break;
}
i = nextOccurrence;
highlightManager.addOccurrenceHighlight(editor, i, i + filter.length(), attributes, HighlightManager.HIDE_BY_TEXT_CHANGE, null, null);
}
}
use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.
the class TempWithQueryHandler method invokeOnVariable.
private static void invokeOnVariable(final PsiFile file, final Project project, final PsiLocalVariable local, final Editor editor) {
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file))
return;
String localName = local.getName();
final PsiExpression initializer = local.getInitializer();
if (initializer == null) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.has.no.initializer", localName));
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
return;
}
final PsiReference[] refs = ReferencesSearch.search(local, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
if (refs.length == 0) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.never.used", localName));
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
return;
}
final HighlightManager highlightManager = HighlightManager.getInstance(project);
ArrayList<PsiReference> array = new ArrayList<>();
EditorColorsManager manager = EditorColorsManager.getInstance();
final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
for (PsiReference ref : refs) {
PsiElement refElement = ref.getElement();
if (PsiUtil.isAccessedForWriting((PsiExpression) refElement)) {
array.add(ref);
}
if (!array.isEmpty()) {
PsiReference[] refsForWriting = array.toArray(new PsiReference[array.size()]);
highlightManager.addOccurrenceHighlights(editor, refsForWriting, attributes, true, null);
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing", localName));
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
return;
}
}
final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, new PsiElement[] { initializer }, local.getType(), REFACTORING_NAME, localName, HelpID.REPLACE_TEMP_WITH_QUERY);
try {
if (!processor.prepare())
return;
} catch (PrepareFailedException e) {
CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
ExtractMethodHandler.highlightPrepareError(e, file, editor, project);
return;
}
final PsiClass targetClass = processor.getTargetClass();
if (targetClass != null && targetClass.isInterface()) {
String message = RefactoringBundle.message("cannot.replace.temp.with.query.in.interface");
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
return;
}
if (processor.showDialog()) {
CommandProcessor.getInstance().executeCommand(project, () -> {
final Runnable action = () -> {
try {
processor.doRefactoring();
local.normalizeDeclaration();
PsiExpression initializer1 = local.getInitializer();
PsiExpression[] exprs = new PsiExpression[refs.length];
for (int idx = 0; idx < refs.length; idx++) {
PsiElement ref = refs[idx].getElement();
exprs[idx] = (PsiExpression) ref.replace(initializer1);
}
PsiDeclarationStatement declaration = (PsiDeclarationStatement) local.getParent();
declaration.delete();
highlightManager.addOccurrenceHighlights(editor, exprs, attributes, true, null);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
};
PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
ApplicationManager.getApplication().runWriteAction(action);
DuplicatesImpl.processDuplicates(processor, project, editor);
});
}, REFACTORING_NAME, null);
}
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
Aggregations