Avoid recreating tables when calculating the longest lines

This commit is contained in:
Guldoman 2021-08-30 02:28:17 +02:00 committed by Francesco
parent 92bbb30d06
commit 4d0656ad7e
2 changed files with 25 additions and 30 deletions

View File

@ -393,12 +393,5 @@ function common.rm(path, recursively)
return true return true
end end
function common.get_table_size(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
return common return common

View File

@ -32,7 +32,7 @@ end
function Doc:reset() function Doc:reset()
self.lines = { "\n" } self.lines = { "\n" }
self.long_lines = { line_numbers = { [1] = true }, length = 0 } self.long_lines = { line_numbers = { }, length = 0 }
self.selections = { 1, 1, 1, 1 } self.selections = { 1, 1, 1, 1 }
self.cursor_clipboard = {} self.cursor_clipboard = {}
self.undo_stack = { idx = 1 } self.undo_stack = { idx = 1 }
@ -62,7 +62,7 @@ end
function Doc:load(filename) function Doc:load(filename)
local fp = assert( io.open(filename, "rb") ) local fp = assert( io.open(filename, "rb") )
local max_length = 0 local max_length = 0
local line_numbers = { [1] = true } local line_numbers = { }
self:reset() self:reset()
self.lines = {} self.lines = {}
local i = 1 local i = 1
@ -72,15 +72,16 @@ function Doc:load(filename)
self.crlf = true self.crlf = true
end end
table.insert(self.lines, line .. "\n") table.insert(self.lines, line .. "\n")
local line_len = string.len(line) + 1 -- account for newline local line_len = #line + 1 -- account for newline
if line_len > max_length then if line_len >= max_length then
max_length = line_len max_length = line_len
line_numbers = { [i] = true } -- new longest line line_numbers[i] = line_len
elseif line_len == max_length then
line_numbers[i] = true -- add to longest lines
end end
i = i + 1 i = i + 1
end end
for n, len in pairs(line_numbers) do
line_numbers[n] = len >= max_length and len or nil
end
if #self.lines == 0 then if #self.lines == 0 then
table.insert(self.lines, "\n") table.insert(self.lines, "\n")
end end
@ -363,12 +364,12 @@ function Doc:raw_insert(line, col, text, undo_stack, time)
if #lines > 1 then if #lines > 1 then
-- Need to shift all the subsequent long lines -- Need to shift all the subsequent long lines
local line_numbers = {} local line_numbers = { }
for n in pairs(self.long_lines.line_numbers) do for n, len in pairs(self.long_lines.line_numbers) do
if n > line then if n > line then
line_numbers[n + #lines - 1] = true line_numbers[n + #lines - 1] = len
else else
line_numbers[n] = true line_numbers[n] = len
end end
end end
self.long_lines.line_numbers = line_numbers self.long_lines.line_numbers = line_numbers
@ -405,14 +406,14 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
local nlines = line2 - line1 + 1 local nlines = line2 - line1 + 1
if nlines > 1 then if nlines > 1 then
-- Need to shift all the subsequent long lines -- Need to shift all the subsequent long lines
local line_numbers = {} local line_numbers = { }
for n in pairs(self.long_lines.line_numbers) do for n, len in pairs(self.long_lines.line_numbers) do
if n > line2 then if n > line2 then
line_numbers[n - nlines + 1] = true line_numbers[n - nlines + 1] = len
elseif n > line1 then elseif n > line1 then
line_numbers[n] = nil -- invalidate any line that has been deleted line_numbers[n] = nil -- invalidate any line that has been deleted
else else
line_numbers[n] = true line_numbers[n] = len
end end
end end
self.long_lines.line_numbers = line_numbers self.long_lines.line_numbers = line_numbers
@ -580,21 +581,21 @@ function Doc:update_max_line_len_range(start_line, end_line)
end_line = math.min(end_line, #self.lines) end_line = math.min(end_line, #self.lines)
for line=start_line,end_line do for line=start_line,end_line do
local line_len = string.len(self.lines[line]) local line_len = #self.lines[line]
if line_len > max_length then if line_len >= max_length then
max_length = line_len max_length = line_len
line_numbers = { [line] = true } line_numbers[line] = line_len
elseif line_len == max_length then
line_numbers[line] = true
else else
if line_numbers[line] then line_numbers[line] = nil end if line_numbers[line] then line_numbers[line] = nil end
end end
end end
for n, len in pairs(line_numbers) do
if common.get_table_size(line_numbers) == 0 then line_numbers[n] = len >= max_length and len or nil
end
if not next(line_numbers) then
-- Recalc needed -- Recalc needed
self.long_lines.length = 0 self.long_lines.length = 0
self.long_lines.line_numbers = { [1] = true } self.long_lines.line_numbers = { }
return self:update_max_line_len_range(1, #self.lines) return self:update_max_line_len_range(1, #self.lines)
end end
@ -602,6 +603,7 @@ function Doc:update_max_line_len_range(start_line, end_line)
self.long_lines.length = max_length self.long_lines.length = max_length
end end
-- For plugins to add custom actions of document change -- For plugins to add custom actions of document change
function Doc:on_text_change(type) function Doc:on_text_change(type)
end end