Skip to content

Commit

Permalink
builtins/gomodifytags: allow more places and add transform tag (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngavinsir authored Nov 17, 2024
1 parent fb7acad commit 537ff5d
Showing 1 changed file with 86 additions and 11 deletions.
97 changes: 86 additions & 11 deletions lua/null-ls/builtins/code_actions/gomodifytags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ return h.make_builtin({
local col = params.range.col

-- Execution helpers
local exec = function(struct_name, field_name, op, tag)
local exec = function(struct_name, field_name, flags)
local cmd_opts = vim.list_extend(
{ opts.command },
opts.args or {} -- customizable common args
Expand All @@ -45,7 +45,7 @@ return h.make_builtin({
if field_name ~= nil then
vim.list_extend(cmd_opts, { "-field", field_name })
end
vim.list_extend(cmd_opts, { op, tag })
vim.list_extend(cmd_opts, flags)

vim.fn.execute(":update") -- write when the buffer has been modified
local cmd = table.concat(cmd_opts, " ")
Expand All @@ -61,9 +61,26 @@ return h.make_builtin({
if tag == nil then
return
end
exec(struct_name, field_name, op, tag)
exec(struct_name, field_name, { op, tag })
end)
end
local get_transform_and_get_input_tag_and_exec = function(struct_name, field_name, op)
vim.ui.select(
{ "snakecase", "camelcase", "lispcase", "pascalcase", "titlecase", "keep" },
{},
function(transform)
if transform == nil then
return
end
vim.ui.input({ prompt = "Enter tags: " }, function(tag)
if tag == nil then
return
end
exec(struct_name, field_name, { op, tag, "-transform", transform })
end)
end
)
end
local add_tags = function(struct_name, field_name)
return {
title = "gomodifytags: add tags",
Expand All @@ -72,6 +89,14 @@ return h.make_builtin({
end,
}
end
local add_tags_with_transform = function(struct_name, field_name)
return {
title = "gomodifytags: add tags with transform",
action = function()
get_transform_and_get_input_tag_and_exec(struct_name, field_name, "-add-tags")
end,
}
end
local remove_tags = function(struct_name, field_name)
return {
title = "gomodifytags: remove tags",
Expand All @@ -84,7 +109,7 @@ return h.make_builtin({
return {
title = "gomodifytags: clear tags",
action = function()
exec(struct_name, field_name, "-clear-tags", nil)
exec(struct_name, field_name, { "-clear-tags", nil })
end,
}
end
Expand All @@ -108,7 +133,7 @@ return h.make_builtin({
return {
title = "gomodifytags: clear options",
action = function()
exec(struct_name, field_name, "-clear-options", nil)
exec(struct_name, field_name, { "-clear-options", nil })
end,
}
end
Expand All @@ -128,12 +153,41 @@ return h.make_builtin({
return actions
end

-- Ops on struct
if (tsnode:type()) == "type_identifier" then
local tspnode = tsnode:parent()
if tspnode == nil or tspnode:type() ~= "type_spec" then
return
if tsnode:type() == "type_declaration" then
if tsnode:child_count() > 1 then
local c = tsnode:child(1)
if c ~= nil then
tsnode = c
end
end
end

if tsnode:type() == "field_declaration_list" then
local p = tsnode:parent()
if p ~= nil then
tsnode = p
end
end

if tsnode:type() == "struct_type" then
local p = tsnode:parent()
if p ~= nil then
tsnode = p
end
end

if tsnode:type() == "type_spec" then
if tsnode:child_count() > 0 then
local c = tsnode:child(0)
if c ~= nil then
tsnode = c
end
end
end

-- Ops on struct
local tspnode = tsnode:parent()
if (tsnode:type()) == "type_identifier" and tspnode ~= nil and tspnode:type() == "type_spec" then
local typename_node = tspnode:field("type")[1]
if typename_node == nil or typename_node:type() ~= "struct_type" then
return
Expand All @@ -145,6 +199,7 @@ return h.make_builtin({
end

table.insert(actions, add_tags(struct_name))
table.insert(actions, add_tags_with_transform(struct_name))
table.insert(actions, remove_tags(struct_name))
table.insert(actions, clear_tags(struct_name))

Expand All @@ -154,10 +209,29 @@ return h.make_builtin({
return actions
end

while tsnode:parent() ~= nil do
if tsnode:type() == "field_declaration" then
break
end
local p = tsnode:parent()
if p ~= nil then
tsnode = p
end
end

if tsnode:type() == "field_declaration" then
if tsnode:child_count() > 0 then
local c = tsnode:child(0)
if c ~= nil then
tsnode = c
end
end
end

-- Ops on struct field
if (tsnode:type()) == "field_identifier" then
local field_name = treesitter_get_node_text(tsnode, 0)
local tspnode = tsnode:parent():parent():parent()
tspnode = tsnode:parent():parent():parent()
if tspnode ~= nil and (tspnode:type()) == "struct_type" then
tspnode = tspnode:parent():child(0)
struct_name = treesitter_get_node_text(tspnode, 0)
Expand All @@ -168,6 +242,7 @@ return h.make_builtin({
end

table.insert(actions, add_tags(struct_name, field_name))
table.insert(actions, add_tags_with_transform(struct_name, field_name))
table.insert(actions, remove_tags(struct_name, field_name))
table.insert(actions, clear_tags(struct_name, field_name))

Expand Down

0 comments on commit 537ff5d

Please sign in to comment.