入门 Zed Industries 2026-09-14 17:42:19 · 72 阅读

第88章 Zed 编辑器中的 Lua 支持

Lua 语言支持通过Lua 扩展提供。

luarc.json

要配置 LuaLS,你可以在项目根目录创建一个 .luarc.json 文件。

{
  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
  "runtime.version": "Lua 5.4",
  "format.enable": true,
  "workspace.library": ["../somedir/library"]
}

所有可用的配置选项详见LuaLS 设置文档。在 Zed 中编辑此文件时,可用设置项会自动补全(例如 runtime.version 会显示 "Lua 5.1"、"Lua 5.2"、"Lua 5.3"、"Lua 5.4" 和 "LuaJIT" 作为允许的值)。请注意,从 VS Code 导入设置选项时,需去除 Lua. 前缀(例如使用 runtime.version 而非 Lua.runtime.version)。

LuaCATS 定义

借助 LuaCATS(Lua Comment and Type System)定义,LuaLS 可提供更完善的 LSP 自动补全建议和类型验证。这些定义适用于许多常见的 Lua 库,并可通过 luarc.json 中的 workspace.library 指定包含它们的本地路径。如果你将定义检出到项目所在的父目录下(例如 ../playdate-luacats、../love2d 等),可以使用相对路径。或者,你可以为每个 LuaCATS 定义仓库在项目内创建子模块。

LÖVE (Love2D)

要在 Zed 中使用LÖVE (Love2D),请将LuaCATS/love2d 检出到项目父目录下名为 love2d-luacats 的文件夹中:

cd .. && git clone https://github.com/LuaCATS/love2d love2d-luacats

然后在你的 .luarc.json 中:

{
  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
  "runtime.version": "Lua 5.4",
  "workspace.library": ["../love2d-luacats"],
  "runtime.special": {
    "love.filesystem.load": "loadfile"
  }
}

PlaydateSDK

要在 Zed 中使用 Playdate Lua SDK,请把 playdate-luacats 克隆到项目的上一级目录:

cd .. && git clone https://github.com/notpeter/playdate-luacats

然后在你的 .luarc.json 中进行配置:

{
  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
  "runtime.version": "Lua 5.4",
  "runtime.nonstandardSymbol": [
    "+=",
    "-=",
    "*=",
    "/=",
    "//=",
    "%=",
    "<<=",
    ">>=",
    "&=",
    "|=",
    "^="
  ],
  "diagnostics.severity": { "duplicate-set-field": "Hint" },
  "diagnostics.globals": ["import"],
  "workspace.library": ["../playdate-luacats"],
  "format.defaultConfig": {
    "indent_style": "space",
    "indent_size": "4"
  },
  "format.enable": true,
  "runtime.builtin": { "io": "disable", "os": "disable", "package": "disable" }
}

Inlay Hints

要在 Zed 中为 LuaLS 启用 Inlay Hints:

  1. 在设置({#kb zed::OpenSettings})中的 Languages > Lua 下配置 inlay hints,或者直接在设置文件中添加:
{
  "languages": {
    "Lua": {
      "inlay_hints": {
        "enabled": true,
        "show_type_hints": true,
        "show_parameter_hints": true,
        "show_other_hints": true
      }
    }
  }
}
  1. 在你的 .luarc.json 中添加 "hint.enable": true。

格式化

LuaLS 格式化

若要通过 LuaLS 启用自动格式化(由 CppCXY/EmmyLuaCodeStyle 提供),请确保你的 .luarc.json 中设置了 "format.enable": true,:

{
  "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
  "format.enable": true
}

在 设置({#kb zed::OpenSettings})中的 Languages > Lua 下配置格式化,或在你的设置文件中添加:

{
  "languages": {
    "Lua": {
      "format_on_save": "on",
      "formatter": "language_server"
    }
  }
}

你可以通过 .editorconfig 自定义各种 EmmyLuaCodeStyle 样式选项,参见 lua.template.editorconfig 查看所有可用选项。

StyLua 格式化

或者,使用 StyLua 进行自动格式化:

  1. 安装 StyLua:brew install stylua 或 cargo install stylua --features lua52,lua53,lua54,luau,luajit(你可以自由移除不需要的 Lua 版本)。
  2. 在 设置({#kb zed::OpenSettings})中的 Languages > Lua 下配置格式化,或在你的设置文件中添加:
{
  "languages": {
    "Lua": {
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "stylua",
          "arguments": [
            "--syntax=Lua54",
            "--respect-ignores",
            "--stdin-filepath",
            "{buffer_path}",
            "-"
          ]
        }
      }
    }
  }
}

你可以在上面的命令行中为 StyLua 指定各种选项(如 --syntax=Lua54),也可以在项目中的 stylua.toml 文件里指定:

syntax = "Lua54"
column_width = 100
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "All"

[sort_requires]
enabled = true

完整的可用选项列表参见:StyLua 选项。

评论 (0)