feat(comment): add comment on a variable

This commit is contained in:
Oleksandr Smirnov 2025-09-04 15:52:48 +03:00
parent 9bffa2c212
commit 094afb2c21
No known key found for this signature in database
7 changed files with 45 additions and 0 deletions

View file

@ -27,6 +27,11 @@ local queries = {
name: (type_identifier) @_name name: (type_identifier) @_name
type: (interface_type)) type: (interface_type))
]], ]],
var = [[
[(var_declaration (var_spec name: (identifier) @_name))
(short_var_declaration
left: (expression_list (identifier) @_name @_var))]
]],
} }
---@param parent_type string[] ---@param parent_type string[]
@ -147,4 +152,9 @@ function ts.get_interface_under_cursor(bufnr)
return do_stuff(bufnr, { "type_declaration" }, queries.interface) return do_stuff(bufnr, { "type_declaration" }, queries.interface)
end end
---@param bufnr integer
function ts.get_variable_under_cursor(bufnr)
return do_stuff(bufnr, { "var_declaration", "short_var_declaration" }, queries.var)
end
return ts return ts

View file

@ -25,6 +25,11 @@ local function generate(bufnr, line)
return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " " return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " "
end end
local v_ok, v_res = pcall(ts.get_variable_under_cursor, bufnr)
if v_ok then
return u.indent(line, v_res.indent) .. "// " .. v_res.name .. " "
end
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr) local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
if f_ok then if f_ok then
return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " " return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " "

5
spec/fixtures/comment/svar_input.go vendored Normal file
View file

@ -0,0 +1,5 @@
package main
func varTest() {
s := "something"
}

6
spec/fixtures/comment/svar_output.go vendored Normal file
View file

@ -0,0 +1,6 @@
package main
func varTest() {
// s
s := "something"
}

5
spec/fixtures/comment/var_input.go vendored Normal file
View file

@ -0,0 +1,5 @@
package main
func test() {
var imAVar string
}

6
spec/fixtures/comment/var_output.go vendored Normal file
View file

@ -0,0 +1,6 @@
package main
func test() {
// imAVar
var imAVar string
}

View file

@ -50,6 +50,14 @@ comment["should add a comment on interface with many method"] = function()
do_the_test("interface_many_method", { 5, 2 }) do_the_test("interface_many_method", { 5, 2 })
end end
comment["should add a comment on a var"] = function()
do_the_test("var", { 4, 2 })
end
comment["should add a comment on a short declared var"] = function()
do_the_test("svar", { 4, 8 })
end
comment["otherwise should add // above cursor"] = function() comment["otherwise should add // above cursor"] = function()
do_the_test("empty", { 1, 1 }) do_the_test("empty", { 1, 1 })
end end