From 39bdf7e68f929fa359d1ca3df500d4c84b04bf18 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 31 Oct 2013 18:42:11 +0100 Subject: [PATCH] external tool macro for directory containing Python interpreter (PY-6533) --- .../application/options/PathMacrosImpl.java | 3 +- python/src/META-INF/python-core.xml | 3 +- .../python/sdk/InterpreterDirectoryMacro.java | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 python/src/com/jetbrains/python/sdk/InterpreterDirectoryMacro.java diff --git a/platform/projectModel-impl/src/com/intellij/application/options/PathMacrosImpl.java b/platform/projectModel-impl/src/com/intellij/application/options/PathMacrosImpl.java index 715834f27595..493b21d85306 100644 --- a/platform/projectModel-impl/src/com/intellij/application/options/PathMacrosImpl.java +++ b/platform/projectModel-impl/src/com/intellij/application/options/PathMacrosImpl.java @@ -129,7 +129,8 @@ public class PathMacrosImpl extends PathMacros implements ApplicationComponent, "SelectionStartLine", "SelectionEndLine", "SelectionStartColumn", - "SelectionEndColumn" + "SelectionEndColumn", + "PyInterpreterDirectory" ); public PathMacrosImpl() { diff --git a/python/src/META-INF/python-core.xml b/python/src/META-INF/python-core.xml index a426b2433c60..35255369d2ed 100644 --- a/python/src/META-INF/python-core.xml +++ b/python/src/META-INF/python-core.xml @@ -473,6 +473,8 @@ + + @@ -504,7 +506,6 @@ - diff --git a/python/src/com/jetbrains/python/sdk/InterpreterDirectoryMacro.java b/python/src/com/jetbrains/python/sdk/InterpreterDirectoryMacro.java new file mode 100644 index 000000000000..a601aeb8dc6a --- /dev/null +++ b/python/src/com/jetbrains/python/sdk/InterpreterDirectoryMacro.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2013 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.jetbrains.python.sdk; + +import com.intellij.ide.macro.Macro; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.PathUtil; +import org.jetbrains.annotations.Nullable; + +/** + * @author yole + */ +public class InterpreterDirectoryMacro extends Macro { + @Override + public String getName() { + return "PyInterpreterDirectory"; + } + + @Override + public String getDescription() { + return "The directory containing the Python interpreter selected for the project"; + } + + @Nullable + @Override + public String expand(DataContext dataContext) throws ExecutionCancelledException { + Module module = LangDataKeys.MODULE.getData(dataContext); + if (module == null) { + Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (project == null) { + return null; + } + Module[] modules = ModuleManager.getInstance(project).getModules(); + if (modules.length == 0) { + return null; + } + module = modules[0]; + } + Sdk sdk = PythonSdkType.findPythonSdk(module); + if (sdk != null) { + VirtualFile homeDir = sdk.getHomeDirectory(); + if (homeDir == null) { + return null; + } + String path = PathUtil.getLocalPath(homeDir.getParent()); + if (path != null) { + return FileUtil.toSystemDependentName(path); + } + } + return null; + } +}