void (@NotNull Project project, @NotNull Editor editor) { if (!Registry.is("editor.highlight.selected.text.occurrences") || !CodeInsightSettings.getInstance().HIGHLIGHT_IDENTIFIER_UNDER_CARET) { return; } ThreadingAssertions.assertEventDispatchThread(); Document document = editor.getDocument(); long stamp = document.getModificationStamp(); if (document.isInBulkUpdate()) { return; } if (!BackgroundHighlightingUtil.isValidEditor(editor)) { return; } MarkupModel markupModel = editor.getMarkupModel(); Collection<RangeHighlighter> oldHighlighters = editor.getUserData(SELECTION_HIGHLIGHTS); if (oldHighlighters != null) { editor.putUserData(SELECTION_HIGHLIGHTS, null); for (RangeHighlighter highlighter : oldHighlighters) { markupModel.removeHighlighter(highlighter); } } CaretModel caretModel = editor.getCaretModel(); if (caretModel.getCaretCount() > 1) { return; } Caret caret = caretModel.getPrimaryCaret(); if (!caret.hasSelection()) { return; } int start = caret.getSelectionStart(); int end = caret.getSelectionEnd(); CharSequence sequence = document.getCharsSequence(); String toFind = sequence.subSequence(start, end).toString(); if (toFind.trim().isEmpty()) { return; } FindManager findManager = FindManager.getInstance(project); FindModel findModel = new FindModel(); EditorSearchSession editorSearchSession = EditorSearchSession.get(editor); if (editorSearchSession != null) { findModel.copyFrom(findManager.getFindInFileModel()); } findModel.setRegularExpressions(false); findModel.setStringToFind(toFind); ReadAction.nonBlocking(() -> { int offset = 0; FindResult result = findManager.findString(sequence, offset, findModel, null); List<FindResult> results = new ArrayList<>(); int count = 0; while (result.isStringFound() && count < LivePreviewController.MATCHES_LIMIT) { count++; results.add(result); offset = result.getEndOffset(); result = findManager.findString(sequence, offset, findModel, null); } return results; }) .coalesceBy(HighlightSelectionKey.class, editor) .expireWhen(() -> document.getModificationStamp() != stamp || editor.isDisposed()) .finishOnUiThread(ModalityState.nonModal(), results -> { if (document.getModificationStamp() != stamp || results.isEmpty()) { return; } List<RangeHighlighter> highlighters = new ArrayList<>(); for (FindResult result : results) { highlighters.add(markupModel.addRangeHighlighter(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES, result.getStartOffset(), result.getEndOffset(), HighlightManagerImpl.OCCURRENCE_LAYER, HighlighterTargetArea.EXACT_RANGE)); } editor.putUserData(SELECTION_HIGHLIGHTS, highlighters); }) .submit(AppExecutorUtil.getAppExecutorService()); }