Lua
通过 Lua 扩展 提供 Lua 支持。
- Tree-sitter: tree-sitter-grammars/tree-sitter-lua
- 语言服务器: LuaLS/lua-language-server
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"
作为允许的值)。请注意,从 VSCode 导入设置选项时,请删除 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 启用 内嵌提示
- 将以下内容添加到您的 Zed settings.json
"languages": {
"Lua": {
"inlay_hints": {
"enabled": true,
"show_type_hints": true,
"show_parameter_hints": true,
"show_other_hints": true
}
}
}
- 将
"hint.enable": true
添加到您的.luarc.json
中。
格式化
LuaLS
要使用 LuaLS 启用自动格式化(由 CppCXY/EmmyLuaCodeStyle 提供),请确保您的 .luarc.json 中有 "format.enable": true,
并将以下内容添加到您的 Zed settings.json
中
{
"languages": {
"Lua": {
"format_on_save": "on",
"formatter": "language_server"
}
}
}
您可以通过 .editorconfig
自定义各种 EmmyLuaCodeStyle 样式选项,有关所有可用选项,请参阅 lua.template.editorconfig。
StyLua
或者,使用 StyLua 进行自动格式化
- 安装 StyLua:
brew install stylua
或cargo install stylua --features lua52,lua53,lua54,luau,luajit
(您可以随意删除任何不需要的 Lua 版本)。 - 将以下内容添加到您的
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 选项。