diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua new file mode 100644 index 0000000..3991651 --- /dev/null +++ b/lua/gopher/comment.lua @@ -0,0 +1,50 @@ +local ts_utils = require "gopher._utils.ts" + +local function generate(row, col) + local comment, ns = nil, nil + + ns = ts_utils.get_package_node_at_pos(row, col) + if ns ~= nil and ns ~= {} then + comment = "// Package " .. ns.name .. " provides " .. ns.name + return comment, ns + end + + ns = ts_utils.get_struct_node_at_pos(row, col) + if ns ~= nil and ns ~= {} then + comment = "// " .. ns.name .. " " .. ns.type .. " " + return comment, ns + end + + ns = ts_utils.get_func_method_node_at_pos(row, col) + if ns ~= nil and ns ~= {} then + comment = "// " .. ns.name .. " " .. ns.type .. " " + return comment, ns + end + + ns = ts_utils.get_interface_node_at_pos(row, col) + if ns ~= nil and ns ~= {} then + comment = "// " .. ns.name .. " " .. ns.type .. " " + return comment, ns + end + + return "// ", {} +end + +return function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + local comment, ns = generate(row + 1, col + 1) + + vim.api.nvim_win_set_cursor(0, { + ns.dim.s.r, + ns.dim.s.c, + }) + + vim.fn.append(row - 1, comment) + + vim.api.nvim_win_set_cursor(0, { + ns.dim.s.r, + #comment + 1, + }) + + vim.cmd [[startinsert!]] +end diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 99ac4dd..3cf438d 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -9,6 +9,7 @@ gopher.mod = require "gopher.gomod" gopher.get = require "gopher.goget" gopher.impl = require "gopher.impl" gopher.generate = require "gopher.gogenerate" +gopher.comment = require "gopher.comment" gopher.test_add = gotests.func_test gopher.test_exported = gotests.all_exported_tests gopher.tests_all = gotests.all_tests diff --git a/plugin/gopher.vim b/plugin/gopher.vim index 234d99e..942cc04 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -7,4 +7,5 @@ command! -nargs=* GoMod :lua require"gopher".mod() command! -nargs=* GoGet :lua require"gopher".get() command! -nargs=* GoImpl :lua require"gopher".impl() command! -nargs=* GoGenerate :lua require"gopher".generate() +command! GoCmt :lua require"gopher".comment() command! GoInstallDeps :lua require"gopher".install_deps()