Skip to content

Commit 320739f

Browse files
feat: Stylua LSP server (#970)
* feat: Stylua LSP server * Reorganise, handle range formatting * Add integration tests * Update help text * Update changelog * Gate LSP behind feature (enabled by default) * Update readme * Fix flakiness of integration tests --------- Co-authored-by: JohnnyMorganz <[email protected]>
1 parent 5c571e3 commit 320739f

File tree

7 files changed

+626
-16
lines changed

7 files changed

+626
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Added option `block_newline_gaps` to determine whether newline gaps at the start / end of blocks should be preserved. Defaults to `Never`, which is the original behaviour. ([#857](https://github.com/JohnnyMorganz/StyLua/pull/857))
13+
- StyLua can now run in a language server mode. Start StyLua with `stylua --lsp` and connect with a language client. ([#936](https://github.com/JohnnyMorganz/StyLua/issues/936))
1314

1415
### Changed
1516

Cargo.lock

Lines changed: 73 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ path = "src/cli/main.rs"
2323
bench = false
2424

2525
[features]
26-
default = ["editorconfig", "wasm-bindgen"]
26+
default = ["editorconfig", "wasm-bindgen", "lsp"]
2727
serialize = []
2828
fromstr = ["strum"]
2929
luau = ["full_moon/roblox"]
@@ -33,6 +33,7 @@ lua54 = ["lua53", "full_moon/lua54"]
3333
luajit = ["full_moon/luajit"]
3434
cfxlua = ["lua54", "full_moon/cfxlua"]
3535
editorconfig = ["ec4rs"]
36+
lsp = ["lsp-server", "lsp-types", "lsp-textdocument"]
3637

3738
[dependencies]
3839
anyhow = "1.0.75"
@@ -56,6 +57,9 @@ strum = { version = "0.25.0", features = ["derive"], optional = true }
5657
thiserror = "1.0.49"
5758
threadpool = "1.8.1"
5859
toml = "0.8.1"
60+
lsp-server = { version = "0.7", optional = true }
61+
lsp-types = { version = "0.97", optional = true }
62+
lsp-textdocument = { version = "0.4.2", optional = true }
5963

6064
[target.'cfg(target_arch = "wasm32")'.dependencies]
6165
wasm-bindgen = { version = "0.2.81", optional = true }

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ Requires sorting is off by default. To enable it, add the following to your `sty
238238
enabled = true
239239
```
240240

241+
### Language Server Mode
242+
243+
StyLua can run as a language server, connecting with language clients that follow the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/).
244+
It will then respond to `textDocument/formatting` and `textDocument/rangeFormatting` requests.
245+
Formatting is only performed on files with a `lua` or `luau` language ID.
246+
247+
You can start the language server by running:
248+
249+
```sh
250+
stylua --lsp
251+
```
252+
253+
StyLua will listen to LSP messages on stdin and respond on stdout.
254+
241255
## Configuration
242256

243257
StyLua has opinionated defaults, but also provides a few options that can be set per project.
@@ -293,7 +307,7 @@ StyLua only offers the following options:
293307
| `quote_style` | `AutoPreferDouble` | Quote style for string literals. Possible options: `AutoPreferDouble`, `AutoPreferSingle`, `ForceDouble`, `ForceSingle`. `AutoPrefer` styles will prefer the specified quote style, but fall back to the alternative if it has fewer string escapes. `Force` styles always use the specified style regardless of escapes. |
294308
| `call_parentheses` | `Always` | Whether parentheses should be applied on function calls with a single string/table argument. Possible options: `Always`, `NoSingleString`, `NoSingleTable`, `None`, `Input`. `Always` applies parentheses in all cases. `NoSingleString` omits parentheses on calls with a single string argument. Similarly, `NoSingleTable` omits parentheses on calls with a single table argument. `None` omits parentheses in both cases. Note: parentheses are still kept in situations where removal can lead to obscurity (e.g. `foo "bar".setup -> foo("bar").setup`, since the index is on the call result, not the string). `Input` removes all automation and preserves parentheses only if they were present in input code: consistency is not enforced. |
295309
| `space_after_function_names` | `Never` | Specify whether to add a space between the function name and parentheses. Possible options: `Never`, `Definitions`, `Calls`, or `Always` |
296-
| `block_newline_gaps` | `Never` | Specify whether to preserve leading and trailing newline gaps for blocks. Possible options: `Never`, `Preserve` |
310+
| `block_newline_gaps` | `Never` | Specify whether to preserve leading and trailing newline gaps for blocks. Possible options: `Never`, `Preserve` |
297311
| `collapse_simple_statement` | `Never` | Specify whether to collapse simple statements. Possible options: `Never`, `FunctionOnly`, `ConditionalOnly`, or `Always` |
298312

299313
Default `stylua.toml`, note you do not need to explicitly specify each option if you want to use the defaults:

0 commit comments

Comments
 (0)