From 72d129f69aff62233655bd9261108e2826959c0d Mon Sep 17 00:00:00 2001 From: sjcobb Date: Mon, 2 Jun 2025 23:48:36 +0100 Subject: [PATCH] astro module overhaul --- modules/plugins/languages/astro.nix | 69 ++++++++++++++++++----------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/modules/plugins/languages/astro.nix b/modules/plugins/languages/astro.nix index b7a69bda..1f3f3003 100644 --- a/modules/plugins/languages/astro.nix +++ b/modules/plugins/languages/astro.nix @@ -12,25 +12,46 @@ inherit (lib.types) enum either listOf package str; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.lua) expToLua; + inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.types) mkGrammarOption diagnostics; cfg = config.vim.languages.astro; - defaultServer = "astro"; + defaultServers = ["astro"]; servers = { astro = { - package = pkgs.astro-language-server; - lspConfig = '' - lspconfig.astro.setup { - capabilities = capabilities, - on_attach = attach_keymaps, - cmd = ${ - if isList cfg.lsp.package - then expToLua cfg.lsp.package - else ''{"${cfg.lsp.package}/bin/astro-ls", "--stdio"}'' - } - } - ''; + enable = true; + cmd = [(getExe pkgs.astro-language-server) "--stdio"]; + filetypes = ["astro"]; + root_markers = ["package.json" "tsconfig.json" "jsconfig.json" ".git"]; + init_options = { + typescript = {}; + }; + before_init = + mkLuaInline + /* + lua + */ + '' + function(_, config) + -- TODO: Make this a shared util function + local get_typescript_server_path = function(root_dir) + local project_roots = vim.fs.find('node_modules', { path = root_dir, upward = true, limit = math.huge }) + for _, project_root in ipairs(project_roots) do + local typescript_path = project_root .. '/typescript' + local stat = vim.loop.fs_stat(typescript_path) + if stat and stat.type == 'directory' then + return typescript_path .. '/lib' + end + end + return ''' + end + + if config.init_options and config.init_options.typescript and not config.init_options.typescript.tsdk then + config.init_options.typescript.tsdk = get_typescript_server_path(config.root_dir) + end + end + ''; }; }; @@ -82,18 +103,10 @@ in { lsp = { enable = mkEnableOption "Astro LSP support" // {default = config.vim.lsp.enable;}; - - server = mkOption { - type = enum (attrNames servers); - default = defaultServer; + servers = mkOption { description = "Astro LSP server to use"; - }; - - package = mkOption { - type = either package (listOf str); - default = servers.${cfg.lsp.server}.package; - example = ''[lib.getExe pkgs.astro-language-server "--minify" "--stdio"]''; - description = "Astro LSP server package, or the command to run as a list of strings"; + type = listOf (enum (attrNames servers)); + default = defaultServers; }; }; @@ -131,8 +144,12 @@ in { }) (mkIf cfg.lsp.enable { - vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.astro-lsp = servers.${cfg.lsp.server}.lspConfig; + vim.lsp.servers = + mapListToAttrs (n: { + name = n; + value = servers.${n}; + }) + cfg.lsp.servers; }) (mkIf cfg.format.enable {