From 2a0846b60410e5714ea626d35064b2152419df77 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Fri, 19 Feb 2021 11:51:49 +0100 Subject: [PATCH] Automatic keep adjusting indent size When too few lines keep adjusting indent size --- data/core/statusview.lua | 2 ++ data/plugins/detectindent.lua | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/data/core/statusview.lua b/data/core/statusview.lua index 2710b1ba..e96c6fe1 100644 --- a/data/core/statusview.lua +++ b/data/core/statusview.lua @@ -119,6 +119,8 @@ function StatusView:get_items() self.separator, string.format("%d%%", line / #dv.doc.lines * 100), }, { + style.text, "spaces: ", tostring(dv.doc.indent_spaces), + style.dim, self.separator2, style.text, style.icon_font, "g", style.font, style.dim, self.separator2, style.text, #dv.doc.lines, " lines", diff --git a/data/plugins/detectindent.lua b/data/plugins/detectindent.lua index 18ce6224..250fba09 100644 --- a/data/plugins/detectindent.lua +++ b/data/plugins/detectindent.lua @@ -91,19 +91,27 @@ local function detect_indent_stat(doc) table.sort(stat, function(a, b) return a[1] < b[1] end) local indent, score = optimal_indent_from_stat(stat) if tab_count > score then - core.log_quiet("Detect-indent: using tabs indentation") - return "hard" - elseif indent then - core.log_quiet("Detect-indent: using indentation size of %d", indent) - return "soft", indent + return "hard", nil, tab_count + else + return "soft", indent or config.indent_size, score or 0 end end +local doc_text_input = Doc.text_input +local adjust_threshold = 5 + local function update_cache(doc) - local type, size = detect_indent_stat(doc) - if type then - cache[doc] = { type = type, size = size } + local type, size, score = detect_indent_stat(doc) + cache[doc] = { type = type, size = size } + doc.indent_spaces = (type == "hard" and "tab" or size) .. (score < adjust_threshold and "*" or "") + if score < adjust_threshold and Doc.text_input == doc_text_input then + Doc.text_input = function(self, ...) + doc_text_input(self, ...) + update_cache(self) + end + elseif score >= adjust_threshold and Doc.text_input ~= doc_text_input then + Doc.text_input = doc_text_input end end