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,18 +10,17 @@ 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
|
|
||||||
self.max_wanted_line = 0
|
|
||||||
coroutine.yield(1 / config.fps)
|
|
||||||
|
|
||||||
else
|
|
||||||
local max = math.min(self.first_invalid_line + 40, self.max_wanted_line)
|
local max = math.min(self.first_invalid_line + 40, self.max_wanted_line)
|
||||||
|
|
||||||
local retokenized_from
|
local retokenized_from
|
||||||
for i = self.first_invalid_line, max do
|
for i = self.first_invalid_line, max do
|
||||||
local state = (i > 1) and self.lines[i - 1].state
|
local state = (i > 1) and self.lines[i - 1].state
|
||||||
|
@ -42,10 +41,18 @@ function Highlighter:new(doc)
|
||||||
core.redraw = true
|
core.redraw = true
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end
|
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