commit 11a7714801b184df92ae9ad304af01b9f317dcf9
parent 8fb2f0330541c15169bff69d6d4e38ce085a8f70
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date: Wed, 27 Jul 2022 13:36:15 +0200
feat: enhance user command integrations
Diffstat:
M | lua/lsp_config.lua | | | 74 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
1 file changed, 65 insertions(+), 9 deletions(-)
diff --git a/lua/lsp_config.lua b/lua/lsp_config.lua
@@ -194,20 +194,27 @@ local find_root = function(markers, file)
return vim.fn.fnamemodify(source, ":p:h")
end
-local function setup_lsp(name, filetypes, command, config)
+local function setup_lsp(name, filetypes, command, ucommands, config)
vim.api.nvim_create_autocmd("Filetype", {
pattern = filetypes,
group = augroup,
callback = function(args)
- for cname, func in pairs(config.ucommands or {}) do
- vim.api.nvim_create_user_command(cname, function(...)
+
+ local cpreffix = name:gsub("[-_ ]", "")
+ cpreffix = cpreffix:sub(1, 1):upper() .. cpreffix:sub(2)
+
+ -- Setup user commands if requested
+ for cname, func in pairs(ucommands) do
+ vim.api.nvim_create_user_command(cpreffix .. cname, function(...)
local client = vim.lsp.get_active_clients { name = name }[1]
- func(client, ...)
- end)
+ func(client, args.buf, ...)
+ end, {})
end
local cfg = vim.deepcopy(config)
cfg.name = name
cfg.cmd = command
+
+ -- Find the root directory
if not cfg.root_dir then
cfg.root_dir = find_root(cfg.root_markers or { ".git" }, args.file)
end
@@ -279,6 +286,7 @@ local system_lsps = {
rust_analyzer = {
filetypes = { "rust" },
+ root_markers = { "Cargo.toml", ".git" },
cmd = { "rustup", "run", "nightly", "rust-analyzer" },
cfg = default_config,
},
@@ -319,6 +327,47 @@ local system_lsps = {
texlab = {
filetypes = { "tex", "bib" },
root_markers = { ".latexmkrc", ".git" },
+ ucommands = {
+ Build = function(client, bufnr)
+ local texlab_build_status = vim.tbl_add_reverse_lookup {
+ Success = 0,
+ Error = 1,
+ Failure = 2,
+ Cancelled = 3,
+ }
+
+ local params = {
+ textDocument = { uri = vim.uri_from_bufnr(bufnr) },
+ }
+
+ client.request('textDocument/build', params, function(err, result)
+ if err then
+ error(tostring(err))
+ end
+ print('Build ' .. texlab_build_status[result.status])
+ end, bufnr)
+ end,
+ Forward = function(client, bufnr)
+ local texlab_forward_status = vim.tbl_add_reverse_lookup {
+ Success = 0,
+ Error = 1,
+ Failure = 2,
+ Unconfigured = 3,
+ }
+
+ local params = {
+ textDocument = { uri = vim.uri_from_bufnr(bufnr) },
+ position = { line = vim.fn.line '.' - 1, character = vim.fn.col '.' },
+ }
+
+ client.request('textDocument/forwardSearch', params, function(err, result)
+ if err then
+ error(tostring(err))
+ end
+ print('Search ' .. texlab_forward_status[result.status])
+ end)
+ end
+ },
cfg = {
capabilities = capabilities,
on_attach = texlab_attach,
@@ -338,15 +387,22 @@ local system_lsps = {
settings = {
texlab = {
build = {
+ executable = 'latexmk',
+ args = { '-pdf', '-interaction=nonstopmode', '-synctex=1', '%f' },
onSave = true,
+ forwardSearchAfter = false,
},
+ latexFormatter = 'latexindent',
+ latexindent = {
+ ['local'] = nil, -- local is a reserved keyword
+ modifyLineBreaks = false,
+ },
+ auxDirectory = '.',
forwardSearch = {
executable = "zathura",
args = { "--synctex-forward", "%l:1:%f", "%p" }
},
- latex = {
- rootDirectory = ".",
- },
+ rootDirectory = ".",
}
},
},
@@ -357,7 +413,7 @@ for lname, config in pairs(system_lsps) do
if not config.filetypes then
vim.notify(string.format("No filetypes defined for %s", lname))
else
- setup_lsp(lname, config.filetypes, config.command or { lname }, config.cfg)
+ setup_lsp(lname, config.filetypes, config.command or { lname }, config.ucommands or {}, config.cfg)
end
end