My nvim configuration
| 1 | ---@brief |
| 2 | --- |
| 3 | --- https://github.com/withastro/language-tools/tree/main/packages/language-server |
| 4 | --- |
| 5 | --- `astro-ls` can be installed via `npm`: |
| 6 | --- ```sh |
| 7 | --- npm install -g @astrojs/language-server |
| 8 | --- ``` |
| 9 | --- |
| 10 | --- If typescript is installed globally, you might get the `\`typescript.tsdk\` init option is required` error. |
| 11 | --- You will need to manually pass the typescript SDK path. Here is an example of a Nix configuration where typescript is installed via Nix's Home-manager: |
| 12 | --- |
| 13 | --- ```nix |
| 14 | --- { config, pkgs, ... }: |
| 15 | --- |
| 16 | --- { |
| 17 | --- home.packages = with pkgs; [ |
| 18 | --- typescript |
| 19 | --- ]; |
| 20 | --- |
| 21 | --- programs.neovim = { |
| 22 | --- plugins = with pkgs.vimPlugins; [ |
| 23 | --- nvim-lspconfig |
| 24 | --- ]; |
| 25 | --- extraPackages = with pkgs; [ |
| 26 | --- astro-language-server |
| 27 | --- ]; |
| 28 | --- initLua = '' |
| 29 | --- vim.lsp.config['astro'] = { |
| 30 | --- init_options = { |
| 31 | --- typescript = { |
| 32 | --- tsdk = ${pkgs.typescript}/lib/node_modules/typescript/lib, |
| 33 | --- }, |
| 34 | --- }, |
| 35 | --- } |
| 36 | --- |
| 37 | --- vim.lsp.enable('astro') |
| 38 | --- |
| 39 | --- -- ... |
| 40 | --- ''; |
| 41 | --- }; |
| 42 | --- } |
| 43 | --- ``` |
| 44 | --- The path can also be passed via a variable, like `vim.g.tsdk = "${pkgs.typescript}/lib/node_modules/typescript/lib"` and then used in the Lua Neovim config. |
| 45 | |
| 46 | local util = require("lspconfig.util") |
| 47 | |
| 48 | ---@type vim.lsp.Config |
| 49 | return { |
| 50 | cmd = function(dispatchers, config) |
| 51 | local cmd = "astro-ls" |
| 52 | if (config or {}).root_dir then |
| 53 | local local_cmd = vim.fs.joinpath(config.root_dir, "node_modules/.bin", cmd) |
| 54 | if vim.fn.executable(local_cmd) == 1 then |
| 55 | cmd = local_cmd |
| 56 | end |
| 57 | end |
| 58 | return vim.lsp.rpc.start({ cmd, "--stdio" }, dispatchers) |
| 59 | end, |
| 60 | filetypes = { "astro" }, |
| 61 | root_markers = { "package.json", "tsconfig.json", "jsconfig.json", ".git" }, |
| 62 | init_options = { |
| 63 | typescript = {}, |
| 64 | }, |
| 65 | before_init = function(_, config) |
| 66 | if config.init_options and config.init_options.typescript and not config.init_options.typescript.tsdk then |
| 67 | config.init_options.typescript.tsdk = util.get_typescript_server_path(config.root_dir) |
| 68 | end |
| 69 | end, |
| 70 | } |