From 97493a1a4eca756e81404bc053de90a4591fb4d9 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Mon, 2 Aug 2021 11:37:00 +0800 Subject: [PATCH] add doc:get_selection_text() --- data/core/doc/init.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index d05d9d45..ae948479 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -124,9 +124,21 @@ function Doc:get_selection(sort) return line1, col1, line2, col2, sort end +function Doc:get_selection_text(limit) + limit = limit or math.huge + local result = {} + for idx, line1, col1, line2, col2 in self:get_selections() do + if idx > limit then break end + if line1 ~= line2 or col1 ~= col2 then + local text = self:get_text(line1, col1, line2, col2) + if text ~= "" then result[#result + 1] = text end + end + end + return table.concat(result, "\n") +end + function Doc:has_selection() - local line1, col1, line2, col2 = self:get_selection(false) - return line1 ~= line2 or col1 ~= col2 + local line1, col1, line2, col2 = self:get_selection(false) return line1 ~= line2 or col1 ~= col2 end function Doc:sanitize_selection()