Skip to content

Commit

Permalink
feat!: 0.7 API update
Browse files Browse the repository at this point in the history
* switch to lua api for autocommands
* switch to nvim_create_user_command
* move to lua plugin initialization
* rename commands -> user commands to match API and free up commands
  namespace

  This will require users to update commands -> user_commands in the
  setup {} call.

BREAKING CHANGE
  • Loading branch information
mjlbach committed Apr 16, 2022
1 parent 99596a8 commit e32f34e
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 236 deletions.
55 changes: 29 additions & 26 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ The global defaults for all servers can be overridden by extending the
if params and params.type <= vim.lsp.protocol.MessageType.Log then
vim.lsp.handlers["window/logMessage"](err, method, params, client_id)
end
end;
end,
["window/showMessage"] = function(err, method, params, client_id)
if params and params.type <= vim.lsp.protocol.MessageType.Warning.Error then
vim.lsp.handlers["window/showMessage"](err, method, params, client_id)
end
end;
end,
}
}
)
Expand All @@ -282,34 +282,37 @@ The `configs` module is a singleton where configs are defined. The schema for
validating using `vim.validate` is:
>
configs.SERVER_NAME = {
default_config = {'t'};
on_new_config = {'f', true};
on_attach = {'f', true};
commands = {'t', true};
docs = {'t', true};
default_config = {'t'},
on_new_config = {'f', true},
on_attach = {'f', true},
user_commands = {'t', true},
docs = {'t', true},
}
<
where the structure of the docs table is as follows:
>
docs = {
description = {'s', true};
default_config = {'t', true};
description = {'s', true},
default_config = {'t', true},
}
<
`commands` is a map of `name:definition` key:value pairs, where `definition`
is a list whose first value is a function implementing the command, and the
rest are either array values which will be formed into flags for the command,
or special keys like `description`. Example:
`user_commands` is a list of tables where each table consists of the fields
`name`, `command`, and `opts`. See |nvim_create_user_command| for more
details.

Example:
>
commands = {
TexlabBuild = {
function()
user_commands = {
{
name = "TexlabBuild"
command = function()
buf_build(0)
end;
"-range";
description = "Build the current buffer";
};
};
end,
opts = {
desc = "Build the current buffer",
}
},
},
<
The `configs.__newindex` metamethod consumes the config definition and returns
an object with a `setup()` method, to be invoked by users:
Expand Down Expand Up @@ -342,13 +345,13 @@ The three steps for adding and enabling a new server configuration are:
if not configs.foo_lsp then
configs.foo_lsp = {
default_config = {
cmd = {'/home/neovim/lua-language-server/run.sh'};
filetypes = {'lua'};
cmd = {'/home/neovim/lua-language-server/run.sh'},
filetypes = {'lua'},
root_dir = function(fname)
return lspconfig.util.find_git_ancestor(fname)
end;
settings = {};
};
end,
settings = {},
},
}
end
Expand Down
63 changes: 0 additions & 63 deletions lua/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,6 @@ local M = {
util = require 'lspconfig.util',
}

M._root = {}

function M.available_servers()
return vim.tbl_keys(configs)
end

-- Called from plugin/lspconfig.vim because it requires knowing that the last
-- script in scriptnames to be executed is lspconfig.
function M._root._setup()
M._root.commands = {
LspInfo = {
function()
require 'lspconfig.ui.lspinfo'()
end,
'-nargs=0',
description = '`:LspInfo` Displays attached, active, and configured language servers',
},
LspStart = {
function(server_name)
if server_name then
if configs[server_name] then
configs[server_name].launch()
end
else
local buffer_filetype = vim.bo.filetype
for _, config in pairs(configs) do
for _, filetype_match in ipairs(config.filetypes or {}) do
if buffer_filetype == filetype_match then
config.launch()
end
end
end
end
end,
'-nargs=? -complete=custom,v:lua.lsp_complete_configured_servers',
description = '`:LspStart` Manually launches a language server.',
},
LspStop = {
function(cmd_args)
for _, client in ipairs(M.util.get_clients_from_cmd_args(cmd_args)) do
client.stop()
end
end,
'-nargs=? -complete=customlist,v:lua.lsp_get_active_client_ids',
description = '`:LspStop` Manually stops the given language client(s).',
},
LspRestart = {
function(cmd_args)
for _, client in ipairs(M.util.get_clients_from_cmd_args(cmd_args)) do
client.stop()
vim.defer_fn(function()
configs[client.name].launch()
end, 500)
end
end,
'-nargs=? -complete=customlist,v:lua.lsp_get_active_client_ids',
description = '`:LspRestart` Manually restart the given language client(s).',
},
}

M.util.create_module_commands('_root', M._root.commands)
end

local mt = {}
function mt:__index(k)
if configs[k] == nil then
Expand Down
82 changes: 41 additions & 41 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ function configs.__newindex(t, config_name, config_def)
default_config = { config_def.default_config, 't' },
on_new_config = { config_def.on_new_config, 'f', true },
on_attach = { config_def.on_attach, 'f', true },
commands = { config_def.commands, 't', true },
user_commands = { config_def.user_commands, 't', true },
}
if config_def.commands then
for k, v in pairs(config_def.commands) do
if config_def.user_commands then
for _, v in pairs(config_def.user_commands) do
validate {
['command.name'] = { k, 's' },
['command.fn'] = { v[1], 'f' },
name = { v.name, 's' },
command = { v.command, 'f' },
}
end
else
config_def.commands = {}
config_def.user_commands = {}
end

local M = {}
Expand All @@ -37,13 +37,13 @@ function configs.__newindex(t, config_name, config_def)
filetypes = { config.filetype, 't', true },
on_new_config = { config.on_new_config, 'f', true },
on_attach = { config.on_attach, 'f', true },
commands = { config.commands, 't', true },
user_commands = { config.user_commands, 't', true },
}
if config.commands then
for k, v in pairs(config.commands) do
if config.user_commands then
for _, v in pairs(config.user_commands) do
validate {
['command.name'] = { k, 's' },
['command.fn'] = { v[1], 'f' },
name = { v.name, 's' },
command = { v.command, 'f' },
}
end
end
Expand All @@ -64,14 +64,14 @@ function configs.__newindex(t, config_name, config_def)
event = 'BufReadPost'
pattern = '*'
end
api.nvim_command(
string.format(
"autocmd %s %s unsilent lua require'lspconfig'[%q].manager.try_add()",
event,
pattern,
config.name
)
)
local lsp_group = vim.api.nvim_create_augroup('lspconfig', { clear = false })
vim.api.nvim_create_autocmd(event, {
pattern = pattern,
callback = function()
M.manager.try_add()
end,
group = lsp_group,
})
end

local get_root_dir = config.root_dir
Expand All @@ -88,13 +88,14 @@ function configs.__newindex(t, config_name, config_def)
end

if root_dir then
api.nvim_command(
string.format(
"autocmd BufReadPost %s/* unsilent lua require'lspconfig'[%q].manager.try_add_wrapper()",
vim.fn.fnameescape(root_dir),
config.name
)
)
local lsp_group = vim.api.nvim_create_augroup('lspconfig', { clear = false })
vim.api.nvim_create_autocmd('BufReadPost', {
pattern = vim.fn.fnameescape(root_dir) .. '/*',
callback = function()
M.manager.try_add_wrapper()
end,
group = lsp_group,
})
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local bufname = api.nvim_buf_get_name(bufnr)
if util.bufname_valid(bufname) then
Expand Down Expand Up @@ -182,15 +183,15 @@ function configs.__newindex(t, config_name, config_def)
if bufnr == api.nvim_get_current_buf() then
M._setup_buffer(client.id, bufnr)
else
api.nvim_command(
string.format(
"autocmd BufEnter <buffer=%d> ++once lua require'lspconfig'[%q]._setup_buffer(%d,%d)",
bufnr,
config_name,
client.id,
bufnr
)
)
local lsp_group = vim.api.nvim_create_augroup('lspconfig', { clear = false })
vim.api.nvim_create_autocmd('BufEnter', {
callback = function()
M._setup_buffer(client.id, bufnr)
end,
group = lsp_group,
buffer = bufnr,
once = true,
})
end
end)

Expand Down Expand Up @@ -272,18 +273,17 @@ function configs.__newindex(t, config_name, config_def)
if client.config._on_attach then
client.config._on_attach(client, bufnr)
end
if client.config.commands and not vim.tbl_isempty(client.config.commands) then
M.commands = vim.tbl_deep_extend('force', M.commands, client.config.commands)
if client.config.user_commands and not vim.tbl_isempty(client.config._user_commands) then
M.user_commands = vim.tbl_deep_extend('force', M.user_commands, client.config.user_commands)
end
if not M.commands_created and not vim.tbl_isempty(M.commands) then
-- Create the module commands
util.create_module_commands(config_name, M.commands)
for _, command_info in pairs(M.user_commands or {}) do
vim.api.nvim_create_user_command(command_info.name, command_info.command, command_info.opts or {})
M.commands_created = true
end
end

M.commands_created = false
M.commands = config_def.commands
M.user_commands = config_def.user_commands
M.name = config_name
M.document_config = config_def

Expand Down
11 changes: 7 additions & 4 deletions lua/lspconfig/server_configurations/clangd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ return {
single_file_support = true,
capabilities = default_capabilities,
},
commands = {
ClangdSwitchSourceHeader = {
function()
user_commands = {
{
name = 'ClangdSwitchSourceHeader',
command = function()
switch_source_header(0)
end,
description = 'Switch between source/header',
opts = {
desc = 'Switch between source/header',
},
},
},
docs = {
Expand Down
11 changes: 7 additions & 4 deletions lua/lspconfig/server_configurations/denols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ return {
['textDocument/references'] = denols_handler,
},
},
commands = {
DenolsCache = {
function()
user_commands = {
{
name = 'DenolsCache',
command = function()
buf_cache(0)
end,
description = 'Cache a module and all of its dependencies.',
opts = {
desc = 'Cache a module and all of its dependencies.',
},
},
},
docs = {
Expand Down
11 changes: 7 additions & 4 deletions lua/lspconfig/server_configurations/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ return {
end,
},
},
commands = {
EslintFixAll = {
function()
user_commands = {
{
name = 'EslintFixAll',
command = function()
fix_all { sync = true, bufnr = 0 }
end,
description = 'Fix all eslint problems for this buffer',
opts = {
desc = 'Fix all eslint problems for this buffer',
},
},
},
docs = {
Expand Down
11 changes: 7 additions & 4 deletions lua/lspconfig/server_configurations/pyright.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ return {
},
},
},
commands = {
PyrightOrganizeImports = {
organize_imports,
description = 'Organize Imports',
user_commands = {
{
name = 'PyrightOrganizeImports',
command = organize_imports,
opts = {
desc = 'Organize Imports',
},
},
},
docs = {
Expand Down
11 changes: 7 additions & 4 deletions lua/lspconfig/server_configurations/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ return {
['rust-analyzer'] = {},
},
},
commands = {
CargoReload = {
function()
user_commands = {
{
name = 'CargoReload',
command = function()
reload_workspace(0)
end,
description = 'Reload current cargo workspace',
opts = {
desc = 'Reload current cargo workspace',
},
},
},
docs = {
Expand Down
Loading

0 comments on commit e32f34e

Please sign in to comment.