且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建读取shebang的Sublime Text 3构建系统

更新时间:2022-04-26 01:19:03

Sublime 构建系统有一个名为 target 的选项,它指定了要调用以执行的 WindowCommand构建.默认情况下,这是内部 exec 命令.您可以创建自己的命令来检查文件中是否存在 shebang 并使用该解释器或其他一些默认解释器.

Sublime build systems have an option named target which specifies a WindowCommand that is to be invoked to perform the build. By default this is the internal exec command. You can create your own command that would examine the file for a shebang and use that interpreter or some default otherwise.

例如(警告:我不是非常精通 Python,所以这可能很丑陋):

For example (caveat: I'm not super proficient in Python so this is probably quite ugly):

import sublime, sublime_plugin

class ShebangerCommand(sublime_plugin.WindowCommand):
    def parseShebang (self, filename):
        with open(filename, 'r') as handle:
            shebang = handle.readline ().strip ().split (' ', 1)[0]
        if shebang.startswith ("#!"):
            return shebang[2:]
        return None

    def createExecDict(self, sourceDict):
        current_file = self.window.active_view ().file_name()
        args = dict (sourceDict)

        interpreter = args.pop ("interpreter_default", "python")
        exec_args = args.pop ("interpreter_args", ["-u"])
        shebang = self.parseShebang (current_file)

        args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter,
                                                   " ".join (exec_args),
                                                   current_file)

        return args

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

您可以将其保存在 Packages/User 中作为 python 文件(例如 shebanger.py).

You would save this in Packages/User as a python file (e.g. shebanger.py).

这将创建一个名为 shebanger 的新命令,该命令收集给定的参数,检查触发构建的窗口的当前活动视图中的文件,以查看第一行是否为 shebang,然后合成 exec 命令所需的参数并运行它.

This creates a new command named shebanger that collects the arguments it's been given, examines the file in the currently active view of the window the build is triggered in to see if the first line is a shebang, and then synthesizes the arguments needed for the exec command and runs it.

由于默认的 python 构建系统假定它正在构建当前文件并将 -u 作为参数传递,这也是该命令复制的内容.但是请注意,此代码并非 100% 正确,因为将忽略 shebang 行中的任何参数,但您已大致了解.

Since the default python build system assumes it is building the current file and passing -u as an argument, that's what this command replicates as well. Note however that this code is not 100% correct because any arguments in the shebang line will be ignored, but you get the general idea.

在使用中,您将修改默认的 Python.sublime-build 文件,使其看起来像这样:

In use, you would modify the default Python.sublime-build file to look like this:

{
    // WindowCommand to execute for this build
    "target": "shebanger",

    // Use this when there is no shebang
    "interpreter_default": "python",

    // Args to pass to the interpreter
    "interpreter_args": ["-u"],

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

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

    "variants":
    [
        {
            "name": "Syntax Check",
            "interpreter_args": ["-m py_compile"],
        }
    ]
}

请注意,在变体中,我们覆盖了解释器参数;如果需要,您也可以覆盖那里的默认解释器.

Notice that in the variant we override what the interpreter arguments are; you could also override the default interpreter there as well if desired.