Don't open the same file twice, select the existing tab

GitOrigin-RevId: d174157330ecbd273e775839d313bb1b60e23c9f
This commit is contained in:
Rustam Vishnyakov
2019-11-15 16:00:08 +00:00
committed by intellij-monorepo-bot
parent 97c38e939e
commit 0e8edaaef9
4 changed files with 43 additions and 25 deletions
@@ -4,7 +4,6 @@ package com.intellij.ide.lightEdit;
import com.intellij.ide.lightEdit.statusBar.LightEditStatusBar;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
@@ -27,14 +26,15 @@ public class LightEditPanel extends JPanel implements Disposable {
Disposer.register(this, statusBar);
}
void addEditorTab(@NotNull Editor editor, @NotNull VirtualFile file) {
myTabs.addEditorTab(editor, file);
public LightEditTabs getTabs() {
return myTabs;
}
public void loadFile(@NotNull VirtualFile file) {
Editor editor = myEditorManager.createEditor(file);
if (editor != null) {
myTabs.addEditorTab(editor, file);
LightEditorInfo editorInfo = myEditorManager.createEditor(file);
if (editorInfo != null) {
myTabs.addEditorTab(editorInfo);
}
}
@@ -6,7 +6,6 @@ import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.WindowWrapper;
import com.intellij.openapi.ui.WindowWrapperBuilder;
@@ -54,9 +53,15 @@ public class LightEditService implements Disposable, LightEditorListener {
public void openFile(@NotNull VirtualFile file) {
showEditorWindow();
Editor editor = myEditorManager.createEditor(file);
if (editor != null) {
getEditPanel().addEditorTab(editor, file);
LightEditorInfo openEditorInfo = myEditorManager.findOpen(file);
if (openEditorInfo == null) {
LightEditorInfo newEditorInfo = myEditorManager.createEditor(file);
if (newEditorInfo != null) {
getEditPanel().getTabs().addEditorTab(newEditorInfo);
}
}
else {
getEditPanel().getTabs().selectTab(openEditorInfo);
}
}
@@ -11,7 +11,6 @@ import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.ui.tabs.TabsListener;
import com.intellij.ui.tabs.impl.JBEditorTabs;
@@ -36,12 +35,11 @@ class LightEditTabs extends JBEditorTabs {
});
}
void addEditorTab(@NotNull Editor editor, @NotNull VirtualFile file) {
TabInfo tabInfo = new TabInfo(createEditorContainer(editor))
.setText(file.getPresentableName())
void addEditorTab(@NotNull LightEditorInfo editorInfo) {
TabInfo tabInfo = new TabInfo(createEditorContainer(editorInfo.getEditor()))
.setText(editorInfo.getFile().getPresentableName())
.setTabColor(EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground());
final LightEditorInfo editorInfo = new LightEditorInfo(editor, file);
tabInfo.setObject(editorInfo);
final DefaultActionGroup tabActions = new DefaultActionGroup();
@@ -70,6 +68,12 @@ class LightEditTabs extends JBEditorTabs {
myEditorManager.fireEditorSelected(selectedEditorInfo);
}
void selectTab(@NotNull LightEditorInfo info) {
getTabs().stream()
.filter(tabInfo -> tabInfo.getObject().equals(info))
.findFirst().ifPresent(tabInfo -> select(tabInfo, true));
}
private class CloseTabAction extends DumbAwareAction {
private final LightEditorInfo myEditorInfo;
@@ -22,26 +22,28 @@ import java.util.ArrayList;
import java.util.List;
public class LightEditorManager implements Disposable {
private final List<Editor> myEditors = new ArrayList<>();
private final List<LightEditorInfo> myEditors = new ArrayList<>();
private final EventDispatcher<LightEditorListener> myEventDispatcher = EventDispatcher.create(LightEditorListener.class);
@NotNull
Editor createEditor(@NotNull Document document) {
private LightEditorInfo createEditor(@NotNull Document document, @NotNull VirtualFile file) {
Editor editor = EditorFactory.getInstance().createEditor(
document, ProjectManager.getInstance().getDefaultProject(), EditorKind.MAIN_EDITOR);
ObjectUtils.consumeIfCast(editor, EditorImpl.class,
editorImpl -> editorImpl.setDropHandler(new LightEditDropHandler()));
myEditors.add(editor);
return editor;
final LightEditorInfo editorInfo = new LightEditorInfo(editor, file);
myEditors.add(editorInfo);
return editorInfo;
}
@Nullable
Editor createEditor(@NotNull VirtualFile file) {
LightEditorInfo createEditor(@NotNull VirtualFile file) {
Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
Editor editor = createEditor(document);
LightEditorInfo editorInfo = createEditor(document, file);
Editor editor = editorInfo.getEditor();
if (editor instanceof EditorEx) ((EditorEx)editor).setHighlighter(getHighlighter(file, editor));
return editor;
return editorInfo;
}
return null;
}
@@ -49,14 +51,14 @@ public class LightEditorManager implements Disposable {
@Override
public void dispose() {
myEditors.stream()
.filter(editor -> !editor.isDisposed())
.forEach(editor -> EditorFactory.getInstance().releaseEditor(editor));
.filter(editorInfo -> !editorInfo.getEditor().isDisposed())
.forEach(editorInfo -> EditorFactory.getInstance().releaseEditor(editorInfo.getEditor()));
myEditors.clear();
}
void closeEditor(@NotNull LightEditorInfo editorInfo) {
Editor editor = editorInfo.getEditor();
myEditors.remove(editor);
myEditors.remove(editorInfo);
if (!editor.isDisposed()) {
EditorFactory.getInstance().releaseEditor(editor);
}
@@ -79,4 +81,11 @@ public class LightEditorManager implements Disposable {
int getEditorCount() {
return myEditors.size();
}
@Nullable
LightEditorInfo findOpen(@NotNull VirtualFile file) {
return myEditors.stream()
.filter(editorInfo -> file.getPath().equals(editorInfo.getFile().getPath()))
.findFirst().orElse(null);
}
}