让 Sublime 在两个相似的构建系统中进行选择 [英] Let Sublime choose among two similar build systems

查看:27
本文介绍了让 Sublime 在两个相似的构建系统中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的设置是工具>构建系统>自动.

我使用两个构建系统:Python.sublime-buildPython64.sublime-build.

I use two build systems: Python.sublime-build and Python64.sublime-build.

当且仅当 .py 文件以 #python64 开头时,如何使 Sublime 使用后者?

How to make that Sublime uses the latter if and only if the .py file begins with #python64?

(这样我就不必在 Sublime's Tools > Build System > Python 64Sublime's Tools > Build System > Python 之间手动切换).

(so that I don't have to manually switch between Sublime's Tools > Build System > Python 64 and Sublime's Tools > Build System > Python).

或者,如何做到这一点:

  • CTRL+B 使用 Python(32 位)

  • CTRL+B uses Python (32 bit)

CTRL+SHIFT+B 使用 Python(64 位)

CTRL+SHIFT+B uses Python (64 bit)

(它们都应该在 Sublime 的底部构建输出面板中显示输出).

(both of them should display the output in Sublime's bottom build output panel).

# Python.sublime-build
{
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

 

# Python64.sublime-build
{
    "cmd": ["c:\\python27-64\\python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)"
}

PS:我使用 Windows,我需要 Python 的两个版本(32 和 64),原因不在此讨论.

PS: I use Windows, and I need both versions of Python (32 and 64), for reasons that would be out of topic here.

推荐答案

通常,Sublime 会根据您正在编辑的文件类型(例如 Python 源文件)自动选择合适的构建系统.在 Sublime Text 3 中,还可以根据某个文件(例如名为 Makefile 的文件)的存在来激活构建系统.

In general, Sublime automatically selects the appropriate build system based on the type of the file that you are editing (e.g. a python source file). In Sublime Text 3, it is also possible to make a build system activate based on the presence of a certain file (e.g. a file called Makefile).

对于此处介绍的用例,这些都不是可行的解决方案,第二个仅在 Sublime Text 3 中可用,在 Sublime Text 2 中不可用.

Neither of these is a viable solution to the use case presented here, and the second is only available in Sublime Text 3 and not Sublime Text 2.

有几种方法可以实现这一点.我在这里提供了两组说明,一组用于 Sublime Text 2,一组用于 Sublime Text 3,因此这是一个更广泛有用的答案.

There are a couple of ways to accomplish this. I'm providing two sets of instructions here, one for Sublime Text 2 and one for Sublime Text 3, so that this is a more broadly useful answer.

构建系统可以有一个名为 target 的可选参数,它指定 sublime 应该执行以执行构建的命令.未指定时,默认为 exec 命令.构建文件的大部分内容实际上只是直接传递给 exec 命令本身的参数.

A build system can have an optional argument named target which specifies the command that sublime should execute in order to perform the build. When this is not specified, the default is the exec command. Most of the contents of the build file are actually just arguments that are directly passed to the exec command itself.

通过指定自定义目标,您可以向构建命令添加额外的逻辑,以便能够分析当前文件并采取适当的行动.

By specifying a custom target you can add extra logic to the build command so that has the power to analyze the current file and act appropriately.

这的第一部分是提供将用于执行构建的自定义命令,这可以通过一些简单的插件代码来完成.这应该作为 python 文件保存在您的 User 包中(例如 Packages\User\python_build.py).

The first part of this is to provide the custom command which will be used to perform the build, which can be done with some simple plugin code. This should be saved in your User package as a python file (e.g. Packages\User\python_build.py).

第二部分是修改您正在使用的构建系统,以便利用新命令执行我们希望它执行的操作.单个构建文件将在两种方式下使用.您可以将此命名为 Python.sublime-build 并启用它作为对 Packages\Python\Python.sublime-build 或您的用户包中现有构建的覆盖Packages\User\Python.sublime-build.

The second part is to modify the build system that you're using in order to take advantage of the new command to do what we want it to do. The single build file will be used both ways. You would name this Python.sublime-build and enable it either as an override to the existing build in Packages\Python\Python.sublime-build or in your User package as Packages\User\Python.sublime-build.

import sublime, sublime_plugin

class PythonBuildCommand(sublime_plugin.WindowCommand):
    def detect_version(self, filename, python32, python64):
        with open(filename, 'r') as handle:
            line = handle.readline ()
        return python64 if (line.startswith ("#") and "64" in line) else python32

    def execArgs(self, sourceArgs):
        current_file = self.window.active_view ().file_name ()
        args = dict (sourceArgs)

        python32 = args.pop ("python32", "python")
        python64 = args.pop ("python64", "python")
        selected = self.detect_version (current_file, python32, python64)

        if "cmd" in args:
            args["cmd"][0] = selected

        return args

    def run(self, **kwargs):
        self.window.run_command ("exec", self.execArgs (kwargs))

Sublime Text 2 构建文件:

{
    "target": "python_build",

    "python32": "python",
    "python64": "c:/python27-64/python",

    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Sublime Text 3 插件:

import sublime, sublime_plugin

class PythonBuildCommand(sublime_plugin.WindowCommand):
    def detect_version(self, filename, python32, python64):
        with open(filename, 'r') as handle:
            line = handle.readline ()
        return python64 if (line.startswith ("#") and "64" in line) else python32

    def execArgs(self, sourceArgs):
        current_file = self.window.active_view ().file_name ()
        args = dict (sourceArgs)

        python32 = args.pop ("python32", "python")
        python64 = args.pop ("python64", "python")
        selected = self.detect_version (current_file, python32, python64)

        if "shell_cmd" in args:
            args["shell_cmd"] = args["shell_cmd"].replace ("python", selected)

        return args

    def run(self, **kwargs):
        self.window.run_command ("exec", self.execArgs (kwargs))

Sublime Text 3 构建文件:

{
    "target": "python_build",

    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "python32": "python",
    "python64": "c:/python27-64/python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}


请注意,插件代码在两个版本的代码中基本相同.Sublime Text 3 支持 shell_cmd 以及 cmd 来指定可执行文件,每个版本中 Python 的默认构建系统都反映了这一点.如果需要,Sublime Text 2 版本也应该在 Sublime Text 3 中工作,只要你使用适当的构建文件.


Notice that the plugin code is mostly the same in both versions of the code. Sublime Text 3 supports shell_cmd as well as cmd for specifying the executable, and the default build systems for Python in each version reflect that. If desired, the Sublime Text 2 version should also work in Sublime Text 3, as long as you use the appropriate build file as well.

无论哪种情况,自定义命令都会检查文件的第一行,看看它应该执行两个版本的python中的哪一个,在构建系统中适当修改命令,然后调用exec 命令来执行构建.

In either case, the custom command will check the first line of the file to see which of the two versions of python that it should execute, modify the command in the build system as appropriate, and then invoke the exec command to perform the build.

构建文件本身需要指定在任何一种情况下使用哪个版本的 python 解释器,如果不是,则回退(由插件中的代码确定)为 python指定.

The build file itself needs to specify which version of the python interpreter to use in either case, with the fallback (as determined by the code in the plugin) being python for both if it is not specified.

如果您使用 Sublime Text 3 并将构建文件放在您的 User 包中,您的构建菜单将包含两次 Python 选项;一次用于内置版本,一次用于您自己的版本.在这种情况下,您可能需要确保选择正确的.

If you are using Sublime Text 3 and place the build file in your User package, your build menu will contain the Python option twice; once for the built in version and once for your own. In this case you may need to ensure that the proper one is selected.

在任一版本的 Sublime 中都没有可以运行构建并指定要使用的构建系统的命令(至少不是我能找到的文档记录).这在两个版本中仍然可以使用键绑定,尽管在 Sublime Text 3 的情况下更容易一些.

There is no command in either version of Sublime that can run a build and also specify the build system to use (at least not a documented one that I can find). This is still possible with a key binding in both versions, although in the case of Sublime Text 3 is is a little easier.

对于 Sublime Text 2,命令 build 将使用当前选择的构建系统执行构建,并且 set_build_system 可用于交换构建系统.

For Sublime Text 2, the command build will perform a build using the currently selected build system and set_build_system can be used to swap the build system around.

为了通过一次按键完成此操作,您需要安装 ChainOfCommand 插件,它允许您将多个命令链接在一起.安装该软件包后,您可以设置以下键绑定:

In order to do this with a single key press, you need to install the ChainOfCommand plugin, which allows you to chain multiple commands together. With that package installed, you can set up the following key bindings:

{
    "keys": ["ctrl+b"],
    "command": "chain", "args": {"commands": [
        ["set_build_system", {"file": "Packages/Python/Python.sublime-build"}],
        ["build"]
    ]}
},
{
    "keys": ["ctrl+shift+b"],
    "command": "chain", "args": {"commands": [
        ["set_build_system", {"file": "Packages/Python/Python64.sublime-build"}],
        ["build"]
    ]}
}

第一个将构建系统更改为 python,然后运行构建,而第二个将其更改为 Python64.根据需要修改构建文件的路径(例如,如果您将一个或两个都存储在 User 包中).

The first of these changes the build system to be python and then runs the build, while the second one changes it to be Python64. Modify the paths to the build files as appropriate (e.g. if you stored one or both in your User package instead).

这有点不理想,因为它使 Ctrl+B 键总是尝试构建 python,即使这不合适.

This is a bit sub-optimal because it makes the Ctrl+B key always try to build python even if that is not appropriate.

我不主动使用 Sublime Text 2,所以我不确定您将如何使这些绑定仅针对 Python 文件.我尝试过的一些在 Sublime Text 2 中有效的方法在这里不起作用.

I don't actively use Sublime Text 2 so I'm unsure of how you would go about making these bindings specific only to a python file. The few things I tried that would work in Sublime Text 2 did not work here.

对于 Sublime Text 3,这更容易一些.此版本支持构建系统中的变体,并且可以告诉 build 命令执行当前所选构建的变体.

For Sublime Text 3, this is a little easier. This version supports variants in a build system, and the build command can be told to execute a variant of the currently selected build.

要使其正常工作,您需要一个类似于以下版本的构建系统.这是标准 Sublime Text 3 python 构建文件的修改版本,它删除了 Syntax Check 变体以支持 Python64 版本.这可以根据需要进行修改.

To get this working, you need a single build system that looks something like the following version. This is a modified version of the standard Sublime Text 3 python build file, which removes the Syntax Check variant in favor of a Python64 version. This could be modified as desired.

{
    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Python64",
            "shell_cmd": "c:/python27-64/python -u \"$file\"",
        }
    ]
}

有了这个,添加以下键绑定:

With this in place, add the following key binding:

{
    "keys": ["ctrl+shift+b"],
    "command": "build", "args": {"variant": "Python64"},
    "context": [
        { "key": "selector", "operator": "equal", "operand": "source.python"},
    ]
},

现在构建系统有一个变体,因此常规 Python 构建将使用 32 位版本,而 Python - Python64 将使用 64 位版本.

Now the build system has a variant, so that the regular Python build will use the 32 bit version and Python - Python64 will use the 64 bit version instead.

此处的键绑定设置为仅在 python 文件中操作,因为在 Sublime Text 3 中,此键序列用于提示您输入要使用的当前构建的变体.

The key binding here is set to operate only in a python file, since in Sublime Text 3 this key sequence is used to prompt you for the variant of the current build to use.

对于一些初始设置,一旦启用此键绑定,您应该打开一个 python 文件并选择 Tools >构建 >Build With... 从菜单中选择 Python 以告诉 Sublime 你想使用 Python 构建.

For some initial setup, once you enable this key binding you should open up a python file and select Tools > Build > Build With... from the menu, then select Python in order to tell Sublime that you want to use the Python build.

从现在开始,当您编辑 python 文件时,Ctrl+B 将执行主构建,即 32 位 python,Ctrl+Shift+B 将执行使用 64 位版本的变体.

From this point forward, while you're editing a python file, Ctrl+B will execute the main build, which is the 32-bit python, and Ctrl+Shift+B will execute the variant that uses the 64-bit version.

这篇关于让 Sublime 在两个相似的构建系统中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆