Search in sources :

Example 1 with EditorEventMulticasterEx

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);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) EditorEventMulticasterEx(com.intellij.openapi.editor.ex.EditorEventMulticasterEx) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) FileEditorManagerEvent(com.intellij.openapi.fileEditor.FileEditorManagerEvent) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener) FocusChangeListener(com.intellij.openapi.editor.ex.FocusChangeListener)

Aggregations

Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 EditorEventMulticasterEx (com.intellij.openapi.editor.ex.EditorEventMulticasterEx)1 FocusChangeListener (com.intellij.openapi.editor.ex.FocusChangeListener)1 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 FileEditorManagerEvent (com.intellij.openapi.fileEditor.FileEditorManagerEvent)1 FileEditorManagerListener (com.intellij.openapi.fileEditor.FileEditorManagerListener)1 TextRange (com.intellij.openapi.util.TextRange)1