use of com.intellij.openapi.editor.ex.EditorEventMulticasterEx in project intellij-community by JetBrains.
the class BraceHighlighter method runActivity.
@Override
public void runActivity(@NotNull final Project project) {
// sorry, upsource
if (ApplicationManager.getApplication().isHeadlessEnvironment())
return;
final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
CaretListener myCaretListener = new CaretAdapter() {
@Override
public void caretPositionChanged(CaretEvent e) {
myAlarm.cancelAllRequests();
Editor editor = e.getEditor();
final SelectionModel selectionModel = editor.getSelectionModel();
// Don't update braces in case of the active selection.
if (editor.getProject() != project || selectionModel.hasSelection()) {
return;
}
final Document document = editor.getDocument();
int line = e.getNewPosition().line;
if (line < 0 || line >= document.getLineCount()) {
return;
}
updateBraces(editor, myAlarm);
}
};
eventMulticaster.addCaretListener(myCaretListener, project);
final SelectionListener mySelectionListener = new SelectionListener() {
@Override
public void selectionChanged(SelectionEvent e) {
myAlarm.cancelAllRequests();
Editor editor = e.getEditor();
if (editor.getProject() != project) {
return;
}
final TextRange oldRange = e.getOldRange();
final TextRange newRange = e.getNewRange();
if (oldRange != null && newRange != null && !(oldRange.isEmpty() ^ newRange.isEmpty())) {
// Don't perform braces update in case of active/absent selection.
return;
}
updateBraces(editor, myAlarm);
}
};
eventMulticaster.addSelectionListener(mySelectionListener, project);
DocumentListener documentListener = new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
myAlarm.cancelAllRequests();
Editor[] editors = EditorFactory.getInstance().getEditors(e.getDocument(), project);
for (Editor editor : editors) {
updateBraces(editor, myAlarm);
}
}
};
eventMulticaster.addDocumentListener(documentListener, project);
final FocusChangeListener myFocusChangeListener = new FocusChangeListener() {
@Override
public void focusLost(Editor editor) {
clearBraces(editor);
}
@Override
public void focusGained(Editor editor) {
updateBraces(editor, myAlarm);
}
};
((EditorEventMulticasterEx) eventMulticaster).addFocusChangeListner(myFocusChangeListener, project);
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
fileEditorManager.addFileEditorManagerListener(new FileEditorManagerListener() {
@Override
public void selectionChanged(@NotNull FileEditorManagerEvent e) {
myAlarm.cancelAllRequests();
}
}, project);
}
Aggregations