From 3cd45c45a3159fe29222c6e4c96da34de79425a3 Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Thu, 4 Sep 2025 14:17:58 +0300 Subject: [PATCH] feat(comment): add comment on on interface method --- lua/gopher/_utils/init.lua | 11 ++++++++- lua/gopher/_utils/ts.lua | 13 +++++++--- lua/gopher/comment.lua | 24 ++++++++++++------- .../comment/interface_many_method_input.go | 6 +++++ .../comment/interface_many_method_output.go | 7 ++++++ .../comment/interface_method_input.go | 5 ++++ .../comment/interface_method_output.go | 6 +++++ spec/integration/comment_test.lua | 8 +++++++ spec/unit/utils_test.lua | 24 +++++++++++++++++++ 9 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 spec/fixtures/comment/interface_many_method_input.go create mode 100644 spec/fixtures/comment/interface_many_method_output.go create mode 100644 spec/fixtures/comment/interface_method_input.go create mode 100644 spec/fixtures/comment/interface_method_output.go diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index e176345..1d717a9 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -3,7 +3,7 @@ local log = require "gopher._utils.log" local utils = {} ---@param msg string ----@param lvl? number by default `vim.log.levels.INFO` +---@param lvl? integer by default `vim.log.levels.INFO` function utils.notify(msg, lvl) lvl = lvl or vim.log.levels.INFO vim.notify(msg, lvl, { @@ -38,4 +38,13 @@ function utils.trimend(s) return r end +-- Since indentation can be spaces or tab, that's my hack around it +---@param line string +---@param indent integer +---@return string +function utils.indent(line, indent) + local char = string.sub(line, 1, 1) + return string.rep(char, indent) +end + return utils diff --git a/lua/gopher/_utils/ts.lua b/lua/gopher/_utils/ts.lua index a0623ad..0b88b64 100644 --- a/lua/gopher/_utils/ts.lua +++ b/lua/gopher/_utils/ts.lua @@ -13,7 +13,8 @@ local queries = { ]], func = [[ [(function_declaration name: (identifier) @_name) - (method_declaration name: (field_identifier) @_name)] + (method_declaration name: (field_identifier) @_name) + (method_elem name: (field_identifier) @_name)] ]], package = [[ (package_identifier) @_name @@ -67,6 +68,7 @@ end ---@field name string ---@field start integer ---@field end_ integer +---@field indent integer ---@field is_varstruct boolean ---@param bufnr integer @@ -94,7 +96,8 @@ local function do_stuff(bufnr, parent_type, query) local res = get_captures(q, parent_node, bufnr) assert(res.name ~= nil, "No capture name found") - local start_row, _, end_row, _ = parent_node:range() + local start_row, start_col, end_row, _ = parent_node:range() + res["indent"] = start_col res["start"] = start_row + 1 res["end_"] = end_row + 1 @@ -120,7 +123,11 @@ end ---@param bufnr integer function ts.get_func_under_cursor(bufnr) --- since this handles both and funcs and methods we should check for both parent nodes - return do_stuff(bufnr, { "function_declaration", "method_declaration" }, queries.func) + return do_stuff(bufnr, { + "method_elem", + "function_declaration", + "method_declaration", + }, queries.func) end ---@param bufnr integer diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua index e5cecd8..0192e51 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -7,25 +7,27 @@ local ts = require "gopher._utils.ts" local log = require "gopher._utils.log" +local u = require "gopher._utils" local comment = {} ---@param bufnr integer +---@param line string ---@return string ---@dochide -local function generate(bufnr) +local function generate(bufnr, line) local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr) if s_ok then - return "// " .. s_res.name .. " " + return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " " end local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr) if f_ok then - return "// " .. f_res.name .. " " + return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " " end local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr) if i_ok then - return "// " .. i_res.name .. " " + return u.indent(line, i_res.indent) .. "// " .. i_res.name .. " " end local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr) @@ -38,12 +40,16 @@ end function comment.comment() local bufnr = vim.api.nvim_get_current_buf() - local cmt = generate(bufnr) - log.debug("generated comment: " .. cmt) + local lnum = vim.fn.getcurpos()[2] + local line = vim.fn.getline(lnum) + local cmt = generate(bufnr, line) + log.debug("generated comment:", { + comment = cmt, + line = line, + }) - local pos = vim.fn.getcurpos()[2] - vim.fn.append(pos - 1, cmt) - vim.fn.setpos(".", { 0, pos, #cmt }) + vim.fn.append(lnum - 1, cmt) + vim.fn.setpos(".", { bufnr, lnum, #cmt }) vim.cmd "startinsert!" end diff --git a/spec/fixtures/comment/interface_many_method_input.go b/spec/fixtures/comment/interface_many_method_input.go new file mode 100644 index 0000000..e5dca33 --- /dev/null +++ b/spec/fixtures/comment/interface_many_method_input.go @@ -0,0 +1,6 @@ +package main + +type Testinger interface { + Get(id string) int + Set(id string, val int) +} diff --git a/spec/fixtures/comment/interface_many_method_output.go b/spec/fixtures/comment/interface_many_method_output.go new file mode 100644 index 0000000..cdeb97c --- /dev/null +++ b/spec/fixtures/comment/interface_many_method_output.go @@ -0,0 +1,7 @@ +package main + +type Testinger interface { + Get(id string) int + // Set + Set(id string, val int) +} diff --git a/spec/fixtures/comment/interface_method_input.go b/spec/fixtures/comment/interface_method_input.go new file mode 100644 index 0000000..7e39a39 --- /dev/null +++ b/spec/fixtures/comment/interface_method_input.go @@ -0,0 +1,5 @@ +package main + +type Testinger interface { + Method(input string) error +} diff --git a/spec/fixtures/comment/interface_method_output.go b/spec/fixtures/comment/interface_method_output.go new file mode 100644 index 0000000..5fa2388 --- /dev/null +++ b/spec/fixtures/comment/interface_method_output.go @@ -0,0 +1,6 @@ +package main + +type Testinger interface { + // Method + Method(input string) error +} diff --git a/spec/integration/comment_test.lua b/spec/integration/comment_test.lua index fdf39fe..b727239 100644 --- a/spec/integration/comment_test.lua +++ b/spec/integration/comment_test.lua @@ -30,6 +30,14 @@ comment["should add comment to interface"] = function() do_the_test("interface", { 3, 6 }) end +comment["should add comment on interface method"] = function() + do_the_test("interface_method", { 4, 2 }) +end + +comment["should add a comment on interface with many method"] = function() + do_the_test("interface_many_method", { 5, 2 }) +end + comment["otherwise should add // above cursor"] = function() do_the_test("empty", { 1, 1 }) end diff --git a/spec/unit/utils_test.lua b/spec/unit/utils_test.lua index d4898c2..2d8f0bc 100644 --- a/spec/unit/utils_test.lua +++ b/spec/unit/utils_test.lua @@ -22,4 +22,28 @@ utils["should .trimend()"] = function() t.eq(u.trimend " hi ", " hi") end +utils["should add .indent() spaces"] = function() + local u = require "gopher._utils" + local line = " func Test() error {" + local indent = 4 + + t.eq(" ", u.indent(line, indent)) +end + +utils["should add .indent() a tab"] = function() + local u = require "gopher._utils" + local line = "\tfunc Test() error {" + local indent = 1 + + t.eq("\t", u.indent(line, indent)) +end + +utils["should add .indent() 2 tabs"] = function() + local u = require "gopher._utils" + local line = "\t\tfunc Test() error {" + local indent = 2 + + t.eq("\t\t", u.indent(line, indent)) +end + return T