lua.md

 1# Lua
 2
 3Lua support is available through the [Lua extension](https://github.com/zed-extensions/lua).
 4
 5- Tree-sitter: [tree-sitter-grammars/tree-sitter-lua](https://github.com/tree-sitter-grammars/tree-sitter-lua)
 6- Language server: [LuaLS/lua-language-server](https://github.com/LuaLS/lua-language-server)
 7
 8## luarc.json
 9
10To configure LuaLS you can create a `.luarc.json` file in the root of your workspace.
11
12See [LuaLS Settings Documentation](https://luals.github.io/wiki/settings/) for all available configuration options.
13
14```json
15{
16  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
17  "runtime.version": "Lua 5.4",
18  "diagnostics.severity": {
19    "duplicate-set-field": "Hint"
20  },
21  "format.enable": true,
22  "format.defaultConfig": {
23    "indent_style": "space",
24    "indent_size": "4"
25  },
26  "workspace.library": ["../somedir/library"]
27}
28```
29
30## Formatting
31
32### LuaLS
33
34To enable auto-formatting with your LuaLS, make sure you have `"format.enable": true,` in your .luarc.json add the following to your Zed `settings.json`:
35
36```json
37{
38  "languages": {
39    "Lua": {
40      "format_on_save": "on",
41      "formatter": "language_server"
42    }
43  }
44}
45```
46
47### StyLua
48
49Alternative you can use [StyLua](https://github.com/JohnnyMorganz/StyLua):
50
511. Install [StyLua](https://github.com/JohnnyMorganz/StyLua): `brew install stylua` or `cargo install stylua --features lua52,lua53,lua54,luau,luajit` (feel free to remove any Lua versions you don't need).
522. Add the following to your `settings.json`:
53
54```json
55{
56  "languages": {
57    "Lua": {
58      "format_on_save": "on",
59      "formatter": {
60        "external": {
61          "command": "stylua",
62          "arguments": [
63            "--syntax=Lua54",
64            "--respect-ignores",
65            "--stdin-filepath",
66            "{buffer_path}",
67            "-"
68          ]
69        }
70      }
71    }
72  }
73}
74```
75
76You can specify various options to StyLua either on the command line above (like `--syntax=Lua54`) or in a `stylua.toml` in your workspace:
77
78```toml
79syntax = "Lua54"
80column_width = 100
81line_endings = "Unix"
82indent_type = "Spaces"
83indent_width = 4
84quote_style = "AutoPreferDouble"
85call_parentheses = "Always"
86collapse_simple_statement = "All"
87
88[sort_requires]
89enabled = true
90```
91
92For a complete list of available options, see: [StyLua Options](https://github.com/JohnnyMorganz/StyLua?tab=readme-ov-file#options).