Skip to content
This repository was archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
fix(providers): multiple client buffer definition preview bug (#40)
Browse files Browse the repository at this point in the history
see #38
  • Loading branch information
wesselvdv authored Nov 27, 2021
1 parent 859b564 commit 518c12e
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions lua/lspsaga/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -497,75 +497,79 @@ function lspfinder.preview_definition(timeout_ms)
return nil
end

if vim.tbl_islist(result) and not vim.tbl_isempty(result[1]) then
local uri = result[1].result[1].uri or result[1].result[1].targetUri
if #uri == 0 then
return
end
local bufnr = vim.uri_to_bufnr(uri)
local link = vim.uri_to_fname(uri)
local short_name
local root_dir = libs.get_lsp_root_dir()

-- reduce filename length by root_dir or home dir
if link:find(root_dir, 1, true) then
short_name = link:sub(root_dir:len() + 2)
elseif link:find(home_dir, 1, true) then
short_name = link:sub(home_dir:len() + 2)
-- some definition still has a too long path prefix
if #short_name > 40 then
short_name = libs.split_by_pathsep(short_name, 4)
end
else
short_name = libs.split_by_pathsep(link, 4)
end
if vim.tbl_islist(result) then
for _, definition in ipairs(result) do
if not vim.tbl_isempty(definition) and not vim.tbl_isempty(definition.result) then
local uri = definition.result[1].uri or definition.result[1].targetUri
if #uri == 0 then
return
end
local bufnr = vim.uri_to_bufnr(uri)
local link = vim.uri_to_fname(uri)
local short_name
local root_dir = libs.get_lsp_root_dir()

-- reduce filename length by root_dir or home dir
if link:find(root_dir, 1, true) then
short_name = link:sub(root_dir:len() + 2)
elseif link:find(home_dir, 1, true) then
short_name = link:sub(home_dir:len() + 2)
-- some definition still has a too long path prefix
if #short_name > 40 then
short_name = libs.split_by_pathsep(short_name, 4)
end
else
short_name = libs.split_by_pathsep(link, 4)
end

if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
local range = result[1].result[1].targetRange or result[1].result[1].range
local start_line = 0
if range.start.line - 3 >= 1 then
start_line = range.start.line - 3
else
start_line = range.start.line
end
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
local range = definition.result[1].targetRange or definition.result[1].range
local start_line = 0
if range.start.line - 3 >= 1 then
start_line = range.start.line - 3
else
start_line = range.start.line
end

local content = vim.api.nvim_buf_get_lines(
bufnr,
start_line,
range["end"].line + 1 + config.max_preview_lines,
false
)
content = vim.list_extend({
config.definition_preview_icon .. "Definition Preview: " .. short_name,
"",
}, content)
local content = vim.api.nvim_buf_get_lines(
bufnr,
start_line,
range["end"].line + 1 + config.max_preview_lines,
false
)
content = vim.list_extend({
config.definition_preview_icon .. "Definition Preview: " .. short_name,
"",
}, content)

local opts = {
relative = "cursor",
style = "minimal",
}

local WIN_WIDTH = api.nvim_get_option "columns"
local max_width = math.floor(WIN_WIDTH * 0.5)
local width, _ = vim.lsp.util._make_floating_popup_size(content, opts)

if width > max_width then
opts.width = max_width
end

local opts = {
relative = "cursor",
style = "minimal",
}
local content_opts = {
contents = content,
filetype = filetype,
highlight = "LspSagaDefPreviewBorder",
}

local WIN_WIDTH = api.nvim_get_option "columns"
local max_width = math.floor(WIN_WIDTH * 0.5)
local width, _ = vim.lsp.util._make_floating_popup_size(content, opts)
local bf, wi = window.create_win_with_border(content_opts, opts)
vim.lsp.util.close_preview_autocmd({ "CursorMoved", "CursorMovedI", "BufHidden", "BufLeave" }, wi)
vim.api.nvim_buf_add_highlight(bf, -1, "DefinitionPreviewTitle", 0, 0, -1)

if width > max_width then
opts.width = max_width
api.nvim_buf_set_var(0, "lspsaga_def_preview", { wi, 1, config.max_preview_lines, 10 })
end
end

local content_opts = {
contents = content,
filetype = filetype,
highlight = "LspSagaDefPreviewBorder",
}

local bf, wi = window.create_win_with_border(content_opts, opts)
vim.lsp.util.close_preview_autocmd({ "CursorMoved", "CursorMovedI", "BufHidden", "BufLeave" }, wi)
vim.api.nvim_buf_add_highlight(bf, -1, "DefinitionPreviewTitle", 0, 0, -1)

api.nvim_buf_set_var(0, "lspsaga_def_preview", { wi, 1, config.max_preview_lines, 10 })
end
end

Expand Down

0 comments on commit 518c12e

Please sign in to comment.