From d827ac877166375657f6d04d711c700ebe26951e Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 16 Feb 2025 12:49:10 +0300 Subject: [PATCH] languages: begin converting language modules to new lsp options format --- modules/plugins/languages/asm.nix | 68 +++++++++++++++++++++++------ modules/plugins/languages/astro.nix | 44 ++++++++++++------- modules/plugins/languages/bash.nix | 41 +++++++++++------ 3 files changed, 109 insertions(+), 44 deletions(-) diff --git a/modules/plugins/languages/asm.nix b/modules/plugins/languages/asm.nix index a0e96cef..d1c90112 100644 --- a/modules/plugins/languages/asm.nix +++ b/modules/plugins/languages/asm.nix @@ -4,12 +4,34 @@ lib, ... }: let + inherit (builtins) attrNames; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; - inherit (lib.types) package; + inherit (lib.types) enum either listOf package str; + inherit (lib.lists) isList; + inherit (lib.meta) getExe; + inherit (lib.generators) mkLuaInline; + inherit (lib.nvim.lua) expToLua toLuaObject; + inherit (lib.nvim.languages) lspOptions; inherit (lib.nvim.types) mkGrammarOption; cfg = config.vim.languages.assembly; + + defaultServer = "asm-lsp"; + servers = { + asm-lsp = { + package = pkgs.asm-lsp; + options = { + capabilities = mkLuaInline "capabilities"; + on_attach = mkLuaInline "attach_keymaps"; + filetypes = ["asm" "vasm"]; + cmd = + if isList cfg.lsp.package + then expToLua cfg.lsp.package + else ["${getExe cfg.lsp.package}"]; + }; + }; + }; in { options.vim.languages.assembly = { enable = mkEnableOption "Assembly support"; @@ -22,28 +44,46 @@ in { lsp = { enable = mkEnableOption "Assembly LSP support (asm-lsp)" // {default = config.vim.languages.enableLSP;}; + server = mkOption { + description = "Assembly LSP server to use"; + type = enum (attrNames servers); + default = defaultServer; + }; + package = mkOption { - type = package; - default = pkgs.asm-lsp; - description = "asm-lsp package"; + description = "asm-lsp LSP server package, or the command to run as a list of strings"; + example = ''[lib.getExe pkgs.asm-lsp "--quiet"]''; + type = either package (listOf str); + default = servers.${cfg.lsp.server}.package; + }; + + options = mkOption { + type = lspOptions; + default = servers.${cfg.lsp.server}.options; + description = '' + LSP options for Assembly language support. + + This option is freeform, you may add options that are not set by default + and they will be merged into the final table passed to lspconfig. + ''; }; }; }; config = mkIf cfg.enable (mkMerge [ (mkIf cfg.treesitter.enable { - vim.treesitter.enable = true; - vim.treesitter.grammars = [cfg.treesitter.package]; + vim.treesitter = { + enable = true; + grammars = [cfg.treesitter.package]; + }; }) (mkIf cfg.lsp.enable { - vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.asm-lsp = '' - lspconfig.asm_lsp.setup { - capabilities = capabilities, - on_attach = default_on_attach, - cmd = {"${cfg.lsp.package}/bin/asm-lsp"}, - } - ''; + vim.lsp.lspconfig = { + enable = true; + sources.asm-lsp = '' + lspconfig.("asm_lsp").setup (${toLuaObject cfg.lsp.options}) + ''; + }; }) ]); } diff --git a/modules/plugins/languages/astro.nix b/modules/plugins/languages/astro.nix index 9e70424b..cdcfdff7 100644 --- a/modules/plugins/languages/astro.nix +++ b/modules/plugins/languages/astro.nix @@ -7,11 +7,12 @@ inherit (builtins) attrNames; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; + inherit (lib.types) enum either listOf package str; inherit (lib.lists) isList; inherit (lib.meta) getExe; - inherit (lib.types) enum either listOf package str; - inherit (lib.nvim.lua) expToLua; - inherit (lib.nvim.languages) diagnosticsToLua; + inherit (lib.generators) mkLuaInline; + inherit (lib.nvim.lua) expToLua toLuaObject; + inherit (lib.nvim.languages) diagnosticsToLua lspOptions; inherit (lib.nvim.types) mkGrammarOption diagnostics; cfg = config.vim.languages.astro; @@ -20,17 +21,16 @@ servers = { astro = { package = pkgs.astro-language-server; - lspConfig = '' - lspconfig.astro.setup { - capabilities = capabilities; - on_attach = attach_keymaps, - cmd = ${ + options = { + capabilities = mkLuaInline "capabilities"; + on_attach = mkLuaInline "attach_keymaps"; + filetypes = ["astro"]; + init_options = {typescript = {};}; + cmd = if isList cfg.lsp.package then expToLua cfg.lsp.package - else ''{"${cfg.lsp.package}/bin/astro-ls", "--stdio"}'' - } - } - ''; + else ["${getExe cfg.lsp.package}" "--stdio"]; + }; }; }; @@ -83,8 +83,7 @@ in { treesitter = { enable = mkEnableOption "Astro treesitter" // {default = config.vim.languages.enableTreesitter;}; - - astroPackage = mkGrammarOption pkgs "astro"; + package = mkGrammarOption pkgs "astro"; }; lsp = { @@ -96,6 +95,17 @@ in { default = defaultServer; }; + options = mkOption { + type = lspOptions; + default = servers.${cfg.lsp.server}.options; + description = '' + LSP options for Astro language support. + + This option is freeform, you may add options that are not set by default + and they will be merged into the final table passed to lspconfig. + ''; + }; + package = mkOption { description = "Astro LSP server package, or the command to run as a list of strings"; example = ''[lib.getExe pkgs.astro-language-server "--minify" "--stdio"]''; @@ -134,12 +144,14 @@ in { config = mkIf cfg.enable (mkMerge [ (mkIf cfg.treesitter.enable { vim.treesitter.enable = true; - vim.treesitter.grammars = [cfg.treesitter.astroPackage]; + vim.treesitter.grammars = [cfg.treesitter.package]; }) (mkIf cfg.lsp.enable { vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.astro-lsp = servers.${cfg.lsp.server}.lspConfig; + vim.lsp.lspconfig.sources.astro-lsp = '' + lspconfig.("astro").setup (${toLuaObject cfg.lsp.options}) + ''; }) (mkIf cfg.format.enable { diff --git a/modules/plugins/languages/bash.nix b/modules/plugins/languages/bash.nix index c0066b3c..088b16df 100644 --- a/modules/plugins/languages/bash.nix +++ b/modules/plugins/languages/bash.nix @@ -7,11 +7,13 @@ inherit (builtins) attrNames; inherit (lib.options) mkOption mkEnableOption literalExpression; inherit (lib.modules) mkIf mkMerge; + inherit (lib.types) enum either listOf package str bool; inherit (lib.lists) isList; - inherit (lib.types) enum either package listOf str bool; - inherit (lib.nvim.languages) diagnosticsToLua; - inherit (lib.nvim.types) diagnostics mkGrammarOption; - inherit (lib.nvim.lua) expToLua; + inherit (lib.meta) getExe; + inherit (lib.generators) mkLuaInline; + inherit (lib.nvim.lua) expToLua toLuaObject; + inherit (lib.nvim.languages) diagnosticsToLua lspOptions; + inherit (lib.nvim.types) mkGrammarOption diagnostics; cfg = config.vim.languages.bash; @@ -19,17 +21,15 @@ servers = { bash-ls = { package = pkgs.bash-language-server; - lspConfig = '' - lspconfig.bashls.setup{ - capabilities = capabilities; - on_attach = default_on_attach; - cmd = ${ + options = { + capabilities = mkLuaInline "capabilities"; + on_attach = mkLuaInline "default_on_attach"; + filetypes = ["bash" "sh"]; + cmd = if isList cfg.lsp.package then expToLua cfg.lsp.package - else ''{"${cfg.lsp.package}/bin/bash-language-server", "start"}'' - }; - } - ''; + else ["${getExe cfg.lsp.package}" "start"]; + }; }; }; @@ -87,6 +87,17 @@ in { type = either package (listOf str); default = pkgs.bash-language-server; }; + + options = mkOption { + type = lspOptions; + default = servers.${cfg.lsp.server}.options; + description = '' + LSP options for Bash language support. + + This option is freeform, you may add options that are not set by default + and they will be merged into the final table passed to lspconfig. + ''; + }; }; format = { @@ -126,7 +137,9 @@ in { (mkIf cfg.lsp.enable { vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.bash-lsp = servers.${cfg.lsp.server}.lspConfig; + vim.lsp.lspconfig.sources.bash-lsp = '' + lspconfig.("bashls").setup (${toLuaObject cfg.lsp.options}) + ''; }) (mkIf cfg.format.enable {