refactor: treesitter utils (#91)
* refactor(ts_utils): i dont know why event it was here * fix: typos * fix(struct_tags)!: remove statement that i used for debug * refactor(ts_util): start from scratch * refactor(struct_tags): use new ts_util * fixup! refactor(struct_tags): use new ts_util * test(struct_tags): add support for multiple structs * fix(gotests): use new api * fix(impl): refactor some logic, use new api * docs(ts): add an explanation * refactor(_utils.ts): all public methods are just adapters * fix(comment): now it works * fixup! refactor(_utils.ts): all public methods are just adapters * fixup! fixup! refactor(_utils.ts): all public methods are just adapters * test(comment): e2e * tests(comment): fix * refactor(utils.ts): fix, docs * test(comment): fix tests again * fix(tests/comments): well, now i fell stupid * refactor(ts): add assert just to be sure that all elements are in the result * fix(ts): type annotations * fix(ts): pass bufnr to vim.treesitter.get_node * chore(ci): disable nightly * chore(ci): reorganize
This commit is contained in:
parent
f171953e43
commit
e9f2eef5e7
31 changed files with 346 additions and 409 deletions
40
.github/workflows/docs.yml
vendored
40
.github/workflows/docs.yml
vendored
|
|
@ -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 " ")
|
||||
33
.github/workflows/linters.yml
vendored
33
.github/workflows/linters.yml
vendored
|
|
@ -9,7 +9,7 @@ on:
|
|||
|
||||
jobs:
|
||||
linters:
|
||||
name: linters
|
||||
name: Lua
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
|
@ -23,3 +23,34 @@ jobs:
|
|||
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 " ")
|
||||
|
|
|
|||
2
.github/workflows/tests.yml
vendored
2
.github/workflows/tests.yml
vendored
|
|
@ -14,7 +14,7 @@ jobs:
|
|||
os: [ubuntu-latest]
|
||||
version:
|
||||
- stable
|
||||
- nightly
|
||||
# - nightly # TODO: enable when stable
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Install Task
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ do
|
|||
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 @@ end
|
|||
---@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 @@ function gocmd.run(subcmd, args)
|
|||
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
|
||||
|
|
|
|||
115
lua/gopher/_utils/ts.lua
Normal file
115
lua/gopher/_utils/ts.lua
Normal file
|
|
@ -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 @@
|
|||
---@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
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
return "// ", {}
|
||||
---@param name string
|
||||
---@return string
|
||||
---@private
|
||||
local function template(name)
|
||||
return "// " .. name .. " "
|
||||
end
|
||||
|
||||
return function()
|
||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
local comment, ns = generate(row + 1, col + 1)
|
||||
---@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
|
||||
|
||||
log.debug("generated comment: " .. comment)
|
||||
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
|
||||
if f_ok then
|
||||
return template(f_res.name)
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, {
|
||||
ns.dim.s.r,
|
||||
ns.dim.s.c,
|
||||
})
|
||||
local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr)
|
||||
if i_ok then
|
||||
return template(i_res.name)
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: param-type-mismatch
|
||||
vim.fn.append(row - 1, comment)
|
||||
local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr)
|
||||
if p_ok then
|
||||
return "// Package " .. p_res.name .. " provides "
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, {
|
||||
ns.dim.s.r,
|
||||
#comment + 1,
|
||||
})
|
||||
|
||||
vim.cmd [[startinsert!]]
|
||||
return "// "
|
||||
end
|
||||
|
||||
function comment.comment()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local cmt = generate(bufnr)
|
||||
log.debug("generated comment: " .. cmt)
|
||||
|
||||
local pos = vim.fn.getcurpos()[2]
|
||||
vim.fn.append(pos - 1, cmt)
|
||||
vim.fn.setpos(".", { 0, pos, #cmt })
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -33,23 +33,11 @@ local struct_tags = {}
|
|||
|
||||
local function modify(...)
|
||||
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
|
||||
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
|
||||
if ns == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- by struct name of line pos
|
||||
local cmd_args = {}
|
||||
if ns.name == nil then
|
||||
local _, csrow, _, _ = unpack(vim.fn.getpos ".")
|
||||
table.insert(cmd_args, "-line")
|
||||
table.insert(cmd_args, csrow)
|
||||
else
|
||||
table.insert(cmd_args, "-struct")
|
||||
table.insert(cmd_args, ns.name)
|
||||
end
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local struct = ts_utils.get_struct_under_cursor(bufnr)
|
||||
|
||||
-- set user args for cmd
|
||||
local cmd_args = {}
|
||||
local arg = { ... }
|
||||
for _, v in ipairs(arg) do
|
||||
table.insert(cmd_args, v)
|
||||
|
|
@ -61,6 +49,8 @@ local function modify(...)
|
|||
c.gotag.transform,
|
||||
"-format",
|
||||
"json",
|
||||
"-struct",
|
||||
struct.name,
|
||||
"-w",
|
||||
"-file",
|
||||
fpath,
|
||||
|
|
@ -96,7 +86,6 @@ end
|
|||
function struct_tags.add(...)
|
||||
local user_tags = { ... }
|
||||
if #user_tags == 0 then
|
||||
vim.print("c.gotag.default_tag", c.gotag.default_tag)
|
||||
user_tags = { c.gotag.default_tag }
|
||||
end
|
||||
|
||||
|
|
|
|||
0
spec/fixtures/comment/empty_input.go
vendored
Normal file
0
spec/fixtures/comment/empty_input.go
vendored
Normal file
2
spec/fixtures/comment/empty_output.go
vendored
Normal file
2
spec/fixtures/comment/empty_output.go
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
//
|
||||
|
||||
5
spec/fixtures/comment/func_input.go
vendored
Normal file
5
spec/fixtures/comment/func_input.go
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package main
|
||||
|
||||
func Test(a int) bool {
|
||||
return false
|
||||
}
|
||||
6
spec/fixtures/comment/func_output.go
vendored
Normal file
6
spec/fixtures/comment/func_output.go
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package main
|
||||
|
||||
// Test
|
||||
func Test(a int) bool {
|
||||
return false
|
||||
}
|
||||
3
spec/fixtures/comment/interface_input.go
vendored
Normal file
3
spec/fixtures/comment/interface_input.go
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package main
|
||||
|
||||
type Testinger interface{}
|
||||
4
spec/fixtures/comment/interface_output.go
vendored
Normal file
4
spec/fixtures/comment/interface_output.go
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package main
|
||||
|
||||
// Testinger
|
||||
type Testinger interface{}
|
||||
7
spec/fixtures/comment/method_input.go
vendored
Normal file
7
spec/fixtures/comment/method_input.go
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
type Method struct{}
|
||||
|
||||
func (Method) Run() error {
|
||||
return nil
|
||||
}
|
||||
8
spec/fixtures/comment/method_output.go
vendored
Normal file
8
spec/fixtures/comment/method_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
|
||||
type Method struct{}
|
||||
|
||||
// Run
|
||||
func (Method) Run() error {
|
||||
return nil
|
||||
}
|
||||
2
spec/fixtures/comment/package_output.go
vendored
2
spec/fixtures/comment/package_output.go
vendored
|
|
@ -1,2 +1,2 @@
|
|||
// Package main provides main
|
||||
// Package main provides
|
||||
package main
|
||||
|
|
|
|||
3
spec/fixtures/comment/struct_input.go
vendored
Normal file
3
spec/fixtures/comment/struct_input.go
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package main
|
||||
|
||||
type CommentStruct struct{}
|
||||
4
spec/fixtures/comment/struct_output.go
vendored
Normal file
4
spec/fixtures/comment/struct_output.go
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package main
|
||||
|
||||
// CommentStruct
|
||||
type CommentStruct struct{}
|
||||
2
spec/fixtures/impl/reader_output.go
vendored
2
spec/fixtures/impl/reader_output.go
vendored
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
18
spec/fixtures/tags/many_input.go
vendored
Normal file
18
spec/fixtures/tags/many_input.go
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
type (
|
||||
TestOne struct {
|
||||
Asdf string
|
||||
ID int
|
||||
}
|
||||
|
||||
TestTwo struct {
|
||||
Fesa int
|
||||
A bool
|
||||
}
|
||||
|
||||
TestThree struct {
|
||||
Asufj int
|
||||
Fs string
|
||||
}
|
||||
)
|
||||
18
spec/fixtures/tags/many_output.go
vendored
Normal file
18
spec/fixtures/tags/many_output.go
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
type (
|
||||
TestOne struct {
|
||||
Asdf string
|
||||
ID int
|
||||
}
|
||||
|
||||
TestTwo struct {
|
||||
Fesa int `testing:"fesa"`
|
||||
A bool `testing:"a"`
|
||||
}
|
||||
|
||||
TestThree struct {
|
||||
Asufj int
|
||||
Fs string
|
||||
}
|
||||
)
|
||||
|
|
@ -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()
|
||||
do_the_test("interface", { 3, 6 })
|
||||
end
|
||||
|
||||
T["comment"]["should add comment to interface"] = function() 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 @@ T["impl"]["works w io.Writer"] = function()
|
|||
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"
|
||||
|
||||
|
|
|
|||
|
|
@ -34,4 +34,16 @@ T["struct_tags"]["works remove"] = function()
|
|||
t.eq(t.readfile(tmp), fixtures.output)
|
||||
end
|
||||
|
||||
T["struct_tags"]["works many structs"] = function()
|
||||
local tmp = t.tmpfile()
|
||||
local fixtures = t.get_fixtures "tags/many"
|
||||
t.writefile(tmp, fixtures.input)
|
||||
|
||||
child.cmd("silent edit " .. tmp)
|
||||
child.fn.setpos(".", { child.fn.bufnr "%", 10, 3, 0 })
|
||||
child.cmd "GoTagAdd testing"
|
||||
|
||||
t.eq(t.readfile(tmp), fixtures.output)
|
||||
end
|
||||
|
||||
return T
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue