1
0
Fork 0

Improved null-ls integration

This commit is contained in:
Daniel Kempkens 2021-12-18 23:55:56 +01:00
parent 8ce9e36a82
commit 86d90510f9
4 changed files with 51 additions and 41 deletions

View file

@ -1,37 +1,18 @@
local lsp = require('lspconfig') local lsp = require('lspconfig')
local lsp_status = require('lsp-status') local lsp_status = require('lsp-status')
local illuminate = require('illuminate') local illuminate = require('illuminate')
local keymap = require('nifoc.keymap') local diagnostic_utils = require('nifoc.utils.diagnostic')
local function enable_lsp_fixer(_)
vim.cmd [[
augroup nifoc_lsp_formatting
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync(nil, 1000)
augroup end
]]
end
local function custom_attach(client, bufnr) local function custom_attach(client, bufnr)
-- Plugin attachments -- Plugin attachments
lsp_status.on_attach(client) lsp_status.on_attach(client)
keymap.lsp_attach(client, bufnr)
if client.resolved_capabilities.document_highlight then if client.resolved_capabilities.document_highlight then
illuminate.on_attach(client) illuminate.on_attach(client)
end end
if vim.b.nifoc_lsp_enabled == nil then diagnostic_utils.maybe_enable_lsp(client, bufnr)
vim.api.nvim_buf_set_var(bufnr, 'nifoc_lsp_enabled', 1) diagnostic_utils.maybe_enable_fixer(client, bufnr)
vim.cmd("setlocal omnifunc=v:lua.vim.lsp.omnifunc")
end
if client.resolved_capabilities.document_formatting and vim.b.nifoc_fixer_enabled == nil then
vim.api.nvim_buf_set_var(bufnr, 'nifoc_fixer_enabled', 1)
enable_lsp_fixer(client)
end
vim.api.nvim_command('doautocmd <nomodeline> User NifocLspAttached') vim.api.nvim_command('doautocmd <nomodeline> User NifocLspAttached')
end end
@ -42,22 +23,19 @@ end
-- Setup -- Setup
vim.cmd('sign define LspDiagnosticsSignError text= texthl=LspDiagnosticsSignError linehl= numhl=') vim.cmd('sign define DiagnosticSignError text= texthl=DiagnosticSignError linehl= numhl=')
vim.cmd('sign define LspDiagnosticsSignWarning text= texthl=LspDiagnosticsSignWarning linehl= numhl=') vim.cmd('sign define DiagnosticSignWarn text= texthl=DiagnosticSignWarn linehl= numhl=')
vim.cmd('sign define LspDiagnosticsSignInformation text= texthl=LspDiagnosticsSignInformation linehl= numhl=') vim.cmd('sign define DiagnosticSignInfo text= texthl=DiagnosticSignInfo linehl= numhl=')
vim.cmd('sign define LspDiagnosticsSignHint text= texthl=LspDiagnosticsSignHint linehl= numhl=') vim.cmd('sign define DiagnosticSignHint text= texthl=DiagnosticSignHint linehl= numhl=')
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( vim.diagnostic.config({
vim.lsp.diagnostic.on_publish_diagnostics, {
underline = true, underline = true,
virtual_text = { virtual_text = {
prefix = '', source = 'if_many',
spacing = 4,
}, },
signs = false, signs = false,
update_in_insert = false, update_in_insert = false,
} })
)
lsp_status.register_progress() lsp_status.register_progress()

View file

@ -1,5 +1,6 @@
local null_ls = require('null-ls') local null_ls = require('null-ls')
local builtins = null_ls.builtins local builtins = null_ls.builtins
local diagnostic_utils = require('nifoc.utils.diagnostic')
null_ls.setup({ null_ls.setup({
sources = { sources = {
@ -9,4 +10,9 @@ null_ls.setup({
builtins.diagnostics.shellcheck.with({extra_args = { '-f', 'gcc', '-x' }}), builtins.diagnostics.shellcheck.with({extra_args = { '-f', 'gcc', '-x' }}),
builtins.formatting.shfmt.with({extra_args = { '-i', '2' }}), builtins.formatting.shfmt.with({extra_args = { '-i', '2' }}),
}, },
on_attach = function(client, bufnr)
diagnostic_utils.maybe_enable_lsp(client, bufnr)
diagnostic_utils.maybe_enable_fixer(client, bufnr)
end,
}) })

View file

@ -154,11 +154,11 @@ function M.lsp_attach(client, bufnr)
d = { d = {
name = "diagnostics", name = "diagnostics",
d = { d = {
"<cmd>LspTroubleToggle lsp_document_diagnostics<CR>", "<cmd>TroubleToggle document_diagnostics<CR>",
"Document Diagnostics" "Document Diagnostics"
}, },
w = { w = {
"<cmd>LspTroubleToggle lsp_workspace_diagnostics<CR>", "<cmd>TroubleToggle workspace_diagnostics<CR>",
"Workspace Diagnostics" "Workspace Diagnostics"
}, },
}, },
@ -172,11 +172,11 @@ function M.lsp_attach(client, bufnr)
f = { f = {
name = "find", name = "find",
r = { r = {
"<cmd>LspTroubleToggle lsp_references<CR>", "<cmd>TroubleToggle lsp_references<CR>",
"References" "References"
}, },
d = { d = {
"<cmd>LspTroubleToggle lsp_definitions<CR>", "<cmd>TroubleToggle lsp_definitions<CR>",
"Definitions" "Definitions"
}, },
}, },

View file

@ -0,0 +1,26 @@
local M = {}
local keymap = require('nifoc.keymap')
function M.maybe_enable_lsp(client, bufnr)
if vim.b.nifoc_lsp_enabled == nil then
vim.api.nvim_buf_set_var(bufnr, 'nifoc_lsp_enabled', 1)
keymap.lsp_attach(client, bufnr)
end
end
function M.maybe_enable_fixer(client, bufnr)
if client.resolved_capabilities.document_formatting and vim.b.nifoc_fixer_enabled == nil then
vim.api.nvim_buf_set_var(bufnr, 'nifoc_fixer_enabled', 1)
vim.cmd [[
augroup nifoc_diagnostic_formatting
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync(nil, 1000)
augroup end
]]
end
end
return M