+2
Taskfile.yml
+2
Taskfile.yml
+13
-1
lua/gopher/_utils/init.lua
+13
-1
lua/gopher/_utils/init.lua
···
3
local utils = {}
4
5
---@param msg string
6
-
---@param lvl? number by default `vim.log.levels.INFO`
7
function utils.notify(msg, lvl)
8
lvl = lvl or vim.log.levels.INFO
9
vim.notify(msg, lvl, {
···
36
function utils.trimend(s)
37
local r, _ = string.gsub(s, "%s+$", "")
38
return r
39
end
40
41
return utils
···
3
local utils = {}
4
5
---@param msg string
6
+
---@param lvl? integer by default `vim.log.levels.INFO`
7
function utils.notify(msg, lvl)
8
lvl = lvl or vim.log.levels.INFO
9
vim.notify(msg, lvl, {
···
36
function utils.trimend(s)
37
local r, _ = string.gsub(s, "%s+$", "")
38
return r
39
+
end
40
+
41
+
-- Since indentation can be spaces or tabs, that's my hack around it
42
+
---@param line string
43
+
---@param indent integer
44
+
---@return string
45
+
function utils.indent(line, indent)
46
+
local char = string.sub(line, 1, 1)
47
+
if char ~= " " and char ~= "\t" then
48
+
char = " "
49
+
end
50
+
return string.rep(char, indent)
51
end
52
53
return utils
+40
-16
lua/gopher/_utils/ts.lua
+40
-16
lua/gopher/_utils/ts.lua
···
11
right: (expression_list (composite_literal
12
type: (struct_type))))]
13
]],
14
func = [[
15
[(function_declaration name: (identifier) @_name)
16
-
(method_declaration name: (field_identifier) @_name)]
17
]],
18
package = [[
19
(package_identifier) @_name
···
23
name: (type_identifier) @_name
24
type: (interface_type))
25
]],
26
}
27
28
---@param parent_type string[]
29
---@param node TSNode
30
---@return TSNode?
31
-
local function get_parrent_node(parent_type, node)
32
---@type TSNode?
33
local current = node
34
while current do
···
64
end
65
66
---@class gopher.TsResult
67
-
---@field name string
68
-
---@field start integer
69
-
---@field end_ integer
70
-
---@field is_varstruct boolean
71
72
---@param bufnr integer
73
---@param parent_type string[]
···
78
error "No treesitter parser found for go"
79
end
80
81
-
local node = vim.treesitter.get_node {
82
-
bufnr = bufnr,
83
-
}
84
if not node then
85
-
error "No nodes found under cursor"
86
end
87
88
-
local parent_node = get_parrent_node(parent_type, node)
89
if not parent_node then
90
-
error "No parent node found under cursor"
91
end
92
93
local q = vim.treesitter.query.parse("go", query)
94
local res = get_captures(q, parent_node, bufnr)
95
assert(res.name ~= nil, "No capture name found")
96
97
-
local start_row, _, end_row, _ = parent_node:range()
98
res["start"] = start_row + 1
99
res["end_"] = end_row + 1
100
···
104
---@param bufnr integer
105
function ts.get_struct_under_cursor(bufnr)
106
--- should be both type_spec and type_declaration
107
-
--- because in cases like `type ( T struct{}, U strict{} )`
108
-
--- i will be choosing always last struct in the list
109
---
110
--- var_declaration is for cases like `var x struct{}`
111
--- short_var_declaration is for cases like `x := struct{}{}`
112
return do_stuff(bufnr, {
113
"type_spec",
114
"type_declaration",
···
118
end
119
120
---@param bufnr integer
121
function ts.get_func_under_cursor(bufnr)
122
--- since this handles both and funcs and methods we should check for both parent nodes
123
-
return do_stuff(bufnr, { "function_declaration", "method_declaration" }, queries.func)
124
end
125
126
---@param bufnr integer
···
131
---@param bufnr integer
132
function ts.get_interface_under_cursor(bufnr)
133
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
134
end
135
136
return ts
···
11
right: (expression_list (composite_literal
12
type: (struct_type))))]
13
]],
14
+
struct_field = [[
15
+
(field_declaration name: (field_identifier) @_name)
16
+
]],
17
func = [[
18
[(function_declaration name: (identifier) @_name)
19
+
(method_declaration name: (field_identifier) @_name)
20
+
(method_elem name: (field_identifier) @_name)]
21
]],
22
package = [[
23
(package_identifier) @_name
···
27
name: (type_identifier) @_name
28
type: (interface_type))
29
]],
30
+
var = [[
31
+
[(var_declaration (var_spec name: (identifier) @_name))
32
+
(short_var_declaration
33
+
left: (expression_list (identifier) @_name @_var))]
34
+
]],
35
}
36
37
---@param parent_type string[]
38
---@param node TSNode
39
---@return TSNode?
40
+
local function get_parent_node(parent_type, node)
41
---@type TSNode?
42
local current = node
43
while current do
···
73
end
74
75
---@class gopher.TsResult
76
+
---@field name string Name of the struct, function, etc
77
+
---@field start integer Line number where the declaration starts
78
+
---@field end_ integer Line number where the declaration ends
79
+
---@field indent integer Number of spaces/tabs in the current cursor line
80
+
---@field is_varstruct boolean Is struct declared as `var S struct{}` or `s := struct{}{}`
81
82
---@param bufnr integer
83
---@param parent_type string[]
···
88
error "No treesitter parser found for go"
89
end
90
91
+
local node = vim.treesitter.get_node { bufnr = bufnr }
92
if not node then
93
+
error "No nodes found under the cursor"
94
end
95
96
+
local parent_node = get_parent_node(parent_type, node)
97
if not parent_node then
98
+
error "No parent node found under the cursor"
99
end
100
101
local q = vim.treesitter.query.parse("go", query)
102
local res = get_captures(q, parent_node, bufnr)
103
assert(res.name ~= nil, "No capture name found")
104
105
+
local start_row, start_col, end_row, _ = parent_node:range()
106
+
res["indent"] = start_col
107
res["start"] = start_row + 1
108
res["end_"] = end_row + 1
109
···
113
---@param bufnr integer
114
function ts.get_struct_under_cursor(bufnr)
115
--- should be both type_spec and type_declaration
116
+
--- because in cases like `type ( T struct{}, U struct{} )`
117
---
118
--- var_declaration is for cases like `var x struct{}`
119
--- short_var_declaration is for cases like `x := struct{}{}`
120
+
---
121
+
--- it always chooses last struct type in the list
122
return do_stuff(bufnr, {
123
"type_spec",
124
"type_declaration",
···
128
end
129
130
---@param bufnr integer
131
+
function ts.get_struct_field_under_cursor(bufnr)
132
+
return do_stuff(bufnr, { "field_declaration" }, queries.struct_field)
133
+
end
134
+
135
+
---@param bufnr integer
136
function ts.get_func_under_cursor(bufnr)
137
--- since this handles both and funcs and methods we should check for both parent nodes
138
+
return do_stuff(bufnr, {
139
+
"method_elem",
140
+
"function_declaration",
141
+
"method_declaration",
142
+
}, queries.func)
143
end
144
145
---@param bufnr integer
···
150
---@param bufnr integer
151
function ts.get_interface_under_cursor(bufnr)
152
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
153
+
end
154
+
155
+
---@param bufnr integer
156
+
function ts.get_variable_under_cursor(bufnr)
157
+
return do_stuff(bufnr, { "var_declaration", "short_var_declaration" }, queries.var)
158
end
159
160
return ts
+28
-16
lua/gopher/comment.lua
+28
-16
lua/gopher/comment.lua
···
7
8
local ts = require "gopher._utils.ts"
9
local log = require "gopher._utils.log"
10
local comment = {}
11
12
-
---@param name string
13
-
---@return string
14
-
---@dochide
15
-
local function template(name)
16
-
return "// " .. name .. " "
17
-
end
18
-
19
---@param bufnr integer
20
---@return string
21
---@dochide
22
-
local function generate(bufnr)
23
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
24
if s_ok then
25
-
return template(s_res.name)
26
end
27
28
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
29
if f_ok then
30
-
return template(f_res.name)
31
end
32
33
local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr)
34
if i_ok then
35
-
return template(i_res.name)
36
end
37
38
local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr)
···
45
46
function comment.comment()
47
local bufnr = vim.api.nvim_get_current_buf()
48
-
local cmt = generate(bufnr)
49
-
log.debug("generated comment: " .. cmt)
50
51
-
local pos = vim.fn.getcurpos()[2]
52
-
vim.fn.append(pos - 1, cmt)
53
-
vim.fn.setpos(".", { 0, pos, #cmt })
54
vim.cmd "startinsert!"
55
end
56
···
7
8
local ts = require "gopher._utils.ts"
9
local log = require "gopher._utils.log"
10
+
local u = require "gopher._utils"
11
local comment = {}
12
13
+
--- NOTE: The order of functions executed inside this function is IMPORTANT.
14
+
--- This function is extremely fragile; run tests after making any changes.
15
+
---
16
---@param bufnr integer
17
+
---@param line string
18
---@return string
19
---@dochide
20
+
local function generate(bufnr, line)
21
+
local sf_ok, sf_res = pcall(ts.get_struct_field_under_cursor, bufnr)
22
+
if sf_ok then
23
+
return u.indent(line, sf_res.indent) .. "// " .. sf_res.name .. " "
24
+
end
25
+
26
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
27
if s_ok then
28
+
return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " "
29
+
end
30
+
31
+
local v_ok, v_res = pcall(ts.get_variable_under_cursor, bufnr)
32
+
if v_ok then
33
+
return u.indent(line, v_res.indent) .. "// " .. v_res.name .. " "
34
end
35
36
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
37
if f_ok then
38
+
return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " "
39
end
40
41
local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr)
42
if i_ok then
43
+
return u.indent(line, i_res.indent) .. "// " .. i_res.name .. " "
44
end
45
46
local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr)
···
53
54
function comment.comment()
55
local bufnr = vim.api.nvim_get_current_buf()
56
+
local lnum = vim.fn.getcurpos()[2]
57
+
local line = vim.fn.getline(lnum)
58
+
local cmt = generate(bufnr, line)
59
+
log.debug("generated comment:", {
60
+
comment = cmt,
61
+
line = line,
62
+
})
63
64
+
vim.fn.append(lnum - 1, cmt)
65
+
vim.fn.setpos(".", { bufnr, lnum, #cmt })
66
vim.cmd "startinsert!"
67
end
68
+6
spec/fixtures/comment/interface_many_method_input.go
+6
spec/fixtures/comment/interface_many_method_input.go
+7
spec/fixtures/comment/interface_many_method_output.go
+7
spec/fixtures/comment/interface_many_method_output.go
+5
spec/fixtures/comment/interface_method_input.go
+5
spec/fixtures/comment/interface_method_input.go
+6
spec/fixtures/comment/interface_method_output.go
+6
spec/fixtures/comment/interface_method_output.go
+18
spec/fixtures/comment/many_structs_fields_input.go
+18
spec/fixtures/comment/many_structs_fields_input.go
+19
spec/fixtures/comment/many_structs_fields_output.go
+19
spec/fixtures/comment/many_structs_fields_output.go
+7
spec/fixtures/comment/struct_fields_input.go
+7
spec/fixtures/comment/struct_fields_input.go
+8
spec/fixtures/comment/struct_fields_output.go
+8
spec/fixtures/comment/struct_fields_output.go
+5
spec/fixtures/comment/svar_input.go
+5
spec/fixtures/comment/svar_input.go
+6
spec/fixtures/comment/svar_output.go
+6
spec/fixtures/comment/svar_output.go
+5
spec/fixtures/comment/var_input.go
+5
spec/fixtures/comment/var_input.go
+6
spec/fixtures/comment/var_output.go
+6
spec/fixtures/comment/var_output.go
+8
spec/fixtures/comment/var_struct_fields_input.go
+8
spec/fixtures/comment/var_struct_fields_input.go
+9
spec/fixtures/comment/var_struct_fields_output.go
+9
spec/fixtures/comment/var_struct_fields_output.go
+28
spec/integration/comment_test.lua
+28
spec/integration/comment_test.lua
···
18
do_the_test("struct", { 4, 1 })
19
end
20
21
comment["should add comment to function"] = function()
22
do_the_test("func", { 3, 1 })
23
end
···
28
29
comment["should add comment to interface"] = function()
30
do_the_test("interface", { 3, 6 })
31
end
32
33
comment["otherwise should add // above cursor"] = function()
···
18
do_the_test("struct", { 4, 1 })
19
end
20
21
+
comment["should add a comment on struct field"] = function()
22
+
do_the_test("struct_fields", { 5, 8 })
23
+
end
24
+
25
+
comment["should add a comment on var struct field"] = function()
26
+
do_the_test("var_struct_fields", { 6, 4 })
27
+
end
28
+
29
+
comment["should add a comment on one field of many structs"] = function()
30
+
do_the_test("many_structs_fields", { 10, 4 })
31
+
end
32
+
33
comment["should add comment to function"] = function()
34
do_the_test("func", { 3, 1 })
35
end
···
40
41
comment["should add comment to interface"] = function()
42
do_the_test("interface", { 3, 6 })
43
+
end
44
+
45
+
comment["should add comment on interface method"] = function()
46
+
do_the_test("interface_method", { 4, 2 })
47
+
end
48
+
49
+
comment["should add a comment on interface with many method"] = function()
50
+
do_the_test("interface_many_method", { 5, 2 })
51
+
end
52
+
53
+
comment["should add a comment on a var"] = function()
54
+
do_the_test("var", { 4, 2 })
55
+
end
56
+
57
+
comment["should add a comment on a short declared var"] = function()
58
+
do_the_test("svar", { 4, 8 })
59
end
60
61
comment["otherwise should add // above cursor"] = function()
+24
spec/unit/utils_test.lua
+24
spec/unit/utils_test.lua
···
22
t.eq(u.trimend " hi ", " hi")
23
end
24
25
+
utils["should add .indent() spaces"] = function()
26
+
local u = require "gopher._utils"
27
+
local line = " func Test() error {"
28
+
local indent = 4
29
+
30
+
t.eq(" ", u.indent(line, indent))
31
+
end
32
+
33
+
utils["should add .indent() a tab"] = function()
34
+
local u = require "gopher._utils"
35
+
local line = "\tfunc Test() error {"
36
+
local indent = 1
37
+
38
+
t.eq("\t", u.indent(line, indent))
39
+
end
40
+
41
+
utils["should add .indent() 2 tabs"] = function()
42
+
local u = require "gopher._utils"
43
+
local line = "\t\tfunc Test() error {"
44
+
local indent = 2
45
+
46
+
t.eq("\t\t", u.indent(line, indent))
47
+
end
48
+
49
return T