go.nvim/lua/go/comment.lua
rayx 0e8cc90e30
Bugfix #160 (#167)
* snips

* update ts node spec format

* luasnip up and running

* bugfix alternate file

* Bring in more snippets

* updates for mockery 2.11

* snips for http handler and more

* update doc

* bench test

* for snips

* update treesitter query and api

* refact get_xxx_at_cursor

* update test spec

* update row to vim range

* update col number

* remove comments

* merge conflicts
2022-07-12 23:11:46 +10:00

86 lines
2.5 KiB
Lua

-- todo
-- for func name(args) rets {}
-- add cmts // name : rets
local comment = {}
local placeholder = _GO_NVIM_CFG.comment_placeholder or ""
local ulog = require("go.utils").log
local api = vim.api
local gen_comment = function()
local comments = nil
local ns = require("go.ts.go").get_package_node_at_pos()
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
comments = "// Package " .. ns.name .. " provides " .. ns.name
return comments, ns
end
ns = require("go.ts.go").get_func_method_node_at_pos()
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
ns = require("go.ts.go").get_struct_node_at_pos()
if ns ~= nil and ns ~= {} then
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
ns = require("go.ts.go").get_interface_node_at_pos()
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
ns = require("go.ts.go").get_type_node_at_pos()
if ns ~= nil and ns ~= {} then
-- ulog("parnode" .. vim.inspect(ns))
comments = "// " .. ns.name .. " " .. ns.type
return comments, ns
end
return ""
end
local wrap_comment = function(comment_line, ns)
if string.len(comment_line) > 0 and placeholder ~= nil and string.len(placeholder) > 0 then
return comment_line .. " " .. placeholder, ns
end
return comment_line, ns
end
comment.gen = function()
local row, col = unpack(api.nvim_win_get_cursor(0))
row, col = row, col + 1
local c, ns = wrap_comment(gen_comment())
local bufnr = api.nvim_get_current_buf()
if ns == nil then
-- nothing found
local ts_utils = require("nvim-treesitter.ts_utils")
ns = ts_utils.get_node_at_cursor()
local ts_query = vim.treesitter.query
local node_text = ts_query.get_node_text(ns, bufnr)
local line = api.nvim_get_current_line()
local regex = "^(%s+)"
local q = line:match(regex)
c = (q or "") .. "// " .. node_text
c, _ = wrap_comment(c, {})
vim.fn.append(row - 1, c)
vim.fn.cursor(row, #c + 1)
return
end
ulog(vim.inspect(ns))
row, col = ns.dim.s.r, ns.dim.s.c
ulog("set cursor " .. tostring(row))
api.nvim_win_set_cursor(0, { row, col })
-- insert doc
vim.fn.append(row - 1, c)
-- set curosr
vim.fn.cursor(row, #c + 1)
-- enter into insert mode
api.nvim_command("startinsert!")
return c
end
return comment