-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathheadings.lua
205 lines (165 loc) · 4.15 KB
/
headings.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
local headings = {};
local utils = require("markview.utils");
--- Gets the heading under the cursor
---@return boolean
---@return string?
---@return table?
headings.get = function ()
local node = vim.treesitter.get_node();
while node:parent() do
if vim.list_contains({
"atx_heading",
"setext_heading"
}, node:type()) then
return true, node:type(), node;
end
node = node:parent();
end
return false, nil, nil;
end
---@class heading_control
---@field increase fun(node: table): nil
---@field decrease fun(node: table): nil
headings.atx = {
increase = function (node)
local text = vim.treesitter.get_node_text(node, vim.api.nvim_get_current_buf());
local markers = text:match("^([#]+)");
local range = { node:range() }
if #markers >= 6 then
return;
end
text = text:gsub("^" .. markers, markers .. "#");
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), range[1], range[3], false, {
text
});
end,
decrease = function (node)
local text = vim.treesitter.get_node_text(node, vim.api.nvim_get_current_buf());
local markers = text:match("^([#]+)");
local range = { node:range() }
if #markers == 1 then
return;
end
text = text:gsub("^" .. markers, vim.fn.strcharpart(markers, 0, vim.fn.strchars(markers) - 1));
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), range[1], range[3], false, {
text
});
end,
};
---@type heading_control
headings.setext = {
increase = function (node)
local marker = node:named_child(1);
if not marker then
return;
end
local text = vim.treesitter.get_node_text(marker, vim.api.nvim_get_current_buf());
local range = { marker:range() };
if text:match("^([-]+)$") then
return;
end
text = text:gsub("%=","%-");
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), range[1], range[3] + 1, false, {
text
});
end,
decrease = function (node)
local marker = node:named_child(1);
if not marker then
return;
end
local text = vim.treesitter.get_node_text(marker, vim.api.nvim_get_current_buf());
local range = { marker:range() };
if text:match("^([=]+)$") then
return;
end
text = text:gsub("%-","%=");
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), range[1], range[3] + 1, false, {
text
});
end
}
--- Increases heading level
headings.increase = function ()
local isHeading, nodeType, node = headings.get();
if isHeading == false then
return;
end
if nodeType == "atx_heading" then
headings.atx.increase(node --[[ @as table ]]);
else
headings.setext.increase(node --[[ @as table ]]);
end
end
--- Decreases heading level
headings.decrease = function ()
local isHeading, nodeType, node = headings.get();
if isHeading == false then
return;
end
if nodeType == "atx_heading" then
headings.atx.decrease(node --[[ @as table ]]);
else
headings.setext.decrease(node --[[ @as table ]]);
end
end
--- Commands for this module.
headings.__completion = utils.create_user_command_class({
default = {
completion = function (arg_lead)
local comp = {};
for _, item in ipairs({ "decrease", "increase" }) do
if item:match(arg_lead) then
table.insert(comp, item);
end
end
table.sort(comp);
return comp;
end,
action = function ()
headings.increase();
end
},
sub_commands = {
["decrease"] = {
action = function ()
headings.decrease();
end
},
["increase"] = {
action = function ()
headings.increase();
end
}
}
});
--- New command
vim.api.nvim_create_user_command("Heading", function (params)
headings.__completion:exec(params)
end, {
nargs = 1,
complete = function (...)
return headings.__completion:comp(...)
end
});
---+${lua, v24 commands}
vim.api.nvim_create_user_command("HeadingIncrease", function ()
require("markview.health").notify("deprecation", {
ignore = true,
option = ":HeadingIncrease",
alter = ":Heading increase"
});
headings.increase();
end, {});
vim.api.nvim_create_user_command("HeadingDecrease", function ()
require("markview.health").notify("deprecation", {
ignore = true,
option = ":HeadingDecrease",
alter = ":Heading decrease"
});
headings.decrease()
end, {});
---_
headings.setup = function ()
end
return headings;