feat(lsp): trade efm and eslint for null-ls

This commit is contained in:
Matt Leong 2021-11-09 18:53:52 -08:00
parent c11986eaa5
commit 3aab287794
10 changed files with 67 additions and 128 deletions

View file

@ -1,6 +1,8 @@
-- Override Cosmic configuration options
--[[
-- You can require null-ls if needed
-- local null_ls = require('null-ls')
local config = {}
@ -39,7 +41,7 @@ config.lsp = {
rust_analyzer = true,
tsserver = {
-- disable formatting
-- disable formatting (defaults to true)
format = false,
-- OR add/override server options
opts = {
@ -50,12 +52,12 @@ config.lsp = {
}
},
efm = {
-- specifc to efm, e.g. 'eslint', 'prettier', 'stylua'
disable_formatters = {'eslint'},
-- See Cosmic defaults lsp/providers/null_ls.lua
null_ls = {}
},
},
-- See Cosmic defaults lsp/providers/tsserver.lua
ts_utils = {}
}
return config

View file

@ -22,17 +22,13 @@ local default_config = {
},
]]
-- enable, but disable formatting
eslint = {
format = false,
},
efm = {
format = true,
disable_formatters = { 'eslint' }, -- e.g. 'eslint', 'prettier', 'stylua'
sumneko_lua = {
format = false, -- disable formatting all together
},
tsserver = {
format = false, -- disable formatting all together
},
null_ls = {},
},
},
}
@ -48,7 +44,7 @@ function config.lsp.can_client_format(client_name)
return (config.lsp.servers[client_name].format == true)
end
return false
return true
end
return config

View file

@ -19,6 +19,7 @@ map('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>')
map('n', '<leader>ga', '<cmd>lua require("telescope.builtin").lsp_code_actions()<cr>')
map('v', '<leader>ga', '<cmd>lua require("telescope.builtin").lsp_range_code_actions()<cr>')
map('n', '<leader>gf', '<cmd>lua vim.lsp.buf.formatting()<cr>')
map('v', '<leader>gf', '<cmd>lua vim.lsp.buf.range_formatting()<cr>')
map('n', '<C-K>', '<cmd>lua require("lsp_signature").signature()<cr>')
-- lsp workspace

View file

@ -49,7 +49,15 @@ M.flags = {
debounce_text_changes = 150,
}
M.capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
local function get_capabilities()
local ok, cmp_nvim_lsp = pcall(require, 'cmp_nvim_lsp')
if not ok then
return {}
end
return cmp_nvim_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities())
end
M.capabilities = get_capabilities()
M.root_dir = function(fname)
local util = require('lspconfig').util

View file

@ -1,84 +0,0 @@
local util = require('lspconfig').util
local config = require('cosmic.config')
local formatters = {}
formatters.stylua = {
formatCommand = 'stylua -s --quote-style AutoPreferSingle --indent-type Spaces --indent-width 2 -',
formatStdin = true,
}
formatters.eslint = {
lintCommand = 'eslint_d -f unix --stdin --stdin-filename ${INPUT}',
lintStdin = true,
lintFormats = { '%f:%l:%c: %m' },
lintIgnoreExitCode = true,
formatCommand = 'eslint_d --stdin --fix-to-stdout --stdin-filename=${INPUT}',
formatStdin = true,
}
formatters.prettier = {
-- formatCommand = 'prettier --stdin-filepath ${INPUT}',
formatCommand = 'prettierd "${INPUT}"',
formatStdin = true,
}
local filetype_defaults = {
'css',
'html',
'lua',
'javascript',
'javascriptreact',
'json',
'markdown',
'scss',
'typescript',
'typescriptreact',
'yaml',
}
formatters.defaults = {
eslint = {
'javascript',
'javascriptreact',
'json',
'typescriptreact',
'typescript',
},
prettier = {
'css',
'html',
'javascript',
'javascriptreact',
'json',
'typescriptreact',
'typescript',
'markdown',
'scss',
'yaml',
},
stylua = { 'lua' },
}
local languages = {}
for formatter, filetypes in pairs(formatters.defaults) do
-- disable specific formatters
if not vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then
for _, filetype in pairs(filetypes) do
languages[filetype] = languages[filetype] or {}
table.insert(languages[filetype], formatters[formatter])
end
end
end
return {
init_options = { documentFormatting = true, codeAction = true },
root_dir = function(fname)
return util.root_pattern('.git')(fname)
or util.root_pattern('tsconfig.base.json')(fname)
or util.root_pattern('package.json')(fname)
or util.root_pattern('.eslintrc.js')(fname)
or util.root_pattern('tsconfig.json')(fname)
end,
filetypes = filetype_defaults,
settings = { languages = languages },
}

View file

@ -1,15 +0,0 @@
local util = require('lspconfig').util
return {
settings = {
packageManager = 'npm',
format = true,
},
root_dir = function(fname)
return util.root_pattern('.git')(fname)
or util.root_pattern('tsconfig.base.json')(fname)
or util.root_pattern('package.json')(fname)
or util.root_pattern('.eslintrc.js')(fname)
or util.root_pattern('tsconfig.json')(fname)
end,
}

View file

@ -19,8 +19,8 @@ lsp_installer.settings({
-- initial default serverse
local requested_servers = {
'eslint',
'efm',
-- 'eslint',
-- 'efm',
'tsserver',
'sumneko_lua',
'jsonls',
@ -63,14 +63,10 @@ lsp_installer.on_server_ready(function(server)
-- set up default cosmic options
if server.name == 'tsserver' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.tsserver'))
elseif server.name == 'efm' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.efm'))
elseif server.name == 'jsonls' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.jsonls'))
elseif server.name == 'sumneko_lua' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.lua'))
elseif server.name == 'eslint' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.eslint'))
end
-- override options if user definds them

View file

@ -0,0 +1,26 @@
local config = require('cosmic.config')
local on_attach = require('cosmic.lsp.providers.defaults').on_attach
local null_ls = require('null-ls')
null_ls.config(vim.tbl_deep_extend('force', {
-- you must define at least one source for the plugin to work
sources = {
null_ls.builtins.formatting.stylua.with({
args = {
'-s',
'--quote-style',
'AutoPreferSingle',
'--indent-type',
'Spaces',
'--indent-width',
'2',
'-',
},
}),
null_ls.builtins.code_actions.gitsigns,
},
}, config.lsp.servers.null_ls))
require('lspconfig')['null-ls'].setup({
on_attach = on_attach,
})

View file

@ -1,4 +1,5 @@
local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach
local config = require('cosmic.config')
local M = {}
function M.on_attach(client, bufnr)
@ -7,7 +8,7 @@ function M.on_attach(client, bufnr)
local ts_utils = require('nvim-lsp-ts-utils')
-- defaults
ts_utils.setup({
ts_utils.setup(vim.tbl_deep_extend('force', {
debug = false,
disable_commands = false,
enable_import_on_completion = true,
@ -24,16 +25,16 @@ function M.on_attach(client, bufnr)
import_all_select_source = false,
-- eslint
eslint_enable_code_actions = false,
eslint_enable_disable_comments = false,
eslint_enable_code_actions = true,
eslint_enable_disable_comments = true,
eslint_bin = 'eslint_d',
eslint_enable_diagnostics = false,
eslint_opts = {},
eslint_enable_diagnostics = true,
-- eslint_opts = {},
-- formatting
enable_formatting = false,
enable_formatting = true,
formatter = 'prettierd',
formatter_opts = {},
-- formatter_opts = {},
-- update imports on file move
update_imports_on_move = true,
@ -43,7 +44,7 @@ function M.on_attach(client, bufnr)
-- filter diagnostics
filter_out_diagnostics_by_severity = {},
filter_out_diagnostics_by_code = {},
})
}, config.lsp.ts_utils or {}))
-- required to fix code action ranges and filter diagnostics
ts_utils.setup_client(client)

View file

@ -115,6 +115,14 @@ return packer.startup(function()
},
})
use({
'jose-elias-alvarez/null-ls.nvim',
config = function()
require('cosmic.lsp.providers.null_ls')
end,
requires = { 'nvim-lua/plenary.nvim', 'neovim/nvim-lspconfig' },
})
use({
'L3MON4D3/LuaSnip',
config = function()