Add hook function for Doc changes

This commit is contained in:
Francesco Abbate 2021-04-05 00:11:56 +02:00
parent 0dc1098705
commit d1984942ea
2 changed files with 30 additions and 14 deletions

View File

@ -242,7 +242,7 @@ local function push_undo(undo_stack, time, type, ...)
end end
local function pop_undo(self, undo_stack, redo_stack) local function pop_undo(self, undo_stack, redo_stack, modified)
-- pop command -- pop command
local cmd = undo_stack[undo_stack.idx - 1] local cmd = undo_stack[undo_stack.idx - 1]
if not cmd then return end if not cmd then return end
@ -251,11 +251,11 @@ local function pop_undo(self, undo_stack, redo_stack)
-- handle command -- handle command
if cmd.type == "insert" then if cmd.type == "insert" then
local line, col, text = table.unpack(cmd) local line, col, text = table.unpack(cmd)
self:raw_insert(line, col, text, redo_stack, cmd.time) self:raw_insert(line, col, text, redo_stack, cmd.time, true)
elseif cmd.type == "remove" then elseif cmd.type == "remove" then
local line1, col1, line2, col2 = table.unpack(cmd) local line1, col1, line2, col2 = table.unpack(cmd)
self:raw_remove(line1, col1, line2, col2, redo_stack, cmd.time) self:raw_remove(line1, col1, line2, col2, redo_stack, cmd.time, true)
elseif cmd.type == "selection" then elseif cmd.type == "selection" then
self.selection.a.line, self.selection.a.col = cmd[1], cmd[2] self.selection.a.line, self.selection.a.col = cmd[1], cmd[2]
@ -266,12 +266,16 @@ local function pop_undo(self, undo_stack, redo_stack)
-- command and continue to execute it -- command and continue to execute it
local next = undo_stack[undo_stack.idx - 1] local next = undo_stack[undo_stack.idx - 1]
if next and math.abs(cmd.time - next.time) < config.undo_merge_timeout then if next and math.abs(cmd.time - next.time) < config.undo_merge_timeout then
return pop_undo(self, undo_stack, redo_stack) return pop_undo(self, undo_stack, redo_stack, modified or cmd.type ~= "selection")
end
if modified then
self:on_text_change("undo")
end end
end end
function Doc:raw_insert(line, col, text, undo_stack, time) function Doc:raw_insert(line, col, text, undo_stack, time, quiet)
-- split text into lines and merge with line at insertion point -- split text into lines and merge with line at insertion point
local lines = split_lines(text) local lines = split_lines(text)
local before = self.lines[line]:sub(1, col - 1) local before = self.lines[line]:sub(1, col - 1)
@ -293,10 +297,14 @@ function Doc:raw_insert(line, col, text, undo_stack, time)
-- update highlighter and assure selection is in bounds -- update highlighter and assure selection is in bounds
self.highlighter:invalidate(line) self.highlighter:invalidate(line)
self:sanitize_selection() self:sanitize_selection()
if not quiet then
self:on_text_change("insert")
end
end end
function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time) function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time, quiet)
-- push undo -- push undo
local text = self:get_text(line1, col1, line2, col2) local text = self:get_text(line1, col1, line2, col2)
push_undo(undo_stack, time, "selection", self:get_selection()) push_undo(undo_stack, time, "selection", self:get_selection())
@ -312,13 +320,17 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
-- update highlighter and assure selection is in bounds -- update highlighter and assure selection is in bounds
self.highlighter:invalidate(line1) self.highlighter:invalidate(line1)
self:sanitize_selection() self:sanitize_selection()
if not quiet then
self:on_text_change("remove")
end
end end
function Doc:insert(line, col, text) function Doc:insert(line, col, text)
self.redo_stack = { idx = 1 } self.redo_stack = { idx = 1 }
line, col = self:sanitize_position(line, col) line, col = self:sanitize_position(line, col)
self:raw_insert(line, col, text, self.undo_stack, system.get_time()) self:raw_insert(line, col, text, self.undo_stack, system.get_time(), false)
end end
@ -327,7 +339,7 @@ function Doc:remove(line1, col1, line2, col2)
line1, col1 = self:sanitize_position(line1, col1) line1, col1 = self:sanitize_position(line1, col1)
line2, col2 = self:sanitize_position(line2, col2) line2, col2 = self:sanitize_position(line2, col2)
line1, col1, line2, col2 = sort_positions(line1, col1, line2, col2) line1, col1, line2, col2 = sort_positions(line1, col1, line2, col2)
self:raw_remove(line1, col1, line2, col2, self.undo_stack, system.get_time()) self:raw_remove(line1, col1, line2, col2, self.undo_stack, system.get_time(), false)
end end
@ -398,5 +410,9 @@ function Doc:select_to(...)
self:set_selection(line, col, line2, col2) self:set_selection(line, col, line2, col2)
end end
-- For plugins to add custom actions of document change
function Doc:on_text_change(type)
end
return Doc return Doc

View File

@ -98,20 +98,20 @@ local function detect_indent_stat(doc)
end end
local doc_text_input = Doc.text_input local doc_on_text_change = Doc.on_text_change
local adjust_threshold = 4 local adjust_threshold = 4
local function update_cache(doc) local function update_cache(doc)
local type, size, score = detect_indent_stat(doc) local type, size, score = detect_indent_stat(doc)
cache[doc] = { type = type, size = size, confirmed = (score >= adjust_threshold) } cache[doc] = { type = type, size = size, confirmed = (score >= adjust_threshold) }
doc.indent_info = cache[doc] doc.indent_info = cache[doc]
if score < adjust_threshold and Doc.text_input == doc_text_input then if score < adjust_threshold and Doc.on_text_change == doc_on_text_change then
Doc.text_input = function(self, ...) Doc.on_text_change = function(self, ...)
doc_text_input(self, ...) doc_on_text_change(self, ...)
update_cache(self) update_cache(self)
end end
elseif score >= adjust_threshold and Doc.text_input ~= doc_text_input then elseif score >= adjust_threshold and Doc.on_text_change ~= doc_on_text_change then
Doc.text_input = doc_text_input Doc.on_text_change = doc_on_text_change
end end
end end