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.enable": true,
22 "format.defaultConfig": {
23 "indent_style": "space",
24 "indent_size": "4"
25 },
26 "workspace.library": [
27 "../soemdir/library"
28 ]
29}
30```
31
32## Formatting
33
34### LuaLS
35
36To 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`:
37
38```json
39{
40 "languages": {
41 "Lua": {
42 "format_on_save": "on",
43 "formatter": "language_server"
44 }
45 }
46}
47```
48
49### StyLua
50
51Alternative you can use [StyLua](https://github.com/JohnnyMorganz/StyLua):
52
531. 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).
542. Add the following to your `settings.json`:
55
56```json
57{
58 "languages": {
59 "Lua": {
60 "format_on_save": "on",
61 "formatter": {
62 "external": {
63 "command": "stylua",
64 "arguments": ["--syntax=Lua54", "-"]
65 }
66 }
67 }
68 }
69}
70```
71
72You can specify various options to StyLua either on the command line above (like `--syntax=Lua54`) or in a `stylua.toml` in your workspace:
73
74```toml
75syntax = "Lua54"
76column_width = 100
77line_endings = "Unix"
78indent_type = "Spaces"
79indent_width = 4
80quote_style = "AutoPreferDouble"
81call_parentheses = "Always"
82collapse_simple_statement = "All"
83
84[sort_requires]
85enabled = true
86```
87
88For a complete list of available options, see: [StyLua Options](https://github.com/JohnnyMorganz/StyLua?tab=readme-ov-file#options).