From 0fefc7886aade51f4db184b49b39cc9874f4747f Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 10 Oct 2024 14:56:41 +0200 Subject: [PATCH] Lua: add bindings to insert text into editor/cursors Change-Id: Ia2b97c08be352bfa7d97141f863ffc41861a30eb Reviewed-by: Marcus Tillmanns --- src/plugins/lua/bindings/texteditor.cpp | 14 +++++++++++++- src/plugins/lua/meta/texteditor.lua | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/plugins/lua/bindings/texteditor.cpp b/src/plugins/lua/bindings/texteditor.cpp index 6c3f9e95d9b..bb98bf4be86 100644 --- a/src/plugins/lua/bindings/texteditor.cpp +++ b/src/plugins/lua/bindings/texteditor.cpp @@ -166,7 +166,11 @@ void setupTextEditorModule() "mainCursor", &MultiTextCursor::mainCursor, "cursors", - [](MultiTextCursor *self) { return sol::as_table(self->cursors()); }); + [](MultiTextCursor *self) { return sol::as_table(self->cursors()); }, + "insertText", + [](MultiTextCursor *self, const QString &text) { + self->insertText(text); + }); result.new_usertype( "Position", @@ -222,6 +226,10 @@ void setupTextEditorModule() ret.end.line = endBlock.blockNumber(); ret.end.column = endPos - endBlock.position() - 1; return ret; + }, + "insertText", + [](QTextCursor *textCursor, const QString &text) { + textCursor->insertText(text); }); result.new_usertype( @@ -255,6 +263,10 @@ void setupTextEditorModule() [](const TextEditorPtr &textEditor) { QTC_ASSERT(textEditor, throw sol::error("TextEditor is not valid")); return textEditor->editorWidget()->suggestionVisible(); + }, + "insertText", + [](TextEditorPtr editor, const QString &text) { + editor->editorWidget()->multiTextCursor().insertText(text); }); result.new_usertype( diff --git a/src/plugins/lua/meta/texteditor.lua b/src/plugins/lua/meta/texteditor.lua index d4575c14cae..5ddb65f7768 100644 --- a/src/plugins/lua/meta/texteditor.lua +++ b/src/plugins/lua/meta/texteditor.lua @@ -38,6 +38,10 @@ function TextCursor:selectedText() end ---@return Range selectionRange The range of selected text of the cursor. function TextCursor:selectionRange() end +---Inserts the passed text at the cursors position overwriting any selected text. +---@param text string The text to insert. +function TextCursor:insertText(text) end + ---@class MultiTextCursor local MultiTextCursor = {} @@ -49,6 +53,10 @@ function MultiTextCursor:mainCursor() end ---@return TextCursor[] cursors The cursors. function MultiTextCursor:cursors() end +---Inserts the passed text at all cursor positions overwriting any selected text. +---@param text string The text to insert. +function MultiTextCursor:insertText(text) end + ---@class Suggestion local Suggestion = {} @@ -106,6 +114,10 @@ function TextEditor:addFloatingWidget(widget, position) end ---@return boolean True if the suggestion is locked, false otherwise. function TextEditor:hasLockedSuggestion() end +---Inserts the passed text at all cursor positions overwriting any selected text. +---@param text string The text to insert. +function TextEditor:insertText(text) end + ---Returns the current editor or nil. ---@return TextEditor|nil editor The currently active editor or nil if there is none. function textEditor.currentEditor() end