Skip to content

Commit

Permalink
Make parameters to goto_preview_{definition/implementation} be a table
Browse files Browse the repository at this point in the history
To make the public APIs open to future extension. See rmagatti#44 and rmagatti#46
for the discussion.
  • Loading branch information
wookayin committed Nov 16, 2021
1 parent af6b8e7 commit d02a3cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ The `post_open_hook` function gets called right before setting the cursor positi
One can use this to set custom key bindings or really anything else they want to do when a new preview window opens.

### ⌨️ Mappings
There are no mappings by default, you can set `default_mappings = true` in the config to make use of the mappings I use or define your own.
There are no mappings by default, you can set `default_mappings = true` in the config to make use of the mappings I use or define your own.

**Default**
```viml
nnoremap gpd <cmd>lua require('goto-preview').goto_preview_definition()<CR>
Expand Down Expand Up @@ -78,7 +79,7 @@ Until more are added one can pass in custom responses through the `lsp_configs`

### Tested with
```
NVIM v0.5.0-dev+7d4f890aa
Build type: Release
LuaJIT 2.1.0-beta3
NVIM v0.5.0-dev+7d4f890aa
Build type: Release
LuaJIT 2.1.0-beta3
```
28 changes: 22 additions & 6 deletions lua/goto-preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,56 @@ local function print_lsp_error(lsp_call)
print("goto-preview: Error calling LSP" + lsp_call + ". The current language lsp might not support it.")
end

M.lsp_request_definition = function(focus_on_open, dismiss_on_move)
--- Preview definition.
--- @param opts table: Custom config
--- • focus_on_open boolean: Focus the floating window when opening it.
--- • dismiss_on_move boolean: Dismiss the floating window when moving the cursor.
--- @see require("goto-preview").setup()
M.lsp_request_definition = function(opts)
local params = vim.lsp.util.make_position_params()
local lsp_call = "textDocument/definition"
local success, _ = pcall(
vim.lsp.buf_request,
0,
lsp_call,
params,
lib.get_handler(lsp_call, focus_on_open, dismiss_on_move)
lib.get_handler(lsp_call, opts)
)
if not success then
print_lsp_error(lsp_call)
end
end

M.lsp_request_implementation = function(focus_on_open, dismiss_on_move)
--- Preview implementation.
--- @param opts table: Custom config
--- • focus_on_open boolean: Focus the floating window when opening it.
--- • dismiss_on_move boolean: Dismiss the floating window when moving the cursor.
--- @see require("goto-preview").setup()
M.lsp_request_implementation = function(opts)
local params = vim.lsp.util.make_position_params()
local lsp_call = "textDocument/implementation"
local success, _ = pcall(
vim.lsp.buf_request,
0,
lsp_call,
params,
lib.get_handler(lsp_call, focus_on_open, dismiss_on_move)
lib.get_handler(lsp_call, opts)
)
if not success then
print_lsp_error(lsp_call)
end
end

M.lsp_request_references = function()
M.lsp_request_references = function(opts)
local params = vim.lsp.util.make_position_params()
local lsp_call = "textDocument/references"
local success, _ = pcall(vim.lsp.buf_request, 0, lsp_call, params, lib.get_handler(lsp_call))
local success, _ = pcall(
vim.lsp.buf_request,
0,
lsp_call,
params,
lib.get_handler(lsp_call, opts)
)
if not success then
print_lsp_error(lsp_call)
end
Expand Down
33 changes: 17 additions & 16 deletions lua/goto-preview/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ M.close_if_is_goto_preview = function(win_handle)
end
end

M.open_floating_win = function(target, position, focus_on_open, dismiss_on_move)
M.open_floating_win = function(target, position, opts)
local buffer = type(target) == "string" and vim.uri_to_bufnr(target) or target
local bufpos = { vim.fn.line(".") - 1, vim.fn.col(".") } -- FOR relative='win'
local zindex = vim.tbl_isempty(M.windows) and 1 or #M.windows + 1

local enter = function()
if focus_on_open ~= nil then
return focus_on_open
if opts.focus_on_open ~= nil then
return opts.focus_on_open
else
return M.conf.focus_on_open
end
end

logger.debug("dismiss_on_move", enter())

logger.debug("focus_on_open", enter())
local new_window = vim.api.nvim_open_win(buffer, enter(), {
relative = "win",
width = M.conf.width,
Expand Down Expand Up @@ -139,8 +139,8 @@ M.open_floating_win = function(target, position, focus_on_open, dismiss_on_move)
}))

local dismiss = function()
if dismiss_on_move ~= nil then
return dismiss_on_move
if opts.dismiss_on_move ~= nil then
return opts.dismiss_on_move
else
return M.conf.dismiss_on_move
end
Expand Down Expand Up @@ -206,7 +206,7 @@ local function open_references_previewer(prompt_title, items)
end
end

local handle = function(result, focus_on_open, dismiss_on_move)
local handle = function(result, opts)
if not result then
return
end
Expand All @@ -218,7 +218,8 @@ local handle = function(result, focus_on_open, dismiss_on_move)

target, cursor_position = M.conf.lsp_configs.get_config(data)

M.open_floating_win(target, cursor_position, focus_on_open, dismiss_on_move)
-- opts: focus_on_open, dismiss_on_move, etc.
M.open_floating_win(target, cursor_position, opts)
end

local handle_references = function(result)
Expand All @@ -232,36 +233,36 @@ local handle_references = function(result)
open_references_previewer("References", items)
end

local legacy_handler = function(lsp_call)
local legacy_handler = function(lsp_call, opts)
return function(_, _, result)
if lsp_call ~= nil and lsp_call == "textDocument/references" then
logger.debug("raw result", vim.inspect(result))
handle_references(result)
else
handle(result)
handle(result, opts)
end
end
end

local handler = function(lsp_call, focus_on_open, dismiss_on_move)
local handler = function(lsp_call, opts)
return function(_, result, _, _)
if lsp_call ~= nil and lsp_call == "textDocument/references" then
logger.debug("raw result", vim.inspect(result))
handle_references(result)
else
handle(result, focus_on_open, dismiss_on_move)
handle(result, opts)
end
end
end

M.get_handler = function(lsp_call, focus_on_open, dismiss_on_move)
M.get_handler = function(lsp_call, opts)
-- Only really need to check one of the handlers
if debug.getinfo(vim.lsp.handlers["textDocument/definition"]).nparams == 4 then
logger.debug("calling new handler")
return handler(lsp_call, focus_on_open, dismiss_on_move)
return handler(lsp_call, opts)
else
logger.debug("calling legacy handler")
return legacy_handler(lsp_call)
return legacy_handler(lsp_call, opts)
end
end

Expand Down

0 comments on commit d02a3cc

Please sign in to comment.