diff --git a/lua/gopher/_utils/ts.lua b/lua/gopher/_utils/ts.lua index 86cc316..c936434 100644 --- a/lua/gopher/_utils/ts.lua +++ b/lua/gopher/_utils/ts.lua @@ -27,6 +27,11 @@ local queries = { name: (type_identifier) @_name type: (interface_type)) ]], + var = [[ + [(var_declaration (var_spec name: (identifier) @_name)) + (short_var_declaration + left: (expression_list (identifier) @_name @_var))] + ]], } ---@param parent_type string[] @@ -147,4 +152,9 @@ function ts.get_interface_under_cursor(bufnr) return do_stuff(bufnr, { "type_declaration" }, queries.interface) 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 diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua index e7ee08e..76090ec 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -25,6 +25,11 @@ local function generate(bufnr, line) return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " " 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) if f_ok then return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " " diff --git a/spec/fixtures/comment/svar_input.go b/spec/fixtures/comment/svar_input.go new file mode 100644 index 0000000..fb75949 --- /dev/null +++ b/spec/fixtures/comment/svar_input.go @@ -0,0 +1,5 @@ +package main + +func varTest() { + s := "something" +} diff --git a/spec/fixtures/comment/svar_output.go b/spec/fixtures/comment/svar_output.go new file mode 100644 index 0000000..e8c917a --- /dev/null +++ b/spec/fixtures/comment/svar_output.go @@ -0,0 +1,6 @@ +package main + +func varTest() { + // s + s := "something" +} diff --git a/spec/fixtures/comment/var_input.go b/spec/fixtures/comment/var_input.go new file mode 100644 index 0000000..47f8257 --- /dev/null +++ b/spec/fixtures/comment/var_input.go @@ -0,0 +1,5 @@ +package main + +func test() { + var imAVar string +} diff --git a/spec/fixtures/comment/var_output.go b/spec/fixtures/comment/var_output.go new file mode 100644 index 0000000..e38aba1 --- /dev/null +++ b/spec/fixtures/comment/var_output.go @@ -0,0 +1,6 @@ +package main + +func test() { + // imAVar + var imAVar string +} diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index a6fb667..e8f54b1 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -50,6 +50,14 @@ comment["should add a comment on interface with many method"] = function() do_the_test("interface_many_method", { 5, 2 }) 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() do_the_test("empty", { 1, 1 }) end