2021-04-19 02:56:32 +00:00
|
|
|
local util = require "navigator.util"
|
|
|
|
local lsphelper = require "navigator.lspwrapper"
|
|
|
|
local locations_to_items = lsphelper.locations_to_items
|
|
|
|
local gui = require "navigator.gui"
|
|
|
|
local log = util.log
|
|
|
|
local TextView = require("guihua.textview")
|
|
|
|
-- callback for lsp definition, implementation and declaration handler
|
2021-09-05 22:34:26 +00:00
|
|
|
local definition_hdlr = util.mk_handler(function(err, locations, ctx, _)
|
2021-04-19 02:56:32 +00:00
|
|
|
-- log(locations)
|
2021-05-17 03:15:15 +00:00
|
|
|
if err ~= nil then
|
2021-09-05 22:34:26 +00:00
|
|
|
print(err, ctx)
|
2021-05-17 03:15:15 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if type(locations) == "number" then
|
|
|
|
log(locations)
|
2021-07-31 05:21:14 +00:00
|
|
|
log("unable to handle request")
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
if locations == nil or vim.tbl_isempty(locations) then
|
|
|
|
print "Definition not found"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if vim.tbl_islist(locations) then
|
|
|
|
if #locations > 1 then
|
|
|
|
local items = locations_to_items(locations)
|
|
|
|
gui.new_list_view({items = items, api = 'Definition'})
|
|
|
|
else
|
|
|
|
vim.lsp.util.jump_to_location(locations[1])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
vim.lsp.util.jump_to_location(locations)
|
|
|
|
end
|
2021-09-05 22:34:26 +00:00
|
|
|
end)
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-05-30 02:25:05 +00:00
|
|
|
local function get_symbol()
|
|
|
|
local currentWord = vim.fn.expand('<cword>')
|
|
|
|
return currentWord
|
|
|
|
end
|
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
local function def_preview(timeout_ms)
|
2021-04-22 23:39:24 +00:00
|
|
|
assert(#vim.lsp.buf_get_clients() > 0, "Must have a client running")
|
2021-04-19 02:56:32 +00:00
|
|
|
local method = "textDocument/definition"
|
|
|
|
local params = vim.lsp.util.make_position_params()
|
2021-05-30 02:25:05 +00:00
|
|
|
local result = vim.lsp.buf_request_sync(0, method, params, timeout_ms or 1000)
|
2021-04-19 02:56:32 +00:00
|
|
|
|
|
|
|
if result == nil or vim.tbl_isempty(result) then
|
|
|
|
print("No result found: " .. method)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2021-05-30 02:25:05 +00:00
|
|
|
log(result)
|
2021-04-19 02:56:32 +00:00
|
|
|
local data = {}
|
|
|
|
-- result = {vim.tbl_deep_extend("force", {}, unpack(result))}
|
|
|
|
-- log("def-preview", result)
|
|
|
|
for key, value in pairs(result) do
|
2021-09-23 07:18:42 +00:00
|
|
|
if result[key] ~= nil and not vim.tbl_isempty(result[key]) then
|
2021-05-30 02:25:05 +00:00
|
|
|
table.insert(data, value.result[1])
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
2021-09-23 07:18:42 +00:00
|
|
|
|
|
|
|
if vim.tbl_isempty(data) then
|
|
|
|
print("No result found: " .. method)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
local range = data[1].targetRange or data[1].range
|
|
|
|
|
|
|
|
local row = range.start.line
|
|
|
|
-- in case there are comments
|
|
|
|
row = math.max(row - 3, 1)
|
|
|
|
local delta = range.start.line - row + 1
|
|
|
|
local uri = data[1].uri or data[1].targetUri
|
2021-05-30 02:25:05 +00:00
|
|
|
if not uri then
|
|
|
|
return
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
2021-05-30 02:25:05 +00:00
|
|
|
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
end
|
2021-04-20 07:42:03 +00:00
|
|
|
-- TODO: 12 should be an option
|
2021-04-19 02:56:32 +00:00
|
|
|
local definition = vim.api.nvim_buf_get_lines(bufnr, row, range["end"].line + 12, false)
|
|
|
|
local def_line = vim.api.nvim_buf_get_lines(bufnr, range.start.line, range.start.line + 1, false)
|
|
|
|
for _ = 1, math.min(3, #definition), 1 do
|
|
|
|
if #definition[1] < 2 then
|
|
|
|
table.remove(definition, 1)
|
|
|
|
delta = delta - 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-05-30 02:25:05 +00:00
|
|
|
local width = 40
|
|
|
|
for key, value in pairs(definition) do
|
|
|
|
log(key, value, width)
|
|
|
|
width = math.max(width, #value)
|
|
|
|
width = math.min(90, width)
|
|
|
|
|
|
|
|
end
|
|
|
|
definition = vim.list_extend({" [" .. get_symbol() .. "] Definition: "}, definition)
|
2021-04-19 02:56:32 +00:00
|
|
|
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
|
|
|
|
|
|
|
|
-- TODO multiple resuts?
|
|
|
|
local opts = {
|
|
|
|
relative = "cursor",
|
|
|
|
style = "minimal",
|
|
|
|
ft = filetype,
|
2021-05-30 02:25:05 +00:00
|
|
|
width = width,
|
2021-04-19 02:56:32 +00:00
|
|
|
data = definition,
|
2021-08-12 13:34:27 +00:00
|
|
|
enter = true,
|
|
|
|
border = _NgConfigValues.border or "shadow"
|
2021-04-19 02:56:32 +00:00
|
|
|
}
|
2021-08-12 13:34:27 +00:00
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
TextView:new(opts)
|
|
|
|
delta = delta + 1 -- header
|
|
|
|
local cmd = "normal! " .. tostring(delta) .. "G"
|
|
|
|
|
|
|
|
vim.cmd(cmd)
|
|
|
|
vim.cmd('set cursorline')
|
|
|
|
if #def_line > 0 then
|
|
|
|
local niddle = require('guihua.util').add_escape(def_line[1])
|
2021-04-20 07:42:03 +00:00
|
|
|
-- log(def_line[1], niddle)
|
2021-04-19 02:56:32 +00:00
|
|
|
vim.fn.matchadd("Search", niddle)
|
|
|
|
end
|
|
|
|
-- TODO:
|
|
|
|
-- https://github.com/oblitum/goyo.vim/blob/master/autoload/goyo.vim#L108-L135
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.lsp.handlers["textDocument/definition"] = definition_hdlr
|
|
|
|
return {
|
|
|
|
definition_handler = definition_hdlr,
|
|
|
|
definition_preview = def_preview,
|
|
|
|
declaration_handler = definition_hdlr,
|
|
|
|
typeDefinition_handler = definition_hdlr
|
|
|
|
}
|