Skip to content

Commit

Permalink
[FIX][I] #711 Ignore non-text editors for editor state
Browse files Browse the repository at this point in the history
Adjusts the logic sharing the local editor state to ignore the opening
of non-text editors.

Adjusts the logic sharing the local editor state to ignore the closing
of non-text editors.

Moves the closing logic to the before listener to allow us to access the
editor before it is actually closed. This is needed to check the editor
type.

With this change, non-text editors are completely ignored by Saros/I.
This was done to match the Eclipse handling of such non-text editors.

Partial fix for #711.
  • Loading branch information
tobous committed Mar 11, 2020
1 parent cee9512 commit 1bdaf0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
4 changes: 3 additions & 1 deletion intellij/src/saros/intellij/editor/LocalEditorHandler.java
Expand Up @@ -153,7 +153,9 @@ private Editor openEditor(
}

/**
* Removes a file from the editorPool and calls {@link EditorManager#generateEditorClosed(SPath)}
* Removes a file from the editorPool and calls {@link EditorManager#generateEditorClosed(SPath)}.
*
* <p>Does nothing if the file is not shared.
*
* @param project the project in which to close the editor
* @param virtualFile the file for which to close the editor
Expand Down
@@ -1,17 +1,24 @@
package saros.intellij.eventhandler.editor.editorstate;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
import saros.intellij.editor.LocalEditorHandler;
import saros.intellij.editor.ProjectAPI;

/**
* Dispatches matching editor activities when an editor for a shared file is opened/selected or
* closed.
*
* <p>The listener for closed editors is called before the editor is closed. This gives us the
* change to check the type of the editor before it is actually closed, which is necessary as we
* only care about text editors.
*/
public class EditorStatusChangeActivityDispatcher extends AbstractLocalEditorStatusChangeHandler {

Expand All @@ -20,17 +27,20 @@ public class EditorStatusChangeActivityDispatcher extends AbstractLocalEditorSta
private final FileEditorManagerListener fileEditorManagerListener =
new FileEditorManagerListener() {
@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
assert isEnabled() : "the file closed listener was triggered while it was disabled";
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
assert isEnabled() : "the selection changed listener was triggered while it was disabled";

generateEditorClosedActivity(file);
generateEditorActivatedActivity(event);
}
};

private final FileEditorManagerListener.Before beforeFileEditorManagerListener =
new FileEditorManagerListener.Before() {
@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
assert isEnabled() : "the selection changed listener was triggered while it was disabled";
public void beforeFileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
assert isEnabled() : "the file closed listener was triggered while it was disabled";

generateEditorActivatedActivity(event);
generateEditorClosedActivity(file);
}
};

Expand All @@ -45,26 +55,39 @@ public EditorStatusChangeActivityDispatcher(
/**
* Calls {@link LocalEditorHandler#closeEditor(Project, VirtualFile)}.
*
* <p>Does nothing if the closed editor is not a text editor.
*
* @param virtualFile the file whose editor was closed
* @see FileEditorManagerListener#fileClosed(FileEditorManager, VirtualFile)
*/
private void generateEditorClosedActivity(@NotNull VirtualFile virtualFile) {
localEditorHandler.closeEditor(project, virtualFile);
if (ProjectAPI.isOpenInTextEditor(project, virtualFile)) {
localEditorHandler.closeEditor(project, virtualFile);
}
}

/**
* Calls {@link LocalEditorHandler#activateEditor(Project, VirtualFile)}.
*
* <p>Does nothing if the opened editor is not a text editor.
*
* @param event the event to react to
* @see FileEditorManagerListener#selectionChanged(FileEditorManagerEvent)
*/
private void generateEditorActivatedActivity(@NotNull FileEditorManagerEvent event) {
localEditorHandler.activateEditor(project, event.getNewFile());
FileEditor newEditor = event.getNewEditor();

if (newEditor == null || newEditor instanceof TextEditor) {
localEditorHandler.activateEditor(project, event.getNewFile());
}
}

@Override
void registerListeners(@NotNull MessageBusConnection messageBusConnection) {
messageBusConnection.subscribe(
fileEditorManagerListener.FILE_EDITOR_MANAGER, fileEditorManagerListener);

messageBusConnection.subscribe(
beforeFileEditorManagerListener.FILE_EDITOR_MANAGER, beforeFileEditorManagerListener);
}
}

0 comments on commit 1bdaf0a

Please sign in to comment.