feat(comment): add comment on a struct field
This commit is contained in:
parent
3cd45c45a3
commit
9bffa2c212
9 changed files with 100 additions and 7 deletions
|
|
@ -11,6 +11,9 @@ local queries = {
|
||||||
right: (expression_list (composite_literal
|
right: (expression_list (composite_literal
|
||||||
type: (struct_type))))]
|
type: (struct_type))))]
|
||||||
]],
|
]],
|
||||||
|
struct_field = [[
|
||||||
|
(field_declaration name: (field_identifier) @_name)
|
||||||
|
]],
|
||||||
func = [[
|
func = [[
|
||||||
[(function_declaration name: (identifier) @_name)
|
[(function_declaration name: (identifier) @_name)
|
||||||
(method_declaration name: (field_identifier) @_name)
|
(method_declaration name: (field_identifier) @_name)
|
||||||
|
|
@ -80,16 +83,14 @@ local function do_stuff(bufnr, parent_type, query)
|
||||||
error "No treesitter parser found for go"
|
error "No treesitter parser found for go"
|
||||||
end
|
end
|
||||||
|
|
||||||
local node = vim.treesitter.get_node {
|
local node = vim.treesitter.get_node { bufnr = bufnr }
|
||||||
bufnr = bufnr,
|
|
||||||
}
|
|
||||||
if not node then
|
if not node then
|
||||||
error "No nodes found under cursor"
|
error "No nodes found under the cursor"
|
||||||
end
|
end
|
||||||
|
|
||||||
local parent_node = get_parrent_node(parent_type, node)
|
local parent_node = get_parrent_node(parent_type, node)
|
||||||
if not parent_node then
|
if not parent_node then
|
||||||
error "No parent node found under cursor"
|
error "No parent node found under the cursor"
|
||||||
end
|
end
|
||||||
|
|
||||||
local q = vim.treesitter.query.parse("go", query)
|
local q = vim.treesitter.query.parse("go", query)
|
||||||
|
|
@ -107,11 +108,12 @@ end
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function ts.get_struct_under_cursor(bufnr)
|
function ts.get_struct_under_cursor(bufnr)
|
||||||
--- should be both type_spec and type_declaration
|
--- should be both type_spec and type_declaration
|
||||||
--- because in cases like `type ( T struct{}, U strict{} )`
|
--- because in cases like `type ( T struct{}, U struct{} )`
|
||||||
--- i will be choosing always last struct in the list
|
|
||||||
---
|
---
|
||||||
--- var_declaration is for cases like `var x struct{}`
|
--- var_declaration is for cases like `var x struct{}`
|
||||||
--- short_var_declaration is for cases like `x := struct{}{}`
|
--- short_var_declaration is for cases like `x := struct{}{}`
|
||||||
|
---
|
||||||
|
--- it always chooses last struct type in the list
|
||||||
return do_stuff(bufnr, {
|
return do_stuff(bufnr, {
|
||||||
"type_spec",
|
"type_spec",
|
||||||
"type_declaration",
|
"type_declaration",
|
||||||
|
|
@ -120,6 +122,11 @@ function ts.get_struct_under_cursor(bufnr)
|
||||||
}, queries.struct)
|
}, queries.struct)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
function ts.get_struct_field_under_cursor(bufnr)
|
||||||
|
return do_stuff(bufnr, { "field_declaration" }, queries.struct_field)
|
||||||
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function ts.get_func_under_cursor(bufnr)
|
function ts.get_func_under_cursor(bufnr)
|
||||||
--- since this handles both and funcs and methods we should check for both parent nodes
|
--- since this handles both and funcs and methods we should check for both parent nodes
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,11 @@ local comment = {}
|
||||||
---@return string
|
---@return string
|
||||||
---@dochide
|
---@dochide
|
||||||
local function generate(bufnr, line)
|
local function generate(bufnr, line)
|
||||||
|
local sf_ok, sf_res = pcall(ts.get_struct_field_under_cursor, bufnr)
|
||||||
|
if sf_ok then
|
||||||
|
return u.indent(line, sf_res.indent) .. "// " .. sf_res.name .. " "
|
||||||
|
end
|
||||||
|
|
||||||
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
|
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
|
||||||
if s_ok then
|
if s_ok then
|
||||||
return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " "
|
return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " "
|
||||||
|
|
|
||||||
18
spec/fixtures/comment/many_structs_fields_input.go
vendored
Normal file
18
spec/fixtures/comment/many_structs_fields_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
|
||||||
|
}
|
||||||
|
)
|
||||||
19
spec/fixtures/comment/many_structs_fields_output.go
vendored
Normal file
19
spec/fixtures/comment/many_structs_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type (
|
||||||
|
TestOne struct {
|
||||||
|
Asdf string
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
TestTwo struct {
|
||||||
|
// Fesa
|
||||||
|
Fesa int
|
||||||
|
A bool
|
||||||
|
}
|
||||||
|
|
||||||
|
TestThree struct {
|
||||||
|
Asufj int
|
||||||
|
Fs string
|
||||||
|
}
|
||||||
|
)
|
||||||
7
spec/fixtures/comment/struct_fields_input.go
vendored
Normal file
7
spec/fixtures/comment/struct_fields_input.go
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type CommentStruct struct {
|
||||||
|
Name string
|
||||||
|
Address string
|
||||||
|
Aliases []string
|
||||||
|
}
|
||||||
8
spec/fixtures/comment/struct_fields_output.go
vendored
Normal file
8
spec/fixtures/comment/struct_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type CommentStruct struct {
|
||||||
|
Name string
|
||||||
|
// Address
|
||||||
|
Address string
|
||||||
|
Aliases []string
|
||||||
|
}
|
||||||
8
spec/fixtures/comment/var_struct_fields_input.go
vendored
Normal file
8
spec/fixtures/comment/var_struct_fields_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s struct {
|
||||||
|
API string
|
||||||
|
Key string
|
||||||
|
}
|
||||||
|
}
|
||||||
9
spec/fixtures/comment/var_struct_fields_output.go
vendored
Normal file
9
spec/fixtures/comment/var_struct_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s struct {
|
||||||
|
API string
|
||||||
|
// Key
|
||||||
|
Key string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,18 @@ comment["should add comment to struct"] = function()
|
||||||
do_the_test("struct", { 4, 1 })
|
do_the_test("struct", { 4, 1 })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on struct field"] = function()
|
||||||
|
do_the_test("struct_fields", { 5, 8 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on var struct field"] = function()
|
||||||
|
do_the_test("var_struct_fields", { 6, 4 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on one field of many structs"] = function()
|
||||||
|
do_the_test("many_structs_fields", { 10, 4 })
|
||||||
|
end
|
||||||
|
|
||||||
comment["should add comment to function"] = function()
|
comment["should add comment to function"] = function()
|
||||||
do_the_test("func", { 3, 1 })
|
do_the_test("func", { 3, 1 })
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue