added support for 'nvim-jdtls' LSP URIs (closes issue #195)

'nvim-jdtls' extends the LSP to support eclipse jdt classes
since classes are inside '.jar' file it displays them as 'jdt://'
URIs which are then handled by 'nvim-jdtls', we can then use
'vim.lsp.util.jump_to_location' to open the URIs.
main
bhagwan 3 years ago
parent 8030ffe280
commit 9aab21b318

@ -67,19 +67,26 @@ M.vimcmd_file = function(vimcmd, selected, opts)
local curbuf = vim.api.nvim_buf_get_name(0)
for i = 1, #selected do
local entry = path.entry_to_file(selected[i])
-- only change buffer if we need to (issue #122)
local fullpath = entry.path
if not path.starts_with_separator(fullpath) then
fullpath = path.join({opts.cwd or vim.loop.cwd(), fullpath})
end
if vimcmd ~= "e" or curbuf ~= fullpath then
vim.cmd(vimcmd .. " " .. vim.fn.fnameescape(entry.path))
end
if entry.line > 1 or entry.col > 1 then
-- add current location to jumplist
-- Java LSP entries, 'jdt://...'
if entry.uri then
vim.cmd("normal! m`")
vim.api.nvim_win_set_cursor(0, {tonumber(entry.line), tonumber(entry.col)-1})
vim.lsp.util.jump_to_location(entry)
vim.cmd("norm! zvzz")
else
-- only change buffer if we need to (issue #122)
local fullpath = entry.path
if not path.starts_with_separator(fullpath) then
fullpath = path.join({opts.cwd or vim.loop.cwd(), fullpath})
end
if vimcmd ~= "e" or curbuf ~= fullpath then
vim.cmd(vimcmd .. " " .. vim.fn.fnameescape(entry.path))
end
if entry.line > 1 or entry.col > 1 then
-- add current location to jumplist
vim.cmd("normal! m`")
vim.api.nvim_win_set_cursor(0, {tonumber(entry.line), tonumber(entry.col)-1})
vim.cmd("norm! zvzz")
end
end
end
end

@ -334,7 +334,7 @@ M.fzf_files = function(opts)
if #selected > 1 then
for i = 2, #selected do
selected[i] = path.entry_to_file(selected[i], opts.cwd).noicons
selected[i] = path.entry_to_file(selected[i], opts.cwd).stripped
if opts.cb_selected then
local cb_ret = opts.cb_selected(opts, selected[i])
if cb_ret then selected[i] = cb_ret end

@ -118,29 +118,57 @@ end
local function stripBeforeLastOccurrenceOf(str, sep)
local idx = lastIndexOf(str, sep) or 0
return str:sub(idx+1)
return str:sub(idx+1), idx
end
function M.entry_to_location(entry)
local uri, line, col = entry:match("^(.*://.*):(%d+):(%d+):")
line = line and tonumber(line-1) or 0
col = col and tonumber(col) or 1
return {
stripped = entry,
line = line+1,
col = col,
uri = uri,
range = {
start = {
line = line,
character = col,
}
}
}
end
function M.entry_to_file(entry, cwd)
-- not very efficient but since this only gets
-- called for preview / process selection
-- it shouldn't really matter
-- Remove ansi coloring and prefixed icons
entry = utils.strip_ansi_coloring(entry)
local s = utils.strsplit(entry, ":")
if not s[1] then return {} end
local file = stripBeforeLastOccurrenceOf(s[1], utils.nbsp)
local noicons = stripBeforeLastOccurrenceOf(entry, utils.nbsp)
local stripped, idx = stripBeforeLastOccurrenceOf(entry, utils.nbsp)
-- entries from 'buffers' contain '[<bufnr>]'
local bufnr = s[1]:match("%[(%d+)")
-- buffer placeholder always comes before the nbsp
local bufnr = idx>1 and entry:sub(1, idx):match("%[(%d+)") or nil
if not bufnr and stripped:match("^.*://") then
-- Issue #195, when using nvim-jdtls
-- https://github.com/mfussenegger/nvim-jdtls
-- LSP entries inside .jar files appear as URIs
-- 'jdt://' which can then be opened with
-- 'vim.lsp.util.jump_to_location' or
-- 'lua require('jdtls').open_jdt_link(vim.fn.expand('jdt://...'))'
-- Convert to location item so we can use 'jump_to_location'
-- This can also work with any 'file://' prefixes
return M.entry_to_location(stripped)
end
local s = utils.strsplit(stripped, ":")
if not s[1] then return {} end
local file = s[1]
local line = tonumber(s[2])
local col = tonumber(s[3])
if cwd and #cwd>0 and not M.starts_with_separator(file) then
file = M.join({cwd, file})
noicons = M.join({cwd, noicons})
stripped = M.join({cwd, stripped})
end
return {
stripped = stripped,
bufnr = bufnr,
noicons = noicons,
path = file,
line = line or 1,
col = col or 1,

@ -259,6 +259,14 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
-- store current preview buffer
self.preview_bufnr = bufnr
self:preview_buf_post(entry)
elseif entry.uri then
-- LSP 'jdt://' entries, see issue #195
-- https://github.com/ibhagwan/fzf-lua/issues/195
vim.api.nvim_win_call(self.win.preview_winid, function()
vim.lsp.util.jump_to_location(entry)
self.preview_bufnr = vim.api.nvim_get_current_buf()
end)
self:preview_buf_post(entry)
else
if entry.bufnr then
-- buffer was unloaded, can happen when calling `lines`
@ -269,7 +277,7 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
-- mark the buffer for unloading the next call
self.preview_bufloaded = true
-- make sure the file is readable (or bad entry.path)
if not vim.loop.fs_stat(entry.path) then return end
if not entry.path or not vim.loop.fs_stat(entry.path) then return end
if utils.perl_file_is_binary(entry.path) then
vim.api.nvim_buf_set_lines(self.preview_bufnr, 0, -1, false, {
"Preview is not supported for binary files."
@ -301,8 +309,8 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
end
function Previewer.buffer_or_file:do_syntax(entry)
if not entry then return end
if not self.preview_bufnr then return end
if not entry or not entry.path then return end
local bufnr = self.preview_bufnr
local preview_winid = self.win.preview_winid
if self.preview_bufloaded and vim.bo[bufnr].filetype == '' then
@ -372,10 +380,10 @@ end
function Previewer.buffer_or_file:update_border(entry)
if self.title then
if self.opts.cwd then
if entry.path and self.opts.cwd then
entry.path = path.relative(entry.path, self.opts.cwd)
end
local title = (' %s '):format(entry.path)
local title = (' %s '):format(entry.path or entry.uri)
if entry.bufnr then
-- local border_width = api.nvim_win_get_width(self.win.preview_winid)
local buf_str = ('buf %d:'):format(entry.bufnr)

Loading…
Cancel
Save