Sublime Text 中设置 Luacheck

260 阅读2分钟

SublimeLinter-luacheck

首先需要安装 SublimeLinter-luacheck 插件,但是由于它只是提供一个对 luacheck 的接口,我们还是手动设置一下。具体的方法是使用 SublimeLinter 提供的通用字段来设置。

但是对于更具体的 rule 规则设置,我们还是需要在项目根目录中通过 .luacheckrc 来进行配置。

或者通过命令行选项来忽略某些规则,比如:"args"``: ``"--ignore 631 621 614 611 111 612 "``,

如果不通过 .luacheckrc 来配置,也可以直接在 SublineLinter 的 linters 字段中的 filter_errors 如下配置:

{
    "filter_errors": "warning: ", // suppress all warnings
    "filter_errors": "no-trailing-spaces: ", // suppress a specific eslint rule
    "filter_errors": "W3\\d\\d: ",  // suppress some flake8/pyflakes rules,
    "filter_errors": "missing <!DOCTYPE> declaration" // typical html tidy message
}

luacheck 默认读取 .luacheckrc 配置文件,也可以使用 --config 来指定配置文件,使用 --no-config 来禁用配置。

  • On Windows: %LOCALAPPDATA%\Luacheck\.luacheckrc
  • On OS X/macOS: ~/Library/Application Support/Luacheck/.luacheckrc
  • On Other Systems: $XDG_CONFIG_HOME/luacheck/.luacheckrc or ~/.config/luacheck/.luacheckrc

Config is simply a Lua script executed by luacheck. It may set various options by assigning to globals or by returning a table with option names as keys.

定义 global

使用 --globals 添加自定义的 global,避免 warning。

"linters": {
    "luacheck": {
        "@disable": false,
        "args": "--ignore 631 621 614 611 612 111 113 --globals vim v",
        "excludes": [],
        "ignore" : "",
    },
}

.luacheckrc

globals = {"foo", "bar"}

LSP-lua 也自带 diagnostic/lint 功能,因此不安装 luacheck 也是可以的。

还可以安装 SublimeLinter-contrib-lua-globals 插件, SublimeLinter that finds globals in lua files.

更多的规则配置请查看:

luacheck.readthedocs.io/en/stable/c…

List of warnings

  • 111 Setting an undefined global variable.
  • 113 Accessing an undefined global variable.
  • 211 Unused local variable.
  • 611 A line consists of nothing but whitespace.
  • 612 A line contains trailing whitespace.
  • 614 Trailing whitespace in a comment.
  • 621 Inconsistent indentation (SPACE followed by TAB).
  • 631 Line is too long.

image.png

参考:

luacheck.readthedocs.io/en/stable/w… www.cnblogs.com/cheerupfory…