Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

astro

URL: https://github.com/withastro/astro/tree/main/packages/language-tools

plugins.lsp.servers.astro.enable

Whether to enable https://github.com/withastro/astro/tree/main/packages/language-tools/language-server

astro-ls can be installed via npm:

npm install -g @astrojs/language-server

If typescript is installed globally, you might get the \typescript.tsdk` init option is required` error. 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:

{ config, pkgs, ... }:

{
  home.packages = with pkgs; [
    typescript
  ];

  programs.neovim = {
    plugins = with pkgs.vimPlugins; [
      nvim-lspconfig
    ];
    extraPackages = with pkgs; [
      astro-language-server
    ];
    initLua = ''
      vim.lsp.config['astro'] = {
        init_options = {
          typescript = {
            tsdk = ${pkgs.typescript}/lib/node_modules/typescript/lib,
          },
        },
      }

      vim.lsp.enable('astro')

      -- ...
    '';
  };
}

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.

WARNING: TypeScript 7.x dropped tsserverlibrary.js from its npm package, so typescript.tsdk cannot resolve from a local or global TS 7.x install. Pin to TS <= 6.x (e.g. npm install -g typescript@~6) for the LSP to start.

For a globally-installed TS (resolved at runtime, unlike the Nix example where the path is known at build time), look up the workspace-local TS first via util.get_typescript_server_path, then fall back to npm root -g:

vim.lsp.config('astro', {
  before_init = function(_, config)
    local util = require('lspconfig.util')
    local tsdk = util.get_typescript_server_path(config.root_dir)
    if tsdk == '' then
      local npm_root = vim.fn.systemlist('npm root -g')
      if vim.v.shell_error == 0 and npm_root[1] then
        tsdk = npm_root[1] .. '/typescript/lib'
      end
    end
    config.init_options = config.init_options or {}
    config.init_options.typescript = config.init_options.typescript or {}
    config.init_options.typescript.tsdk = tsdk
  end,
})
vim.lsp.enable('astro')

For other package managers, replace npm root -g with:

  • pnpm: pnpm root -g
  • yarn classic: yarn global dir … /node_modules
  • yarn berry (>=2): globals unsupported — use a workspace-local TS instead.

Type: boolean

Default:

false

Example:

true

Declared by:

plugins.lsp.servers.astro.package

The astro package to use.

Alternatively, astro should be installed on your $PATH.

Type: null or package

Default:

pkgs.astro-language-server

Declared by:

plugins.lsp.servers.astro.packageFallback

When enabled, the language server package will be added to the end of the PATH (suffix) instead of the beginning (prefix).

This can be useful if you want local versions of the language server (e.g. from a devshell) to override the Nixvim version.

Type: boolean

Default:

false

Declared by:

plugins.lsp.servers.astro.autostart

Whether to call vim.lsp.enable() for astro.

Type: boolean

Default:

true

Example:

false

Declared by:

plugins.lsp.servers.astro.cmd

A list where each entry corresponds to the blankspace delimited part of the command that launches the server.

The first entry is the binary used to run the language server. Additional entries are passed as arguments.

Type: null or (list of string)

Default: null when package is null, otherwise null

Declared by:

plugins.lsp.servers.astro.extraOptions

Extra options for the astro language server.

Type: attribute set of anything

Default:

{ }

Declared by:

plugins.lsp.servers.astro.filetypes

Set of filetypes for which to attempt to resolve {root_dir}. May be empty, or server may specify a default value.

Type: null or (list of string)

Default:

null

Declared by:

plugins.lsp.servers.astro.rootMarkers

A list of files that mark the root of the project/workspace.

Vim’s LSP will try to share the same language server instance for all buffers matching filetypes within the same project.

A new server instance is only spawned when opening a buffer with a different project root.

See :h lsp-config and :h vim.fs.root().

Type: null or (list of (string or raw lua code)) or raw lua code

Default: null

Plugin default: null

Declared by:

plugins.lsp.servers.astro.settings

The settings for this LSP.

Type: open submodule of attribute set of lua value

Default:

{ }

Example:

{
  callback = lib.nixvim.mkRaw ''
  function()
    print('nixvim')
  end
'';
  foo_bar = 42;
  hostname = "localhost:8080";
}

Declared by: