void (@NotNull Project project, @NotNull Disposable parentDisposable, @NotNull Alarm alarm) { EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster(); eventMulticaster.addCaretListener(new CaretListener() { @Override public void caretPositionChanged(@NotNull CaretEvent e) { if (e.getCaret() != e.getEditor().getCaretModel().getPrimaryCaret()) return; onCaretUpdate(e.getEditor(), project, alarm); } @Override public void caretAdded(@NotNull CaretEvent e) { if (e.getCaret() != e.getEditor().getCaretModel().getPrimaryCaret()) return; onCaretUpdate(e.getEditor(), project, alarm); } @Override public void caretRemoved(@NotNull CaretEvent e) { onCaretUpdate(e.getEditor(), project, alarm); } }, parentDisposable); SelectionListener selectionListener = new SelectionListener() { @Override public void selectionChanged(@NotNull SelectionEvent e) { alarm.cancelAllRequests(); Editor editor = e.getEditor(); if (editor.getProject() != project) { return; } highlightSelection(project, editor); TextRange oldRange = e.getOldRange(); TextRange newRange = e.getNewRange(); if (oldRange != null && newRange != null && oldRange.isEmpty() == newRange.isEmpty()) { // Don't update braces in case of active/absent selection. return; } updateHighlighted(project, editor, alarm); } }; eventMulticaster.addSelectionListener(selectionListener, parentDisposable); DocumentListener documentListener = new DocumentListener() { @Override public void documentChanged(@NotNull DocumentEvent e) { alarm.cancelAllRequests(); EditorFactory.getInstance().editors(e.getDocument(), project).forEach( editor -> updateHighlighted(project, editor, alarm) ); } }; eventMulticaster.addDocumentListener(documentListener, parentDisposable); MessageBusConnection connection = project.getMessageBus().connect(parentDisposable); connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() { @Override public void selectionChanged(@NotNull FileEditorManagerEvent e) { alarm.cancelAllRequests(); FileEditor oldEditor = e.getOldEditor(); if (oldEditor instanceof TextEditor) { clearBraces(project, ((TextEditor)oldEditor).getEditor(), alarm); } FileEditor newEditor = e.getNewEditor(); if (newEditor instanceof TextEditor) { updateHighlighted(project, ((TextEditor)newEditor).getEditor(), alarm); } } }); connection.subscribe(TemplateManager.TEMPLATE_STARTED_TOPIC, state -> { if (state.isFinished()) return; updateHighlighted(project, state.getEditor(), alarm); state.addTemplateStateListener(new TemplateEditingAdapter() { @Override public void templateFinished(@NotNull Template template, boolean brokenOff) { updateHighlighted(project, state.getEditor(), alarm); } }); }); }