Skip to content

Commit

Permalink
Merge pull request #11 from jmbuhr/multi-definitions
Browse files Browse the repository at this point in the history
handle languages that return multiple definitions
  • Loading branch information
jmbuhr authored Jan 17, 2023
2 parents 766586c + 59ce6fe commit 0c3c1a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
30 changes: 23 additions & 7 deletions lua/otter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,41 @@ M.sync_raft = keeper.sync_raft
M.send_request = keeper.send_request
M.export = keeper.export_raft


-- example implementations to work with the send_request function
M.ask_definition = function()
local main_nr = api.nvim_get_current_buf()
local main_uri = vim.uri_from_bufnr(main_nr)

local function redirect_definition(res)
if res.uri ~= nil then
if require 'otter.tools.functions'.is_otterpath(res.uri) then
res.uri = main_uri
end
end
if res.targetUri ~= nil then
if require 'otter.tools.functions'.is_otterpath(res.targetUri) then
res.targetUri = main_uri
end
end
return res
end

M.send_request(main_nr, "textDocument/definition", function(response)
if #response == 0 then
return redirect_definition(response)
end

local modified_response = {}
for _, res in ipairs(response) do
if res.uri ~= nil then
if require 'otter.tools.functions'.is_otterpath(res.uri) then
res.uri = main_uri
end
table.insert(modified_response, res)
end
return modified_response
table.insert(modified_response, redirect_definition(res))
end
return modified_response
end
)
end


local function replace_header_div(response)
response.contents = response.contents:gsub('<div class="container">', '')
return response
Expand Down
33 changes: 18 additions & 15 deletions lua/otter/keeper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,28 @@ M.send_request = function(main_nr, request, filter)
uri = uri
}
vim.lsp.buf_request(otter_nr, request, position_params, function(err, response, method, ...)
if response ~= nil then
if filter == nil then
vim.lsp.handlers[request](err, response, method, ...)
else
-- if response is a list of responses, filter every response
if #response > 0 then
local responses = {}
for _, res in ipairs(response) do
table.insert(responses, filter(res))
if response == nil then return nil end
if filter == nil then
vim.lsp.handlers[request](err, response, method, ...)
else
-- if response is a list of responses, filter every response
if #response > 0 then
local responses = {}
for _, res in ipairs(response) do
local filtered_res = filter(res)
if filtered_res then
table.insert(responses, filtered_res)
end
response = responses
else
-- otherwise apply the filter to the one response
response = filter(response)
end
vim.lsp.handlers[request](err, response, method, ...)
response = responses
else
-- otherwise apply the filter to the one response
response = filter(response)
end
vim.lsp.handlers[request](err, response, method, ...)
end
end)
end
)
end
end

Expand Down

0 comments on commit 0c3c1a2

Please sign in to comment.