Skip to content

Commit

Permalink
feat(go): enhanced document search with lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7a7a committed Jul 21, 2024
1 parent d921f21 commit aad901a
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions lua/gx/handlers/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,59 @@ local M = {
filename = nil,
}

local function is_import_spec(node)
return node:type() == "import_spec"
end

local function is_gopls_attached()
return #vim.lsp.get_clients({ name = "gopls" }) > 0
end

local function request_hover_info()
local method = "textDocument/hover"
local params = vim.lsp.util.make_position_params()
-- The default timeout is 1000ms
return vim.lsp.buf_request_sync(0, method, params)
end

-- TODO check if it is an internal pkg
local function get_link_from_response(res_tbl)
local res = res_tbl[1]
if res and res.result then
local value = res.result.contents.value
return value:match("https://pkg%.go%.dev[%w.#/]+")
end
return nil
end

function M.handle()
local node = vim.treesitter.get_node()
if not node then
return
end
if node:type() ~= "import_spec" then

if not is_import_spec(node) then
if node:type() == "import_declaration" then
node = node:named_child(0)
else
node = node:parent()
end
if node:type() ~= "import_spec" then

if not is_import_spec(node) then
if not is_gopls_attached() then
return
end

local res_tbl, err = request_hover_info()
if err or not res_tbl or vim.tbl_isempty(res_tbl) then
return
end

local link = get_link_from_response(res_tbl)
if link then
return link
end

return
end
end
Expand Down

0 comments on commit aad901a

Please sign in to comment.