From 2f48da2261bc061d6a1ce8d40fa2d321e98b5691 Mon Sep 17 00:00:00 2001 From: Alexander Kirillin Date: Wed, 7 Mar 2012 16:17:07 +0400 Subject: [PATCH] OC-2581: Open .h and .m files side-by-side --- .../openapi/fileEditor/impl/EditorWindow.java | 13 +++++ .../impl/FileEditorAssociateFinder.java | 35 +++++++++++++ .../impl/FileEditorManagerImpl.java | 50 ++++++++++++++++++- .../src/META-INF/PlatformExtensionPoints.xml | 1 + 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorAssociateFinder.java diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorWindow.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorWindow.java index 7e39542cf1fc..0513afa4175e 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorWindow.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorWindow.java @@ -24,6 +24,7 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ScrollType; import com.intellij.openapi.editor.ScrollingModel; +import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.fileEditor.FileEditorManagerListener; import com.intellij.openapi.fileEditor.TextEditor; @@ -665,6 +666,18 @@ public class EditorWindow { */ // open only selected file in the new splitter instead of opening all tabs final VirtualFile file = selectedEditor.getFile(); + + if (virtualFile == null) { + for (FileEditorAssociateFinder finder : Extensions.getExtensions(FileEditorAssociateFinder.EP_NAME)) { + VirtualFile associatedFile = finder.getAssociatedFileToOpen(fileEditorManager.getProject(), file); + + if (associatedFile != null) { + virtualFile = associatedFile; + break; + } + } + } + final VirtualFile nextFile = virtualFile == null ? file : virtualFile; final FileEditor[] editors = fileEditorManager.openFileImpl3(res, nextFile, false, null, true).first; syncCaretIfPossible(editors); diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorAssociateFinder.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorAssociateFinder.java new file mode 100644 index 000000000000..34e3810fe07a --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorAssociateFinder.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.fileEditor.impl; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Created by IntelliJ IDEA. + * User: angus + * Date: 3/6/12 + * Time: 1:37 PM + */ +public interface FileEditorAssociateFinder { + ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.editorFileAssociateFinder"); + + @Nullable + VirtualFile getAssociatedFileToOpen(@NotNull Project project, @NotNull VirtualFile original); +} diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java index 3d9fb9834528..b1fd162773b9 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.java @@ -122,6 +122,17 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Projec myDockManager = dockManager; myListenerList = new MessageListenerList(myProject.getMessageBus(), FileEditorManagerListener.FILE_EDITOR_MANAGER); + + if (Extensions.getExtensions(FileEditorAssociateFinder.EP_NAME).length > 0) { + myListenerList.add(new FileEditorManagerAdapter() { + @Override + public void selectionChanged(FileEditorManagerEvent event) { + EditorsSplitters splitters = getSplitters(); + openAssociatedFile(event.getNewFile(), splitters.getCurrentWindow(), splitters); + } + }); + } + myQueue.setTrackUiActivity(true); } @@ -597,12 +608,39 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Projec else { wndToOpenIn = getSplitters().getCurrentWindow(); } + + EditorsSplitters splitters = getSplitters(); + if (wndToOpenIn == null) { - wndToOpenIn = getSplitters().getOrCreateCurrentWindow(file); + wndToOpenIn = splitters.getOrCreateCurrentWindow(file); } + + openAssociatedFile(file, wndToOpenIn, splitters); return openFileImpl2(wndToOpenIn, file, focusEditor); } + private void openAssociatedFile(VirtualFile file, EditorWindow wndToOpenIn, EditorsSplitters splitters) { + EditorWindow[] windows = splitters.getWindows(); + + if (file != null && windows.length == 2) { + for (FileEditorAssociateFinder finder : Extensions.getExtensions(FileEditorAssociateFinder.EP_NAME)) { + VirtualFile associatedFile = finder.getAssociatedFileToOpen(myProject, file); + + if (associatedFile != null) { + EditorWindow currentWindow = splitters.getCurrentWindow(); + int idx = windows[0] == wndToOpenIn ? 1 : 0; + openFileImpl2(windows[idx], associatedFile, false); + + if (currentWindow != null) { + splitters.setCurrentWindow(currentWindow, false); + } + + break; + } + } + } + } + @NotNull @Override public Pair openFileWithProviders(@NotNull VirtualFile file, @@ -1309,6 +1347,16 @@ public class FileEditorManagerImpl extends FileEditorManagerEx implements Projec final boolean filesEqual = oldData.first == null ? newData.first == null : oldData.first.equals(newData.first); final boolean editorsEqual = oldData.second == null ? newData.second == null : oldData.second.equals(newData.second); if (!filesEqual || !editorsEqual) { + if (oldData.first != null && newData.first != null) { + for (FileEditorAssociateFinder finder : Extensions.getExtensions(FileEditorAssociateFinder.EP_NAME)) { + VirtualFile associatedFile = finder.getAssociatedFileToOpen(myProject, oldData.first); + + if (associatedFile == newData.first) { + return; + } + } + } + final FileEditorManagerEvent event = new FileEditorManagerEvent(this, oldData.first, oldData.second, oldData.third, newData.first, newData.second, newData.third); final FileEditorManagerListener publisher = getProject().getMessageBus().syncPublisher(FileEditorManagerListener.FILE_EDITOR_MANAGER); diff --git a/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml b/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml index f75d8c589fd5..78db4f2f25d5 100644 --- a/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/PlatformExtensionPoints.xml @@ -79,6 +79,7 @@ +