mirror of
https://github.com/LunarVim/LunarVim.git
synced 2025-07-12 18:34:41 +02:00
begin lsp refactor
This commit is contained in:
parent
9611a50acc
commit
f220dc4893
6 changed files with 184 additions and 181 deletions
2
init.lua
2
init.lua
|
@ -35,7 +35,7 @@ utils.toggle_autoformat()
|
||||||
local commands = require "core.commands"
|
local commands = require "core.commands"
|
||||||
commands.load(commands.defaults)
|
commands.load(commands.defaults)
|
||||||
|
|
||||||
require("lsp").setup_handlers()
|
require("lsp").config()
|
||||||
|
|
||||||
local null_status_ok, null_ls = pcall(require, "null-ls")
|
local null_status_ok, null_ls = pcall(require, "null-ls")
|
||||||
if null_status_ok then
|
if null_status_ok then
|
||||||
|
|
21
lua/lsp/handlers.lua
Normal file
21
lua/lsp/handlers.lua
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
-- Set Default Prefix.
|
||||||
|
-- Note: You can set a prefix per lsp server in the lv-globals.lua file
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.setup()
|
||||||
|
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
|
||||||
|
virtual_text = lvim.lsp.diagnostics.virtual_text,
|
||||||
|
signs = lvim.lsp.diagnostics.signs,
|
||||||
|
underline = lvim.lsp.document_highlight,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
|
||||||
|
border = lvim.lsp.popup_border,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
|
||||||
|
border = lvim.lsp.popup_border,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
188
lua/lsp/init.lua
188
lua/lsp/init.lua
|
@ -1,34 +1,10 @@
|
||||||
local lsp_config = {}
|
local lsp_config = {}
|
||||||
|
|
||||||
vim.fn.sign_define(
|
function lsp_config.config()
|
||||||
"LspDiagnosticsSignError",
|
require("lsp.kind").setup()
|
||||||
{ texthl = "LspDiagnosticsSignError", text = "", numhl = "LspDiagnosticsSignError" }
|
require("lsp.handlers").setup()
|
||||||
)
|
require("lsp.signs").setup()
|
||||||
vim.fn.sign_define(
|
end
|
||||||
"LspDiagnosticsSignWarning",
|
|
||||||
{ texthl = "LspDiagnosticsSignWarning", text = "", numhl = "LspDiagnosticsSignWarning" }
|
|
||||||
)
|
|
||||||
vim.fn.sign_define(
|
|
||||||
"LspDiagnosticsSignHint",
|
|
||||||
{ texthl = "LspDiagnosticsSignHint", text = "", numhl = "LspDiagnosticsSignHint" }
|
|
||||||
)
|
|
||||||
vim.fn.sign_define(
|
|
||||||
"LspDiagnosticsSignInformation",
|
|
||||||
{ texthl = "LspDiagnosticsSignInformation", text = "", numhl = "LspDiagnosticsSignInformation" }
|
|
||||||
)
|
|
||||||
|
|
||||||
-- local opts = { border = "single" }
|
|
||||||
-- TODO revisit this
|
|
||||||
-- local border = {
|
|
||||||
-- { "🭽", "FloatBorder" },
|
|
||||||
-- { "▔", "FloatBorder" },
|
|
||||||
-- { "🭾", "FloatBorder" },
|
|
||||||
-- { "▕", "FloatBorder" },
|
|
||||||
-- { "🭿", "FloatBorder" },
|
|
||||||
-- { "▁", "FloatBorder" },
|
|
||||||
-- { "🭼", "FloatBorder" },
|
|
||||||
-- { "▏", "FloatBorder" },
|
|
||||||
-- }
|
|
||||||
|
|
||||||
-- My font didn't like this :/
|
-- My font didn't like this :/
|
||||||
-- vim.api.nvim_set_keymap(
|
-- vim.api.nvim_set_keymap(
|
||||||
|
@ -51,170 +27,22 @@ function lsp_config.setup_default_bindings()
|
||||||
{ noremap = true, silent = true }
|
{ noremap = true, silent = true }
|
||||||
)
|
)
|
||||||
|
|
||||||
vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp'.PeekDefinition()<CR>"
|
vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.utils'.PeekDefinition()<CR>"
|
||||||
vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
|
vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
|
||||||
vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
|
vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
|
||||||
vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
|
vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
|
||||||
-- vim.cmd "nnoremap <silent> <tab> <cmd>lua vim.lsp.buf.signature_help()<CR>"
|
-- vim.cmd "nnoremap <silent> <tab> <cmd>lua vim.lsp.buf.signature_help()<CR>"
|
||||||
-- scroll down hover doc or scroll in definition preview
|
-- scroll down hover doc or scroll in definition preview
|
||||||
-- scroll up hover doc
|
-- scroll up hover doc
|
||||||
vim.cmd 'command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()'
|
-- vim.cmd 'command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set Default Prefix.
|
|
||||||
-- Note: You can set a prefix per lsp server in the lv-globals.lua file
|
|
||||||
function lsp_config.setup_handlers()
|
|
||||||
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
|
|
||||||
virtual_text = lvim.lsp.diagnostics.virtual_text,
|
|
||||||
signs = lvim.lsp.diagnostics.signs,
|
|
||||||
underline = lvim.lsp.document_highlight,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
|
|
||||||
border = lvim.lsp.popup_border,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
|
|
||||||
border = lvim.lsp.popup_border,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
-- symbols for autocomplete
|
|
||||||
vim.lsp.protocol.CompletionItemKind = {
|
|
||||||
" (Text) ",
|
|
||||||
" (Method)",
|
|
||||||
" (Function)",
|
|
||||||
" (Constructor)",
|
|
||||||
" ﴲ (Field)",
|
|
||||||
"[] (Variable)",
|
|
||||||
" (Class)",
|
|
||||||
" ﰮ (Interface)",
|
|
||||||
" (Module)",
|
|
||||||
" 襁 (Property)",
|
|
||||||
" (Unit)",
|
|
||||||
" (Value)",
|
|
||||||
" 練 (Enum)",
|
|
||||||
" (Keyword)",
|
|
||||||
" (Snippet)",
|
|
||||||
" (Color)",
|
|
||||||
" (File)",
|
|
||||||
" (Reference)",
|
|
||||||
" (Folder)",
|
|
||||||
" (EnumMember)",
|
|
||||||
" ﲀ (Constant)",
|
|
||||||
" ﳤ (Struct)",
|
|
||||||
" (Event)",
|
|
||||||
" (Operator)",
|
|
||||||
" (TypeParameter)",
|
|
||||||
}
|
|
||||||
|
|
||||||
--[[ " autoformat
|
|
||||||
autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)
|
|
||||||
autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
|
|
||||||
autocmd BufWritePre *.lua lua vim.lsp.buf.formatting_sync(nil, 100) ]]
|
|
||||||
-- Java
|
|
||||||
-- autocmd FileType java nnoremap ca <Cmd>lua require('jdtls').code_action()<CR>
|
|
||||||
|
|
||||||
local function lsp_highlight_document(client)
|
|
||||||
if lvim.lsp.document_highlight == false then
|
|
||||||
return -- we don't need further
|
|
||||||
end
|
|
||||||
-- Set autocommands conditional on server_capabilities
|
|
||||||
if client.resolved_capabilities.document_highlight then
|
|
||||||
vim.api.nvim_exec(
|
|
||||||
[[
|
|
||||||
hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
|
|
||||||
hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
|
|
||||||
hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
|
|
||||||
augroup lsp_document_highlight
|
|
||||||
autocmd! * <buffer>
|
|
||||||
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
|
||||||
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
|
||||||
augroup END
|
|
||||||
]],
|
|
||||||
false
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
|
|
||||||
function lsp_config.preview_location(location, context, before_context)
|
|
||||||
-- location may be LocationLink or Location (more useful for the former)
|
|
||||||
context = context or 15
|
|
||||||
before_context = before_context or 0
|
|
||||||
local uri = location.targetUri or location.uri
|
|
||||||
if uri == nil then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local bufnr = vim.uri_to_bufnr(uri)
|
|
||||||
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
|
||||||
vim.fn.bufload(bufnr)
|
|
||||||
end
|
|
||||||
|
|
||||||
local range = location.targetRange or location.range
|
|
||||||
local contents = vim.api.nvim_buf_get_lines(
|
|
||||||
bufnr,
|
|
||||||
range.start.line - before_context,
|
|
||||||
range["end"].line + 1 + context,
|
|
||||||
false
|
|
||||||
)
|
|
||||||
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
|
|
||||||
return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border })
|
|
||||||
end
|
|
||||||
|
|
||||||
function lsp_config.preview_location_callback(_, method, result)
|
|
||||||
local context = 15
|
|
||||||
if result == nil or vim.tbl_isempty(result) then
|
|
||||||
print("No location found: " .. method)
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
if vim.tbl_islist(result) then
|
|
||||||
lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result[1], context)
|
|
||||||
else
|
|
||||||
lsp_config.floating_buf, lsp_config.floating_win = lsp_config.preview_location(result, context)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function lsp_config.PeekDefinition()
|
|
||||||
if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
|
|
||||||
vim.api.nvim_set_current_win(lsp_config.floating_win)
|
|
||||||
else
|
|
||||||
local params = vim.lsp.util.make_position_params()
|
|
||||||
return vim.lsp.buf_request(0, "textDocument/definition", params, lsp_config.preview_location_callback)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function lsp_config.PeekTypeDefinition()
|
|
||||||
if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
|
|
||||||
vim.api.nvim_set_current_win(lsp_config.floating_win)
|
|
||||||
else
|
|
||||||
local params = vim.lsp.util.make_position_params()
|
|
||||||
return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, lsp_config.preview_location_callback)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function lsp_config.PeekImplementation()
|
|
||||||
if vim.tbl_contains(vim.api.nvim_list_wins(), lsp_config.floating_win) then
|
|
||||||
vim.api.nvim_set_current_win(lsp_config.floating_win)
|
|
||||||
else
|
|
||||||
local params = vim.lsp.util.make_position_params()
|
|
||||||
return vim.lsp.buf_request(0, "textDocument/implementation", params, lsp_config.preview_location_callback)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function lsp_config.common_on_attach(client, bufnr)
|
|
||||||
if lvim.lsp.on_attach_callback then
|
|
||||||
lvim.lsp.on_attach_callback(client, bufnr)
|
|
||||||
end
|
|
||||||
lsp_highlight_document(client)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function no_formatter_on_attach(client, bufnr)
|
local function no_formatter_on_attach(client, bufnr)
|
||||||
if lvim.lsp.on_attach_callback then
|
if lvim.lsp.on_attach_callback then
|
||||||
lvim.lsp.on_attach_callback(client, bufnr)
|
lvim.lsp.on_attach_callback(client, bufnr)
|
||||||
end
|
end
|
||||||
lsp_highlight_document(client)
|
require("lsp.utils").lsp_highlight_document(client)
|
||||||
client.resolved_capabilities.document_formatting = false
|
client.resolved_capabilities.document_formatting = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
33
lua/lsp/kind.lua
Normal file
33
lua/lsp/kind.lua
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.setup()
|
||||||
|
vim.lsp.protocol.CompletionItemKind = {
|
||||||
|
-- symbols for autocomplete
|
||||||
|
" (Text) ",
|
||||||
|
" (Method)",
|
||||||
|
" (Function)",
|
||||||
|
" (Constructor)",
|
||||||
|
" ﴲ (Field)",
|
||||||
|
"[] (Variable)",
|
||||||
|
" (Class)",
|
||||||
|
" ﰮ (Interface)",
|
||||||
|
" (Module)",
|
||||||
|
" 襁 (Property)",
|
||||||
|
" (Unit)",
|
||||||
|
" (Value)",
|
||||||
|
" 練 (Enum)",
|
||||||
|
" (Keyword)",
|
||||||
|
" (Snippet)",
|
||||||
|
" (Color)",
|
||||||
|
" (File)",
|
||||||
|
" (Reference)",
|
||||||
|
" (Folder)",
|
||||||
|
" (EnumMember)",
|
||||||
|
" ﲀ (Constant)",
|
||||||
|
" ﳤ (Struct)",
|
||||||
|
" (Event)",
|
||||||
|
" (Operator)",
|
||||||
|
" (TypeParameter)",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
return M
|
20
lua/lsp/signs.lua
Normal file
20
lua/lsp/signs.lua
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
local M = {}
|
||||||
|
function M.setup()
|
||||||
|
vim.fn.sign_define(
|
||||||
|
"LspDiagnosticsSignError",
|
||||||
|
{ texthl = "LspDiagnosticsSignError", text = "", numhl = "LspDiagnosticsSignError" }
|
||||||
|
)
|
||||||
|
vim.fn.sign_define(
|
||||||
|
"LspDiagnosticsSignWarning",
|
||||||
|
{ texthl = "LspDiagnosticsSignWarning", text = "", numhl = "LspDiagnosticsSignWarning" }
|
||||||
|
)
|
||||||
|
vim.fn.sign_define(
|
||||||
|
"LspDiagnosticsSignHint",
|
||||||
|
{ texthl = "LspDiagnosticsSignHint", text = "", numhl = "LspDiagnosticsSignHint" }
|
||||||
|
)
|
||||||
|
vim.fn.sign_define(
|
||||||
|
"LspDiagnosticsSignInformation",
|
||||||
|
{ texthl = "LspDiagnosticsSignInformation", text = "", numhl = "LspDiagnosticsSignInformation" }
|
||||||
|
)
|
||||||
|
end
|
||||||
|
return M
|
101
lua/lsp/utils.lua
Normal file
101
lua/lsp/utils.lua
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local function lsp_highlight_document(client)
|
||||||
|
if lvim.lsp.document_highlight == false then
|
||||||
|
return -- we don't need further
|
||||||
|
end
|
||||||
|
-- Set autocommands conditional on server_capabilities
|
||||||
|
if client.resolved_capabilities.document_highlight then
|
||||||
|
vim.api.nvim_exec(
|
||||||
|
[[
|
||||||
|
hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
|
||||||
|
hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
|
||||||
|
hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
|
||||||
|
augroup lsp_document_highlight
|
||||||
|
autocmd! * <buffer>
|
||||||
|
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
||||||
|
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
||||||
|
augroup END
|
||||||
|
]],
|
||||||
|
false
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.lsp_highlight_document(client)
|
||||||
|
lsp_highlight_document(client)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/
|
||||||
|
function M.preview_location(location, context, before_context)
|
||||||
|
-- location may be LocationLink or Location (more useful for the former)
|
||||||
|
context = context or 15
|
||||||
|
before_context = before_context or 0
|
||||||
|
local uri = location.targetUri or location.uri
|
||||||
|
if uri == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local bufnr = vim.uri_to_bufnr(uri)
|
||||||
|
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||||
|
vim.fn.bufload(bufnr)
|
||||||
|
end
|
||||||
|
|
||||||
|
local range = location.targetRange or location.range
|
||||||
|
local contents = vim.api.nvim_buf_get_lines(
|
||||||
|
bufnr,
|
||||||
|
range.start.line - before_context,
|
||||||
|
range["end"].line + 1 + context,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
|
||||||
|
return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border })
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.preview_location_callback(_, method, result)
|
||||||
|
local context = 15
|
||||||
|
if result == nil or vim.tbl_isempty(result) then
|
||||||
|
print("No location found: " .. method)
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if vim.tbl_islist(result) then
|
||||||
|
M.floating_buf, M.floating_win = M.preview_location(result[1], context)
|
||||||
|
else
|
||||||
|
M.floating_buf, M.floating_win = M.preview_location(result, context)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.PeekDefinition()
|
||||||
|
if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
|
||||||
|
vim.api.nvim_set_current_win(M.floating_win)
|
||||||
|
else
|
||||||
|
local params = vim.lsp.util.make_position_params()
|
||||||
|
return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.PeekTypeDefinition()
|
||||||
|
if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
|
||||||
|
vim.api.nvim_set_current_win(M.floating_win)
|
||||||
|
else
|
||||||
|
local params = vim.lsp.util.make_position_params()
|
||||||
|
return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.PeekImplementation()
|
||||||
|
if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
|
||||||
|
vim.api.nvim_set_current_win(M.floating_win)
|
||||||
|
else
|
||||||
|
local params = vim.lsp.util.make_position_params()
|
||||||
|
return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.common_on_attach(client, bufnr)
|
||||||
|
if lvim.lsp.on_attach_callback then
|
||||||
|
lvim.lsp.on_attach_callback(client, bufnr)
|
||||||
|
end
|
||||||
|
lsp_highlight_document(client)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
Loading…
Add table
Add a link
Reference in a new issue