diff options
author | Guldoman <giulio.lettieri@gmail.com> | 2021-11-20 03:57:26 +0100 |
---|---|---|
committer | Guldoman <giulio.lettieri@gmail.com> | 2021-11-20 03:57:26 +0100 |
commit | 4f31214e176156add0edb409910979ba09536d28 (patch) | |
tree | 0853ee9df690ed28bd80ec61f8279c2a052a2ced /plugins/indent_convert.lua | |
parent | 333388602d6be6a967d07f0324bc8cefb8351a17 (diff) | |
download | lite-xl-plugins-4f31214e176156add0edb409910979ba09536d28.tar.gz lite-xl-plugins-4f31214e176156add0edb409910979ba09536d28.zip |
`indent_convert`: Use `Doc:get_indent_info` if available
Diffstat (limited to 'plugins/indent_convert.lua')
-rw-r--r-- | plugins/indent_convert.lua | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/plugins/indent_convert.lua b/plugins/indent_convert.lua index 103f271..6eb82b0 100644 --- a/plugins/indent_convert.lua +++ b/plugins/indent_convert.lua @@ -36,8 +36,8 @@ config.plugins.indent_convert = { -- The workaround is to find the longest possible repetition of N*X spaces and -- use that information to replace the longest repetition of spaces starting -- from the beginning of the line, then the second longest... -local function spaces_replacer(text) - local spaces = string.rep(" ", config.indent_size) +local function spaces_replacer(text, indent_size) + local spaces = string.rep(" ", indent_size) local total = 0 local n local reps = 0 @@ -57,8 +57,8 @@ local function spaces_replacer(text) return text, total end -local function tabs_replacer(text) - local spaces = string.rep(" ", config.indent_size) +local function tabs_replacer(text, indent_size) + local spaces = string.rep(" ", indent_size) local total = 0 local n -- replace the last tab before the text until there aren't anymore @@ -69,9 +69,20 @@ local function tabs_replacer(text) return text, total end +local function replacer(doc, fn) + local size = config.indent_size + if type(doc.get_indent_info) == "function" then + -- use the new `Doc:get_indent_info` function + size = select(2, doc:get_indent_info()) + end + return function(text) + return fn(text, size) + end +end + local function tabs_to_spaces() local doc = core.active_view.doc - doc:replace(tabs_replacer) + doc:replace(replacer(doc, tabs_replacer)) if config.plugins.indent_convert.update_indent_type then doc.indent_info = { type = "soft", @@ -83,7 +94,7 @@ end local function spaces_to_tabs() local doc = core.active_view.doc - doc:replace(spaces_replacer) + doc:replace(replacer(doc, spaces_replacer)) if config.plugins.indent_convert.update_indent_type then doc.indent_info = { type = "hard", |