Skip to content

Commit 4136a82

Browse files
authored
feat: go import handler based on treesitter (#47)
1 parent 3fec4be commit 4136a82

File tree

8 files changed

+79
-0
lines changed

8 files changed

+79
-0
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
path = test/vendor/plenary.nvim
77
url = https://github.com/nvim-lua/plenary.nvim
88
ignore = dirty
9+
[submodule "test/vendor/nvim-treesitter"]
10+
path = test/vendor/nvim-treesitter
11+
url = https://github.com/nvim-treesitter/nvim-treesitter
12+
ignore = dirty

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* open github issues directly in the browser (e.g. `Fixes #22` opens `https://github.com/chrishrb/gx.nvim/issues/22`)
1414
* dependencies from `package.json` (e.g. line `"express": "^4.18.2",` in the `package.json` opens `https://www.npmjs.com/package/express`)
1515
* formulae and casks from `Brewfile` (e.g. line `brew "neovim"` in the `Brewfile` opens `https://formulae.brew.sh/formula/neovim`)
16+
* go packages from an import statement (e.g. line `import "github.com/joho/godotenv"` opens `https://pkg.go.dev/github.com/joho/godotenv`)
1617
* if there is no url found under the cursor, the word/selection is automatically searched on the web
1718
* supports user defined handlers to extend the functionality
1819
* support for macOS, Linux and Windows
@@ -51,6 +52,7 @@ require("lazy").setup({
5152
brewfile = true, -- open Homebrew formulaes and casks
5253
package_json = true, -- open dependencies from package.json
5354
search = true, -- search the web/selection on the web if nothing else is found
55+
go = true, -- open pkg.go.dev from an import statement (uses treesitter)
5456
jira = { -- custom handler to open Jira tickets (these have higher precedence than builtin handlers)
5557
name = "jira", -- set name of handler
5658
handle = function(mode, line, _)

lua/gx/handler.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local package_json_handler = require("gx.handlers.package_json")
44
local plugin_handler = require("gx.handlers.plugin")
55
local url_handler = require("gx.handlers.url")
66
local github_handler = require("gx.handlers.github")
7+
local go_handler = require("gx.handlers.go")
78
local commit_handler = require("gx.handlers.commit")
89
local markdown_handler = require("gx.handlers.markdown")
910
local cve_handler = require("gx.handlers.cve")
@@ -41,6 +42,7 @@ local function resolve_handlers(handlers)
4142
add_handler(resolved, plugin_handler, handlers.plugin and exists.plugin == nil)
4243
add_handler(resolved, github_handler, handlers.github and exists.github == nil)
4344
add_handler(resolved, commit_handler, handlers.commit and exists.commit == nil)
45+
add_handler(resolved, go_handler, handlers.go and exists.go == nil)
4446
add_handler(resolved, markdown_handler, handlers.markdown and exists.markdown == nil)
4547
add_handler(resolved, cve_handler, handlers.cve and exists.cve == nil)
4648
add_handler(resolved, url_handler, handlers.url and exists.url == nil)

lua/gx/handlers/go.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local M = {
2+
-- every filetype and filename
3+
name = "go",
4+
filetype = { "go" },
5+
filename = nil,
6+
}
7+
8+
function M.handle()
9+
local node = vim.treesitter.get_node()
10+
if not node then
11+
return
12+
end
13+
if node:type() ~= "import_spec" then
14+
if node:type() == "import_declaration" then
15+
node = node:named_child(0)
16+
else
17+
node = node:parent()
18+
end
19+
if node:type() ~= "import_spec" then
20+
return
21+
end
22+
end
23+
24+
local path_node = node:field("path")[1]
25+
local start_line, start_col, end_line, end_col = path_node:range()
26+
27+
local line = vim.api.nvim_buf_get_lines(0, start_line, end_line + 1, false)[1]
28+
local pkg = line:sub(start_col + 2, end_col - 1) -- remove quotes
29+
30+
return "https://pkg.go.dev/" .. pkg
31+
end
32+
33+
return M

test/fixtures/test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "fmt"
4+
5+
import myio "io"
6+
7+
import (
8+
"github.com/joho/godotenv"
9+
corev1 "k8s.io/api/core/v1"
10+
)

test/spec.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set rtp^=test/vendor/plenary.nvim/
22
set rtp^=test/vendor/matcher_combinators.lua/
3+
set rtp^=test/vendor/nvim-treesitter/
34
set rtp^=./
45

56
runtime plugin/plenary.vim
@@ -11,3 +12,6 @@ lua require('matcher_combinators.luassert')
1112
runtime plugin/gx.lua
1213
" lua require('gx').setup({ name = 'Jane Doe' })
1314
lua require('gx').setup()
15+
16+
" needed for go handler testing
17+
lua require('nvim-treesitter.configs').setup({ ensure_installed = { "go" }, sync_install = true })
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local handler = require("gx.handlers.go")
2+
3+
describe("go handler works", function()
4+
vim.cmd("edit test/fixtures/test.go")
5+
local function test_line(line, startpos, endpos, expected)
6+
for i = startpos, endpos do
7+
vim.api.nvim_win_set_cursor(0, { line, i })
8+
assert.equals(expected, handler.handle())
9+
end
10+
end
11+
it("parses single line imports", function()
12+
test_line(3, 0, 11, "https://pkg.go.dev/fmt")
13+
end)
14+
it("parses single line named imports", function()
15+
test_line(5, 0, 15, "https://pkg.go.dev/io")
16+
end)
17+
it("parses imports in list", function()
18+
test_line(8, 1, 26, "https://pkg.go.dev/github.com/joho/godotenv")
19+
end)
20+
it("parses named imports in list", function()
21+
test_line(9, 1, 27, "https://pkg.go.dev/k8s.io/api/core/v1")
22+
end)
23+
end)

test/vendor/nvim-treesitter

Submodule nvim-treesitter added at 19f69a5

0 commit comments

Comments
 (0)