From 5952e60f0f253d2777dfebcb9a64af7133af5040 Mon Sep 17 00:00:00 2001 From: ppenguin Date: Sat, 17 Aug 2024 18:49:41 +0200 Subject: [PATCH 1/2] add language HCL Terraform doesn't register hcl and doesn't offer good DX if manually set for editing e.g. nomad HCL files. Incl. reformat with alejandra --- modules/plugins/languages/default.nix | 1 + modules/plugins/languages/hcl.nix | 71 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 modules/plugins/languages/hcl.nix diff --git a/modules/plugins/languages/default.nix b/modules/plugins/languages/default.nix index e86a521..5bcbad6 100644 --- a/modules/plugins/languages/default.nix +++ b/modules/plugins/languages/default.nix @@ -8,6 +8,7 @@ in { ./css.nix ./elixir.nix ./go.nix + ./hcl.nix ./html.nix ./java.nix ./lua.nix diff --git a/modules/plugins/languages/hcl.nix b/modules/plugins/languages/hcl.nix new file mode 100644 index 0000000..ddf80e6 --- /dev/null +++ b/modules/plugins/languages/hcl.nix @@ -0,0 +1,71 @@ +{ + config, + pkgs, + lib, + ... +}: let + inherit (lib.options) mkEnableOption mkOption; + inherit (lib.modules) mkIf mkMerge; + inherit (lib.types) package; + inherit (lib.nvim.types) mkGrammarOption; + + cfg = config.vim.languages.hcl; +in { + options.vim.languages.hcl = { + enable = mkEnableOption "HCL support"; + + treesitter = { + enable = mkEnableOption "HCL treesitter" // {default = config.vim.languages.enableTreesitter;}; + package = mkGrammarOption pkgs "hcl"; + }; + + lsp = { + enable = mkEnableOption "HCL LSP support (terraform-ls)" // {default = config.vim.languages.enableLSP;}; + # TODO: it would be cooler to use vscode-extensions.hashicorp.hcl probably, shouldn't be too hard + # TODO: formatter, suppied by above or ... + # FIXME: or should we somehow integrate this: + #` https://git.mzte.de/nvim-plugins/null-ls.nvim/commit/e1fb7e2b2e4400835e23b9603a19813be119852b ?? + package = mkOption { + description = "HCL ls package (terraform-ls)"; + type = package; + default = pkgs.terraform-ls; + }; + }; + }; + config = mkIf cfg.enable (mkMerge [ + { + vim.pluginRC.hcl = '' + vim.api.nvim_create_autocmd("FileType", { + pattern = "hcl", + callback = function(opts) + local bo = vim.bo[opts.buf] + bo.tabstop = 2 + bo.shiftwidth = 2 + bo.softtabstop = 2 + end + }) + + local ft = require('Comment.ft') + ft + .set('hcl', '#%s') + ''; + } + (mkIf cfg.treesitter.enable { + vim.treesitter.enable = true; + vim.treesitter.grammars = [cfg.treesitter.package]; + }) + + (mkIf cfg.lsp.enable { + vim.lsp.lspconfig.enable = true; + vim.lsp.lspconfig.sources = lib.optionalAttrs (! config.vim.languages.terraform.lsp.enable) { + terraform-ls = '' + lspconfig.terraformls.setup { + capabilities = capabilities, + on_attach=default_on_attach, + cmd = {"${cfg.lsp.package}/bin/terraform-ls", "serve"}, + } + ''; + }; + }) + ]); +} From ce9d7ee8704f0b35c1ca4aeba91b565ef8889e45 Mon Sep 17 00:00:00 2001 From: ppenguin Date: Thu, 31 Oct 2024 18:58:38 +0100 Subject: [PATCH 2/2] cleanup, add formatter --- modules/plugins/languages/hcl.nix | 73 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/modules/plugins/languages/hcl.nix b/modules/plugins/languages/hcl.nix index ddf80e6..a819751 100644 --- a/modules/plugins/languages/hcl.nix +++ b/modules/plugins/languages/hcl.nix @@ -4,12 +4,42 @@ lib, ... }: let + inherit (builtins) attrNames; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; - inherit (lib.types) package; + inherit (lib.types) package bool enum; inherit (lib.nvim.types) mkGrammarOption; cfg = config.vim.languages.hcl; + + defaultServer = "terraform-ls"; + servers = { + terraform-ls = rec { + package = pkgs.terraform-ls; + lspConfig = '' + lspconfig.terraformls.setup { + capabilities = capabilities, + on_attach=default_on_attach, + cmd = {"${lib.getExe package}", "serve"}, + } + ''; + }; + }; + + defaultFormat = "hclfmt"; + formats = { + hclfmt = rec { + package = pkgs.hclfmt; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.hclfmt.with({ + command = "${lib.getExe package}", + }) + ) + ''; + }; + }; in { options.vim.languages.hcl = { enable = mkEnableOption "HCL support"; @@ -21,17 +51,33 @@ in { lsp = { enable = mkEnableOption "HCL LSP support (terraform-ls)" // {default = config.vim.languages.enableLSP;}; - # TODO: it would be cooler to use vscode-extensions.hashicorp.hcl probably, shouldn't be too hard - # TODO: formatter, suppied by above or ... - # FIXME: or should we somehow integrate this: - #` https://git.mzte.de/nvim-plugins/null-ls.nvim/commit/e1fb7e2b2e4400835e23b9603a19813be119852b ?? + # TODO: (maybe, is it better?) it would be cooler to use vscode-extensions.hashicorp.hcl probably, shouldn't be too hard package = mkOption { - description = "HCL ls package (terraform-ls)"; + description = "HCL language server package (terraform-ls)"; type = package; - default = pkgs.terraform-ls; + default = servers.${defaultServer}.package; + }; + }; + + format = { + enable = mkOption { + description = "Enable HCL formatting"; + type = bool; + default = config.vim.languages.enableFormat; + }; + type = mkOption { + description = "HCL formatter to use"; + type = enum (attrNames formats); + default = defaultFormat; + }; + package = mkOption { + description = "HCL formatter package"; + type = package; + default = formats.${cfg.format.type}.package; }; }; }; + config = mkIf cfg.enable (mkMerge [ { vim.pluginRC.hcl = '' @@ -58,14 +104,13 @@ in { (mkIf cfg.lsp.enable { vim.lsp.lspconfig.enable = true; vim.lsp.lspconfig.sources = lib.optionalAttrs (! config.vim.languages.terraform.lsp.enable) { - terraform-ls = '' - lspconfig.terraformls.setup { - capabilities = capabilities, - on_attach=default_on_attach, - cmd = {"${cfg.lsp.package}/bin/terraform-ls", "serve"}, - } - ''; + terraform-ls = servers.${cfg.lsp.server}.lspConfig; }; }) + + (mkIf cfg.format.enable { + vim.lsp.null-ls.enable = true; + vim.lsp.null-ls.sources.hcl-format = formats.${cfg.format.type}.nullConfig; + }) ]); }