[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

Add comment generator (#10)

* feat(_utils.ts): get package

* feat(_utils.ts): get interface

* feat(_utils.ts): update type annotations

* feat(comments): first naive implementation

* docs: add comment [skip ci]

authored by olexsmir.xyz and committed by GitHub 0fcdceeb 40a2839e

Changed files
+97 -2
lua
gopher
plugin
+8
README.md
··· 128 :GoGenerate % 129 ``` 130 131 ## Thanks 132 133 - [go.nvim](https://github.com/ray-x/go.nvim)
··· 128 :GoGenerate % 129 ``` 130 131 + 8. Generate doc comment 132 + 133 + First set a cursor on **public** package/function/interface/struct and execure: 134 + 135 + ```vim 136 + :GoCmt 137 + ``` 138 + 139 ## Thanks 140 141 - [go.nvim](https://github.com/ray-x/go.nvim)
+37 -2
lua/gopher/_utils/ts/init.lua
··· 4 querys = { 5 struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], 6 em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], 7 method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]], 8 func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]], 9 }, ··· 21 22 ---@param row string 23 ---@param col string 24 - ---@param bufnr string 25 ---@return table|nil 26 function M.get_struct_node_at_pos(row, col, bufnr) 27 local query = M.querys.struct_block .. " " .. M.querys.em_struct_block ··· 36 37 ---@param row string 38 ---@param col string 39 - ---@param bufnr string 40 ---@return table|nil 41 function M.get_func_method_node_at_pos(row, col, bufnr) 42 local query = M.querys.func .. " " .. M.querys.method_name ··· 44 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 45 if ns == nil then 46 u.notify("function not found", "warn") 47 else 48 return ns[#ns] 49 end
··· 4 querys = { 5 struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], 6 em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], 7 + package = [[(package_clause (package_identifier)@package.name)@package.clause]], 8 + interface = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], 9 method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]], 10 func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]], 11 }, ··· 23 24 ---@param row string 25 ---@param col string 26 + ---@param bufnr string|nil 27 ---@return table|nil 28 function M.get_struct_node_at_pos(row, col, bufnr) 29 local query = M.querys.struct_block .. " " .. M.querys.em_struct_block ··· 38 39 ---@param row string 40 ---@param col string 41 + ---@param bufnr string|nil 42 ---@return table|nil 43 function M.get_func_method_node_at_pos(row, col, bufnr) 44 local query = M.querys.func .. " " .. M.querys.method_name ··· 46 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 47 if ns == nil then 48 u.notify("function not found", "warn") 49 + else 50 + return ns[#ns] 51 + end 52 + end 53 + 54 + ---@param row string 55 + ---@param col string 56 + ---@param bufnr string|nil 57 + ---@return table|nil 58 + function M.get_package_node_at_pos(row, col, bufnr) 59 + -- stylua: ignore 60 + if row > 10 then return end 61 + local query = M.querys.package 62 + local bufn = bufnr or vim.api.nvim_get_current_buf() 63 + local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 64 + if ns == nil then 65 + u.notify("package not found", "warn") 66 + return nil 67 + else 68 + return ns[#ns] 69 + end 70 + end 71 + 72 + ---@param row string 73 + ---@param col string 74 + ---@param bufnr string|nil 75 + ---@return table|nil 76 + function M.get_interface_node_at_pos(row, col, bufnr) 77 + local query = M.querys.interface 78 + local bufn = bufnr or vim.api.nvim_get_current_buf() 79 + local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 80 + if ns == nil then 81 + u.notify("interface not found", "warn") 82 else 83 return ns[#ns] 84 end
+50
lua/gopher/comment.lua
···
··· 1 + local ts_utils = require "gopher._utils.ts" 2 + 3 + local function generate(row, col) 4 + local comment, ns = nil, nil 5 + 6 + ns = ts_utils.get_package_node_at_pos(row, col) 7 + if ns ~= nil and ns ~= {} then 8 + comment = "// Package " .. ns.name .. " provides " .. ns.name 9 + return comment, ns 10 + end 11 + 12 + ns = ts_utils.get_struct_node_at_pos(row, col) 13 + if ns ~= nil and ns ~= {} then 14 + comment = "// " .. ns.name .. " " .. ns.type .. " " 15 + return comment, ns 16 + end 17 + 18 + ns = ts_utils.get_func_method_node_at_pos(row, col) 19 + if ns ~= nil and ns ~= {} then 20 + comment = "// " .. ns.name .. " " .. ns.type .. " " 21 + return comment, ns 22 + end 23 + 24 + ns = ts_utils.get_interface_node_at_pos(row, col) 25 + if ns ~= nil and ns ~= {} then 26 + comment = "// " .. ns.name .. " " .. ns.type .. " " 27 + return comment, ns 28 + end 29 + 30 + return "// ", {} 31 + end 32 + 33 + return function() 34 + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) 35 + local comment, ns = generate(row + 1, col + 1) 36 + 37 + vim.api.nvim_win_set_cursor(0, { 38 + ns.dim.s.r, 39 + ns.dim.s.c, 40 + }) 41 + 42 + vim.fn.append(row - 1, comment) 43 + 44 + vim.api.nvim_win_set_cursor(0, { 45 + ns.dim.s.r, 46 + #comment + 1, 47 + }) 48 + 49 + vim.cmd [[startinsert!]] 50 + end
+1
lua/gopher/init.lua
··· 9 gopher.get = require "gopher.goget" 10 gopher.impl = require "gopher.impl" 11 gopher.generate = require "gopher.gogenerate" 12 gopher.test_add = gotests.func_test 13 gopher.test_exported = gotests.all_exported_tests 14 gopher.tests_all = gotests.all_tests
··· 9 gopher.get = require "gopher.goget" 10 gopher.impl = require "gopher.impl" 11 gopher.generate = require "gopher.gogenerate" 12 + gopher.comment = require "gopher.comment" 13 gopher.test_add = gotests.func_test 14 gopher.test_exported = gotests.all_exported_tests 15 gopher.tests_all = gotests.all_tests
+1
plugin/gopher.vim
··· 7 command! -nargs=* GoGet :lua require"gopher".get(<f-args>) 8 command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>) 9 command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>) 10 command! GoInstallDeps :lua require"gopher".install_deps()
··· 7 command! -nargs=* GoGet :lua require"gopher".get(<f-args>) 8 command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>) 9 command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>) 10 + command! GoCmt :lua require"gopher".comment() 11 command! GoInstallDeps :lua require"gopher".install_deps()