1# Lua
2
3Lua support is available through the [Lua extension](https://github.com/zed-industries/zed/tree/main/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.defaultConfig": {
22 "indent_style": "space",
23 "indent_size": "4"
24 },
25 // Location(s) of any LuaCATS / EmmyLua annotation stubs
26 "workspace.library": [
27 // "path/to/library/directory"
28 ]
29}
30```
31
32## Formatting
33
34Zed can enable auto-formatting of code with formatters like [StyLua](https://github.com/JohnnyMorganz/StyLua).
35
361. 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).
372. Add the following to your `settings.json`:
38
39```json
40{
41 "languages": {
42 "Lua": {
43 "format_on_save": "on",
44 "formatter": {
45 "external": {
46 "command": "stylua",
47 "arguments": ["--syntax=Lua54", "-"]
48 }
49 }
50 }
51 }
52}
53```
54
55You can specify various options to StyLua either on the command line above (like `--syntax=Lua54`) or in a `stylua.toml` in your workspace:
56
57```toml
58syntax = "Lua54"
59column_width = 100
60line_endings = "Unix"
61indent_type = "Spaces"
62indent_width = 4
63quote_style = "AutoPreferDouble"
64call_parentheses = "Always"
65collapse_simple_statement = "All"
66
67[sort_requires]
68enabled = true
69```
70
71For a complete list of available options, see: [StyLua Options](https://github.com/JohnnyMorganz/StyLua?tab=readme-ov-file#options).