Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Mar 29, 2024
1 parent 073e190 commit 595bdae
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 18 deletions.
78 changes: 78 additions & 0 deletions lua/dotvim/packages/lsp/init.lua
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
196 changes: 196 additions & 0 deletions lua/dotvim/packages/lsp/methods.lua
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@type dora.core.plugin.PluginOption
---@type dotvim.core.plugin.PluginOption
return {
"dnlhc/glance.nvim",
cmd = "Glance",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@type dora.core.plugin.PluginOption
---@type dotvim.core.plugin.PluginOption
return {
"onsails/lspkind.nvim",
lazy = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---@type dora.core.plugin.PluginOption
---@type dotvim.utils
local Utils = require("dora.utils")

---@type dotvim.core.plugin.PluginOption
return {
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
opts = function()
---@type dora.config
local config = require("dora.config")

return {
diagnostics = {
underline = true,
Expand All @@ -18,19 +18,19 @@ return {
severity_sort = true,
signs = {
text = {
[vim.diagnostic.severity.ERROR] = config.icon.predefined_icon(
[vim.diagnostic.severity.ERROR] = Utils.icon.predefined_icon(
"DiagnosticError",
1
),
[vim.diagnostic.severity.WARN] = config.icon.predefined_icon(
[vim.diagnostic.severity.WARN] = Utils.icon.predefined_icon(
"DiagnosticWarn",
1
),
[vim.diagnostic.severity.INFO] = config.icon.predefined_icon(
[vim.diagnostic.severity.INFO] = Utils.icon.predefined_icon(
"DiagnosticInfo",
1
),
[vim.diagnostic.severity.HINT] = config.icon.predefined_icon(
[vim.diagnostic.severity.HINT] = Utils.icon.predefined_icon(
"DiagnosticHint",
1
),
Expand All @@ -48,16 +48,13 @@ return {
}
end,
config = function(_, opts)
---@type dora.lib
local lib = require("dora.lib")

vim.diagnostic.config(vim.deepcopy(opts.diagnostics))

local capabilities = vim.tbl_deep_extend(
"force",
{},
vim.lsp.protocol.make_client_capabilities(),
lib.func.require_then("cmp_nvim_lsp", function(cmp_nvim_lsp)
Utils.fn.require_then("cmp_nvim_lsp", function(cmp_nvim_lsp)
return cmp_nvim_lsp.default_capabilities()
end) or {},
opts.capabilities or {}
Expand All @@ -74,13 +71,10 @@ return {
return default_config.cmd
end

---@type dora.utils
local Utils = require("dora.utils")

---@param cmd string[]
local function try_to_replace_executable_from_nix_or_mason(cmd)
local executable = cmd[1]
local new_executable = Utils.which_binary(executable)
local new_executable = Utils.which(executable)
return { new_executable, unpack(cmd, 2) }
end

Expand Down
11 changes: 11 additions & 0 deletions lua/dotvim/utils/fn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ function M.invoke_once(fun)
end
end

---@generic T
---@param name string
---@param callback fun(module): T
---@return T?
function M.require_then(name, callback)
local has_module, module = pcall(require, name)
if has_module then
return callback(module)
end
end

---@class dotvim.utils.fn.CacheManager
---@field private entries table<string, any>
local CacheManager = {}
Expand Down
Loading

0 comments on commit 595bdae

Please sign in to comment.