【问题标题】:Auto-run a command on saving in Sublime Text在 Sublime Text 中保存时自动运行命令
【发布时间】:2020-05-30 17:36:14
【问题描述】:

当我在 Sublime Text 中保存 .js.jsx 文件时,是否可以自动运行 eslint

我现在正在使用ESLint sublime package,但每次使用Cmd + Option + e 时我都必须手动运行它。

谢谢!

【问题讨论】:

    标签: sublimetext3 sublimetext sublime-text-plugin sublimelinter


    【解决方案1】:

    是的,这可以通过一个简单的事件驱动插件来完成,就像我在下面写的那样。

    插件已经过测试,但没有使用eslint 命令,因为我不想安装该软件包。显然,可以运行任何命令,而不是插件的 run_command("eslint") 行中的 eslint。如果所需的命令采用 args,则可以像这样指定它们:run_command("command", {"arg_1": val, "arg_2": val})

    on_post_save_async(self, view) 方法(在我下面的插件中)将在 view(即活动缓冲区)被保存后调用 - 请注意,这包括自动保存。 on_post_save_async() 在单独的线程中运行,不会阻塞应用程序。您可以更改插件以使用类似的方法,具体取决于您是否希望在文件保存发生之前或之后调用 eslint,以及该方法是否应该阻止应用程序或在其自己的非阻塞线程中运行。以下是 4 种选择:

    • on_pre_save(self, view):在保存视图之前调用。它会阻塞应用程序,直到方法返回。
    • on_pre_save_async(self, view):在保存视图之前调用。在单独的线程中运行,不会阻塞应用程序。
    • on_post_save(self, view):保存视图后调用。它会阻塞应用程序,直到方法返回。
    • on_post_save_async(self, view):在保存视图后调用。在单独的线程中运行,并且不会阻塞应用程序。 [目前在下面的插件中使用。]
    • Sublime Text EventListener 文档 is located here - 也有加载方法。

    将下面的插件保存在您的 Sublime Text 包层次结构中的某个位置,并带有 .py 扩展名。例如~/.config/sublime-text-3/Packages/User/AutoRunESLintOnSave.py 应该可以立即使用。

    import sublime, sublime_plugin
    
    class AutoRunESLintOnSave(sublime_plugin.EventListener):
        """ A class to listen for events triggered by ST. """
    
        def on_post_save_async(self, view):
            """
            This is called after a view has been saved. It runs in a separate thread
            and does not block the application.
            """
    
            file_path = view.file_name()
            if not file_path:
                return
            NOT_FOUND = -1
            pos_dot = file_path.rfind(".")
            if pos_dot == NOT_FOUND:
                return
            file_extension = file_path[pos_dot:]
            if file_extension.lower() in [".js", ".jsx"]:
                view.window().run_command("eslint")
    
                # Slight variations are needed for an ApplicationCommand,
                # a WindowCommand, or a TextCommand.
                #
                # view.run_command("text_command")
                # view.window().run_command("window_command")
                # sublime.run_command("application_command")
                #
                # Need args? Use this:
                #
                # view.run_command("command", {"arg_1": val, "arg_2": val})
    

    您可以使用缓冲区的语法,而不是使用文件扩展名来触发运行eslint 命令,代码更加简洁。

        def on_post_save_async(self, view):
            """ Syntax version. """
    
            current_syntax = view.settings().get("syntax")
            if ("JavaScript.sublime-syntax" in current_syntax
                    or "JSX.sublime-syntax" in current_syntax):
                view.window().run_command("eslint")
    
            # You could, of course, use an exact match:
            #
            # current_syntax = view.settings().get("syntax")
            # if current_syntax == "Packages/JavaScript/JavaScript.sublime-syntax":
            #     view.window().run_command("eslint")
            #
            # Run `view.settings().get("syntax")` in the console for the active syntax path.
    

    【讨论】:

    • 谢谢,在view 之后添加.window() 后就可以使用了。
    • 我已经更新了我的答案,以显示 run_command("command") 被称为取决于 commandApplicationCommandWindowCommand 还是 TextCommand 的方式的变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多