Skip to content

Instantly share code, notes, and snippets.

@carbolymer
Last active September 21, 2023 13:00
Show Gist options
  • Save carbolymer/f0e10a3bbd07a022f8eccf1c52e65356 to your computer and use it in GitHub Desktop.
Save carbolymer/f0e10a3bbd07a022f8eccf1c52e65356 to your computer and use it in GitHub Desktop.
LSP config for HLS
local lsp = require 'lspconfig'
local tb = require 'telescope.builtin'
local setup_diagnostics = function()
local ns = vim.api.nvim_create_namespace('severe-diagnostics')
-- reference to the original handler
local orig_signs_handler = vim.diagnostic.handlers.signs
---Overriden diagnostics signs helper to only show the single most relevant sign
---@see `:h diagnostic-handlers`
vim.diagnostic.handlers.signs = {
show = function(_, bufnr, _, opts)
-- get all diagnostics from the whole buffer rather
-- than just the diagnostics passed to the handler
local diagnostics = vim.diagnostic.get(bufnr)
local max_severity_per_line = {}
for _, d in pairs(diagnostics) do
local m = max_severity_per_line[d.lnum]
if not m or d.severity < m.severity then
max_severity_per_line[d.lnum] = d
end
end
-- Pass the filtered diagnostics (with our custom namespace) to
-- the original handler
local filtered_diagnostics = vim.tbl_values(max_severity_per_line)
local success, result = pcall(function()
-- pass the filtered diagnostics (with the
-- custom namespace) to the original handler
orig_signs_handler.show(ns, bufnr, filtered_diagnostics, opts)
end)
if not success then
print(ns, bufnr, vim.inspect(filtered_diagnostics), vim.inspect(opts), 'Showing max diagnostic failed', result)
end
end,
hide = function(_, bufnr)
orig_signs_handler.hide(ns, bufnr)
end,
}
end
local keymap = vim.keymap.set
local def_opts = { noremap = true, silent = true, }
local on_attach = function(client, bufnr)
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
local bufopts = vim.tbl_extend('keep', def_opts, { buffer = bufnr })
keymap('n', 'K', vim.lsp.buf.hover, bufopts)
keymap('n', '<leader>k', vim.diagnostic.open_float, bufopts)
keymap('n', 'gd', tb.lsp_definitions, bufopts)
keymap('n', '<leader>go', tb.lsp_outgoing_calls, bufopts)
keymap('n', '<leader>gi', tb.lsp_incoming_calls, bufopts)
keymap('n', '<leader>ac', vim.lsp.buf.code_action, bufopts)
keymap('n', '<leader>la', vim.lsp.codelens.run, bufopts)
keymap('n', '<leader>rn', vim.lsp.buf.rename, bufopts)
keymap('n', '<leader>F', function() vim.lsp.buf.format {async = true} end, bufopts)
keymap('v', '<leader>F', vim.lsp.buf.format, bufopts)
keymap('n', '<leader>ls', tb.lsp_document_symbols, bufopts)
keymap('n', '<leader>lw', tb.lsp_workspace_symbols, bufopts)
keymap('n', '<leader>lq', tb.lsp_dynamic_workspace_symbols, bufopts)
keymap('n', '<leader>rf', tb.lsp_references, bufopts)
keymap('n', '<leader>dn', vim.diagnostic.goto_next, bufopts)
keymap('n', '<leader>dp', vim.diagnostic.goto_prev, bufopts)
keymap('n', '<leader>lo', '<cmd>:SymbolsOutline<CR>', bufopts)
end
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- Haskell language server via haskell-tools
vim.g.haskell_tools = {
tools = {
hover = { enable = false }
},
hls = {
on_attach = function(client, bufnr, ht)
on_attach(client, bufnr)
local bufopts = vim.tbl_extend('keep', def_opts, { buffer = bufnr, })
keymap('n', '<leader>hs', ht.hoogle.hoogle_signature, bufopts)
-- Toggle a GHCi repl for the current package
keymap('n', '<leader>rp', ht.repl.toggle, bufopts)
-- Toggle a GHCi repl for the current buffer
keymap('n', '<leader>rb', function()
ht.repl.toggle(vim.api.nvim_buf_get_name(0))
end, bufopts)
keymap('n', '<leader>rq', ht.repl.quit, bufopts)
end,
default_settings = {
-- project checking leads to poor performance on large projects
-- checkProject = false,
}
}
}
local signs = { Error = ' ', Warn = ' ', Hint = ' ', Info = ' ' }
for type, icon in pairs(signs) do
local hl = 'DiagnosticSign' .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
vim.cmd[[ hi link LspCodeLens FloatBorder ]]
-- display only one sign with the highest severity
setup_diagnostics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment