From d81b40752d03377ee37ca068dd3990b7a68fe734 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 18 Dec 2012 11:52:09 +0100 Subject: [PATCH] Make open file XML-RPC handler able to accept relative paths --- .../intellij/ide/OpenFileXmlRpcHandler.java | 128 ++++++++++++++++++ .../intellij/ide/OpenFileXmlRpcHandler.java | 65 --------- 2 files changed, 128 insertions(+), 65 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java delete mode 100644 platform/platform-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java diff --git a/platform/lang-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java b/platform/lang-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java new file mode 100644 index 000000000000..f954cd742ae1 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java @@ -0,0 +1,128 @@ +/* + * 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.ide; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.fileEditor.FileEditorManager; +import com.intellij.openapi.fileEditor.FileEditorProvider; +import com.intellij.openapi.fileEditor.OpenFileDescriptor; +import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectLocator; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.openapi.util.Computable; +import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vcs.ProjectLevelVcsManager; +import com.intellij.openapi.vcs.VcsRoot; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.Nullable; + +import java.io.File; + +/** + * @author mike + */ +public class OpenFileXmlRpcHandler { + private static final Logger LOG = Logger.getInstance(OpenFileXmlRpcHandler.class); + + // XML-RPC interface method - keep the signature intact + @SuppressWarnings("UnusedDeclaration") + public boolean open(String path) { + LOG.debug("open(" + path + ")"); + return doOpen(path, -1, -1); + } + + // XML-RPC interface method - keep the signature intact + @SuppressWarnings("UnusedDeclaration") + public boolean openAndNavigate(String path, int line, int column) { + LOG.debug("openAndNavigate(" + path + ", " + line + ", " + column + ")"); + return doOpen(path, line, column); + } + + private static boolean doOpen(final String path, final int line, final int column) { + ApplicationManager.getApplication().invokeLater(new Runnable() { + public void run() { + Pair data = new File(path).isAbsolute() ? findByAbsolutePath(path) : findByRelativePath(path); + if (data == null) return; + + FileEditorProvider[] providers = FileEditorProviderManager.getInstance().getProviders(data.second, data.first); + if (providers.length == 0) return; + + OpenFileDescriptor descriptor = new OpenFileDescriptor(data.second, data.first, line, column); + FileEditorManager.getInstance(data.second).openTextEditor(descriptor, true); + } + }); + return true; + } + + @Nullable + private static Pair findByAbsolutePath(String path) { + File file = new File(FileUtil.toSystemDependentName(path)); + if (file.exists()) { + VirtualFile vFile = findVirtualFile(file); + if (vFile != null) { + Project project = ProjectLocator.getInstance().guessProjectForFile(vFile); + if (project != null) { + return Pair.create(vFile, project); + } + } + } + + return null; + } + + @Nullable + private static Pair findByRelativePath(String path) { + Project[] projects = ProjectManager.getInstance().getOpenProjects(); + String localPath = FileUtil.toSystemDependentName(path); + + for (Project project : projects) { + File file = new File(project.getBasePath(), localPath); + if (file.exists()) { + VirtualFile vFile = findVirtualFile(file); + return vFile != null ? Pair.create(vFile, project) : null; + } + } + + for (Project project : projects) { + for (VcsRoot vcsRoot : ProjectLevelVcsManager.getInstance(project).getAllVcsRoots()) { + VirtualFile root = vcsRoot.getPath(); + if (root != null) { + File file = new File(FileUtil.toSystemDependentName(root.getPath()), localPath); + if (file.exists()) { + VirtualFile vFile = findVirtualFile(file); + return vFile != null ? Pair.create(vFile, project) : null; + } + } + } + } + + return null; + } + + @Nullable + private static VirtualFile findVirtualFile(final File file) { + return ApplicationManager.getApplication().runWriteAction(new Computable() { + @Override + public VirtualFile compute() { + return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); + } + }); + } +} diff --git a/platform/platform-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java b/platform/platform-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java deleted file mode 100644 index b5568725180e..000000000000 --- a/platform/platform-impl/src/com/intellij/ide/OpenFileXmlRpcHandler.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2000-2009 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.ide; - -import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.fileEditor.OpenFileDescriptor; -import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectLocator; -import com.intellij.openapi.util.Computable; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VirtualFile; - -/** - * @author mike - */ -@SuppressWarnings({"MethodMayBeStatic"}) -public class OpenFileXmlRpcHandler { - public boolean open(final String absolutePath) { - return openAndNavigate(absolutePath, -1, -1); - } - - public boolean openAndNavigate(final String absolutePath, final int line, final int column) { - final Application application = ApplicationManager.getApplication(); - - application.invokeLater(new Runnable() { - public void run() { - final String path = FileUtil.toSystemIndependentName(absolutePath); - final VirtualFile virtualFile = application.runWriteAction(new Computable() { - @Override - public VirtualFile compute() { - return LocalFileSystem.getInstance().refreshAndFindFileByPath(path); - } - }); - if (virtualFile == null) return; - - Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile); - if (project == null) return; - - FileEditorProviderManager editorProviderManager = FileEditorProviderManager.getInstance(); - if (editorProviderManager.getProviders(project, virtualFile).length == 0) return; - OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, line, column); - FileEditorManager.getInstance(project).openTextEditor(descriptor, true); - } - }); - - return true; - } -}