@@ -1,40 +0,0 @@
-name: docs - -on: - push: - branches: - - main - - develop - pull_request: - -jobs: - docs: - name: linters - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Install Task - uses: arduino/setup-task@v1 - with: - version: 3.x - repo-token: ${{ secrets.GITHUB_TOKEN }} - - - name: Install NeoVim - uses: rhysd/action-setup-vim@v1 - with: - neovim: true - version: stable - - - name: Cache .tests - uses: actions/cache@v4 - with: - path: | - ${{ github.workspace }}/.tests - key: ${{ runner.os }}-tests-${{ hashFiles('${{ github.workspace }}/.tests') }} - - - name: Generate docs - run: task docgen - - - name: Check docs diff - run: exit $(git status --porcelain doc | wc -l | tr -d " ")
@@ -9,7 +9,7 @@ pull_request:
jobs: linters: - name: linters + name: Lua runs-on: ubuntu-latest steps: - uses: actions/checkout@v4@@ -23,3 +23,34 @@ - uses: NTBBloodbath/selene-action@v1.0.0
with: token: ${{ secrets.GITHUB_TOKEN }} args: . + + docs: + name: Docs + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Task + uses: arduino/setup-task@v1 + with: + version: 3.x + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install NeoVim + uses: rhysd/action-setup-vim@v1 + with: + neovim: true + version: stable + + - name: Cache .tests + uses: actions/cache@v4 + with: + path: | + ${{ github.workspace }}/.tests + key: ${{ runner.os }}-tests-${{ hashFiles('${{ github.workspace }}/.tests') }} + + - name: Generate docs + run: task docgen + + - name: Diff + run: exit $(git status --porcelain doc | wc -l | tr -d " ")
@@ -14,7 +14,7 @@ matrix:
os: [ubuntu-latest] version: - stable - - nightly + # - nightly # TODO: enable when stable runs-on: ${{ matrix.os }} steps: - name: Install Task
@@ -91,7 +91,7 @@
local log_at_level = function(level_config, message_maker, ...) -- Return early if we're below the current_log_level -- - -- the log level source get from config directly because otherwise it doesnt work + -- the log level source get from config directly because otherwise it doesn't work if level_config.level < c.log_level then return end
@@ -28,7 +28,7 @@ ---@param args string[]
---@return string[]|nil function gocmd.run(subcmd, args) if #args == 0 then - error "please provice any arguments" + error "please provide any arguments" end if subcmd == "get" then@@ -45,7 +45,7 @@ on_exit = function(data, status)
if status ~= 0 then error("gocmd failed: " .. data) end - u.notify(c.go .. " " .. subcmd .. " successful runned") + u.notify(c.go .. " " .. subcmd .. " ran successful") end, }) end
@@ -0,0 +1,115 @@
+local ts = {} +local queries = { + struct = [[ + (type_spec name: (type_identifier) @_name + type: (struct_type)) + ]], + func = [[ + [(function_declaration name: (identifier) @_name) + (method_declaration name: (field_identifier) @_name)] + ]], + package = [[ + (package_identifier) @_name + ]], + interface = [[ + (type_spec + name: (type_identifier) @_name + type: (interface_type)) + ]], +} + +---@param parent_type string[] +---@param node TSNode +---@return TSNode? +local function get_parrent_node(parent_type, node) + ---@type TSNode? + local current = node + while current do + if vim.tbl_contains(parent_type, current:type()) then + break + end + + current = current:parent() + if current == nil then + return nil + end + end + return current +end + +---@param query vim.treesitter.Query +---@param node TSNode +---@param bufnr integer +---@return {name:string} +local function get_captures(query, node, bufnr) + local res = {} + for _, match, _ in query:iter_matches(node, bufnr) do + for capture_id, captured_node in pairs(match) do + local capture_name = query.captures[capture_id] + if capture_name == "_name" then + res["name"] = vim.treesitter.get_node_text(captured_node, bufnr) + end + end + end + + return res +end + +---@class gopher.TsResult +---@field name string +---@field start_line integer +---@field end_line integer + +---@param bufnr integer +---@param parent_type string[] +---@param query string +---@return gopher.TsResult +local function do_stuff(bufnr, parent_type, query) + local node = vim.treesitter.get_node { + bufnr = bufnr, + } + if not node then + error "No nodes found under cursor" + end + + local parent_node = get_parrent_node(parent_type, node) + if not parent_node then + error "No parent node found under cursor" + end + + local q = vim.treesitter.query.parse("go", 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() + res["start_line"] = start_row + 1 + res["end_line"] = end_row + 1 + + return res +end + +---@param bufnr integer +function ts.get_struct_under_cursor(bufnr) + --- should be both type_spec and type_declaration + --- because in cases like `type ( T struct{}, U strict{} )` + --- i will be choosing always last struct in the list + return do_stuff(bufnr, { "type_spec", "type_declaration" }, queries.struct) +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) +end + +---@param bufnr integer +function ts.get_package_under_cursor(bufnr) + return do_stuff(bufnr, { "package_clause" }, queries.package) +end + +---@param bufnr integer +function ts.get_interface_under_cursor(bufnr) + return do_stuff(bufnr, { "type_declaration" }, queries.interface) +end + +return ts
@@ -1,104 +0,0 @@
----@diagnostic disable: param-type-mismatch -local nodes = require "gopher._utils.ts.nodes" -local u = require "gopher._utils" -local ts = { - querys = { - struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], - em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], - package = [[(package_clause (package_identifier)@package.name)@package.clause]], - interface = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], - method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]], - func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]], - }, -} - ----@return table -local function get_name_defaults() - return { - ["func"] = "function", - ["if"] = "if", - ["else"] = "else", - ["for"] = "for", - } -end - ----@param row string ----@param col string ----@param bufnr string|nil ----@param do_notify boolean|nil ----@return table|nil -function ts.get_struct_node_at_pos(row, col, bufnr, do_notify) - local notify = do_notify or true - local query = ts.querys.struct_block .. " " .. ts.querys.em_struct_block - local bufn = bufnr or vim.api.nvim_get_current_buf() - local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) - if ns == nil then - if notify then - u.deferred_notify("struct not found", vim.log.levels.WARN) - end - else - return ns[#ns] - end -end - ----@param row string ----@param col string ----@param bufnr string|nil ----@param do_notify boolean|nil ----@return table|nil -function ts.get_func_method_node_at_pos(row, col, bufnr, do_notify) - local notify = do_notify or true - local query = ts.querys.func .. " " .. ts.querys.method_name - local bufn = bufnr or vim.api.nvim_get_current_buf() - local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) - if ns == nil then - if notify then - u.deferred_notify("function not found", vim.log.levels.WARN) - end - else - return ns[#ns] - end -end - ----@param row string ----@param col string ----@param bufnr string|nil ----@param do_notify boolean|nil ----@return table|nil -function ts.get_package_node_at_pos(row, col, bufnr, do_notify) - local notify = do_notify or true - -- stylua: ignore - if row > 10 then return end - local query = ts.querys.package - local bufn = bufnr or vim.api.nvim_get_current_buf() - local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) - if ns == nil then - if notify then - u.deferred_notify("package not found", vim.log.levels.WARN) - return nil - end - else - return ns[#ns] - end -end - ----@param row string ----@param col string ----@param bufnr string|nil ----@param do_notify boolean|nil ----@return table|nil -function ts.get_interface_node_at_pos(row, col, bufnr, do_notify) - local notify = do_notify or true - local query = ts.querys.interface - local bufn = bufnr or vim.api.nvim_get_current_buf() - local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) - if ns == nil then - if notify then - u.deferred_notify("interface not found", vim.log.levels.WARN) - end - else - return ns[#ns] - end -end - -return ts
@@ -1,143 +0,0 @@
-local ts_query = require "nvim-treesitter.query" -local parsers = require "nvim-treesitter.parsers" -local locals = require "nvim-treesitter.locals" -local u = require "gopher._utils" -local M = {} - -local function intersects(row, col, sRow, sCol, eRow, eCol) - if sRow > row or eRow < row then - return false - end - - if sRow == row and sCol > col then - return false - end - - if eRow == row and eCol < col then - return false - end - - return true -end - ----@param nodes table ----@param row string ----@param col string ----@return table -function M.intersect_nodes(nodes, row, col) - local found = {} - for idx = 1, #nodes do - local node = nodes[idx] - local sRow = node.dim.s.r - local sCol = node.dim.s.c - local eRow = node.dim.e.r - local eCol = node.dim.e.c - - if intersects(row, col, sRow, sCol, eRow, eCol) then - table.insert(found, node) - end - end - - return found -end - ----@param nodes table ----@return table -function M.sort_nodes(nodes) - table.sort(nodes, function(a, b) - return M.count_parents(a) < M.count_parents(b) - end) - - return nodes -end - ----@param query string ----@param lang string ----@param bufnr integer ----@param pos_row string ----@return string -function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) - bufnr = bufnr or 0 - pos_row = pos_row or 30000 - - local ok, parsed_query = pcall(function() - return vim.treesitter.query.parse(lang, query) - end) - if not ok then - return nil - end - - local parser = parsers.get_parser(bufnr, lang) - local root = parser:parse()[1]:root() - local start_row, _, end_row, _ = root:range() - local results = {} - - for match in ts_query.iter_prepared_matches(parsed_query, root, bufnr, start_row, end_row) do - local sRow, sCol, eRow, eCol, declaration_node - local type, name, op = "", "", "" - locals.recurse_local_nodes(match, function(_, node, path) - local idx = string.find(path, ".[^.]*$") - op = string.sub(path, idx + 1, #path) - type = string.sub(path, 1, idx - 1) - - if op == "name" then - name = vim.treesitter.get_node_text(node, bufnr) - elseif op == "declaration" or op == "clause" then - declaration_node = node - sRow, sCol, eRow, eCol = node:range() - sRow = sRow + 1 - eRow = eRow + 1 - sCol = sCol + 1 - eCol = eCol + 1 - end - end) - - if declaration_node ~= nil then - table.insert(results, { - declaring_node = declaration_node, - dim = { s = { r = sRow, c = sCol }, e = { r = eRow, c = eCol } }, - name = name, - operator = op, - type = type, - }) - end - end - - return results -end - ----@param query string ----@param default string ----@param bufnr string ----@param row string ----@param col string ----@return table -function M.nodes_at_cursor(query, default, bufnr, row, col) - bufnr = bufnr or vim.api.nvim_get_current_buf() - local ft = vim.api.nvim_buf_get_option(bufnr, "ft") - if row == nil or col == nil then - row, col = unpack(vim.api.nvim_win_get_cursor(0)) - end - - local nodes = M.get_all_nodes(query, ft, default, bufnr, row, col) - if nodes == nil then - u.deferred_notify( - "Unable to find any nodes. Place your cursor on a go symbol and try again", - vim.log.levels.DEBUG - ) - return nil - end - - nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col)) - if nodes == nil or #nodes == 0 then - u.deferred_notify( - "Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col), - vim.log.levels.DEBUG - ) - return nil - end - - return nodes -end - -return M
@@ -3,57 +3,53 @@ ---@tag gopher.nvim-comments
---@usage Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line. ---@text This module provides a way to generate comments for Go code. +local ts = require "gopher._utils.ts" local log = require "gopher._utils.log" +local comment = {} -local function generate(row, col) - local ts_utils = require "gopher._utils.ts" - local comment, ns = nil, nil +---@param name string +---@return string +---@private +local function template(name) + return "// " .. name .. " " +end - ns = ts_utils.get_package_node_at_pos(row, col, nil, false) - if ns ~= nil then - comment = "// Package " .. ns.name .. " provides " .. ns.name - return comment, ns +---@param bufnr integer +---@return string +---@private +local function generate(bufnr) + local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr) + if s_ok then + return template(s_res.name) end - ns = ts_utils.get_struct_node_at_pos(row, col, nil, false) - if ns ~= nil then - comment = "// " .. ns.name .. " " .. ns.type .. " " - return comment, ns + local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr) + if f_ok then + return template(f_res.name) end - ns = ts_utils.get_func_method_node_at_pos(row, col, nil, false) - if ns ~= nil then - comment = "// " .. ns.name .. " " .. ns.type .. " " - return comment, ns + local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr) + if i_ok then + return template(i_res.name) end - ns = ts_utils.get_interface_node_at_pos(row, col, nil, false) - if ns ~= nil then - comment = "// " .. ns.name .. " " .. ns.type .. " " - return comment, ns + local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr) + if p_ok then + return "// Package " .. p_res.name .. " provides " end - return "// ", {} + return "// " end -return function() - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - local comment, ns = generate(row + 1, col + 1) +function comment.comment() + local bufnr = vim.api.nvim_get_current_buf() + local cmt = generate(bufnr) + log.debug("generated comment: " .. cmt) - log.debug("generated comment: " .. comment) - - vim.api.nvim_win_set_cursor(0, { - ns.dim.s.r, - ns.dim.s.c, - }) - - ---@diagnostic disable-next-line: param-type-mismatch - vim.fn.append(row - 1, comment) + local pos = vim.fn.getcurpos()[2] + vim.fn.append(pos - 1, cmt) + vim.fn.setpos(".", { 0, pos, #cmt }) + vim.cmd "startinsert!" +end - vim.api.nvim_win_set_cursor(0, { - ns.dim.s.r, - #comment + 1, - }) - - vim.cmd [[startinsert!]] -end +return comment
@@ -77,13 +77,10 @@ end
-- generate unit test for one function function gotests.func_test() - local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) - if ns == nil or ns.name == nil then - u.notify("cursor on func/method and execute the command again", vim.log.levels.WARN) - return - end + local bufnr = vim.api.nvim_get_current_buf() + local func = ts_utils.get_func_under_cursor(bufnr) - add_test { "-only", ns.name } + add_test { "-only", func.name } end -- generate unit tests for all functions in current file
@@ -38,48 +38,22 @@ local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils" local impl = {} ----@return string ----@private -local function get_struct() - local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) - if ns == nil then - u.notify "put cursor on a struct or specify a receiver" - return "" - end - - vim.api.nvim_win_set_cursor(0, { - ns.dim.e.r, - ns.dim.e.c, - }) - - return ns.name -end - function impl.impl(...) local args = { ... } - local iface, recv_name = "", "" - local recv = get_struct() + local iface, recv = "", "" + local bufnr = vim.api.nvim_get_current_buf() - if #args == 0 then - iface = vim.fn.input "impl: generating method stubs for interface: " - vim.cmd "redraw!" - if iface == "" then - u.deferred_notify("usage: GoImpl f *File io.Reader", vim.log.levels.INFO) - return - end - elseif #args == 1 then -- :GoImpl io.Reader - recv = string.lower(recv) .. " *" .. recv - vim.cmd "redraw!" - iface = select(1, ...) + if #args == 1 then -- :GoImpl io.Reader + local st = ts_utils.get_struct_under_cursor(bufnr) + iface = args[1] + recv = string.lower(st.name) .. " *" .. st.name elseif #args == 2 then -- :GoImpl w io.Writer - recv_name = select(1, ...) - recv = string.format("%s *%s", recv_name, recv) - iface = select(#args, ...) - elseif #args > 2 then - iface = select(#args, ...) - recv = select(#args - 1, ...) - recv_name = select(#args - 2, ...) - recv = string.format("%s %s", recv_name, recv) + local st = ts_utils.get_struct_under_cursor(bufnr) + iface = args[2] + recv = args[1] .. " *" .. st.name + elseif #args == 3 then -- :GoImpl r Struct io.Reader + recv = args[1] .. " *" .. args[2] + iface = args[3] end local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface }
@@ -38,7 +38,7 @@ gopher.install_deps = require("gopher.installer").install_deps
gopher.impl = require("gopher.impl").impl gopher.iferr = require("gopher.iferr").iferr -gopher.comment = require "gopher.comment" +gopher.comment = require("gopher.comment").comment gopher.tags = { add = tags.add,
@@ -0,0 +1,5 @@
+package main + +func Test(a int) bool { + return false +}
@@ -0,0 +1,6 @@
+package main + +// Test +func Test(a int) bool { + return false +}
@@ -0,0 +1,3 @@
+package main + +type Testinger interface{}
@@ -0,0 +1,4 @@
+package main + +// Testinger +type Testinger interface{}
@@ -0,0 +1,7 @@
+package main + +type Method struct{} + +func (Method) Run() error { + return nil +}
@@ -0,0 +1,8 @@
+package main + +type Method struct{} + +// Run +func (Method) Run() error { + return nil +}
@@ -1,2 +1,2 @@
-// Package main provides main +// Package main provides package main
@@ -0,0 +1,4 @@
+package main + +// CommentStruct +type CommentStruct struct{}
@@ -1,6 +1,6 @@
package main -func (r Read2) Read(p []byte) (n int, err error) { +func (r *Read2) Read(p []byte) (n int, err error) { panic("not implemented") // TODO: Implement }
@@ -5,23 +5,50 @@ local T = MiniTest.new_set {
hooks = { post_once = child.stop, pre_case = function() - MiniTest.skip "This module should be fixed first" child.restart { "-u", t.mininit_path } end, }, } + +local function do_the_test(fixture, pos) + local tmp = t.tmpfile() + local fixtures = t.get_fixtures("comment/" .. fixture) + t.writefile(tmp, fixtures.input) + + child.cmd("silent edit " .. tmp) + child.fn.setpos(".", { child.fn.bufnr "%", unpack(pos) }) + child.cmd "GoCmt" + child.cmd "write" + + t.eq(t.readfile(tmp), fixtures.output) + + -- without it all other(not even from this module) tests are falling + t.deletefile(tmp) +end + T["comment"] = MiniTest.new_set {} +T["comment"]["should add comment to package"] = function() + do_the_test("package", { 1, 1 }) +end -T["comment"]["should add comment to package"] = function() end +T["comment"]["should add comment to struct"] = function() + do_the_test("struct", { 4, 1 }) +end -T["comment"]["should add comment to struct"] = function() end +T["comment"]["should add comment to function"] = function() + do_the_test("func", { 3, 1 }) +end -T["comment"]["should add comment to function"] = function() end +T["comment"]["should add comment to method"] = function() + do_the_test("method", { 5, 1 }) +end -T["comment"]["should add comment to method"] = function() end - -T["comment"]["should add comment to interface"] = function() end +T["comment"]["should add comment to interface"] = function() + do_the_test("interface", { 3, 6 }) +end -T["comment"]["otherwise should add // above cursor"] = function() end +T["comment"]["otherwise should add // above cursor"] = function() + do_the_test("empty", { 1, 1 }) +end return T
@@ -16,7 +16,7 @@ local fixtures = t.get_fixtures "impl/writer"
t.writefile(tmp, fixtures.input) child.cmd("silent edit " .. tmp) - child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6 }) + child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 0 }) child.cmd "GoImpl w io.Writer" child.cmd "write"
@@ -31,6 +31,11 @@ function testutils.writefile(fpath, contents)
vim.fn.writefile(vim.split(contents, "\n"), fpath) end +---@param fpath string +function testutils.deletefile(fpath) + vim.fn.delete(fpath) +end + ---@param fixture string ---@return {input: string, output: string} function testutils.get_fixtures(fixture)