replace efm with null-ls, fix formatting/linting

This commit is contained in:
Ray Elliott 2022-01-22 16:58:29 +00:00
parent 81cdb5b2e1
commit 1509f9fbd9
4 changed files with 130 additions and 94 deletions

View File

@ -51,6 +51,15 @@ Plug '~/nvim-paper-tonic'
" https://github.com/neovim/nvim-lspconfig/wiki " https://github.com/neovim/nvim-lspconfig/wiki
Plug 'neovim/nvim-lspconfig' Plug 'neovim/nvim-lspconfig'
"}}} "}}}
" #plenary.nvim {{{
" https://github.com/nvim-lua/plenary.nvim
" required for null-ls
Plug 'nvim-lua/plenary.nvim'
"}}}
" #null-ls.nvim {{{
" https://github.com/jose-elias-alvarez/null-ls.nvim
Plug 'jose-elias-alvarez/null-ls.nvim'
"}}}
" #nvim-cmp {{{ " #nvim-cmp {{{
" https://github.com/hrsh7th/nvim-cmp/ " https://github.com/hrsh7th/nvim-cmp/
" completion " completion
@ -318,7 +327,8 @@ runtime macros/matchit.vim
lua <<EOF lua <<EOF
require('init-plugins') require('init-plugins')
require('init-treesitter') require('init-treesitter')
require('init-efm-langserver') -- require('init-efm-langserver')
require('init-null-ls')
require('init-lspconfig') require('init-lspconfig')
require('init-cmp') require('init-cmp')
EOF EOF

View File

@ -1,70 +0,0 @@
-- " npm install -g eslint_d
local eslint = {
lintCommand = "eslint_d -f unix --stdin --stdin-filename ${INPUT}",
lintStdin = true,
lintFormats = {"%f:%l:%c: %m"},
lintIgnoreExitCode = true,
formatCommand = "eslint_d --fix-to-stdout --stdin --stdin-filename=${INPUT}",
formatStdin = true
}
local shellcheck = {
lintCommand = "shellcheck -f gcc -x",
lintSource = "shellcheck",
lintFormats = {
"%f:%l:%c: %trror: %m",
"%f:%l:%c: %tarning: %m",
"%f:%l:%c: %tote: %m",
}
}
local flake8 = {
lintCommand = "flake8 --stdin-display-name ${INPUT} -",
lintStdin = true,
lintFormats = {"%f:%l:%c: %m"}
}
local black = {
formatCommand = "black --quiet -",
formatStdin = true
}
-- sudo pacman -Syu jq
local jq = {
lintCommand = "jq ."
}
-- npm install -g fixjson
local fixjson = {
formatCommand = "fixjson"
}
-- https://aur.archlinux.org/packages/lua-format/
local luaformat = {
formatCommand = 'lua-formatt -i',
formatStdin = true
}
local util = require "lspconfig".util
local on_attach_efm = function(client)
if client.resolved_capabilities.document_formatting then
vim.cmd [[augroup lsp_formatting]]
vim.cmd [[autocmd!]]
vim.cmd [[autocmd BufWritePre <buffer> :lua vim.lsp.buf.formatting_seq_sync({}, 1000)]]
vim.cmd [[augroup END]]
end
end
require "lspconfig".efm.setup {
init_options = {documentFormatting = true},
on_attach = on_attach_efm,
filetypes = {"javascript", "json", "sh", "python", "lua"},
root_dir = function(fname)
return util.root_pattern("tsconfig.json")(fname) or
util.root_pattern(".eslintrc.js", ".git")(fname);
end,
settings = {
rootMarkers = {".eslintrc.js", ".git/"},
languages = {
javascript = {eslint},
json = {jq, fixjson},
python = {flake8, black},
sh = {shellcheck},
lua = {luaformat}
}
}
}

View File

@ -5,24 +5,39 @@ local nvim_lsp = require('lspconfig')
local servers = { local servers = {
-- npm i -g bash-language-server -- npm i -g bash-language-server
-- TODO - don't run for sh files but have something else instead - shellcheck? -- TODO - don't run for sh files but have something else instead - shellcheck?
-- no formatting support
'bashls', 'bashls',
-- npm i -g vscode-langservers-extracted -- npm i -g vscode-langservers-extracted
-- no formatting support
'cssls', 'cssls',
-- npm install -g intelephense -- npm install -g intelephense
-- suports formatting
'intelephense', 'intelephense',
-- https://phpactor.readthedocs.io/en/master/usage/standalone.html -- https://phpactor.readthedocs.io/en/master/usage/standalone.html
'phpactor', 'phpactor',
-- npm install -g pyright -- npm install -g pyright
-- static type checker
'pyright', 'pyright',
-- https://github.com/neovim/nvim-lspconfig/wiki -- https://github.com/neovim/nvim-lspconfig/wiki
-- 'tailwindcss', -- 'tailwindcss',
-- npm install -g typescript typescript-language-server -- npm install -g typescript typescript-language-server
-- supports formatting
'tsserver', 'tsserver',
-- npm install -g vim-language-server -- npm install -g vim-language-server
-- no formatting support
'vimls', 'vimls',
-- npm install -g vls
'vuels', -- vue
-- use vetur
'yamlls', 'yamlls',
-- no formatting support
} }
-- Use an on_attach function to only map the following keys -- Use an on_attach function to only map the following keys
@ -36,33 +51,33 @@ local on_attach = function(client, bufnr)
-- Mappings. -- Mappings.
local opts = { noremap=true, silent=true } local opts = { noremap=true, silent=true }
-- See `:help vim.lsp.*` for documentation on any of the below functions -- See `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts) buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev({float = {...}})<CR>', opts)
buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts) buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next({float = {...}})<CR>', opts)
buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts) buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts) buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts) buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts) buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
buf_set_keymap('n', 'gl', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts) buf_set_keymap('n', 'gl', '<cmd>lua vim.diagnostic.open_float(buffer, {{opts}, scope="line", pos=17})<CR>', opts)
buf_set_keymap('n', 'gh', '<cmd>lua vim.lsp.buf.hover()<CR>', opts) buf_set_keymap('n', 'gh', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts) buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('n', '<space>T', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts) buf_set_keymap('n', '<space>T', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('n', '<space>l', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts) buf_set_keymap('n', '<space>l', '<cmd>lua vim.diagnostic.setloclist()<CR>', opts)
buf_set_keymap('n', '<space>q', '<cmd>lua vim.diagnostic.setqflist()<CR>', opts)
buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts) buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts) buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts) -- buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
-- buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts) -- buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
-- buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts) -- buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
-- buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts) -- buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
if client.name == 'tsserver' or client.name == 'intelephense' then if client.name == 'tsserver' or client.name == 'intelephense' then
print(client.name .. ' - disabling formatting')
client.resolved_capabilities.document_formatting = false; client.resolved_capabilities.document_formatting = false;
end end
if client.resolved_capabilities.document_formatting then if client.resolved_capabilities.document_formatting then
vim.cmd [[augroup lsp_formatting]] vim.cmd [[augroup lsp_formatting]]
vim.cmd [[autocmd!]] vim.cmd [[autocmd!]]
vim.cmd [[autocmd BufWritePre <buffer> :lua vim.lsp.buf.formatting_seq_sync({}, 1000)]] vim.cmd [[autocmd BufWritePre <buffer> :lua vim.lsp.buf.formatting_seq_sync({}, 3000)]]
vim.cmd [[augroup END]] vim.cmd [[augroup END]]
end end
end end
@ -81,18 +96,31 @@ for _, lsp in ipairs(servers) do
} }
end end
-- npm i -g stylelint-lsp local runtime_path = vim.split(package.path, ';')
nvim_lsp.stylelint_lsp.setup { table.insert(runtime_path, "lua/?.lua")
on_attach = on_attach, table.insert(runtime_path, "lua/?/init.lua")
capabilities = capabilities,
flags = { require'lspconfig'.sumneko_lua.setup {
debounce_text_changes = 150,
},
filetypes = {'css', 'scss', 'less'},
settings = { settings = {
stylelintplus = { Lua = {
autoFixOnSave = true, runtime = {
autoFixOnFormat = true, -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
} version = 'LuaJIT',
} -- Setup your lua path
path = runtime_path,
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = {'vim'},
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
},
},
} }

68
lua/init-null-ls.lua Normal file
View File

@ -0,0 +1,68 @@
local null_ls = require("null-ls")
local sources = {
-- -- prettier - using perttierd instead
-- null_ls.builtins.formatting.prettier.with({
-- extra_filetypes = { "php" },
-- prefer_local = "node_modules/.bin",
-- }),
-- -- prettierd with php
-- null_ls.builtins.formatting.prettierd.with({
-- extra_filetypes = { "php" },
-- }),
-- prettierd without php (use phpcbf instead)
null_ls.builtins.formatting.prettierd,
-- phpcs - standards wordpress
null_ls.builtins.diagnostics.phpcs.with({
condition = function(utils)
return utils.root_has_file({ "vendor/bin/phpcs" })
end,
command = "vendor/bin/phpcs",
args = { "--standard=WordPress", "--report=json", "-s", "-" },
}),
-- phpcbf - standards wordpress
null_ls.builtins.formatting.phpcbf.with({
condition = function(utils)
return utils.root_has_file({ "vendor/bin/phpcbf" })
end,
command = "vendor/bin/phpcbf",
args = { "--standard=WordPress", "-" },
}),
-- phpcs
null_ls.builtins.diagnostics.phpcs.with({
condition = function(utils)
return not utils.root_has_file({ "vendor/bin/phpcs" })
end,
}),
-- phpcbf
null_ls.builtins.formatting.phpcbf.with({
condition = function(utils)
return not utils.root_has_file({ "vendor/bin/phpcbf" })
end,
}),
-- black
null_ls.builtins.formatting.black,
-- shellcheck,
null_ls.builtins.diagnostics.shellcheck,
-- flake8
null_ls.builtins.diagnostics.flake8,
-- luaFormatter
null_ls.builtins.formatting.lua_format,
}
null_ls.setup({
sources = sources,
on_attach = function(client)
if client.resolved_capabilities.document_formatting then
vim.cmd([[
augroup lsp_formatting
autocmd! * <buffer>
autocmd BufWritePre <buffer> :lua vim.lsp.buf.formatting_seq_sync({}, 3000)
augroup END
]])
end
end,
})