From 66faf7b897bac80ceff4c502722b046014cbf783 Mon Sep 17 00:00:00 2001 From: Lukasz Papierkowski Date: Fri, 20 Dec 2024 16:25:07 +0100 Subject: [PATCH] LUA: Add TextCursor's movePosition and create within TextEditor Change-Id: Ic4fd9c65c92641c4267cd2fdb3937746c2065baa Reviewed-by: Marcus Tillmanns --- src/plugins/lua/bindings/texteditor.cpp | 64 +++++++++++++++++++- src/plugins/lua/meta/texteditor.lua | 78 +++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) diff --git a/src/plugins/lua/bindings/texteditor.cpp b/src/plugins/lua/bindings/texteditor.cpp index 2a270d52121..16abd9fcfa6 100644 --- a/src/plugins/lua/bindings/texteditor.cpp +++ b/src/plugins/lua/bindings/texteditor.cpp @@ -240,9 +240,21 @@ void setupTextEditorModule() "to", sol::property(&Range::end, &Range::end)); - result.new_usertype( + auto textCursorType = result.new_usertype( "TextCursor", sol::no_constructor, + "create", + sol::overload( + []() { + return QTextCursor(); + }, + [](QTextDocument *doc) { + return QTextCursor(doc); + }, + [](const QTextCursor &other) { + return QTextCursor(other); + } + ), "position", &QTextCursor::position, "blockNumber", @@ -279,7 +291,55 @@ void setupTextEditorModule() return ret; }, "insertText", - [](QTextCursor *textCursor, const QString &text) { textCursor->insertText(text); }); + [](QTextCursor *textCursor, const QString &text) { textCursor->insertText(text); }, + "movePosition", + sol::overload( + [](QTextCursor *cursor, QTextCursor::MoveOperation op) { + cursor->movePosition(op); + }, + [](QTextCursor *cursor, QTextCursor::MoveOperation op, QTextCursor::MoveMode mode) { + cursor->movePosition(op, mode); + }, + [](QTextCursor *cursor, QTextCursor::MoveOperation op, QTextCursor::MoveMode mode, int n) { + cursor->movePosition(op, mode, n); + } + )); + + textCursorType["MoveMode"] = lua.create_table_with( + "MoveAnchor", QTextCursor::MoveAnchor, + "KeepAnchor", QTextCursor::KeepAnchor + ); + + textCursorType["MoveOperation"] = lua.create_table_with( + "NoMove", QTextCursor::NoMove, + + "Start", QTextCursor::Start, + "Up", QTextCursor::Up, + "StartOfLine", QTextCursor::StartOfLine, + "StartOfBlock", QTextCursor::StartOfBlock, + "StartOfWord", QTextCursor::StartOfWord, + "PreviousBlock", QTextCursor::PreviousBlock, + "PreviousCharacter",QTextCursor::PreviousCharacter, + "PreviousWord", QTextCursor::PreviousWord, + "Left", QTextCursor::Left, + "WordLeft", QTextCursor::WordLeft, + + "End", QTextCursor::End, + "Down", QTextCursor::Down, + "EndOfLine", QTextCursor::EndOfLine, + "EndOfWord", QTextCursor::EndOfWord, + "EndOfBlock", QTextCursor::EndOfBlock, + "NextBlock", QTextCursor::NextBlock, + "NextCharacter", QTextCursor::NextCharacter, + "NextWord", QTextCursor::NextWord, + "Right", QTextCursor::Right, + "WordRight", QTextCursor::WordRight, + + "NextCell", QTextCursor::NextCell, + "PreviousCell", QTextCursor::PreviousCell, + "NextRow", QTextCursor::NextRow, + "PreviousRow", QTextCursor::PreviousRow + ); using LayoutOrWidget = std::variant; diff --git a/src/plugins/lua/meta/texteditor.lua b/src/plugins/lua/meta/texteditor.lua index 0007d09a18c..80b5fbf884b 100644 --- a/src/plugins/lua/meta/texteditor.lua +++ b/src/plugins/lua/meta/texteditor.lua @@ -18,6 +18,63 @@ local Range = {} ---@class TextCursor local TextCursor = {} +---@enum TextCursor.MoveOperation +---Move operations for a TextCursor. +TextCursor.MoveOperation = { + NoMove = 0, + + Start = 0, + Up = 0, + StartOfLine = 0, + StartOfBlock = 0, + StartOfWord = 0, + PreviousBlock = 0, + PreviousCharacter = 0, + PreviousWord = 0, + Left = 0, + WordLeft = 0, + + End = 0, + Down = 0, + EndOfLine = 0, + EndOfWord = 0, + EndOfBlock = 0, + NextBlock = 0, + NextCharacter = 0, + NextWord = 0, + Right = 0, + WordRight = 0, + + NextCell = 0, + PreviousCell = 0, + NextRow = 0, + PreviousRow = 0 +} + +---@enum TextCursor.MoveMode +---Specifies how the cursor moves (with or without anchoring the selection). +TextCursor.MoveMode = { + MoveAnchor = 0, + KeepAnchor = 0 +} + +---Creates a new `TextCursor` object. +--- +---**Overload 1**: `TextCursor.create()` +--- No parameters. Creates a default-constructed cursor. +--- +---**Overload 2**: `TextCursor.create(doc)` +--- - `doc`: A `QTextDocument*` (or usertype) from which to create the cursor. +--- +---**Overload 3**: `TextCursor.create(other)` +--- - `other`: Another `TextCursor` to copy. +--- +---@overload fun(): TextCursor +---@overload fun(doc: any): TextCursor +---@overload fun(other: TextCursor): TextCursor +---@return TextCursor +function TextCursor.create(...) end + ---Returns the position of the cursor. ---@return integer position The position of the cursor. function TextCursor:position() end @@ -46,6 +103,27 @@ function TextCursor:selectionRange() end ---@param text string The text to insert. function TextCursor:insertText(text) end +---Moves the cursor using a specified operation, and optionally a mode and/or repetition count. +--- +---**Overload 1**: `cursor:movePosition(operation)` +--- - `operation`: A `TextCursor.MoveOperation`. +--- - Moves once, using the default mode `MoveAnchor`. +--- +---**Overload 2**: `cursor:movePosition(operation, mode)` +--- - `operation`: A `TextCursor.MoveOperation`. +--- - `mode`: A `TextCursor.MoveMode` (e.g. `MoveAnchor` or `KeepAnchor`). +--- +---**Overload 3**: `cursor:movePosition(operation, mode, n)` +--- - `operation`: A `TextCursor.MoveOperation`. +--- - `mode`: A `TextCursor.MoveMode`. +--- - `n`: Number of times to repeat the move. +--- +---@overload fun(operation: TextCursor.MoveOperation) +---@overload fun(operation: TextCursor.MoveOperation, mode: TextCursor.MoveMode) +---@overload fun(operation: TextCursor.MoveOperation, mode: TextCursor.MoveMode, n: integer) +function TextCursor:movePosition(operation, mode, n) end + + ---@class MultiTextCursor local MultiTextCursor = {}