diff --git a/src/plugins/lua/bindings/texteditor.cpp b/src/plugins/lua/bindings/texteditor.cpp index af716a32326..fc7e2633712 100644 --- a/src/plugins/lua/bindings/texteditor.cpp +++ b/src/plugins/lua/bindings/texteditor.cpp @@ -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", + 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", + sol::no_constructor, + "from", + sol::property([](Range &range) { return range.begin; }), + "to", + sol::property([](Range &range) { return range.end; })); + result.new_usertype( "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( diff --git a/src/plugins/lua/meta/texteditor.lua b/src/plugins/lua/meta/texteditor.lua index b152111d694..ebf1d0e6ad0 100644 --- a/src/plugins/lua/meta/texteditor.lua +++ b/src/plugins/lua/meta/texteditor.lua @@ -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 = {}