refactor: add mason.nvim (#89)

This commit is contained in:
Matthew Leong 2022-12-29 09:48:42 -08:00 committed by GitHub
parent daaafc1f8d
commit 03fc837085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 116 additions and 91 deletions

View file

@ -16,6 +16,25 @@ local config = {
rename_notification = true,
-- Enable non-default servers, use default lsp config
-- Check here for configs that will be used by default: https://github.com/williamboman/nvim-lsp-installer/tree/main/lua/nvim-lsp-installer/servers
-- lsp servers that should be installed
ensure_installed = {
'astro',
'bashls',
'cssls',
'gopls',
'graphql',
'html',
'jsonls',
'tsserver',
'sumneko_lua',
'pyright',
'svelte',
'tailwindcss',
'yamlls',
},
-- lsp servers that should be enabled
servers = {
-- Enable rust_analyzer
rust_analyzer = true,

View file

@ -9,6 +9,26 @@ local default_config = {
format_on_save = true, -- true/false or table of filetypes {'.ts', '.js',}
format_timeout = 3000,
rename_notification = true,
-- lsp servers that should be installed
ensure_installed = {
'astro',
--[[ 'actionlint', ]]
--[[ 'eslint_d', ]]
--[[ 'prettierd', ]]
'bashls',
'cssls',
'gopls',
'graphql',
'html',
'jsonls',
'tsserver',
'sumneko_lua',
'pyright',
'svelte',
'tailwindcss',
'yamlls',
},
-- lsp servers that should be enabled
servers = {
astro = true,
tailwindcss = true,

View file

@ -1,4 +1,3 @@
-- set up lsp servers
require('cosmic.lsp.diagnostics')
require('cosmic.lsp.commands')

View file

@ -6,7 +6,6 @@ local defaults = {
auto_session_enabled = false,
auto_save_enabled = true,
auto_restore_enabled = false,
auto_session_use_git_branch = true,
}
return {

View file

@ -0,0 +1,71 @@
local user_config = require('cosmic.core.user')
return {
'williamboman/mason-lspconfig.nvim',
config = function()
require('mason').setup({
ui = {
border = user_config.border,
},
})
require('mason-lspconfig').setup({
ensure_installed = user_config.lsp.ensure_installed,
})
-- set up lsp servers
local u = require('cosmic.utils')
local default_config = require('cosmic.lsp.providers.defaults')
local lspconfig = require('lspconfig')
-- initial default servers
-- by default tsserver/ts_utils and null_ls are enabled
local requested_servers = {}
-- get disabled servers from config
local disabled_servers = {}
for config_server, config_opt in pairs(user_config.lsp.servers) do
-- null ls doesn't need to be setup by lspconfig
if not config_server == 'null_ls' then
print(config_server)
end
--[[ print(config_server) ]]
if config_opt == false or config_server == 'null_ls' then
table.insert(disabled_servers, config_server)
elseif not vim.tbl_contains(requested_servers, config_server) then
-- add additonally defined servers to be installed
table.insert(requested_servers, config_server)
end
end
for _, requested_server in pairs(requested_servers) do
local opts = default_config
-- disable server if config disabled server list says so
opts.autostart = true
if vim.tbl_contains(disabled_servers, requested_server) then
opts.autostart = false
end
-- set up default cosmic options
if requested_server == 'tsserver' then
opts = u.merge(opts, require('cosmic.lsp.providers.tsserver'))
elseif requested_server == 'jsonls' then
opts = u.merge(opts, require('cosmic.lsp.providers.jsonls'))
elseif requested_server == 'pyright' then
opts = u.merge(opts, require('cosmic.lsp.providers.pyright'))
elseif requested_server == 'sumneko_lua' then
opts = u.merge(opts, require('cosmic.lsp.providers.sumneko_lua'))
end
-- override options if user definds them
if type(user_config.lsp.servers[requested_server]) == 'table' then
if user_config.lsp.servers[requested_server].opts ~= nil then
opts = u.merge(opts, user_config.lsp.servers[requested_server].opts)
end
end
lspconfig[requested_server].setup(opts)
end
end,
event = 'BufEnter',
}

View file

@ -0,0 +1,3 @@
return {
'williamboman/mason.nvim',
}

View file

@ -1,9 +1,9 @@
local u = require('cosmic.utils')
local defaults = require('cosmic.lsp.providers.defaults')
local null_ls = require('null-ls')
local config = require('cosmic.core.user')
local user_config = require('cosmic.core.user')
local config_opts = u.merge(config.lsp.servers.null_ls or {}, {
local config_opts = u.merge(user_config.lsp.servers.null_ls or {}, {
default_cosmic_sources = true,
})

View file

@ -1,77 +0,0 @@
local u = require('cosmic.utils')
local default_config = require('cosmic.lsp.providers.defaults')
local config = require('cosmic.core.user')
local lsp_installer = require('nvim-lsp-installer')
lsp_installer.settings({
ui = {
keymaps = {
-- Keymap to expand a server in the UI
toggle_server_expand = 'i',
-- Keymap to install a server
install_server = '<CR>',
-- Keymap to reinstall/update a server
update_server = 'u',
-- Keymap to uninstall a server
uninstall_server = 'x',
},
},
})
-- initial default servers
-- by default tsserver/ts_utils and null_ls are enabled
local requested_servers = {}
-- get disabled servers from config
local disabled_servers = {}
for config_server, config_opt in pairs(config.lsp.servers) do
if config_opt == false then
table.insert(disabled_servers, config_server)
elseif not vim.tbl_contains(requested_servers, config_server) then
-- add additonally defined servers to be installed
table.insert(requested_servers, config_server)
end
end
-- go through requested_servers and ensure installation
local lsp_installer_servers = require('nvim-lsp-installer.servers')
for _, requested_server in pairs(requested_servers) do
local ok, server = lsp_installer_servers.get_server(requested_server)
if ok then
if not server:is_installed() then
server:install()
end
end
end
lsp_installer.on_server_ready(function(server)
local opts = default_config
-- disable server if config disabled server list says so
opts.autostart = true
if vim.tbl_contains(disabled_servers, server.name) then
opts.autostart = false
end
-- set up default cosmic options
if server.name == 'tsserver' then
opts = u.merge(opts, require('cosmic.lsp.providers.tsserver'))
elseif server.name == 'jsonls' then
opts = u.merge(opts, require('cosmic.lsp.providers.jsonls'))
elseif server.name == 'pyright' then
opts = u.merge(opts, require('cosmic.lsp.providers.pyright'))
elseif server.name == 'sumneko_lua' then
opts = u.merge(opts, require('cosmic.lsp.providers.sumneko_lua'))
end
-- override options if user definds them
if type(config.lsp.servers[server.name]) == 'table' then
if config.lsp.servers[server.name].opts ~= nil then
opts = u.merge(opts, config.lsp.servers[server.name].opts)
end
end
-- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart)
server:setup(opts)
vim.cmd([[ do User LspAttachBuffers ]])
end)

View file

@ -1,9 +0,0 @@
local user_config = require('cosmic.core.user')
return {
'williamboman/nvim-lsp-installer',
config = function()
require('cosmic.plugins.nvim-lsp-installer.config')
end,
event = 'BufEnter',
enabled = not vim.tbl_contains(user_config.disable_builtin_plugins, 'nvim-lsp-installer'),
}

View file

@ -39,7 +39,7 @@ Full featured native LSP functionality!
- Explore files via [nvim-tree](https://github.com/kyazdani42/nvim-tree.lua)
- Fuzzy finder via [Telescope](https://github.com/nvim-telescope/telescope.nvim)
- Floating terminal with [vim-floaterm](https://github.com/voldikss/vim-floaterm)
- Auto LSP installation via [nvim-lsp-installer](https://github.com/williamboman/nvim-lsp-installer)
- Auto LSP/formatter/linter installation via [mason.nvim](https://github.com/williamboman/mason.nvim)
- Autocompletion via [nvim-cmp](https://github.com/hrsh7th/nvim-cmp)
- Additional TypeScript support via [nvim-lsp-ts-utils](https://github.com/jose-elias-alvarez/nvim-lsp-ts-utils)
- Snippet support via [LuaSnip](https://github.com/L3MON4D3/LuaSnip)