forked from theniceboy/nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_ternary.lua
64 lines (51 loc) · 1.51 KB
/
swap_ternary.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- swap_ternary.lua
local ts = require('vim.treesitter')
local get_node_text = ts.get_node_text
local ts_utils = require("nvim-treesitter.ts_utils") -- requires nvim-treesitter
local parsers = require("nvim-treesitter.parsers")
local M = {}
local function get_ternary_node(node)
if not node then
return
end
if node:type() ~= "conditional_expression" then
node = node:parent()
return get_ternary_node(node)
end
return node
end
function M.swap_ternary()
local bufnr = vim.api.nvim_get_current_buf()
local lang = parsers.get_buf_lang(bufnr)
if not parsers.has_parser(lang) then
print("No treesitter parser for current language")
return
end
local node = ts_utils.get_node_at_cursor(0)
local ternary_node = get_ternary_node(node)
if not ternary_node then
return
end
local t_consequence = ternary_node:field("consequence")
local result = ""
local t_alternate = ternary_node:field("alternative")
for i = 1, #t_alternate do
local text = get_node_text(t_alternate[i], bufnr)
result = result .. text
end
result = result .. " : "
for i = 1, #t_consequence do
local text = get_node_text(t_consequence[i], bufnr)
result = result .. text
end
local t = {}
local lines = string.gmatch(result, "[^\r\n]+")
for line in lines do
table.insert(t, line)
end
-- replace the ternary with the swapped text
local start_row, start_col = t_consequence[1]:start()
local end_row, end_col = t_alternate[#t_alternate]:end_()
vim.api.nvim_buf_set_text(bufnr, start_row, start_col, end_row, end_col, t)
end
return M