Smithy Syntax Highlighting and LSP Support on Helix
Intro
I will start this post by assuming you are familiar with Helix and Smithy IDL. I am a huge fan of Helix, and started using it for almost everything. At work, my team uses Smithy IDL, which Helix doesn't support out of the box. So I decided to write a small guide on how to add syntax highlighting and LSP support for Smithy.
Syntax Highlight
- Start by creating your language configuration file in your configuration directory, which is
~/.config/helix/
in Linux and MacOs, and%AppData%\helix\
in Windows:
touch ~/.config/helix/languages.toml
- Then add the following:
[[language]]
name = "smithy"
scope = "source.smithy"
auto-format = true
file-types = ["smithy"]
indent = { tab-width = 4, unit = " " } # you can customize this to your preference
roots = ["smithy-build.json"]
[[grammar]]
name = "smithy"
source = { git = "https://github.com/indoorvivants/tree-sitter-smithy" , rev = "084537ae85d186448c447de959a4955c0b855d2b"}
- This config will tell Helix to pull the tree-sitter grammar from the github repo, which I think is meant for NeoVim, but we can use it for Helix as well. Once you save the file, you can download and build the grammar with the following command:
hx -g fetch && hx -g build
- Once it finishes, you still need to download the highlight queries file, that for some reason Helix doesn't download for you.
curl https://raw.githubusercontent.com/indoorvivants/tree-sitter-smithy/main/queries/highlights.scm -o highlights.scm
- Now copy your query files to the
runtime
folder in your installation path, in my case I installed helix via Homebrew, so is just:
mkdir -p /opt/homebrew/Cellar/helix/22.12/libexec/runtime/queries/smithy #make sure this directory exists
cp highlights.scm /opt/homebrew/Cellar/helix/22.12/libexec/runtime/queries/smithy/
As you can see, this is my path for version 22.12, next time I update Helix, I will have to do it again. But you can install helix manually instead, then commit the query and grammar files you just downloaded, and rebase after that for subsequent updates. You can also set the runtime folder location with the HELIX_RUNTIME environment variable before installing.
LSP Support
We are going to use the official Smithy LSP Server , and Coursier to run it. This is the same
approach that the VSCode Smithy Extension use, if you know a better way to run a Java server "ala npx
" shoot me an email.
- First let's install Coursier, (I'm assuming you have JDK installed):
curl -fLo coursier https://github.com/coursier/launchers/raw/master/coursier &&
chmod +x coursier &&
./coursier # you may move this binary to your $PATH
If you are on MacOS then you can install it via Homebrew:
brew install coursier/formulas/coursier
- Use coursier to resolve all the dependencies of the server:
# The current version of the server is `0.2.2`
coursier resolve --mode force software.amazon.smithy:smithy-language-server:0.2.2 -r m2local
- Now set the command for Helix to run the LSP server, in your
languages.toml
file, under the language section, add:
language-server = {command = "coursier", args = ["launch", "software.amazon.smithy:smithy-language-server:0.2.2", "-r", "m2local", "--", "0"] }
The final version of the config file will look like this:
[[language]]
name = "smithy"
scope = "source.smithy"
auto-format = true
file-types = ["smithy"]
indent = { tab-width = 4, unit = " " } # you can customize this to your preference
roots = ["smithy-build.json"]
language-server = { command = "coursier", args = ["launch", "software.amazon.smithy:smithy-language-server:0.2.2", "-r", "m2local", "--", "0"] }
[[grammar]]
name = "smithy"
source = { git = "https://github.com/indoorvivants/tree-sitter-smithy" , rev = "084537ae85d186448c447de959a4955c0b855d2b"}
Exit Helix and now when you open a *.smithy
file, you should see the workspace diagnostic provided by the LSP server, and syntax highlight support.
Final Notes
There is a couple of things missing that I will eventually fix:
- LSP code actions. I wasn't able to get it working on Helix. Will investigate further.
- Open a PR to the Helix project for Smithy grammar/queries support.