Lua: Implement LUA to get a selection text range

Change-Id: If2c24cc8c2063617fa378fed158ae65000c00c91
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Mariusz Szczepanik
2024-09-18 16:12:59 +02:00
committed by mua
parent 104d7583b9
commit ade8cb7b0c
2 changed files with 55 additions and 0 deletions

View File

@@ -17,6 +17,7 @@
#include "sol/sol.hpp"
using namespace Utils;
using namespace Text;
namespace {
@@ -166,6 +167,23 @@ void setupTextEditorModule()
"cursors",
[](MultiTextCursor *self) { return sol::as_table(self->cursors()); });
result.new_usertype<Position>(
"Position",
sol::no_constructor,
"line",
sol::property([](Position &pos) { return pos.line; }),
"column",
sol::property([](Position &pos) { return pos.column; }));
// In range can't use begin/end as "end" is a reserved word for LUA scripts
result.new_usertype<Range>(
"Range",
sol::no_constructor,
"from",
sol::property([](Range &range) { return range.begin; }),
"to",
sol::property([](Range &range) { return range.end; }));
result.new_usertype<QTextCursor>(
"TextCursor",
sol::no_constructor,
@@ -180,6 +198,29 @@ void setupTextEditorModule()
"selectedText",
[](QTextCursor *cursor) {
return cursor->selectedText().replace(QChar::ParagraphSeparator, '\n');
},
"selectionRange",
[](const QTextCursor &textCursor) -> Range {
Range ret;
if (!textCursor.hasSelection())
throw sol::error("Cursor has no selection");
int startPos = textCursor.selectionStart();
int endPos = textCursor.selectionEnd();
QTextDocument *doc = textCursor.document();
if (!doc)
throw sol::error("Cursor has no document");
QTextBlock startBlock = doc->findBlock(startPos);
QTextBlock endBlock = doc->findBlock(endPos);
ret.begin.line = startBlock.blockNumber();
ret.begin.column = startPos - startBlock.position() - 1;
ret.end.line = endBlock.blockNumber();
ret.end.column = endPos - endBlock.position() - 1;
return ret;
});
result.new_usertype<TextEditor::BaseTextEditor>(

View File

@@ -1,6 +1,16 @@
---@meta TextEditor
local textEditor = {}
---@class Position
---@field line integer The line number.
---@field column integer The column number.
local Position = {}
---@class Range
---@field from Position The beginning position of the range.
---@field to Position The end position of the range.
local Range = {}
---@class TextCursor
local TextCursor = {}
@@ -24,6 +34,10 @@ function TextCursor:hasSelection() end
---@return string selectedText The selected text of the cursor.
function TextCursor:selectedText() end
---Returns the range of selected text of the cursor.
---@return Range selectionRange The range of selected text of the cursor.
function TextCursor:selectionRange() end
---@class MultiTextCursor
local MultiTextCursor = {}