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 定义

LuaLS 可以借助 LuaCATS(Lua 注释和类型系统)定义提供增强的 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" }
}

内联提示

要在 Zed 中为 LuaLS 启用内联提示

  1. 将以下内容添加到您的 Zed settings.json
  "languages": {
    "Lua": {
      "inlay_hints": {
        "enabled": true,
        "show_type_hints": true,
        "show_parameter_hints": true,
        "show_other_hints": true
      }
    }
  }
  1. "hint.enable": true 添加到您的 .luarc.json

格式化

LuaLS 格式化

要启用 LuaLS 自动格式化(由 CppCXY/EmmyLuaCodeStyle 提供),请确保您的 .luarc.json 中包含 "format.enable": true,

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

然后将以下内容添加到您的 Zed settings.json

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

您可以通过 .editorconfig 自定义各种 EmmyLuaCodeStyle 样式选项,有关所有可用选项,请参阅lua.template.editorconfig

StyLua 格式化

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

  1. 安装 StyLuabrew install styluacargo install stylua --features lua52,lua53,lua54,luau,luajit(您可以随意删除不需要的任何 Lua 版本)。
  2. 将以下内容添加到您的 settings.json
{
  "languages": {
    "Lua": {
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "stylua",
          "arguments": [
            "--syntax=Lua54",
            "--respect-ignores",
            "--stdin-filepath",
            "{buffer_path}",
            "-"
          ]
        }
      }
    }
  }
}

您可以通过上述命令行(如 --syntax=Lua54)或工作区中的 stylua.toml 向 StyLua 指定各种选项

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 选项