feat(nvim): add terraform LSP and tooling support#5
Open
maybebyte wants to merge 2 commits into
Open
Conversation
Bring .tf and .tfvars files to feature parity with other supported
languages: terraform-ls LSP attaches automatically, conform runs
`terraform fmt` on save, nvim-lint runs tflint, and treesitter
highlights both filetypes (the .tfvars buffer reuses the terraform
parser via vim.treesitter.language.register).
Two subtleties caught during verification:
* Neovim's builtin .tf detection is content-based and falls back to
the legacy `tf` filetype on empty buffers, which blocks the LSP
from attaching to fresh files. lua/my/settings/filetypes.lua
forces `.tf` -> `terraform` unconditionally.
* mason-lspconfig auto-enables tflint as an LSP whenever its Mason
package is installed, which produces duplicate diagnostics
alongside the nvim-lint runner. The bare `mason-lspconfig.setup()`
call now passes `automatic_enable = { exclude = { "tflint" } }`
to keep linting in nvim-lint where the rest of the project's
linters live.
terraform-ls and tflint are installed via mason-tool-installer; the
`terraform` CLI itself is not in the Mason registry and must come
from a system package manager / tfenv / asdf (documented in the
mason-tool-installer header comment alongside perltidy and
clang-format).
Owner
Author
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code |
The `terraform` package is in fact in the Mason registry (categories: Formatter, Linter, Runtime) — the previous commit's claim that it was not was wrong. Add it to mason-tool-installer's formatters block so conform's `terraform_fmt` works out of the box without requiring the user to install the CLI separately via tfenv / asdf / system package manager. Drop the matching "manual-only" entry from the header comment, and update the README Formatters row to reflect that the binary is Mason-managed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds full Terraform tooling support to the Neovim config so
.tfand.tfvarsfiles get the same treatment as every other supported language:terraform-ls(HashiCorp) auto-installed viamason-tool-installer; auto-enabled bymason-lspconfigwithdefaults; inherits the global
'*'capabilities and the existingLspAttachkeymap/inlay-hint logic.terraform_fmtwired intoconform.nvimfor bothterraformandterraform-varsfiletypes (shells out toterraform fmt -; CLI itself is manual-install).tflintwired intonvim-lint; installed via Mason.terraformandhclparsers added toensure_installed;.tfvarsbuffers reuse theterraformparservia
vim.treesitter.language.register("terraform", "terraform-vars").Two subtleties caught during verification
Empty
.tffiles don't get theterraformfiletype. Neovim'sbuilt-in
detect.tfinspects file content and falls back to thelegacy
tffiletype when the buffer is empty or comment-only, whichprevents
terraform-lsfrom attaching to fresh files. Fixed via anew
lua/my/settings/filetypes.luathat forces*.tf -> terraformunconditionally.
tflintruns as both an LSP and an nvim-lint linter. When theMason
tflintpackage is installed,mason-lspconfig'sautomatic_enablepicks it up as a language server (lspconfig has atflintserver definition that wrapstflint --langserver). Thatproduces duplicate diagnostics alongside the nvim-lint runner.
mason-lspconfig.setup()now passesautomatic_enable = { exclude = { "tflint" } }so linting stays innvim-lint where the rest of the project's linters live.
Files
lua/my/plugins/mason-tool-installer.lua—terraformls+tflintadded; header comment notes
terraformCLI as manual-onlylua/my/plugins/lspconfig.lua—automatic_enableexclude fortflintlua/my/plugins/conform.lua—terraform+terraform-varsinftlazy-load andformatters_by_ftlua/my/plugins/nvim-lint.lua—terraform = { "tflint" }lua/my/plugins/treesitter.lua—hcl+terraformparsers andterraform-varslanguage registerlua/my/settings/filetypes.lua(new) +init.luawire-up — forces.tf -> terraformto bypass content-based detectionREADME.md— Formatters/Linters tables updated;tflint --initnote addedTest plan
Verified end-to-end on a clean Neovim instance:
.tf(empty file) → filetypeterraform;terraformlsattaches
.tfvars→ filetypeterraform-vars; treesitter highlighteractive (proves
language.registerworked);terraformlsattaches
terraform-lsandtflintauto-installed viamason-tool-installeronVimEntertreesitterparsersterraformandhclinstallednvim-lintlinters_by_ft.terraform == { "tflint" }conformformatters_by_ft.terraform == { "terraform_fmt" }and same for
terraform-varsterraformlsattaches to a.tfbuffer (thetflintLSPduplicate is correctly suppressed)
Outstanding
terraformCLI is not in the Mason registry, so format-on-save is ano-op until the user installs it via system package manager /
tfenv/
asdf. Documented in both themason-tool-installerheader commentand the README Formatters table.