2020-05-07 22:14:46 +02:00
|
|
|
local core = require "core"
|
|
|
|
local config = require "core.config"
|
|
|
|
local tokenizer = require "core.tokenizer"
|
|
|
|
local Object = require "core.object"
|
|
|
|
|
|
|
|
|
|
|
|
local Highlighter = Object:extend()
|
|
|
|
|
|
|
|
|
|
|
|
function Highlighter:new(doc)
|
|
|
|
self.doc = doc
|
2020-05-09 17:23:07 +02:00
|
|
|
self:reset()
|
2020-05-07 22:14:46 +02:00
|
|
|
|
|
|
|
-- init incremental syntax highlighting
|
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
2020-05-09 09:56:30 +02:00
|
|
|
if self.first_invalid_line > self.max_wanted_line then
|
2020-05-07 22:14:46 +02:00
|
|
|
self.max_wanted_line = 0
|
|
|
|
coroutine.yield(1 / config.fps)
|
|
|
|
|
|
|
|
else
|
2020-05-09 09:56:30 +02:00
|
|
|
local max = math.min(self.first_invalid_line + 40, self.max_wanted_line)
|
2020-05-07 22:14:46 +02:00
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
for i = self.first_invalid_line, max do
|
2020-05-07 22:14:46 +02:00
|
|
|
local state = (i > 1) and self.lines[i - 1].state
|
|
|
|
local line = self.lines[i]
|
2021-10-07 19:03:16 +02:00
|
|
|
if not (line and line.init_state == state and line.text == self.doc.lines[i]) then
|
2020-05-07 22:14:46 +02:00
|
|
|
self.lines[i] = self:tokenize_line(i, state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-09 09:56:30 +02:00
|
|
|
self.first_invalid_line = max + 1
|
2020-05-07 22:14:46 +02:00
|
|
|
core.redraw = true
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-09 17:23:07 +02:00
|
|
|
function Highlighter:reset()
|
|
|
|
self.lines = {}
|
|
|
|
self.first_invalid_line = 1
|
|
|
|
self.max_wanted_line = 0
|
|
|
|
end
|
|
|
|
|
2020-05-07 22:14:46 +02:00
|
|
|
function Highlighter:invalidate(idx)
|
2020-06-02 23:50:03 +02:00
|
|
|
self.first_invalid_line = math.min(self.first_invalid_line, idx)
|
2020-05-09 09:56:30 +02:00
|
|
|
self.max_wanted_line = math.min(self.max_wanted_line, #self.doc.lines)
|
2020-05-07 22:14:46 +02:00
|
|
|
end
|
|
|
|
|
2021-10-07 19:19:03 +02:00
|
|
|
function Highlighter:insert_notify(line, n)
|
|
|
|
self:invalidate(line)
|
|
|
|
for i = 1, n do
|
2021-10-26 00:12:16 +02:00
|
|
|
table.insert(self.lines, line, false)
|
2021-10-07 19:19:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Highlighter:remove_notify(line, n)
|
|
|
|
self:invalidate(line)
|
|
|
|
for i = 1, n do
|
|
|
|
table.remove(self.lines, line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 22:14:46 +02:00
|
|
|
|
|
|
|
function Highlighter:tokenize_line(idx, state)
|
2020-05-08 21:29:22 +02:00
|
|
|
local res = {}
|
|
|
|
res.init_state = state
|
|
|
|
res.text = self.doc.lines[idx]
|
|
|
|
res.tokens, res.state = tokenizer.tokenize(self.doc.syntax, res.text, state)
|
|
|
|
return res
|
2020-05-07 22:14:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Highlighter:get_line(idx)
|
|
|
|
local line = self.lines[idx]
|
|
|
|
if not line or line.text ~= self.doc.lines[idx] then
|
|
|
|
local prev = self.lines[idx - 1]
|
|
|
|
line = self:tokenize_line(idx, prev and prev.state)
|
|
|
|
self.lines[idx] = line
|
|
|
|
end
|
|
|
|
self.max_wanted_line = math.max(self.max_wanted_line, idx)
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Highlighter:each_token(idx)
|
|
|
|
return tokenizer.each_token(self:get_line(idx).tokens)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return Highlighter
|