From 7db85cf0aed8010d6aafba7215d423c792676910 Mon Sep 17 00:00:00 2001 From: peturparkur Date: Thu, 27 Jun 2024 20:52:05 +0100 Subject: [PATCH] better lsp handling + utility to only check mason if not found locally installed --- lua/kickstart/plugins/lsp.lua | 64 ++++++++++++++++++++++------------- lua/utils/mason.lua | 50 ++++++++++++++------------- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/lua/kickstart/plugins/lsp.lua b/lua/kickstart/plugins/lsp.lua index af497077..8744d00e 100644 --- a/lua/kickstart/plugins/lsp.lua +++ b/lua/kickstart/plugins/lsp.lua @@ -211,19 +211,19 @@ return { local servers = { -- clangd = {}, -- gopls = {}, - -- pyright = { - -- settings = { - -- python = { - -- analysis = { - -- autoSearchPaths = true, - -- diagnosticMode = 'workspace', - -- useLibraryCodeForTypes = true, - -- autoImportCompletions = true, - -- }, - -- }, - -- }, - -- disableLanguageServices = false, - -- }, + pyright = { + settings = { + python = { + analysis = { + autoSearchPaths = true, + diagnosticMode = 'workspace', + useLibraryCodeForTypes = true, + autoImportCompletions = true, + }, + }, + }, + disableLanguageServices = false, + }, basedpyright = { settings = { basedpyright = { @@ -255,11 +255,15 @@ return { -- But for many setups, the LSP (`tsserver`) will work just fine -- tsserver = {}, -- - + nixd = {}, + bashls = { + alias = 'bash-language-server', + }, lua_ls = { -- cmd = {...}, -- filetypes = { ...}, -- capabilities = {}, + alias = 'lua-language-server', settings = { Lua = { completion = { @@ -289,16 +293,28 @@ return { -- require('mason-tool-installer').setup { ensure_installed = ensure_installed } -- INFO: Using my own utils function instead of mason-lspconfig as it checks if the stuff is already installed -- outside of mason. This is useful for NixOS setup where mason version just doesn't work sometimes due to libc issues. - require('utils.mason').install { - -- "python-lsp-server", - 'pyright', - 'basedpyright', - 'bash-language-server', - -- "rnix-lsp", - 'lua-language-server', - -- "docker-compose-language-service", - -- "nil", - } + local installed = {} + local i = 0 + for server, config in pairs(servers) do + if config.alias then + installed[i] = config.alias + else + installed[i] = server + end + i = i + 1 + end + table.insert(installed, 'stylua') + require('utils.mason').install(installed) + -- require('utils.mason').install { + -- -- "python-lsp-server", + -- 'pyright', + -- 'basedpyright', + -- 'bash-language-server', + -- -- "rnix-lsp", + -- 'lua-language-server', + -- -- "docker-compose-language-service", + -- -- "nil", + -- } local lsp = require 'lspconfig' diff --git a/lua/utils/mason.lua b/lua/utils/mason.lua index f4ca5d0d..e5553c75 100644 --- a/lua/utils/mason.lua +++ b/lua/utils/mason.lua @@ -2,34 +2,36 @@ local M = {} -- any cases where name of package is different from the binary name local name_to_bin = { - ["csharp-language-server"] = "csharp-ls", - ["python-lsp-server"] = "pylsp", - ["docker-compose-language-service"] = "docker-compose-langserver", + ['csharp-language-server'] = 'csharp-ls', + ['python-lsp-server'] = 'pylsp', + ['docker-compose-language-service'] = 'docker-compose-langserver', } M.install = function(ensure_installed) - -- Allow for passing in a single string - if type(ensure_installed) == "string" then - ensure_installed = { ensure_installed } - end + -- Allow for passing in a single string + if type(ensure_installed) == 'string' then + ensure_installed = { ensure_installed } + end - -- Function to check if the executable exists in the PATH - local function executable_exists(name) - if name_to_bin[name] then - return vim.fn.executable(name) == 1 or vim.fn.executable(name_to_bin[name]) == 1 - end - return vim.fn.executable(name) == 1 - end + -- Function to check if the executable exists in the PATH + local function executable_exists(name) + if name_to_bin[name] then + return vim.fn.executable(name_to_bin[name]) == 1 + end + return vim.fn.executable(name) == 1 + end - local registry = require("mason-registry") - registry.refresh(function() - for _, pkg_name in ipairs(ensure_installed) do - local pkg = registry.get_package(pkg_name) - if not executable_exists(pkg_name) and not pkg:is_installed() then - pkg:install() - end - end - end) + local registry = require 'mason-registry' + registry.refresh(function() + for _, pkg_name in ipairs(ensure_installed) do + if not executable_exists(pkg_name) then + local pkg = registry.get_package(pkg_name) + if not pkg:is_installed() then + pkg:install() + end + end + end + end) end -return M \ No newline at end of file +return M