Skip to content

feat: move entirely to built in vim.lsp.config #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 27 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ AstroLSP provides a simple API for configuring and setting up language servers i

## ⚡️ Requirements

- Neovim >= 0.10
- Neovim >= 0.11

## 📦 Installation

Expand Down Expand Up @@ -89,29 +89,24 @@ local opts = {
desc = "Format file with LSP",
},
},
-- Configure default capabilities for language servers (`:h vim.lsp.protocol.make_client.capabilities()`)
capabilities = {
textDocument = {
foldingRange = { dynamicRegistration = false },
},
},
-- Configure language servers for `lspconfig` (`:h lspconfig-setup`)
-- Configure language servers with `vim.lsp.config` (`:h vim.lsp.config`)
config = {
lua_ls = {
settings = {
Lua = {
hint = { enable = true, arrayIndex = "Disable" },
-- Configure LSP defaults
["*"] = {
-- Configure default capabilities
capabilities = {
textDocument = {
foldingRange = { dynamicRegistration = false },
},
},
},
clangd = {
capabilities = {
offsetEncoding = "utf-8",
-- Custom flags table to be passed to all language servers
flags = {
exit_timeout = 5000,
},
},
},
defaults = {
hover = { border = "rounded", silent = true } -- customize lsp hover window
hover = { border = "rounded", silent = true }, -- customize lsp hover window
signature_help = false, -- disable any default customizations
},
-- Configuration of LSP file operation functionality
Expand All @@ -128,10 +123,6 @@ local opts = {
didDelete = true,
},
},
-- A custom flags table to be passed to all language servers (`:h lspconfig-setup`)
flags = {
exit_timeout = 5000,
},
-- Configuration options for controlling formatting with language servers
formatting = {
-- control auto formatting on save
Expand All @@ -158,10 +149,10 @@ local opts = {
},
-- Configure how language servers get set up
handlers = {
-- default handler, first entry with no key
function(server, opts) require("lspconfig")[server].setup(opts) end,
-- default handler uses key "*"
["*"] = vim.lsp.enable,
-- custom function handler for pyright
pyright = function(_, opts) require("lspconfig").pyright.setup(opts) end,
pyright = function() vim.lsp.enable "pyright" end,
-- set to false to disable the setup of a language server
rust_analyzer = false,
},
Expand Down Expand Up @@ -198,25 +189,12 @@ local opts = {
},
},
},
-- Extra configuration for the `mason-lspconfig.nvim` plugin
mason_lspconfig = {
-- Allow registering more Mason packages as language servers for autodetection/setup
servers = {
-- The key is the lspconfig server name to register a package for
nextflow_ls = {
-- The Mason package name to register to the language server
package = "nextflow-language-server",
-- The filetypes that apply to the package and language server
filetypes = { "nextflow" },
-- (Optional) any default configuration changes that may need to happen (can be a table or a function that returns a table)
config = { cmd = { "nextflow-language-server" } }
}
}
}
-- A list like table of servers that should be setup, useful for enabling language servers not installed with Mason.
servers = { "dartls" },
-- A custom `on_attach` function to be run after the default `on_attach` function, takes two parameters `client` and `bufnr` (`:h lspconfig-setup`)
on_attach = function(client, bufnr) client.server_capabilities.semanticTokensProvider = nil end,
on_attach = function(client, bufnr)
-- custom on_attach code to run on all servers
end,
}
```

Expand All @@ -229,42 +207,21 @@ local opts = {
```lua
{
"neovim/nvim-lspconfig",
dependencies = {
{ "AstroNvim/astrolsp", opts = {} },
},
config = function()
-- set up servers configured with AstroLSP
vim.tbl_map(require("astrolsp").lsp_setup, require("astrolsp").config.servers)
end,
dependencies = { "AstroNvim/astrolsp", opts = {} },
opts = {}
}
```

### [nvim-lspconfig][lspconfig] + [mason.nvim][mason] + [mason-lspconfig.nvim][mason-lspconfig]

```lua
{
"neovim/nvim-lspconfig",
"williamboman/mason-lspconfig.nvim",
dependencies = {
{ "AstroNvim/astrolsp", opts = {} },
{
"williamboman/mason-lspconfig.nvim", -- MUST be set up before `nvim-lspconfig`
dependencies = { "williamboman/mason.nvim" },
opts = {
-- use AstroLSP setup for mason-lspconfig
handlers = { function(server) require("astrolsp").lsp_setup(server) end },
},
config = function(_, opts)
-- Optionally tell AstroLSP to register new language servers before calling the `setup` function
-- this enables the `mason-lspconfig.servers` option in the AstroLSP configuration
require("astrolsp.mason-lspconfig").register_servers()
require("mason-lspconfig").setup(opts)
end
},
"williamboman/mason.nvim",
{ "neovim/nvim-lspconfig", dependencies = { "AstroNvim/astrolsp", opts = {} } },
},
config = function()
-- set up servers configured with AstroLSP
vim.tbl_map(require("astrolsp").lsp_setup, require("astrolsp").config.servers)
end,
opts = {}
}
```

Expand All @@ -276,7 +233,9 @@ local opts = {
dependencies = {
{ "AstroNvim/astrolsp", opts = {} },
},
opts = function() return { on_attach = require("astrolsp").on_attach } end,
opts = function(_, opts)
opts.on_attach = require("astrolsp").on_attach
end
}
```

Expand Down
76 changes: 12 additions & 64 deletions lua/astrolsp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,7 @@
---@field hover vim.lsp.buf.hover.Opts|false? control the default options for `vim.lsp.buf.hover()` (`:h vim.lsp.buf.hover.Opts`)
---@field signature_help vim.lsp.buf.signature_help.Opts|false? control the default options for `vim.lsp.buf.signature_help()` (`:h vim.lsp.buf.signature_help.Opts`)

---@class AstroLSPMasonLspconfigServer
---@field public package string the Mason package name
---@field filetypes string|string[] the filetype(s) that the server applies to
---@field config? table|(fun(): table) extensions tothe default language server configuration

---@alias AstroLSPMasonLspconfigServers { [string]: AstroLSPMasonLspconfigServer }

---@class AstroLSPMasonLspconfigOpts
---@field servers AstroLSPMasonLspconfigServers? a table of servers to register with mason-lspconfig.nvim

---@class AstroLSPOpts
---_EXPERIMENTAL_ Use the native `vim.lsp.config` for LSP configuration
---@field native_lsp_config boolean?
---Configuration of auto commands
---The key into the table is the group name for the auto commands (`:h augroup`) and the value
---is a list of autocmd tables where `event` key is the event(s) that trigger the auto command
Expand Down Expand Up @@ -133,33 +121,22 @@
---}
---```
---@field features AstroLSPFeatureOpts?
---Configure default capabilities for language servers (`:h vim.lsp.protocol.make_client.capabilities()`)
---Configure options for language servers passed to `vim.lsp.config(key, config)` (`:h vim.lsp.config`)
---Example
--
---```lua
---capabilities = {
--- textDocument = {
--- foldingRange = { dynamicRegistration = false }
--- }
---}
---```
---@field capabilities lsp.ClientCapabilities?
---Configure language servers for `lspconfig` (`:h lspconfig-setup`)
---Example:
--
---```lua
---config = {
--- lua_ls = {
--- settings = {
--- Lua = {
--- hint = { enable = true, arrayIndex = "Disable" }
--- ["*"] = {
--- capabilities = {
--- textDocument = {
--- foldingRange = { dynamicRegistration = false }
--- }
--- }
--- },
--- flags = { exit_timeout = 5000 },
--- },
--- clangd = { capabilities = { offsetEncoding = "utf-8" } },
---}
---```
---@field config lspconfig.options?
---@field config table<string,vim.lsp.Config>?
---Configure default options passed to `vim.lsp.buf` functions
---Example:
---
Expand Down Expand Up @@ -196,13 +173,6 @@
--- }
---```
---@field file_operations AstroLSPFileOperationsOpts|false?
---A custom flags table to be passed to all language servers (`:h lspconfig-setup`)
---Example:
--
---```lua
---flags = { exit_timeout = 5000 }
---```
---@field flags table?
---Configuration options for controlling formatting with language servers
---Example:
--
Expand Down Expand Up @@ -240,18 +210,16 @@
---```lua
---handlers = {
--- -- default handler
--- function(server, opts)
--- require("lspconfig")[server].setup(opts)
--- end,
--- ["*"] = vim.lsp.enable,
--- -- custom function handler for pyright
--- pyright = function(_, opts)
--- require("lspconfig").pyright.setup(opts)
--- pyright = function()
--- -- some custom logic
--- end,
--- -- set to false to disable the setup of a language server
--- rust_analyzer = false,
---}
---```
---@field handlers table<string|integer,fun(server:string,opts:_.lspconfig.options)|boolean?>?
---@field handlers table<string,fun(server:string)|boolean?>?
---Configure global LSP handlers, set a method to `false` to use the Neovim default (`:h vim.lsp.handlers`)
---Example:
--
Expand Down Expand Up @@ -297,21 +265,6 @@
---}
---```
---@field mappings AstroLSPMappings?
---Extra options for the `mason-lspconfig.nvim` plugin such as registering new packages as language servers.
---Example:
--
---```lua
---mason_lspconfig = {
--- servers = {
--- nextflow_ls = {
--- package = "nextflow-language-server",
--- filetypes = "nextflow",
--- config = { cmd = { "nextflow-language-server" } }
--- }
--- }
---}
---```
---@field mason_lspconfig AstroLSPMasonLspconfigOpts?
---A list like table of servers that should be setup, useful for enabling language servers not installed with Mason.
---Example:
--
Expand All @@ -331,7 +284,6 @@

---@type AstroLSPOpts
local M = {
native_lsp_config = false,
autocmds = {},
commands = {},
features = {
Expand All @@ -340,17 +292,13 @@ local M = {
semantic_tokens = true,
signature_help = false,
},
capabilities = {},
---@diagnostic disable-next-line: missing-fields
config = {},
defaults = {},
file_operations = { timeout = 10000, operations = {} },
flags = {},
formatting = { format_on_save = { enabled = true }, disabled = {} },
handlers = {},
lsp_handlers = {},
mappings = {},
mason_lspconfig = {},
servers = {},
on_attach = nil,
}
Expand Down
12 changes: 4 additions & 8 deletions lua/astrolsp/file_operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
---@class astrolsp.file_operations
local M = {}

local utils = require "astrolsp.utils"

local config = vim.tbl_get(require "astrolsp", "config", "file_operations") or {}

---@class AstroLSPFileOperationsRename
Expand Down Expand Up @@ -56,8 +54,7 @@ function M.didCreateFiles(fnames)
local filters = did_create.filters or {}
local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames)
if next(filtered) then
utils.notify(
client,
client:notify(
"workspace/didCreateFiles",
{ files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) }
)
Expand All @@ -77,8 +74,7 @@ function M.didDeleteFiles(fnames)
local filters = did_delete.filters or {}
local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames)
if next(filtered) then
utils.notify(
client,
client:notify(
"workspace/didDeleteFiles",
{ files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) }
)
Expand All @@ -101,7 +97,7 @@ function M.didRenameFiles(renames)
renames
)
if next(filtered) then
utils.notify(client, "workspace/didRenameFiles", {
client:notify("workspace/didRenameFiles", {
files = vim.tbl_map(
function(rename) return { oldUri = vim.uri_from_fname(rename.from), newUri = vim.uri_from_fname(rename.to) } end,
filtered
Expand All @@ -116,7 +112,7 @@ end
---@param req string
---@param params table
local function getWorkspaceEdit(client, req, params)
local resp = utils.request_sync(client, req, params, config.timeout)
local resp = client:request_sync(req, params, config.timeout)
if resp and resp.result then return resp.result end
end

Expand Down
Loading