Auto-运行 一个在 Sublime Text 中保存的命令
Auto-run a command on saving in Sublime Text
当我在 Sublime Text 中保存 .js
或 .jsx
文件时,是否可以自动 运行 eslint
?
我现在正在使用 ESLint sublime package,但每次使用 Cmd + Option + e
时我都必须手动 运行。
谢谢!
是的,这可以通过一个简单的事件驱动插件来完成,就像我在下面写的那样。
该插件已经过测试,但未使用 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()
运行s 在单独的线程中并且不会阻塞应用程序。您可以更改插件以使用类似的方法,具体取决于您是希望在文件保存之前还是之后调用 eslint
以及该方法是应该阻止应用程序还是 运行 在它自己的非阻塞线程。这些是 4 个备选方案:
on_pre_save(self, view)
:在保存视图之前调用。它会阻止应用程序,直到方法 returns.
on_pre_save_async(self, view)
:在保存视图之前调用。在单独的线程中运行,并且不会阻塞应用程序。
on_post_save(self, view)
:保存视图后调用。它会阻止应用程序,直到方法 returns.
on_post_save_async(self, view)
:保存视图后调用。在单独的线程中运行,并且不会阻塞应用程序。 [目前在下面的插件中使用。]
- Sublime Text
EventListener
文档 is located here - 也有加载方法。
使用 .py
扩展名将插件保存在 Sublime Text 包层次结构中的某个位置。例如~/.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.
当我在 Sublime Text 中保存 .js
或 .jsx
文件时,是否可以自动 运行 eslint
?
我现在正在使用 ESLint sublime package,但每次使用 Cmd + Option + e
时我都必须手动 运行。
谢谢!
是的,这可以通过一个简单的事件驱动插件来完成,就像我在下面写的那样。
该插件已经过测试,但未使用 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()
运行s 在单独的线程中并且不会阻塞应用程序。您可以更改插件以使用类似的方法,具体取决于您是希望在文件保存之前还是之后调用 eslint
以及该方法是应该阻止应用程序还是 运行 在它自己的非阻塞线程。这些是 4 个备选方案:
on_pre_save(self, view)
:在保存视图之前调用。它会阻止应用程序,直到方法 returns.on_pre_save_async(self, view)
:在保存视图之前调用。在单独的线程中运行,并且不会阻塞应用程序。on_post_save(self, view)
:保存视图后调用。它会阻止应用程序,直到方法 returns.on_post_save_async(self, view)
:保存视图后调用。在单独的线程中运行,并且不会阻塞应用程序。 [目前在下面的插件中使用。]- Sublime Text
EventListener
文档 is located here - 也有加载方法。
使用 .py
扩展名将插件保存在 Sublime Text 包层次结构中的某个位置。例如~/.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.