问题描述
当我在 Sublime Text 中保存 .js
或 .jsx
文件时,是否可以自动运行 eslint
?
Is it possible to run eslint
automatically when I save either a .js
or .jsx
file in Sublime Text?
我现在正在使用 ESLint sublime 包,但我每次都必须手动运行它使用 Cmd + Option + e
.
I'm using the ESLint sublime package right now but I have to manually run it each time using Cmd + Option + e
.
谢谢!
推荐答案
是的,这可以通过一个简单的事件驱动插件来完成,就像我在下面写的那样.
Yes this can be done with a simple event driven plugin like the one I've written below.
插件已经过测试,但没有使用 eslint
命令,因为我不想安装那个包.显然,可以在插件的 run_command("eslint")
行中运行任何命令而不是 eslint
.如果所需的命令采用 args,则可以像这样指定它们:run_command("command", {"arg_1": val, "arg_2": val})
.
The plugin has been tested but not with the eslint
command since I did not want to install that package. Clearly any command could be run instead of eslint
in the plugin's run_command("eslint")
line. If the desired command takes args then they can be specified like this: run_command("command", {"arg_1": val, "arg_2": val})
.
on_post_save_async(self, view)
方法(在我下面的插件中)将在 view
(即活动缓冲区)被保存后被调用 - 注意这个包括自动保存.on_post_save_async()
在单独的线程中运行,不会阻塞应用程序.您可以更改插件以使用类似的方法,具体取决于您是希望在文件保存之前还是之后调用 eslint
以及该方法是应阻止应用程序还是以其自己的非阻塞方式运行线.这些是 4 个替代方案:
The on_post_save_async(self, view)
method (in my plugin below) will be called after the view
, i.e. the active buffer, has been saved - note that this includes auto-saves. on_post_save_async()
runs in a separate thread and does not block the application. You could alter the plugin to use a similar method depending of whether you want eslint
called before or after the file save has taken place and whether the method should block the application or be run in its own non-blocking thread. These are the 4 alternatives:
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
文档 是位于此处 - 也有加载方法.
on_pre_save(self, view)
: Called just before a view is saved. It blocks the application until the method returns.on_pre_save_async(self, view)
: Called just before a view is saved. Runs in a separate thread, and does not block the application.on_post_save(self, view)
: Called after a view has been saved. It blocks the application until the method returns.on_post_save_async(self, view)
: Called after a view has been saved. Runs in a separate thread, and does not block the application. [Currently used in the plugin below.]- The Sublime Text
EventListener
documentation is located here - there are on load methods too.
使用 .py
扩展名将插件保存在 Sublime Text 包层次结构中的某个位置.例如~/.config/sublime-text-3/Packages/User/AutoRunESLintOnSave.py
它应该可以立即工作.
Save the plugin below somewhere in in your Sublime Text packages hierarchy with a .py
extension. e.g. ~/.config/sublime-text-3/Packages/User/AutoRunESLintOnSave.py
and it should work straight away.
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
命令,代码更加简洁.
Instead of using file extensions to trigger running the eslint
command you could use the buffer's syntax, the code is even more concise.
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 中保存时自动运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!