From 94f7c8b33371e49d6f026f5be6a41acd73ad9c6b Mon Sep 17 00:00:00 2001 From: Guldoman Date: Wed, 2 Nov 2022 22:52:27 +0100 Subject: Add workaround for sorted `Doc:get_selection` issue (#141) --- plugins/regexreplacepreview.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'plugins/regexreplacepreview.lua') diff --git a/plugins/regexreplacepreview.lua b/plugins/regexreplacepreview.lua index dfc9e45..747e9ba 100644 --- a/plugins/regexreplacepreview.lua +++ b/plugins/regexreplacepreview.lua @@ -3,6 +3,18 @@ local core = require "core" local keymap = require "core.keymap" local command = require "core.command" +-- Workaround for bug in Lite XL 2.1 +-- Remove this when b029f5993edb7dee5ccd2ba55faac1ec22e24609 is in a release +local function get_selection(doc, sort) + local line1, col1, line2, col2 = doc:get_selection_idx(doc.last_selection) + if line1 then + return doc:get_selection_idx(doc.last_selection, sort) + else + return doc:get_selection_idx(1, sort) + end +end + + -- Takes the following pattern: /pattern/replace/ -- Capture groupings can be replaced using \1 through \9 local function regex_replace_file(view, pattern, old_lines, raw, start_line, end_line) @@ -91,8 +103,8 @@ command.add("core.docview!", { ["regex-replace-preview:find-replace-regex"] = function(view) local old_lines = {} local doc = view.doc - local original_selection = { doc:get_selection(true) } - local selection = doc:has_selection() and { doc:get_selection(true) } or {} + local original_selection = { get_selection(doc, true) } + local selection = doc:has_selection() and { get_selection(doc, true) } or {} core.command_view:enter("Regex Replace (enter pattern as /old/new/)", { text = "/", submit = function(pattern) -- cgit v1.2.3