Skip to content

Commit 3ace3ab

Browse files
committed
feat(advanced_lsp): migrate more of the documentation to use the new LSP configuration settings for the vim.lsp.config API
1 parent dc6fe89 commit 3ace3ab

1 file changed

Lines changed: 67 additions & 142 deletions

File tree

src/content/docs/recipes/advanced_lsp.mdx

Lines changed: 67 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LSP configuration is mostly done through the help of [AstroLSP](https://github.c
77

88
## Configuring Language Servers
99

10-
Our main tool for configuring and setting up language servers is with the [nvim-lspconfig plugin](https://github.com/neovim/nvim-lspconfig). This plugin provides configurations for many common language servers (A full list can be found in [nvim-lspconfig server configurations documentation](https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md)). These baseline configuration options are not always sufficient to meet everyone's needs. For the complete set of options that can be used when configuraing language servers, check out `:h lsp-config` in your editor.
10+
Our main tool for configuring and setting up language servers is with the [nvim-lspconfig plugin](https://github.com/neovim/nvim-lspconfig). This plugin provides configurations for many common language servers (A full list can be found in [nvim-lspconfig server configurations documentation](https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md) or `:h lspconfig-all`). These baseline configuration options are not always sufficient to meet everyone's needs. For the complete set of options that can be used when configuraing language servers, check out `:h lsp-config` in your editor.
1111

12-
AstroLSP automatically enables language servers installed through Mason and for servers specified manually (See [LSP Setup Without Installer](#lsp-setup-without-installer)). Neovim allows for easy language server customization within the `lsp/` folder in the root of your configuration (Check out `:h lsp-config`). While the `lsp/` directory is the recommended way to configurat language servers, AstroLSP also provides a simple `config` table in the plugin's options for extending the built in server configurations provided by `nvim-lspconfig`. This can be helpful if you want to conditionally make modifications in your plugin configuration.
12+
AstroLSP automatically enables language servers installed through Mason and for servers specified manually (See [LSP Setup Without Installer](#lsp-setup-without-installer)). Neovim allows for easy language server customization within the `after/lsp/` folder in the root of your configuration (Check out `:h lsp-config`). While the `after/lsp/` directory is the recommended way to configurat language servers, AstroLSP also provides a simple `config` table in the plugin's options for extending the built in server configurations provided by `nvim-lspconfig`. This can be helpful if you want to conditionally make modifications in your plugin configuration.
1313

1414
```lua title="lsp/clangd.lua"
1515
return {
@@ -325,7 +325,7 @@ Many of these can be found pre-configured in the [AstroNvim Community Repository
325325

326326
:::
327327

328-
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.nvim` 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:
328+
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.nvim` for installing the language servers. You can use the `handlers` table for specifying how language servers should be setup such as using a language specific plugin. This function for each handler can take a single parameter which is the name of the server. The current lsp configuration settings can be retried with `vim.lsp.config[server_name]`. These options include things such as our built in `capabilities`, `on_attach`, as well as the user defined options in and `lsp/<server_name>.lua` and `after/lsp/<server_name>.lua` files the `config` table. Here are a couple examples for some common LSP plugins:
329329

330330
### Deno ([deno-nvim](https://github.com/sigmaSd/deno-nvim))
331331

@@ -349,10 +349,10 @@ return {
349349
"AstroNvim/astrolsp",
350350
---@type AstroLSPOpts
351351
opts = {
352-
setup_handlers = {
352+
handlers = {
353353
-- add custom handler
354-
denols = function(_, opts)
355-
require("deno-nvim").setup({ server = opts })
354+
denols = function()
355+
require("deno-nvim").setup({ server = vim.lsp.config.denols })
356356
end,
357357
},
358358
},
@@ -383,30 +383,26 @@ return {
383383

384384
:::
385385

386-
To conditionally enable `tsserver`/`denols` based on the presence of `package.json`/`deno.json`, add the following plugin specs:
386+
To conditionally enable `tsserver`/`denols` based on the presence of `package.json`/`deno.json`, put the following LSP definitions in your `after/lsp/` directory:
387387

388-
```lua title="lua/plugins/typescript_deno.lua"
388+
```lua title="after/lsp/denols.lua"
389+
---@type vim.lsp.Config
389390
return {
390-
"AstroNvim/astrolsp",
391-
---@param opts AstroLSPOpts
392-
opts = function(plugin, opts)
393-
require("astrocore").extend_tbl(opts, {
394-
config = {
395-
denols = {
396-
root_dir = require("lspconfig.util").root_pattern(
397-
"deno.json",
398-
"deno.jsonc"
399-
),
400-
},
401-
tsserver = {
402-
root_dir = require("lspconfig.util").root_pattern("package.json"),
403-
},
404-
-- eslint = {
405-
-- root_dir = require("lspconfig.util").root_pattern("package.json", ".eslintrc.json", ".eslintrc.js"),
406-
-- },
407-
},
408-
})
409-
end,
391+
root_markers = { "deno.json", "deno.jsonc" },
392+
}
393+
```
394+
395+
```lua title="after/lsp/tsserver.lua"
396+
---@type vim.lsp.Config
397+
return {
398+
root_markers = { "package.json" },
399+
}
400+
```
401+
402+
```lua title="after/lsp/eslint.lua"
403+
---@type vim.lsp.Config
404+
return {
405+
root_markers = { "package.json", ".eslintrc.json", ".eslintrc.js" },
410406
}
411407
```
412408

@@ -475,30 +471,23 @@ return {
475471

476472
:::
477473

478-
```lua title="lua/plugins/clangd.lua"
474+
```lua title="after/lsp/clangd.lua"
475+
---@type vim.lsp.Config
479476
return {
480-
{
481-
"AstroNvim/astrolsp",
482-
---@type AstroLSPOpts
483-
opts = {
484-
config = {
485-
clangd = {
486-
capabilities = {
487-
offsetEncoding = "utf-8",
488-
},
489-
},
490-
},
491-
},
477+
capabilities = {
478+
offsetEncoding = "utf-8",
492479
},
480+
}
481+
```
482+
483+
```lua title="lua/plugins/clangd.lua"
484+
return {
493485
{
494486
"p00f/clangd_extensions.nvim", -- install lsp plugin
495487
lazy = true,
496488
init = function()
497489
-- load clangd extensions when clangd attaches
498-
local augroup =
499-
vim.api.nvim_create_augroup("clangd_extensions", { clear = true })
500490
vim.api.nvim_create_autocmd("LspAttach", {
501-
group = augroup,
502491
desc = "Load clangd_extensions with clangd",
503492
callback = function(args)
504493
if
@@ -507,7 +496,7 @@ return {
507496
then
508497
require("clangd_extensions")
509498
-- add more `clangd` setup here as needed such as loading autocmds
510-
vim.api.nvim_del_augroup_by_id(augroup) -- delete auto command since it only needs to happen once
499+
return true -- delete the autocommand once the plugin is loaded
511500
end
512501
end,
513502
})
@@ -539,37 +528,38 @@ return {
539528

540529
:::
541530

531+
```lua title="after/lsp/dartls.lua"
532+
---@type vim.lsp.Config
533+
return {
534+
config = {
535+
dartls = {
536+
-- any changes you want to make to the LSP setup, for example
537+
color = {
538+
enabled = true,
539+
},
540+
settings = {
541+
showTodos = true,
542+
completeFunctionCalls = true,
543+
},
544+
},
545+
},
546+
}
547+
```
548+
542549
```lua title="lua/plugins/flutter-tools.lua"
543550
return {
544551
{ "akinsho/flutter-tools.nvim", lazy = true }, -- add lsp plugin
545552
{
546553
"AstroNvim/astrolsp",
547-
---@param opts AstroLSPOpts
548-
opts = function(plugin, opts)
549-
opts.servers = opts.servers or {}
550-
table.insert(opts.servers, "dartls")
551-
552-
opts = require("astrocore").extend_tbl(opts, {
553-
setup_handlers = {
554-
-- add custom handler
555-
dartls = function(_, dartls_opts)
556-
require("flutter-tools").setup({ lsp = dartls_opts })
557-
end,
558-
},
559-
config = {
560-
dartls = {
561-
-- any changes you want to make to the LSP setup, for example
562-
color = {
563-
enabled = true,
564-
},
565-
settings = {
566-
showTodos = true,
567-
completeFunctionCalls = true,
568-
},
569-
},
570-
},
571-
})
572-
end,
554+
---@type AstroLSPOpts
555+
opts = {
556+
servers = { "dartls" },
557+
handlers = {
558+
dartls = function()
559+
require("flutter-tools").setup({ lsp = vim.lsp.config.dartls })
560+
end,
561+
},
562+
},
573563
},
574564
}
575565
```
@@ -596,18 +586,13 @@ return {
596586
version = "^5",
597587
lazy = false, -- This plugin is already lazy
598588
opts = function(_, opts)
599-
local astrolsp_avail, astrolsp = pcall(require, "astrolsp")
600-
local astrolsp_opts = (
601-
astrolsp_avail and astrolsp.lsp_opts("rust_analyzer")
602-
) or {}
589+
local server_opts = vim.lsp.config.rust_analyzer
603590
local server = {
604591
---@type table | (fun(project_root:string|nil, default_settings: table|nil):table) -- The rust-analyzer settings or a function that creates them.
605592
settings = function(project_root, default_settings)
606-
local astrolsp_settings = astrolsp_opts.settings or {}
607-
608593
local merge_table = require("astrocore").extend_tbl(
609594
default_settings or {},
610-
astrolsp_settings
595+
server_opts.settings or {}
611596
)
612597
local ra = require("rustaceanvim.config.server")
613598
-- load_rust_analyzer_settings merges any found settings with the passed in default settings table and then returns that table
@@ -617,7 +602,7 @@ return {
617602
})
618603
end,
619604
}
620-
return { server = require("astrocore").extend_tbl(astrolsp_opts, server) }
605+
return { server = require("astrocore").extend_tbl(server_opts, server) }
621606
end,
622607
-- configure `rustaceanvim` by setting the `vim.g.rustaceanvim` variable
623608
config = function(_, opts)
@@ -658,69 +643,10 @@ return {
658643

659644
```lua title="lua/plugins/jdtls.lua"
660645
return {
661-
{ "mfussenegger/nvim-jdtls", lazy = true }, -- load jdtls on module
662646
{
663647
"AstroNvim/astrolsp",
664-
---@type AstroLSPOpts
665-
opts = {
666-
setup_handlers = {
667-
-- add custom handler
668-
jdtls = function(_, opts)
669-
vim.api.nvim_create_autocmd("Filetype", {
670-
pattern = "java", -- autocmd to start jdtls
671-
callback = function()
672-
if opts.root_dir and opts.root_dir ~= "" then
673-
require("jdtls").start_or_attach(opts)
674-
end
675-
end,
676-
})
677-
end,
678-
},
679-
config = {
680-
-- set jdtls server settings
681-
jdtls = function()
682-
-- use this function notation to build some variables
683-
local root_markers =
684-
{ ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
685-
local root_dir = require("jdtls.setup").find_root(root_markers)
686-
687-
-- calculate workspace dir
688-
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
689-
local workspace_dir = vim.fn.stdpath("data")
690-
.. "/site/java/workspace-root/"
691-
.. project_name
692-
os.execute("mkdir " .. workspace_dir)
693-
694-
-- get the mason jdtls path
695-
local jdtls_files = vim.fn.expand("$MASON/share/jdtls")
696-
697-
-- return the server config
698-
return {
699-
cmd = {
700-
"java",
701-
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
702-
"-Dosgi.bundles.defaultStartLevel=4",
703-
"-Declipse.product=org.eclipse.jdt.ls.core.product",
704-
"-Dlog.protocol=true",
705-
"-Dlog.level=ALL",
706-
"-javaagent:" .. jdtls_files .. "/lombok.jar",
707-
"-Xms1g",
708-
"--add-modules=ALL-SYSTEM",
709-
"--add-opens",
710-
"java.base/java.util=ALL-UNNAMED",
711-
"--add-opens",
712-
"java.base/java.lang=ALL-UNNAMED",
713-
"-jar",
714-
jdtls_files .. "/plugins/org.eclipse.equinox.launcher.jar",
715-
"-configuration",
716-
jdtls_files .. "/config",
717-
"-data",
718-
workspace_dir,
719-
},
720-
root_dir = root_dir,
721-
}
722-
end,
723-
},
648+
dependencies = {
649+
{ "mfussenegger/nvim-jdtls" }, -- load jdtls before loading language servers
724650
},
725651
},
726652
{
@@ -789,8 +715,7 @@ return {
789715
autocmds = {
790716
no_insert_inlay_hints = {
791717
-- only create for language servers that support inlay hints
792-
-- (and only if vim.lsp.inlay_hint is available)
793-
cond = vim.lsp.inlay_hint and "textDocument/inlayHint" or false,
718+
cond = "textDocument/inlayHint",
794719
{
795720
-- when going into insert mode
796721
event = "InsertEnter",

0 commit comments

Comments
 (0)