Skip to content

Commit 0ed141a

Browse files
chore: replace usage of table.insert with list[#list + 1]
1 parent d228513 commit 0ed141a

27 files changed

+181
-180
lines changed

doc/custom-handlers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ local function parse_python(ctx)
9292
local capture = query.captures[id]
9393
local start_row, _, _, _ = node:range()
9494
if capture == 'def' then
95-
table.insert(marks, {
95+
marks[#marks + 1] = {
9696
conceal = true,
9797
start_row = start_row,
9898
start_col = 0,

lua/render-markdown/command.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function M.matches(prefix, values)
5353
local result = {}
5454
for _, value in ipairs(values) do
5555
if vim.startswith(value, prefix) then
56-
table.insert(result, value)
56+
result[#result + 1] = value
5757
end
5858
end
5959
return result

lua/render-markdown/core/conceal.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Conceal:add(row, entry)
5353
else
5454
-- If the section is covered by an existing one don't add it
5555
if entry.width > 0 and not self:has(line, entry) then
56-
table.insert(line.sections, entry)
56+
line.sections[#line.sections + 1] = entry
5757
end
5858
end
5959
end

lua/render-markdown/core/context.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function Context.new(props, offset)
3737

3838
local ranges = {}
3939
for _, window in ipairs(Env.buf.windows(props.buf)) do
40-
table.insert(ranges, Context.compute_range(props.buf, window, offset))
40+
ranges[#ranges + 1] = Context.compute_range(props.buf, window, offset)
4141
end
4242
self.ranges = Range.coalesce(ranges)
4343
self.callouts = {}
@@ -137,7 +137,8 @@ function Context:add_offset(row, offset)
137137
if self.offsets[row] == nil then
138138
self.offsets[row] = {}
139139
end
140-
table.insert(self.offsets[row], offset)
140+
local offsets = self.offsets[row]
141+
offsets[#offsets + 1] = offset
141142
end
142143

143144
---@private

lua/render-markdown/core/log.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function M.add(level, name, ...)
8484
for i = 1, select('#', ...) do
8585
local value = select(i, ...)
8686
local message = type(value) == 'string' and value or vim.inspect(value)
87-
table.insert(messages, message)
87+
messages[#messages + 1] = message
8888
end
8989
---@type render.md.log.Entry
9090
local entry = {
@@ -93,7 +93,7 @@ function M.add(level, name, ...)
9393
name = name,
9494
message = table.concat(messages, ' | '),
9595
}
96-
table.insert(M.entries, entry)
96+
M.entries[#M.entries + 1] = entry
9797
-- Periodically flush logs to disk
9898
if #M.entries > 1000 then
9999
M.flush()

lua/render-markdown/core/range.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function Range.coalesce(ranges)
5252
if range.top <= current.bottom + 1 then
5353
current.bottom = math.max(current.bottom, range.bottom)
5454
else
55-
table.insert(result, range)
55+
result[#result + 1] = range
5656
end
5757
end
5858
return result

lua/render-markdown/core/ui.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function M.get_row_marks(buf, win)
5656
local marks = {}
5757
for _, extmark in ipairs(buffer:get_marks()) do
5858
if extmark:inside(hidden) then
59-
table.insert(marks, extmark:get())
59+
marks[#marks + 1] = extmark:get()
6060
end
6161
end
6262
return row, marks
@@ -205,7 +205,7 @@ function M.parse_buffer(props)
205205
parser:for_each_tree(function(tree, language_tree)
206206
local language = language_tree:lang()
207207
if language == 'markdown' then
208-
table.insert(markdown_roots, tree:root())
208+
markdown_roots[#markdown_roots + 1] = tree:root()
209209
else
210210
vim.list_extend(marks, M.parse_tree({ buf = buf, root = tree:root() }, language))
211211
end

lua/render-markdown/debug/diff.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ end
3030
function M.append_keys(keys, t)
3131
for key in pairs(t) do
3232
if not vim.tbl_contains(keys, key) then
33-
table.insert(keys, key)
33+
keys[#keys + 1] = key
3434
end
3535
end
3636
end

lua/render-markdown/debug/marks.lua

Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
local Iter = require('render-markdown.lib.iter')
22

3+
---@class render.md.debug.Range
4+
---@field [1] integer
5+
---@field [2]? integer
6+
37
---@class render.md.debug.Mark
48
---@field conceal boolean
59
---@field opts render.md.MarkOpts
6-
---@field row { [1]: integer, [2]: integer }
7-
---@field col { [1]: integer, [2]: integer }
10+
---@field row render.md.debug.Range
11+
---@field col render.md.debug.Range
812
local Mark = {}
913
Mark.__index = Mark
1014

@@ -13,126 +17,122 @@ Mark.__index = Mark
1317
function Mark.new(mark)
1418
local self = setmetatable({}, Mark)
1519
self.conceal, self.opts = mark.conceal, mark.opts
16-
self.row = { mark.start_row, mark.opts.end_row or mark.start_row }
17-
self.col = { mark.start_col, mark.opts.end_col or mark.start_col }
20+
self.row = { mark.start_row, mark.opts.end_row }
21+
self.col = { mark.start_col, mark.opts.end_col }
1822
return self
1923
end
2024

25+
---@param a render.md.debug.Mark
26+
---@param b render.md.debug.Mark
27+
---@return boolean
28+
function Mark.__lt(a, b)
29+
local as, bs = a:priorities(), b:priorities()
30+
for i = 1, math.max(#as, #bs) do
31+
if as[i] ~= bs[i] then
32+
return as[i] < bs[i]
33+
end
34+
end
35+
return false
36+
end
37+
38+
---@private
2139
---@return integer[]
2240
function Mark:priorities()
23-
local row_offset = 0
41+
local virt_row = 0
2442
if self.opts.virt_lines ~= nil then
25-
row_offset = self.opts.virt_lines_above and -0.5 or 0.5
43+
virt_row = self.opts.virt_lines_above and -0.5 or 0.5
2644
end
27-
local col = self.opts.virt_text_win_col or 0
28-
local result = { self.row[1] + row_offset, self.row[2] + row_offset }
29-
return vim.list_extend(result, { math.max(self.col[1], col), math.max(self.col[2], col) })
45+
local win_col = self.opts.virt_text_win_col or 0
46+
return {
47+
-- rows
48+
self.row[1] + virt_row,
49+
(self.row[2] or self.row[1]) + virt_row,
50+
-- cols
51+
math.max(self.col[1], win_col),
52+
math.max((self.col[2] or self.col[1]), win_col),
53+
}
3054
end
3155

3256
---@return string
3357
function Mark:__tostring()
34-
---@param text string
35-
---@return string
36-
local function serialize_text(text)
37-
local chars = vim.fn.str2list(text)
38-
if #chars <= 1 then
39-
return string.format('"%s"', text)
40-
end
41-
local first = chars[1]
42-
for _, char in ipairs(chars) do
43-
if first ~= char then
44-
return string.format('"%s"', text)
45-
end
46-
end
47-
return string.format('rep(%s, %d)', vim.fn.nr2char(first), #chars)
48-
end
49-
50-
---@param highlight number|string|string[]
51-
---@return string
52-
local function serialize_highlight(highlight)
53-
if type(highlight) == 'table' then
54-
highlight = table.concat(highlight, '+')
55-
end
56-
local result, _ = highlight:gsub('RenderMarkdown_?', '')
57-
result, _ = result:gsub('Inverse', 'I')
58-
return string.format('(%s)', result)
59-
end
60-
61-
---@param line { [1]?: string, [2]?: number|string|string[] }[]
62-
---@return string[]?
63-
local function virt_line(line)
64-
local result = {}
65-
for _, part in ipairs(line) do
66-
local serialized, text, highlight = {}, part[1], part[2]
67-
if text ~= nil then
68-
table.insert(serialized, serialize_text(text))
69-
end
70-
if highlight ~= nil then
71-
table.insert(serialized, serialize_highlight(highlight))
72-
end
73-
if #serialized > 0 then
74-
table.insert(result, table.concat(serialized, '::'))
75-
end
76-
end
77-
return #result > 0 and result or nil
78-
end
79-
80-
---@param vals { [1]: integer, [2]: integer }
81-
---@return string|integer
82-
local function collapse(vals)
83-
return vals[1] == vals[2] and vals[1] or string.format('%d -> %d', vals[1], vals[2])
84-
end
85-
86-
local lines = {
87-
string.rep('=', vim.o.columns - 10),
88-
string.format('row: %s', collapse(self.row)),
89-
string.format('column: %s', collapse(self.col)),
90-
string.format('hide: %s', self.conceal),
91-
}
58+
local lines = {}
59+
lines[#lines + 1] = string.rep('=', vim.o.columns - 10)
60+
lines[#lines + 1] = string.format('row: %s', Mark.collapse(self.row))
61+
lines[#lines + 1] = string.format('column: %s', Mark.collapse(self.col))
62+
lines[#lines + 1] = string.format('hide: %s', vim.inspect(self.conceal))
9263

9364
---@param name string
9465
---@param value any
95-
local function append(name, value)
96-
if type(value) == 'table' then
97-
value = virt_line(value)
98-
end
66+
---@param f fun(value: any): string
67+
local function add(name, value, f)
9968
if value ~= nil then
100-
if type(value) == 'table' then
101-
value = table.concat(value, ' + ')
102-
end
103-
if type(value) == 'string' and #value == 0 then
104-
value = vim.inspect(value)
105-
end
106-
table.insert(lines, string.format(' %s: %s', name, value))
69+
lines[#lines + 1] = string.format(' %s: %s', name, f(value))
10770
end
10871
end
10972

110-
append('conceal', self.opts.conceal)
111-
append('sign', { { self.opts.sign_text, self.opts.sign_hl_group } })
112-
append('virt_text', self.opts.virt_text)
113-
append('virt_text_pos', self.opts.virt_text_pos)
114-
append('virt_text_win_col', self.opts.virt_text_win_col)
115-
append('virt_text_repeat_linebreak', self.opts.virt_text_repeat_linebreak)
116-
append('virt_line', (self.opts.virt_lines or {})[1])
117-
append('virt_line_above', self.opts.virt_lines_above)
118-
append('hl_group', { { nil, self.opts.hl_group } })
119-
append('hl_eol', self.opts.hl_eol)
120-
append('hl_mode', self.opts.hl_mode)
121-
append('priority', self.opts.priority)
73+
local opts = self.opts
74+
add('conceal', opts.conceal, vim.inspect)
75+
add('conceal_lines', opts.conceal_lines, vim.inspect)
76+
add('sign_text', opts.sign_text, Mark.text)
77+
add('sign_hl_group', opts.sign_hl_group, Mark.highlight)
78+
add('virt_text', opts.virt_text, Mark.line)
79+
add('virt_text_pos', opts.virt_text_pos, tostring)
80+
add('virt_text_win_col', opts.virt_text_win_col, vim.inspect)
81+
add('virt_text_repeat_linebreak', opts.virt_text_repeat_linebreak, vim.inspect)
82+
add('virt_line', (opts.virt_lines or {})[1], Mark.line)
83+
add('virt_line_above', opts.virt_lines_above, vim.inspect)
84+
add('hl_group', opts.hl_group, Mark.highlight)
85+
add('hl_eol', opts.hl_eol, vim.inspect)
86+
add('hl_mode', opts.hl_mode, tostring)
87+
add('priority', opts.priority, tostring)
12288
return table.concat(lines, '\n')
12389
end
12490

125-
---@param a render.md.debug.Mark
126-
---@param b render.md.debug.Mark
127-
---@return boolean
128-
function Mark.__lt(a, b)
129-
local as, bs = a:priorities(), b:priorities()
130-
for i = 1, math.max(#as, #bs) do
131-
if as[i] ~= bs[i] then
132-
return as[i] < bs[i]
133-
end
91+
---@private
92+
---@param range render.md.debug.Range
93+
---@return string
94+
function Mark.collapse(range)
95+
local s, e = range[1], range[2]
96+
return e == nil and tostring(s) or string.format('%d -> %d', s, e)
97+
end
98+
99+
---@private
100+
---@param line render.md.MarkLine
101+
---@return string
102+
function Mark.line(line)
103+
local result = {}
104+
for _, text in ipairs(line) do
105+
result[#result + 1] = string.format('(%s, %s)', Mark.text(text[1]), Mark.highlight(text[2]))
106+
end
107+
return table.concat(result, ' + ')
108+
end
109+
110+
---@private
111+
---@param text string
112+
---@return string
113+
function Mark.text(text)
114+
local chars = vim.fn.str2list(text)
115+
local first, same = chars[1], true
116+
for _, char in ipairs(chars) do
117+
same = same and (first == char)
118+
end
119+
if #chars > 1 and same then
120+
local char = vim.fn.nr2char(first)
121+
return string.format('rep(%s, %d)', char, #chars)
122+
else
123+
return text
134124
end
135-
return false
125+
end
126+
127+
---@private
128+
---@param highlight string|string[]
129+
---@return string
130+
function Mark.highlight(highlight)
131+
if type(highlight) == 'table' then
132+
highlight = table.concat(highlight, '+')
133+
end
134+
local result = highlight:gsub('RenderMarkdown', 'Rm')
135+
return result
136136
end
137137

138138
---@class render.md.debug.Marks
@@ -141,14 +141,15 @@ local M = {}
141141
---@param row integer
142142
---@param marks render.md.Mark[]
143143
function M.debug(row, marks)
144-
print(string.format('Decorations on row: %d', row))
144+
vim.print(string.format('Row: %d', row))
145145
if #marks == 0 then
146-
print('No decorations found')
147-
end
148-
local debug_marks = Iter.list.map(marks, Mark.new)
149-
table.sort(debug_marks)
150-
for _, mark in ipairs(debug_marks) do
151-
print(mark)
146+
vim.print('No decorations found')
147+
else
148+
local debug_marks = Iter.list.map(marks, Mark.new)
149+
table.sort(debug_marks)
150+
for _, mark in ipairs(debug_marks) do
151+
vim.print(tostring(mark))
152+
end
152153
end
153154
end
154155

lua/render-markdown/debug/validator.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function Spec:handle_types(custom, ts)
169169
types = ts
170170
end
171171
if self.nilable and not vim.tbl_contains(types, 'nil') then
172-
table.insert(types, 'nil')
172+
types[#types + 1] = 'nil'
173173
end
174174
return types, table.concat(vim.list_extend(custom, types), ' or ')
175175
end
@@ -232,13 +232,13 @@ function Validator:check(path, config, specs)
232232
if info ~= nil then
233233
message = message .. string.format(', info: %s', info)
234234
end
235-
table.insert(self.errors, message)
235+
self.errors[#self.errors + 1] = message
236236
end
237237
end
238238
for key, _ in pairs(config) do
239239
if specs[key] == nil then
240240
local message = string.format('%s.%s - invalid key', path, key)
241-
table.insert(self.errors, message)
241+
self.errors[#self.errors + 1] = message
242242
end
243243
end
244244
end

0 commit comments

Comments
 (0)