go.nvim/lua/go/fixplurals.lua

43 lines
1.3 KiB
Lua
Raw Normal View History

2022-05-03 14:14:55 +00:00
-- lua implementation of the fixplurals
local ts_utils = require("nvim-treesitter.ts_utils")
local info = require("go.utils").info
2023-04-04 11:55:20 +00:00
local get_node_text = require('go.utils').get_node_text
2022-05-22 10:11:14 +00:00
local function fixplurals()
2022-05-03 14:14:55 +00:00
local n = ts_utils.get_node_at_cursor()
local p = n:parent()
if p:type() ~= "parameter_declaration" then
return info("not in parameter declaration")
end
if p:named_child_count() ~= 2 then
return info("no plural parameter")
end
local type_node = p:named_child(1)
local type = get_node_text(type_node, 0)
local edits = {}
while ts_utils.get_next_node(p) ~= nil do
local next_node = ts_utils.get_next_node(p)
if next_node:type() == "parameter_declaration" then
2022-06-02 03:25:08 +00:00
local type_node2 = next_node:named_child(1)
2022-05-03 14:14:55 +00:00
local type_next = get_node_text(type_node2, 0)
if type == type_next then
2022-06-02 03:25:08 +00:00
local range1 = ts_utils.node_to_lsp_range(p:named_child(1))
2022-05-03 14:14:55 +00:00
range1["start"]["character"] = range1["start"]["character"] - 1
local edit1 = { range = range1, newText = "" }
2022-06-02 03:25:08 +00:00
table.insert(edits, 1, edit1)
2022-05-03 14:14:55 +00:00
end
2022-06-02 03:25:08 +00:00
2022-05-03 14:14:55 +00:00
p = next_node
else
break
end
end
2022-06-02 03:25:08 +00:00
2022-05-03 14:14:55 +00:00
if #edits == 0 then
return info("no plural parameter")
end
vim.lsp.util.apply_text_edits(edits, 0, "utf-8")
end
return { fixplurals = fixplurals }