Merge branch 'doc-change-hook'

This commit is contained in:
Francesco Abbate 2021-04-06 08:28:24 +02:00
commit e5f2120a27
2 changed files with 23 additions and 10 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
@ -262,11 +262,17 @@ local function pop_undo(self, undo_stack, redo_stack)
self.selection.b.line, self.selection.b.col = cmd[3], cmd[4] self.selection.b.line, self.selection.b.col = cmd[3], cmd[4]
end end
modified = modified or (cmd.type ~= "selection")
-- if next undo command is within the merge timeout then treat as a single -- if next undo command is within the merge timeout then treat as a single
-- 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)
end
if modified then
self:on_text_change("undo")
end end
end end
@ -319,6 +325,7 @@ 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())
self:on_text_change("insert")
end end
@ -328,16 +335,17 @@ function Doc:remove(line1, col1, line2, col2)
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())
self:on_text_change("remove")
end end
function Doc:undo() function Doc:undo()
pop_undo(self, self.undo_stack, self.redo_stack) pop_undo(self, self.undo_stack, self.redo_stack, false)
end end
function Doc:redo() function Doc:redo()
pop_undo(self, self.redo_stack, self.undo_stack) pop_undo(self, self.redo_stack, self.undo_stack, false)
end end
@ -398,5 +406,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,21 @@ 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 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 then
Doc.text_input = doc_text_input Doc.on_text_change = doc_on_text_change
doc_on_text_change = nil
end end
end end