LUA: Add TextEditor:addFloatingWidget binding

Change-Id: Ifaa93a7728a607cf09dc60160c679747b67521d2
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Lukasz Papierkowski
2024-08-20 15:32:02 +02:00
committed by lie
parent 7d73d279f7
commit b3951ff821
2 changed files with 38 additions and 3 deletions

View File

@@ -8,6 +8,7 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h> #include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <utils/layoutbuilder.h>
#include <utils/stringutils.h> #include <utils/stringutils.h>
#include <utils/tooltip/tooltip.h> #include <utils/tooltip/tooltip.h>
#include <utils/utilsicons.h> #include <utils/utilsicons.h>
@@ -375,6 +376,22 @@ TextEditor::TextEditorWidget *getSuggestionReadyEditorWidget(TextEditor::TextDoc
return widget; 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
namespace Lua::Internal { namespace Lua::Internal {
@@ -515,6 +532,17 @@ void setupTextEditorModule()
sol::no_constructor, sol::no_constructor,
"document", "document",
&TextEditor::BaseTextEditor::textDocument, &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", "cursor",
[](TextEditor::BaseTextEditor *textEditor) { [](TextEditor::BaseTextEditor *textEditor) {
return textEditor->editorWidget()->multiTextCursor(); return textEditor->editorWidget()->multiTextCursor();

View File

@@ -27,7 +27,7 @@ function Suggestion:create(startLine, startCharacter, endLine, endCharacter, tex
local CyclicSuggestion = {} local CyclicSuggestion = {}
---@return boolean True if the suggestion is locked, false otherwise. ---@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 function CyclicSuggestion:isLocked() end
---@class TextDocument ---@class TextDocument
@@ -47,8 +47,8 @@ function TextDocument:blockAndColumn(position) end
---@return integer blockCount The number of blocks in the document. ---@return integer blockCount The number of blocks in the document.
function TextDocument:blockCount() end function TextDocument:blockCount() end
--- Sets the suggestions for the document and enables tooltip on the mouse cursor hover. ---Sets the suggestions for the document and enables tooltip on the mouse cursor hover.
---@param suggestions Suggestion[] A list of possible suggestions to display ---@param suggestions Suggestion[] A list of possible suggestions to display.
function TextDocument:setSuggestions(suggestions) end function TextDocument:setSuggestions(suggestions) end
---@class TextEditor ---@class TextEditor
@@ -62,6 +62,13 @@ function TextEditor:document() end
---@return MultiTextCursor cursor The cursor of the editor. ---@return MultiTextCursor cursor The cursor of the editor.
function TextEditor:cursor() end 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. ---Returns the current editor or nil.
---@return TextEditor|nil editor The currently active editor or nil if there is none. ---@return TextEditor|nil editor The currently active editor or nil if there is none.
function textEditor.currentEditor() end function textEditor.currentEditor() end