Browse Source

feat: Refactored langs boilerplate into helper functions

my-config
connorgmeean 3 years ago
parent
commit
b009d4f6d0
  1. 24
      lua/doom/modules/css/config.lua
  2. 25
      lua/doom/modules/javascript/config.lua
  3. 46
      lua/doom/modules/langs_utils.lua
  4. 27
      lua/doom/modules/lua/config.lua
  5. 40
      lua/doom/modules/rust/config.lua
  6. 25
      lua/doom/modules/typescript/config.lua
  7. 72
      lua/doom/modules/vue/config.lua

24
lua/doom/modules/css/config.lua

@ -1,8 +1,9 @@
local utils = require("doom.utils")
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local langs_utils = require('doom.modules.langs_utils')
local config = vim.tbl_deep_extend("force", doom.lua, {
local config = vim.tbl_deep_extend("force", {
capabilities = utils.get_capabilities(),
on_attach = function(client)
if not is_plugin_disabled("illuminate") then
@ -14,24 +15,9 @@ local config = vim.tbl_deep_extend("force", doom.lua, {
end,
})
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server(doom.css.language_server_name)
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
server:setup(config)
end)
end
else
lspconfig.sumneko_lua.setup(config)
end
langs_utils.use_lsp(doom.css.language_server_name, {
config = config,
})
vim.defer_fn(function()
require("nvim-treesitter.install").ensure_installed("css")

25
lua/doom/modules/javascript/config.lua

@ -1,8 +1,8 @@
local utils = require("doom.utils")
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local langs_utils = require('doom.modules.langs_utils')
local config = vim.tbl_deep_extend("force", doom.lua, {
local config = vim.tbl_deep_extend("force", {
capabilities = utils.get_capabilities(),
on_attach = function(client)
if not is_plugin_disabled("illuminate") then
@ -14,24 +14,9 @@ local config = vim.tbl_deep_extend("force", doom.lua, {
end,
})
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server("tsserver")
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
server:setup(config)
end)
end
else
lspconfig.sumneko_lua.setup(config)
end
langs_utils.use_lsp('tsserver', {
config = config,
})
vim.defer_fn(function()
require("nvim-treesitter.install").update()("javascript")

46
lua/doom/modules/langs_utils.lua

@ -22,4 +22,50 @@ module.use_null_ls_source = function(sources)
end
end
module.use_lsp = function(lsp_name, opts)
local lsp = require('lspconfig')
local lsp_configs = require("lspconfig.configs")
-- Apply or merge lsp configs
local config_name = opts.name and opts.name or lsp_name
if opts.config then
if lsp_configs[config_name] then
lsp_configs[config_name] = vim.tbl_deep_extend('force', lsp_configs[config_name], opts.config)
else
lsp_configs[config_name] = opts.config
end
end
-- Start server and bind to buffers
local start_lsp = function()
lsp[config_name].setup({})
local server = lsp[config_name]
local buffer_handler = server.filetypes and server.manager.try_add_wrapper or server.manager.try_add
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
buffer_handler(bufnr)
end
end
-- Auto install if possible
if not utils.is_plugin_disabled('auto_install') and not opts.no_installer then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server(lsp_name)
if server_available then
lsp_configs[config_name].cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
start_lsp()
end)
end
else
start_lsp()
end
end
return module

27
lua/doom/modules/lua/config.lua

@ -1,6 +1,6 @@
local utils = require("doom.utils")
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local langs_utils = require('doom.modules.langs_utils')
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
@ -24,28 +24,10 @@ local config = vim.tbl_deep_extend("force", doom.lua, {
end
end,
})
local start_lsp = function()
lspconfig.sumneko_lua.setup(config)
end
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server("sumneko_lua")
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
start_lsp()
end)
end
else
start_lsp()
end
langs_utils.use_lsp('sumneko_lua', {
config = config,
})
vim.defer_fn(function()
require("nvim-treesitter.install").ensure_installed("lua")
@ -53,7 +35,6 @@ end, 0)
-- Setup null-ls
if doom.linter then
local langs_utils = require('doom.modules.langs_utils')
local null_ls = require("null-ls")
langs_utils.use_null_ls_source({

40
lua/doom/modules/rust/config.lua

@ -1,19 +1,8 @@
local utils = require("doom.utils")
local langs_utils = require('doom.modules.langs_utils')
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")
local config = vim.tbl_deep_extend("force", doom.lua, {
settings = {
Lua = {
runtime = {
path = runtime_path,
},
},
},
local config = vim.tbl_deep_extend("force", {
capabilities = utils.get_capabilities(),
on_attach = function(client)
if not is_plugin_disabled("illuminate") then
@ -24,28 +13,10 @@ local config = vim.tbl_deep_extend("force", doom.lua, {
end
end,
})
local start_lsp = function()
lspconfig.rust_analyzer.setup(config)
end
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server("rust_analyzer")
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
start_lsp()
end)
end
else
start_lsp()
end
langs_utils.use_lsp('rust_analyzer', {
config = config,
})
vim.defer_fn(function()
require("nvim-treesitter.install").ensure_installed("rust")
@ -53,7 +24,6 @@ end, 0)
-- Setup null-ls
if doom.linter then
local langs_utils = require('doom.modules.langs_utils')
local null_ls = require("null-ls")
langs_utils.use_null_ls_source({

25
lua/doom/modules/typescript/config.lua

@ -1,8 +1,8 @@
local utils = require("doom.utils")
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local langs_utils = require('doom.modules.langs_utils')
local config = vim.tbl_deep_extend("force", doom.lua, {
local config = vim.tbl_deep_extend("force", {
capabilities = utils.get_capabilities(),
on_attach = function(client)
if not is_plugin_disabled("illuminate") then
@ -14,24 +14,9 @@ local config = vim.tbl_deep_extend("force", doom.lua, {
end,
})
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server("tsserver")
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
server:setup(config)
end)
end
else
lspconfig.sumneko_lua.setup(config)
end
langs_utils.use_lsp('tsserver', {
config = config,
})
vim.defer_fn(function()
require("nvim-treesitter.install").update()("typescript")

72
lua/doom/modules/vue/config.lua

@ -1,8 +1,8 @@
local utils = require("doom.utils")
local is_plugin_disabled = utils.is_plugin_disabled
local lspconfig = require("lspconfig")
local lspconfig_configs = require("lspconfig.configs")
local lspconfig_util = require("lspconfig/util")
local langs_utils = require('doom.modules.langs_utils')
-- volar needs works with typescript server, needs to get the typescript server from the project's node_modules
local function on_new_config(new_config, new_root_dir)
@ -30,22 +30,17 @@ end
local volar_root_dir = lspconfig_util.root_pattern("package.json")
-- Runtime config with extra capabilities
local config = {
capabilities = utils.get_capabilities(),
local config = langs_utils.lsp_ensure_client_capabilities({
on_attach = function(client)
if not is_plugin_disabled("illuminate") then
utils.illuminate_attach(client)
end
if type(doom.lua.on_attach) == "function" then
doom.lua.on_attach(client)
end
end,
}
})
local volar = lspconfig.volar -- Get the volar config to set the `cmd`
-- Contains base configuration necessary for volar to start
local base_config = {
local base_config = vim.tbl_deep_extend('keep', config, {
default_config = {
cmd = volar.document_config.default_config.cmd,
root_dir = volar_root_dir,
@ -56,52 +51,28 @@ local base_config = {
},
},
},
}
})
lspconfig_configs.volar_api = vim.tbl_deep_extend("keep", base_config, doom.vue.volar_api)
lspconfig_configs.volar_doc = vim.tbl_deep_extend("keep", base_config, doom.vue.volar_doc)
lspconfig_configs.volar_html = vim.tbl_deep_extend("keep", base_config, doom.vue.volar_html)
--
local start_lsp = function()
lspconfig.volar_api.setup(config)
lspconfig.volar_doc.setup(config)
lspconfig.volar_html.setup(config)
end
local volar_api_config = vim.tbl_deep_extend('force', {}, doom.vue.volar_api, base_config)
langs_utils.use_lsp('volar', {
name = 'volar_api',
config = volar_api_config,
})
local attach_buffers = function()
local servers = { "volar_api", "volar_doc", "volar_html" }
for _, server_name in ipairs(servers) do
local lsp_server = require("lspconfig")[server_name]
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if lsp_server.filetypes then
lsp_server.manager.try_add_wrapper(bufnr)
else
lsp_server.manager.try_add(bufnr)
end
end
end
end
local volar_doc_config = vim.tbl_deep_extend('force', {}, doom.vue.volar_doc, base_config)
langs_utils.use_lsp('volar', {
name = 'volar_doc',
config = volar_doc_config,
})
local volar_html_config = vim.tbl_deep_extend('force', {}, doom.vue.volar_html, base_config)
langs_utils.use_lsp('volar', {
name = 'volar_html',
config = volar_html_config,
})
if not is_plugin_disabled("auto_install") then
local lsp_installer = require("nvim-lsp-installer.servers")
local server_available, server = lsp_installer.get_server("volar")
if server_available then
config.cmd_env = server:get_default_options().cmd_env
if not server:is_installed() then
vim.defer_fn(function()
server:install()
end, 50)
end
server:on_ready(function()
start_lsp()
attach_buffers()
end)
end
else
start_lsp()
end
vim.defer_fn(function()
local ts_install = require("nvim-treesitter.install")
@ -110,7 +81,6 @@ end, 0)
-- Setup null-ls
if doom.linter then
local langs_utils = require('doom.modules.langs_utils')
local null_ls = require("null-ls")
langs_utils.use_null_ls_source({

Loading…
Cancel
Save