diff --git a/src/plugins/lua/bindings/texteditor.cpp b/src/plugins/lua/bindings/texteditor.cpp index 33e8da0a3b4..a55a65b86ac 100644 --- a/src/plugins/lua/bindings/texteditor.cpp +++ b/src/plugins/lua/bindings/texteditor.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -375,6 +376,22 @@ TextEditor::TextEditorWidget *getSuggestionReadyEditorWidget(TextEditor::TextDoc return widget; } +void addFloatingWidget(TextEditor::BaseTextEditor *editor, QWidget *widget, int position) +{ + widget->setParent(editor->editorWidget()->viewport()); + const auto editorWidget = editor->editorWidget(); + + QTextCursor cursor = QTextCursor(editor->textDocument()->document()); + cursor.setPosition(position); + const QRect cursorRect = editorWidget->cursorRect(cursor); + + QPoint widgetPos = cursorRect.bottomLeft(); + widgetPos.ry() += (cursorRect.top() - cursorRect.bottom()) / 2; + + widget->move(widgetPos); + widget->show(); +} + } // namespace namespace Lua::Internal { @@ -515,6 +532,17 @@ void setupTextEditorModule() sol::no_constructor, "document", &TextEditor::BaseTextEditor::textDocument, + "addFloatingWidget", + sol::overload( + [](TextEditor::BaseTextEditor *textEditor, QWidget *widget, int position) { + addFloatingWidget(textEditor, widget, position); + }, + [](TextEditor::BaseTextEditor *textEditor, Layouting::Widget *widget, int position) { + addFloatingWidget(textEditor, widget->emerge(), position); + }, + [](TextEditor::BaseTextEditor *textEditor, Layouting::Layout *layout, int position) { + addFloatingWidget(textEditor, layout->emerge(), position); + }), "cursor", [](TextEditor::BaseTextEditor *textEditor) { return textEditor->editorWidget()->multiTextCursor(); diff --git a/src/plugins/lua/meta/texteditor.lua b/src/plugins/lua/meta/texteditor.lua index 795a6616fca..8cd4bce5d5e 100644 --- a/src/plugins/lua/meta/texteditor.lua +++ b/src/plugins/lua/meta/texteditor.lua @@ -27,7 +27,7 @@ function Suggestion:create(startLine, startCharacter, endLine, endCharacter, tex local CyclicSuggestion = {} ---@return boolean True if the suggestion is locked, false otherwise. ----Suggestion is locked when the user selects it and already started aplying it partially. +---Suggestion is locked when the user selects it and already started applying it partially. function CyclicSuggestion:isLocked() end ---@class TextDocument @@ -47,8 +47,8 @@ function TextDocument:blockAndColumn(position) end ---@return integer blockCount The number of blocks in the document. function TextDocument:blockCount() end ---- Sets the suggestions for the document and enables tooltip on the mouse cursor hover. ----@param suggestions Suggestion[] A list of possible suggestions to display +---Sets the suggestions for the document and enables tooltip on the mouse cursor hover. +---@param suggestions Suggestion[] A list of possible suggestions to display. function TextDocument:setSuggestions(suggestions) end ---@class TextEditor @@ -62,6 +62,13 @@ function TextEditor:document() end ---@return MultiTextCursor cursor The cursor of the editor. function TextEditor:cursor() end +---Adds a floating widget at the specified position in the text editor. +---The widget will be positioned at the location corresponding to the given position in the +---text document and will be automatically managed to stay pined to that position. +---@param widget Widget The widget to be added as a floating widget. +---@param position integer The position in the document where the widget should appear. +function TextEditor:addFloatingWidget(widget, position) 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