feat(config): add new config file
* Added efm formatter options * Auto install lsp servers
This commit is contained in:
parent
a417d1df3d
commit
d4e314928d
5 changed files with 145 additions and 16 deletions
6
init.lua
6
init.lua
|
@ -1 +1,5 @@
|
||||||
require('cosmic')
|
local ok, err = pcall(require, 'cosmic')
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
error(string.format('Error loading core...\n\n%s', err))
|
||||||
|
end
|
||||||
|
|
13
lua/cosmic/config.lua
Normal file
13
lua/cosmic/config.lua
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
local config = {
|
||||||
|
lsp = {
|
||||||
|
format_on_save = true,
|
||||||
|
servers = {
|
||||||
|
eslint = nil,
|
||||||
|
efm = {
|
||||||
|
-- disable_formatters = {'eslint'}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
|
@ -45,4 +45,6 @@ M.root_dir = function(fname)
|
||||||
or util.root_pattern('tsconfig.json')(fname)
|
or util.root_pattern('tsconfig.json')(fname)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.autostart = true
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -1,17 +1,28 @@
|
||||||
local util = require('lspconfig').util
|
local util = require('lspconfig').util
|
||||||
|
local config = require('cosmic.config')
|
||||||
|
local formatters = {}
|
||||||
|
|
||||||
local stylua = {
|
formatters.stylua = {
|
||||||
formatCommand = 'stylua -s --quote-style AutoPreferSingle --indent-type Spaces --indent-width 2 -',
|
formatCommand = 'stylua -s --quote-style AutoPreferSingle --indent-type Spaces --indent-width 2 -',
|
||||||
formatStdin = true,
|
formatStdin = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
local prettier = {
|
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 = 'prettier --stdin-filepath ${INPUT}',
|
||||||
formatCommand = 'prettierd "${INPUT}"',
|
formatCommand = 'prettierd "${INPUT}"',
|
||||||
formatStdin = true,
|
formatStdin = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
local filetypes = {
|
--[[ local filetypes = {
|
||||||
css = { prettier },
|
css = { prettier },
|
||||||
html = { prettier },
|
html = { prettier },
|
||||||
lua = { stylua },
|
lua = { stylua },
|
||||||
|
@ -23,8 +34,71 @@ local filetypes = {
|
||||||
typescript = { prettier },
|
typescript = { prettier },
|
||||||
typescriptreact = { prettier },
|
typescriptreact = { prettier },
|
||||||
yaml = { prettier },
|
yaml = { prettier },
|
||||||
|
} ]]
|
||||||
|
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 function is_formatter_disabled(formatter)
|
||||||
|
if config.lsp.servers.efm.disable_formatters then
|
||||||
|
for i in pairs(config.lsp.servers.efm.disable_formatters) do
|
||||||
|
local disabled = config.lsp.servers.efm.disable_formatters[i]
|
||||||
|
if disabled == formatter then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local languages = {}
|
||||||
|
for formatter, filetypes in pairs(formatters.defaults) do
|
||||||
|
if not is_formatter_disabled(formatter) then
|
||||||
|
-- print(formatter)
|
||||||
|
for i in pairs(formatters.defaults[formatter]) do
|
||||||
|
local filetype = formatters.defaults[formatter][i]
|
||||||
|
-- print(filetype)
|
||||||
|
languages[filetype] = languages[filetype] or {}
|
||||||
|
table.insert(languages[filetype], { [formatter] = formatters[formatter] })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- print(vim.inspect(languages))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init_options = { documentFormatting = true, codeAction = true },
|
init_options = { documentFormatting = true, codeAction = true },
|
||||||
root_dir = function(fname)
|
root_dir = function(fname)
|
||||||
|
@ -34,6 +108,6 @@ return {
|
||||||
or util.root_pattern('.eslintrc.js')(fname)
|
or util.root_pattern('.eslintrc.js')(fname)
|
||||||
or util.root_pattern('tsconfig.json')(fname)
|
or util.root_pattern('tsconfig.json')(fname)
|
||||||
end,
|
end,
|
||||||
filetypes = vim.tbl_keys(filetypes),
|
filetype_defaults = vim.tbl_keys(filetype_defaults),
|
||||||
settings = { languages = filetypes },
|
settings = { languages = languages },
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
local default_config = require('cosmic.lsp.providers.defaults')
|
local default_config = require('cosmic.lsp.providers.defaults')
|
||||||
|
local config = require('cosmic.config')
|
||||||
local lsp_installer = require('nvim-lsp-installer')
|
local lsp_installer = require('nvim-lsp-installer')
|
||||||
|
|
||||||
lsp_installer.settings({
|
lsp_installer.settings({
|
||||||
|
@ -16,24 +17,59 @@ lsp_installer.settings({
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- initial default serverse
|
||||||
|
local requested_servers = {
|
||||||
|
'eslint',
|
||||||
|
'efm',
|
||||||
|
'tsserver',
|
||||||
|
'sumneko_lua',
|
||||||
|
'jsonls',
|
||||||
|
'cssls',
|
||||||
|
'html',
|
||||||
|
-- 'intelephense',
|
||||||
|
-- 'pyright',
|
||||||
|
-- 'gopls',
|
||||||
|
}
|
||||||
|
|
||||||
|
-- add servers set in config to requested_servers table
|
||||||
|
for config_server in pairs(config.lsp.servers) do
|
||||||
|
if requested_servers[config_server] == nil then
|
||||||
|
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 server in pairs(requested_servers) do
|
||||||
|
local requested_server = requested_servers[server]
|
||||||
|
|
||||||
|
local ok, serv = lsp_installer_servers.get_server(requested_server)
|
||||||
|
if ok then
|
||||||
|
if not serv:is_installed() then
|
||||||
|
serv:install()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[ Auto-format *.rs (rust) files prior to saving them
|
||||||
|
autocmd BufWritePre *.rs lua vim.lsp.buf.formatting_sync(nil, 1000) ]]
|
||||||
|
|
||||||
lsp_installer.on_server_ready(function(server)
|
lsp_installer.on_server_ready(function(server)
|
||||||
local opts = default_config
|
local opts = default_config
|
||||||
|
--[[ if config.lsp.servers[server.name] == nil then
|
||||||
|
opts.autostart = false
|
||||||
|
end ]]
|
||||||
|
|
||||||
if server.name == 'sumneko_lua' then
|
if server.name == 'sumneko_lua' then
|
||||||
local config = require('cosmic.lsp.providers.lua')
|
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.lua'))
|
||||||
opts = vim.tbl_deep_extend('force', opts, config)
|
|
||||||
elseif server.name == 'tsserver' then
|
elseif server.name == 'tsserver' then
|
||||||
local config = require('cosmic.lsp.providers.tsserver')
|
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.tsserver'))
|
||||||
opts = vim.tbl_deep_extend('force', opts, config)
|
|
||||||
elseif server.name == 'efm' then
|
elseif server.name == 'efm' then
|
||||||
local config = require('cosmic.lsp.providers.efm')
|
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.efm'))
|
||||||
opts = vim.tbl_deep_extend('force', opts, config)
|
|
||||||
elseif server.name == 'jsonls' then
|
elseif server.name == 'jsonls' then
|
||||||
local config = require('cosmic.lsp.providers.jsonls')
|
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.jsonls'))
|
||||||
opts = vim.tbl_deep_extend('force', opts, config)
|
|
||||||
elseif server.name == 'eslint' then
|
elseif server.name == 'eslint' then
|
||||||
local config = require('cosmic.lsp.providers.eslint')
|
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.eslint'))
|
||||||
opts = vim.tbl_deep_extend('force', opts, config)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart)
|
-- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart)
|
||||||
|
|
Loading…
Add table
Reference in a new issue