Advanced LSP Setup
LSP configuration is mostly done through the help of AstroLSP, the AstroNvim language server configuration engine plugin. This provides a simple to use interface for configuration while handling the complex integration between the AstroNvim features, nvim-lspconfig, mason.nvim and mason related tooling, and none-ls.nvim. For full documentation on how to use and configure this plugin you can check out the plugin’s README or run :h astrolsp
in AstroNvim. All of the following recipes are provided through lazy.nvim
plugin specifications and can be added to your own plugins.
Configuring Language Servers
Our main tool for configuring and setting up language servers is with the nvim-lspconfig plugin. This plugin provides configurations for many common language servers (A full list can be found in nvim-lspconfig server configurations documentation). These baseline configuration options are not always sufficient to meet everyone’s needs, and are typically configured by calling require("lspconfig")[<server_name>].setup(opts)
where opts
is a table of options. For the complete set of options that can be used in the nvim-lspconfig
setup check out :h lspconfig-setup
in your editor.
AstroLSP automatically calls these setup
functions for language servers installed through Mason and for servers specified manually (See LSP Setup Without Installer). AstroLSP also provides a simple config
table in the plugin’s options for extending the built in server configurations provided by nvim-lspconfig
.
Custom LSP Definition
nvim-lspconfig
is great, but it doesn’t support all language servers that exist. You may want to set up a custom server where you manually define the cmd
and the root_dir
. This can also be done completely through servers
and config
tables similar to configuring servers that are supported by nvim-lspconfig
! For these custom servers, the minimum requirement is defining a cmd
in the config
entry, but to get automatic starting of language servers you also need to set filetypes
and root_dir
. Here is a simple example setting up a Prolog LSP with swipl
:
LSP Setup Without Installer
AstroNvim comes with mason-lspconfig as an easy interface for setting up and installing language servers, but this might not be adequate for all users. The LSP installer doesn’t support all of the language servers that Neovim’s LSP config supports and some users may already have the language servers installed on their machine and don’t want to reinstall it separately. In these cases we have added an easy interface for setting up these servers. The following plugin specification for AstroLSP simply sets up pyright
language server for a user with pyright
already available on their system:
Customizing auto-format on save
AstroNvim has made formatting on save part of the default functionality out of the box. If you don’t want your code to get auto formatted on save, you can disable it in the AstroLSP configuration:
We have also added an extension to just true
or false
for this option to give more the user the ability to disable the auto formatting for specific filetypes. For example:
If you would rather use a whitelist of filetypes for formatting on save rather than a blacklist type model, you can do that as well with the allow_filetypes
table. If you have allow_filetypes
it will take precedence over ignore_filetypes
. So please only use one of these options at a time. Here is an example:
For even more control, you can provide a filter function with the key filter
. This function takes a single parameter of the current buffer number and returns a boolean value of whether you want to format on save or not (true
means format, false
means do not format). This function will run on each save to calculate if it should format.
With the formatting on save enabled, we have also provided the mapping <Leader>uf
and <Leader>uF
to toggle the auto formatting temporarily for either the current buffer or globally (Note: Format on save must be enabled in the AstroLSP formatting
options for this option and keybinding to do anything).
Controlling Formatting
Since Neovim v0.8 there have been improvements to how language servers are used for formatting files. Previously Neovim could only use a single language server to format files at a time and would prompt on each format if multiple were available. This led to users disabling formatting capabilities for different language servers and losing that functionality all together for convenience. Now you are able to format with many formatters at the same time and filter them with a function. To empower this, AstroNvim has a configuration option for controlling what formatters are used. This can be done either with a filter function or a list of disabled clients.
Disabling formatting for a filter function
Using the filter
option you can supply filter function to be run on each client that has formatting capabilities and if it returns true
then it will be used for formatting and if it returns false
then it will not be used. This applies to whenever you format your code either on save, with <Leader>lf
, or with :Format
.
Disabling formatting for a list of language servers
Using the disabled
option you can supply an array like list of language server client names and those clients will be disabled with you format your code either on save, with <Leader>lf
, or with :Format
.
Using both filter function and disabled list
When using the options together, a client listed in the disabled
list will always be disabled and then all other clients will then be passed into the filter
function. For example, we can simplify our previous filter
function by just disabling the lua_ls
client in the disabled
table:
Configure other formatting options
The formatting
options also allows you to specify other parameters for the vim.lsp.buf.format()
call. Any of the other formatting options are allowed to be used here to be used as the default options. This means being able to easily adjust the default timeout_ms
for formatting in AstroNvim or making asynchronous formatting the default. For example you can do the following to increase the formatting timeout along with adjust the filtering:
LSP Specific Plugins
There are some plugins available for doing advanced setup of language servers that require the user to not use the lspconfig
setup call and instead use their own plugin setup for handling this. AstroNvim provides a nice way to do this while still using mason-lspconfig
for installing the language servers. You can use the setup_handlers
table for specifying how language servers should be setup such as using a language specific plugin. This function for each handler has two parameters, the first is the name of the server and the second is the options we would be passing to the lspconfig
setup call. These options include things such as our built in capabilities
, on_attach
, as well as the user defined options in the config
table. Here are a couple examples for some common LSP plugins:
Typescript (typescript.nvim)
Deno (deno-nvim)
tsserver + denols
Since both tsserver
and denols
(and others such as eslint
and prettier
) attach to TypeScript/JavaScript files, some extra configuration may be required if both are installed.
To conditionally enable tsserver
/denols
based on the presence of package.json
/deno.json
, add the following plugin specs:
For none-ls
packages (such as prettier
, prettierd
, or eslint_d
), set the following to handlers in the mason-null-ls
options:
C/C++ (clangd_extensions.nvim)
Dart Flutter (flutter-tools.nvim)
Requires dart
to be available on the system.
Rust (rust-tools.nvim)
Java (nvim-jdtls)
Automatic Signature Help
Some users may want to have automatic pop ups of function signatures while editing with a language server similar to that of functionality provided by noice.nvim
and lsp_signature.nvim
. By default this behavior is disabled, but can easily be enabled by modifying features.signature_help
in the AstroLSP opts
:
Inlay Hints
Since Neovim v0.10 there is the ability to handle and render inline virtual text and therefore first class support of inlay hints. AstroLSP comes with a built in feature for enabling inlay hints easily as well as toggling them during runtime. By default inlay hints are disabled, but they can easily be enabled by default by modifying features.inlay_hints
in the AstroLSP opts
:
Disable in Insert Mode
Some users may like the look of inlay hints but prefer for them to be disabled with actively editing text in insert mode. This recipe also respects the current state of inlay hints and will only toggle them if inlay hints are enabled in a given buffer. This can easily be achieved with the AstroLSP autocmds
feature: