Skip to content

Commit

Permalink
perf: cache go module name
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7a7a committed Jul 21, 2024
1 parent 542c578 commit 9a7ebe4
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions lua/gx/handlers/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,66 @@ local function is_gopls_attached()
return #vim.lsp.get_clients({ name = "gopls" }) > 0
end

local cached_mod_name
local function get_mode_name()
if cached_mod_name then
return cached_mod_name
end

local files = vim.fs.find("go.mod", { upward = true })
if #files == 0 or vim.tbl_isempty(files) then
return ""
end

local mod = files[1]
for line in io.lines(mod) do
local file, err = io.open(mod, "r")
if not file then
print("[gx.nvim]Error opening go.mod: " .. (err or "unknown error"))
return ""
end

for line in file:lines() do
local name = line:match("^module%s+([^\n]+)")
return name
if name then
file:close()
cached_mod_name = name
return name
end
end

file:close()
return ""
end

local function is_internal(url)
local mod = get_mode_name()
return mod ~= "" and url:find(mod, 1, true)
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

local function get_link_from_response(res_tbl)
local function get_url_from_response(res_tbl)
local res = res_tbl[1]
if res and res.result then
local value = res.result.contents.value
-- Test case
-- https://pkg.go.dev/github.com/json-iterator/[email protected]#API.Unmarshal
-- https://pkg.go.dev/context#Context
local link = value:match("%(https://pkg%.go%.dev[%w%p]+%)")
if not link then
return
local url = value:match("%(https://pkg%.go%.dev[%S]+%)")
if not url then
return nil
end
link = link:sub(2, -2)

local mod = get_mode_name()
if link:find(mod) then
url = url:sub(2, -2)
if is_internal(url) then
return nil
end
return link

return url
end

return nil
Expand Down Expand Up @@ -81,7 +101,7 @@ function M.handle()
return
end

local link = get_link_from_response(res_tbl)
local link = get_url_from_response(res_tbl)
if link then
return link
end
Expand Down

0 comments on commit 9a7ebe4

Please sign in to comment.