From 0a55b246b50571824589e7c2fc12a0753267df44 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 27 May 2021 16:17:19 +0200 Subject: [PATCH] Use thread in detectindent plugin --- data/plugins/detectindent.lua | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/data/plugins/detectindent.lua b/data/plugins/detectindent.lua index 63fe8a52..814eaaa1 100644 --- a/data/plugins/detectindent.lua +++ b/data/plugins/detectindent.lua @@ -99,29 +99,11 @@ local function detect_indent_stat(doc) end -local doc_on_text_change = Doc.on_text_change -local adjust_threshold = 4 - -local current_on_text_change = nil - local function update_cache(doc) local type, size, score = detect_indent_stat(doc) - cache[doc] = { type = type, size = size, confirmed = (score >= adjust_threshold) } + local score_threshold = 4 + cache[doc] = { type = type, size = size, confirmed = (score >= score_threshold) } doc.indent_info = cache[doc] - if score < adjust_threshold and doc_on_text_change then - current_on_text_change = function(self, ...) - update_cache(self) - end - elseif score >= adjust_threshold and doc_on_text_change then - current_on_text_change = nil - end -end - -function Doc.on_text_change(...) - if current_on_text_change then - current_on_text_change(...) - end - doc_on_text_change(...) end @@ -129,6 +111,14 @@ local new = Doc.new function Doc:new(...) new(self, ...) update_cache(self) + if not cache[self].confirmed then + core.add_thread(function () + while not cache[self].confirmed do + update_cache(self) + coroutine.yield(1) + end + end, self) + end end local clean = Doc.clean