Experimental Lua LSP Extension

I was curious about the official LSP API and what the experience of creating a language API using was like, so I spent an afternoon last week throwing together an experimental plugin for Lua using the LSP API.

Using the LSP API, we’re able to get the following from the language server:

  • Syntax highlighting
  • Code folding
  • Go-to reference across files in the project
  • Hover documentation (including support for localization)
  • Code formatting

The references are kind of weird because there’s no visual indication (highlight/underline/cursor change) that they’re available.

Syntax highlighting doesn’t seem to work all that great either with the default settings or customized. It just doesn’t want to highlight most tokens. I don’t know if that’s an LSP API thing or a LuaLS issue.

Unrelated to LSP, adding support for basic run configurations and “execute the current file” were pretty straightforward to implement.

To support user preferences and environment customization, I added a settings screen for choosing the lua and lua-language-server paths; choosing a supported language for the LSP (defaults to IDE language when possible); and also supporting configuration via .luarc or .luarc.json (including JSON Schema validation).

Given I’m not sure there’s a need in the marketplace for this specific plugin, this probably where I’ll leave things. Setting aside the syntax highlighting issue I didn’t figure out, the whole process was pretty simple. The code has been uploaded to github for future reference (GitHub - DavidSeptimus/luals-intellij-plugin: An experimental LuaLS integration for JetBrains IDEs).

3 Likes

Okay, my curiosity got the better of me and I took another look into the syntax highlighting issues and found out the following:

1. The order of the tokenTypes list supplied by an instance of LspSemanticTokensSupport must match the order of the tokens expected by the language server.
2. I needed to enable semantic keyword highlighting in my project’s .luarc.json file since that’s disabled by default.

After fixing those two issues, syntax highlighting works as expected!

Regarding the first point.
The server should return a legend with the order of the tokens used by it in the response for initialization. I’d expect IntelliJ to use this info to arrange the tokens correctly. Sad, if it doesn’t

Turns out I was mistaken about that point. Order doesn’t matter. It was just the configuration. Turns out I shouldn’t have bothered overriding tokenTypes at all since the default is fine for Lua.

1 Like