I somewhat recently started moving from VSCode to Neovim. I was surprised when it didn't have syntax highlighting or some autocomplete out of the box. VSCode had some of this built in or would prompt for extension installation fairly often so I didn't consider what was involved much.
I thought I noticed a strange behavior in snowflake's terraform provider and I used that as the impetus to upping my neovim game by adding in language server functionality The following is what I learned and how I set that up.
LSP Components
A client (usually an IDE or editor) that sends requests to a language server when files are opened, closed, or modified or when other actions occur. The language server responds to these requests providing features such as:
- Autocomplete
- Syntax highlighting
- Refactoring tools
- Many more
Setup
The following assumes neovim, nvim-lspconfig, and terraform are already installed.
mkdir lsp-setup && cd lsp-setup
cat << EOF > main.tf
terraform {
required_providers {
snowflake {
source = "snowflakedb/snowflake"
}
}
}
EOF
terraform init
Running terraform init will display an error Unexpected "snowflake" block because line 3 should include an equals snowflake = {. Open the file and there's no syntax highlighting on the error. Let's make that happen.
- Install the official hashicorp terraform lsp.
- Open
main.tf. - Run
:LspStart terraformls. - Gaze upon the red squiggly underline on line 3.
]dwill make the cursor jump to the next issue.- <C-w>d will display the error details.
For one more cool feature try adding any resource and autocomplete should kick in. Note: until you fix the error and run terraform init "resource" will autocomplete but provider resources like "snowflake_database" will not.