-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
384 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
---@type dotvim.core.package.PackageOption | ||
local M = { | ||
name = "dotvim.packages.lsp", | ||
plugins = {}, | ||
} | ||
|
||
local methods = require("dotvim.core.lsp.methods") | ||
|
||
---@type dotvim.core | ||
local Core = require("dotvim.core") | ||
|
||
---@param buffer number | ||
local function create_lsp_autocmds(buffer) | ||
-- display diagnostic win on CursorHold | ||
vim.api.nvim_create_autocmd("CursorHold", { | ||
buffer = buffer, | ||
callback = methods.open_diagnostic, | ||
}) | ||
end | ||
|
||
---@param buffer? number | ||
local function setup_lsp_keymaps(buffer) | ||
---comment normal map | ||
---@param lhs string | ||
---@param rhs any | ||
---@param desc string | ||
local nmap = function(lhs, rhs, desc) | ||
vim.keymap.set("n", lhs, rhs, { desc = desc, buffer = buffer }) | ||
end | ||
|
||
nmap("gD", methods.declaration, "goto-declaration") | ||
|
||
nmap("gd", methods.definitions, "goto-definition") | ||
|
||
nmap("gt", methods.type_definitions, "goto-type-definition") | ||
|
||
if buffer ~= nil then | ||
local current_k_map = vim.fn.mapcheck("K", "n") | ||
-- empty or contains "nvim/runtime" | ||
if current_k_map == "" or current_k_map:find("nvim/runtime") ~= nil then | ||
nmap("K", methods.show_hover, "show-hover") | ||
end | ||
else | ||
nmap("K", methods.show_hover, "show-hover") | ||
end | ||
|
||
nmap("gi", methods.implementations, "goto-impl") | ||
|
||
nmap("gR", methods.rename, "rename-symbol") | ||
|
||
nmap("ga", methods.code_action, "code-action") | ||
|
||
nmap("gr", methods.references, "inspect-references") | ||
|
||
nmap("[c", methods.prev_diagnostic, "previous-diagnostic") | ||
|
||
nmap("]c", methods.next_diagnostic, "next-diagnostic") | ||
end | ||
|
||
M.setup = function() | ||
-- if in vscode environment, create key bindings globally, else only create | ||
-- keybindings on lsp enabled buffers | ||
if vim.g.vscode then | ||
setup_lsp_keymaps() | ||
else | ||
Core.lsp.on_lsp_attach(function(_, buffer) | ||
local exists, value = | ||
pcall(vim.api.nvim_buf_get_var, buffer, "_dotvim_lsp_attached") | ||
if not exists or not value then | ||
create_lsp_autocmds(buffer) | ||
setup_lsp_keymaps(buffer) | ||
vim.api.nvim_buf_set_var(buffer, "_dotvim_lsp_attached", true) | ||
end | ||
end) | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
local Methods = {} | ||
|
||
function Methods.declaration() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.revealDeclaration") | ||
else | ||
vim.lsp.buf.declaration() | ||
end | ||
end | ||
|
||
function Methods.definitions() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.revealDefinition") | ||
else | ||
require("glance").open("definitions") | ||
end | ||
end | ||
|
||
function Methods.type_definitions() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.goToTypeDefinition") | ||
else | ||
require("glance").open("type_definitions") | ||
end | ||
end | ||
|
||
function Methods.implementations() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.goToImplementation") | ||
else | ||
require("glance").open("implementations") | ||
end | ||
end | ||
|
||
function Methods.references() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("references-view.findReferences") | ||
else | ||
require("glance").open("references") | ||
end | ||
end | ||
|
||
function Methods.code_action() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.quickFix") | ||
else | ||
vim.lsp.buf.code_action() | ||
end | ||
end | ||
|
||
function Methods.next_diagnostic() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.marker.nextInFiles") | ||
else | ||
vim.diagnostic.goto_next { wrap = false } | ||
end | ||
end | ||
|
||
function Methods.prev_diagnostic() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.marker.prevInFiles") | ||
else | ||
vim.diagnostic.goto_prev { wrap = false } | ||
end | ||
end | ||
|
||
local lsp_hover_group = | ||
vim.api.nvim_create_augroup("dtovim_lsp_hover", { clear = true }) | ||
|
||
function Methods.show_hover() | ||
if vim.g.vscode then | ||
require("vscode-neovim").call("editor.action.showHover") | ||
else | ||
vim.o.eventignore = "CursorHold" | ||
vim.api.nvim_exec_autocmds("User", { | ||
pattern = "DoraShowHover", | ||
}) | ||
vim.lsp.buf.hover() | ||
vim.api.nvim_create_autocmd({ "CursorMoved" }, { | ||
group = lsp_hover_group, | ||
buffer = 0, | ||
command = 'set eventignore=""', | ||
once = true, | ||
}) | ||
end | ||
end | ||
|
||
local function close_preview_window(winnr, bufnrs) | ||
vim.schedule(function() | ||
-- exit if we are in one of ignored buffers | ||
if bufnrs and vim.list_contains(bufnrs, vim.api.nvim_get_current_buf()) then | ||
return | ||
end | ||
|
||
local augroup = "preview_window_" .. winnr | ||
pcall(vim.api.nvim_del_augroup_by_name, augroup) | ||
pcall(vim.api.nvim_win_close, winnr, true) | ||
end) | ||
end | ||
|
||
local function close_preview_autocmd(events, winnr, bufnrs) | ||
local augroup = vim.api.nvim_create_augroup("preview_window_" .. winnr, { | ||
clear = true, | ||
}) | ||
|
||
-- close the preview window when entered a buffer that is not | ||
-- the floating window buffer or the buffer that spawned it | ||
vim.api.nvim_create_autocmd("BufEnter", { | ||
group = augroup, | ||
callback = function() | ||
close_preview_window(winnr, bufnrs) | ||
end, | ||
}) | ||
|
||
if #events > 0 then | ||
local simple_events = {} | ||
local events_with_pattern = {} | ||
|
||
for _, event in ipairs(events) do | ||
-- split event with space | ||
local parts = vim.split(event, " ", { | ||
trimempty = false, | ||
}) | ||
if #parts == 1 then | ||
table.insert(simple_events, event) | ||
else | ||
local pattern = table.concat({ unpack(parts, 2) }, " ") | ||
table.insert(events_with_pattern, { | ||
event = parts[1], | ||
pattern = pattern, | ||
}) | ||
end | ||
end | ||
|
||
vim.api.nvim_create_autocmd(simple_events, { | ||
group = augroup, | ||
buffer = bufnrs[2], | ||
callback = function() | ||
close_preview_window(winnr) | ||
end, | ||
}) | ||
for _, event in ipairs(events_with_pattern) do | ||
vim.api.nvim_create_autocmd(event.event, { | ||
group = augroup, | ||
buffer = bufnrs[2], | ||
callback = function(ev) | ||
if ev.match == event.pattern then | ||
close_preview_window(winnr) | ||
end | ||
end, | ||
}) | ||
end | ||
end | ||
end | ||
|
||
function Methods.open_diagnostic() | ||
local opts = { | ||
focusable = false, | ||
border = "solid", | ||
source = "if_many", | ||
prefix = " ", | ||
focus = false, | ||
scope = "cursor", | ||
} | ||
|
||
local bufnr, win = vim.diagnostic.open_float(opts) | ||
|
||
if bufnr == nil then | ||
return | ||
end | ||
|
||
vim.api.nvim_set_option_value( | ||
"winhl", | ||
"FloatBorder:NormalFloat,Normal:NormalFloat", | ||
{ win = win } | ||
) | ||
|
||
close_preview_autocmd({ | ||
"CursorMoved", | ||
"InsertEnter", | ||
"User DoraShowHover", | ||
"BufLeave", | ||
"FocusLost", | ||
}, win, { bufnr, vim.api.nvim_get_current_buf() }) | ||
end | ||
|
||
function Methods.rename(new_name, options) | ||
options = options or {} | ||
local filter = function(client) | ||
return not vim.tbl_contains({ "null-ls", "copilot" }, client.name) | ||
end | ||
options.filter = options.filter or filter | ||
vim.lsp.buf.rename(new_name, options) | ||
end | ||
|
||
return Methods |
2 changes: 1 addition & 1 deletion
2
...im/legacy/packages/lsp/plugins/glance.lua → lua/dotvim/packages/lsp/plugins/glance.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...m/legacy/packages/lsp/plugins/lspkind.lua → lua/dotvim/packages/lsp/plugins/lspkind.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.