From 97864420c32626183371fa1a1f8f94a342aa27b9 Mon Sep 17 00:00:00 2001 From: THEON-1 Date: Wed, 3 Dec 2025 17:13:52 +0100 Subject: [PATCH] switched from Mason + mason-lspconfig to custom install --- lua/config/startup.lua | 2 +- lua/lsp/arduino.lua | 22 ++++- lua/lsp/init.lua | 5 +- lua/lsp/latex.lua | 171 +++++++++++++++++++++++++++++++++ lua/lsp/lua.lua | 17 ++++ lua/lsp/python.lua | 18 ++++ lua/plugins/markdown.lua | 1 + lua/plugins/mason.lua | 1 + lua/plugins/nvim-lspconfig.lua | 1 + 9 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 lua/lsp/latex.lua create mode 100644 lua/lsp/lua.lua create mode 100644 lua/lsp/python.lua diff --git a/lua/config/startup.lua b/lua/config/startup.lua index 24e8be3..a2b03e2 100644 --- a/lua/config/startup.lua +++ b/lua/config/startup.lua @@ -6,7 +6,7 @@ local tail_process = vim.system({ logfile, }, {}, function(res) vim.schedule(function () - vim.fn.writefile(vim.split(res.stdout, "\n", {plain = true}), logfile) + vim.fn.writefile(vim.split(res.stdout, "\n", { plain = true }), logfile) end) end) diff --git a/lua/lsp/arduino.lua b/lua/lsp/arduino.lua index d73ec02..d92f21b 100644 --- a/lua/lsp/arduino.lua +++ b/lua/lsp/arduino.lua @@ -1,5 +1,6 @@ -vim.lsp.config("arduino_language_server", { +---@type vim.lsp.Config +local config = { cmd = { "arduino-language-server", --"--fqbn", @@ -9,5 +10,22 @@ vim.lsp.config("arduino_language_server", { "arduino", "cpp", }, -}) + root_dir = function (bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + on_dir(vim.fs.dirname(vim.fs.find({ vim.fn.fnamemodify(fname, ':r') }, { type = "directory", upward = true })[1])) + end, + capabilities = { + textDocument = { + ---@diagnostic disable-next-line: assign-type-mismatch + semanticTokens = vim.NIL, + }, + workspace = { + ---@diagnostic disable-next-line: assign-type-mismatch + semanticTokens = vim.NIL, + }, + }, +} + +vim.lsp.config['arduino_language_server'] = config +vim.lsp.enable('arduino_language_server') diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index d12cc9d..ff45efd 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -1,3 +1,6 @@ - +# https://github.com/neovim/nvim-lspconfig/blob/master/lsp require("lsp.arduino") +require("lsp.latex") +require("lsp.lua") +require("lsp.python") diff --git a/lua/lsp/latex.lua b/lua/lsp/latex.lua new file mode 100644 index 0000000..ebc8c1a --- /dev/null +++ b/lua/lsp/latex.lua @@ -0,0 +1,171 @@ +local function buf_build(client, bufnr) + local win = vim.api.nvim_get_current_win() + local params = vim.lsp.util.make_position_params(win, client.offset_encoding) + client:request('textDocument/build', params, function(err, result) + if err then + error(tostring(err)) + end + local texlab_build_status = { + [0] = 'Success', + [1] = 'Error', + [2] = 'Failure', + [3] = 'Cancelled', + } + vim.notify('Build ' .. texlab_build_status[result.status], vim.log.levels.INFO) + end, bufnr) +end + +local function buf_search(client, bufnr) + local win = vim.api.nvim_get_current_win() + local params = vim.lsp.util.make_position_params(win, client.offset_encoding) + client:request('textDocument/forwardSearch', params, function(err, result) + if err then + error(tostring(err)) + end + local texlab_forward_status = { + [0] = 'Success', + [1] = 'Error', + [2] = 'Failure', + [3] = 'Unconfigured', + } + vim.notify('Search ' .. texlab_forward_status[result.status], vim.log.levels.INFO) + end, bufnr) +end + +local function buf_cancel_build(client, bufnr) + return client:exec_cmd({ + title = 'cancel', + command = 'texlab.cancelBuild', + }, { bufnr = bufnr }) +end + +local function dependency_graph(client) + client:exec_cmd({ command = 'texlab.showDependencyGraph' }, { bufnr = 0 }, function(err, result) + if err then + return vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR) + end + vim.notify('The dependency graph has been generated:\n' .. result, vim.log.levels.INFO) + end) +end + +local function command_factory(cmd) + local cmd_tbl = { + Auxiliary = 'texlab.cleanAuxiliary', + Artifacts = 'texlab.cleanArtifacts', + } + return function(client, bufnr) + return client:exec_cmd({ + title = ('clean_%s'):format(cmd), + command = cmd_tbl[cmd], + arguments = { { uri = vim.uri_from_bufnr(bufnr) } }, + }, { bufnr = bufnr }, function(err, _) + if err then + vim.notify(('Failed to clean %s files: %s'):format(cmd, err.message), vim.log.levels.ERROR) + else + vim.notify(('Command %s executed successfully'):format(cmd), vim.log.levels.INFO) + end + end) + end +end + +local function buf_find_envs(client, bufnr) + local win = vim.api.nvim_get_current_win() + client:exec_cmd({ + command = 'texlab.findEnvironments', + arguments = { vim.lsp.util.make_position_params(win, client.offset_encoding) }, + }, { bufnr = bufnr }, function(err, result) + if err then + return vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR) + end + local env_names = {} + local max_length = 1 + for _, env in ipairs(result) do + table.insert(env_names, env.name.text) + max_length = math.max(max_length, string.len(env.name.text)) + end + for i, name in ipairs(env_names) do + env_names[i] = string.rep(' ', i - 1) .. name + end + vim.lsp.util.open_floating_preview(env_names, '', { + height = #env_names, + width = math.max((max_length + #env_names - 1), (string.len 'Environments')), + focusable = false, + focus = false, + title = 'Environments', + }) + end) +end + +local function buf_change_env(client, bufnr) + vim.ui.input({ prompt = 'New environment name: ' }, function(input) + if not input or input == '' then + return vim.notify('No environment name provided', vim.log.levels.WARN) + end + local pos = vim.api.nvim_win_get_cursor(0) + return client:exec_cmd({ + title = 'change_environment', + command = 'texlab.changeEnvironment', + arguments = { + { + textDocument = { uri = vim.uri_from_bufnr(bufnr) }, + position = { line = pos[1] - 1, character = pos[2] }, + newName = tostring(input), + }, + }, + }, { bufnr = bufnr }) + end) +end + +---@type vim.lsp.Config +local config = { + cmd = { 'texlab' }, + filetypes = { 'tex', 'plaintex', 'bib' }, + root_markers = { '.git', '.latexmkrc', 'latexmkrc', '.texlabroot', 'texlabroot', 'Tectonic.toml' }, + settings = { + texlab = { + rootDirectory = nil, + build = { + executable = 'latexmk', + args = { '-pdf', '-interaction=nonstopmode', '-synctex=1', '%f' }, + onSave = false, + forwardSearchAfter = false, + }, + forwardSearch = { + executable = nil, + args = {}, + }, + chktex = { + onOpenAndSave = false, + onEdit = false, + }, + diagnosticsDelay = 300, + latexFormatter = 'latexindent', + latexindent = { + ['local'] = nil, -- local is a reserved keyword + modifyLineBreaks = false, + }, + bibtexFormatter = 'texlab', + formatterLineLength = 80, + }, + }, + on_attach = function(client, bufnr) + for _, cmd in ipairs({ + { name = 'TexlabBuild', fn = buf_build, desc = 'Build the current buffer' }, + { name = 'TexlabForward', fn = buf_search, desc = 'Forward search from current position' }, + { name = 'TexlabCancelBuild', fn = buf_cancel_build, desc = 'Cancel the current build' }, + { name = 'TexlabDependencyGraph', fn = dependency_graph, desc = 'Show the dependency graph' }, + { name = 'TexlabCleanArtifacts', fn = command_factory('Artifacts'), desc = 'Clean the artifacts' }, + { name = 'TexlabCleanAuxiliary', fn = command_factory('Auxiliary'), desc = 'Clean the auxiliary files' }, + { name = 'TexlabFindEnvironments', fn = buf_find_envs, desc = 'Find the environments at current position' }, + { name = 'TexlabChangeEnvironment', fn = buf_change_env, desc = 'Change the environment at current position' }, + }) do + vim.api.nvim_buf_create_user_command(bufnr, 'Lsp' .. cmd.name, function() + cmd.fn(client, bufnr) + end, { desc = cmd.desc }) + end + end, +} + +vim.lsp.config['texlab'] = config +vim.lsp.enable('texlab') + diff --git a/lua/lsp/lua.lua b/lua/lsp/lua.lua new file mode 100644 index 0000000..f226ebd --- /dev/null +++ b/lua/lsp/lua.lua @@ -0,0 +1,17 @@ +---@type vim.lsp.Config +local config = { + cmd = { 'lua-language-server' }, + filetypes = { 'lua' }, + root_markers = { { '.luarc.json', '.luarc.jsonc' }, '.git' }, + settings = { + codeLens = { enable = true }, + hine = { + enable = true, + semicolon = 'Disable', + }, + }, +} + +vim.lsp.config['lua_ls'] = config +vim.lsp.enable('lua_ls') + diff --git a/lua/lsp/python.lua b/lua/lsp/python.lua new file mode 100644 index 0000000..3d087e5 --- /dev/null +++ b/lua/lsp/python.lua @@ -0,0 +1,18 @@ + +---@type vim.lsp.Config +local config = { + cmd = { 'pylsp' }, + filetypes = { 'python' }, + root_markers = { + 'pyproject.toml', + 'setup.py', + 'setup.cfg', + 'requirements.txt', + 'Pipfile', + '.git', + }, +} + +vim.lsp.config['python-language-server'] = config +vim.lsp.enable('python-language-server') + diff --git a/lua/plugins/markdown.lua b/lua/plugins/markdown.lua index b889e42..140a279 100644 --- a/lua/plugins/markdown.lua +++ b/lua/plugins/markdown.lua @@ -25,5 +25,6 @@ return { 'latex2text', }, }, + enabled = false, } diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua index 8a5828b..3cd405f 100644 --- a/lua/plugins/mason.lua +++ b/lua/plugins/mason.lua @@ -19,5 +19,6 @@ return { }, "neovim/nvim-lspconfig", }, + enabled = false, } diff --git a/lua/plugins/nvim-lspconfig.lua b/lua/plugins/nvim-lspconfig.lua index a1042bc..8d98f8d 100644 --- a/lua/plugins/nvim-lspconfig.lua +++ b/lua/plugins/nvim-lspconfig.lua @@ -3,5 +3,6 @@ return { cmd = { 'LspInfo', 'LspInstall', 'LspStart' }, event = { 'BufReadPre', 'BufNewFile' }, dependencies = { 'saghen/blink.cmp' }, + enabled = false, }