highlighter: autostop co-routine when not needed (#881)
* highlighter: autostop co-routine when not needed * applied @Guldoman suggestions
This commit is contained in:
parent
56e465c351
commit
9c7304f555
|
@ -10,42 +10,49 @@ local Highlighter = Object:extend()
|
||||||
|
|
||||||
function Highlighter:new(doc)
|
function Highlighter:new(doc)
|
||||||
self.doc = doc
|
self.doc = doc
|
||||||
|
self.running = false
|
||||||
self:reset()
|
self:reset()
|
||||||
|
end
|
||||||
|
|
||||||
-- init incremental syntax highlighting
|
-- init incremental syntax highlighting
|
||||||
|
function Highlighter:start()
|
||||||
|
if self.running then return end
|
||||||
|
self.running = true
|
||||||
core.add_thread(function()
|
core.add_thread(function()
|
||||||
while true do
|
while self.first_invalid_line < self.max_wanted_line do
|
||||||
if self.first_invalid_line > self.max_wanted_line then
|
local max = math.min(self.first_invalid_line + 40, self.max_wanted_line)
|
||||||
self.max_wanted_line = 0
|
local retokenized_from
|
||||||
coroutine.yield(1 / config.fps)
|
for i = self.first_invalid_line, max do
|
||||||
|
local state = (i > 1) and self.lines[i - 1].state
|
||||||
else
|
local line = self.lines[i]
|
||||||
local max = math.min(self.first_invalid_line + 40, self.max_wanted_line)
|
if not (line and line.init_state == state and line.text == self.doc.lines[i]) then
|
||||||
|
retokenized_from = retokenized_from or i
|
||||||
local retokenized_from
|
self.lines[i] = self:tokenize_line(i, state)
|
||||||
for i = self.first_invalid_line, max do
|
elseif retokenized_from then
|
||||||
local state = (i > 1) and self.lines[i - 1].state
|
self:update_notify(retokenized_from, i - retokenized_from - 1)
|
||||||
local line = self.lines[i]
|
retokenized_from = nil
|
||||||
if not (line and line.init_state == state and line.text == self.doc.lines[i]) then
|
|
||||||
retokenized_from = retokenized_from or i
|
|
||||||
self.lines[i] = self:tokenize_line(i, state)
|
|
||||||
elseif retokenized_from then
|
|
||||||
self:update_notify(retokenized_from, i - retokenized_from - 1)
|
|
||||||
retokenized_from = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if retokenized_from then
|
|
||||||
self:update_notify(retokenized_from, max - retokenized_from)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.first_invalid_line = max + 1
|
|
||||||
core.redraw = true
|
|
||||||
coroutine.yield()
|
|
||||||
end
|
end
|
||||||
|
if retokenized_from then
|
||||||
|
self:update_notify(retokenized_from, max - retokenized_from)
|
||||||
|
end
|
||||||
|
|
||||||
|
self.first_invalid_line = max + 1
|
||||||
|
core.redraw = true
|
||||||
|
coroutine.yield()
|
||||||
end
|
end
|
||||||
|
self.max_wanted_line = 0
|
||||||
|
self.running = false
|
||||||
end, self)
|
end, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function set_max_wanted_lines(self, amount)
|
||||||
|
self.max_wanted_line = amount
|
||||||
|
if self.first_invalid_line < self.max_wanted_line then
|
||||||
|
self:start()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function Highlighter:reset()
|
function Highlighter:reset()
|
||||||
self.lines = {}
|
self.lines = {}
|
||||||
|
@ -62,7 +69,7 @@ end
|
||||||
|
|
||||||
function Highlighter:invalidate(idx)
|
function Highlighter:invalidate(idx)
|
||||||
self.first_invalid_line = math.min(self.first_invalid_line, idx)
|
self.first_invalid_line = math.min(self.first_invalid_line, idx)
|
||||||
self.max_wanted_line = math.min(self.max_wanted_line, #self.doc.lines)
|
set_max_wanted_lines(self, math.min(self.max_wanted_line, #self.doc.lines))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Highlighter:insert_notify(line, n)
|
function Highlighter:insert_notify(line, n)
|
||||||
|
@ -101,7 +108,7 @@ function Highlighter:get_line(idx)
|
||||||
self.lines[idx] = line
|
self.lines[idx] = line
|
||||||
self:update_notify(idx, 0)
|
self:update_notify(idx, 0)
|
||||||
end
|
end
|
||||||
self.max_wanted_line = math.max(self.max_wanted_line, idx)
|
set_max_wanted_lines(self, math.max(self.max_wanted_line, idx))
|
||||||
return line
|
return line
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue