From 9cc4e66ea1c8bfc79513de4d279cbb5e02dc4bde Mon Sep 17 00:00:00 2001 From: Lukasz Papierkowski Date: Wed, 25 Sep 2024 13:20:15 +0200 Subject: [PATCH] LUA: Improve TextEditor.Suggestion.create() and include position Change-Id: I4735d68fe031fe8aaf1c6a4da1e843a949a91f22 Reviewed-by: Marcus Tillmanns --- src/plugins/lua/bindings/texteditor.cpp | 42 +++++++++++++++++++------ src/plugins/lua/meta/texteditor.lua | 15 +++++---- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/plugins/lua/bindings/texteditor.cpp b/src/plugins/lua/bindings/texteditor.cpp index 81fbbbefc8d..5bda01ff3e7 100644 --- a/src/plugins/lua/bindings/texteditor.cpp +++ b/src/plugins/lua/bindings/texteditor.cpp @@ -21,6 +21,16 @@ using namespace Text; namespace { +template +Return get_or_throw(const Argument &arg, const char *key) +{ + const auto value = arg.template get>(key); + if (!value) { + throw sol::error(std::string("Failed to get value for key: ") + key); + } + return *value; +} + TextEditor::TextEditorWidget *getSuggestionReadyEditorWidget(TextEditor::TextDocument *document) { const auto textEditor = TextEditor::BaseTextEditor::currentTextEditor(); @@ -249,15 +259,29 @@ void setupTextEditorModule() result.new_usertype( "Suggestion", "create", - [](int start_line, - int start_character, - int end_line, - int end_character, - const QString &text) -> TextEditor::TextSuggestion::Data { - auto one_based = [](int zero_based) { return zero_based + 1; }; - Text::Position start_pos = {one_based(start_line), start_character}; - Text::Position end_pos = {one_based(end_line), end_character}; - return {Text::Range{start_pos, end_pos}, start_pos, text}; + [](const sol::table &suggestion) -> TextEditor::TextSuggestion::Data { + const auto one_based = [](int zero_based) { return zero_based + 1; }; + const auto position = get_or_throw(suggestion, "position"); + const auto position_line = get_or_throw(position, "line"); + const auto position_column = get_or_throw(position, "column"); + + const auto range = get_or_throw(suggestion, "range"); + + const auto from = get_or_throw(range, "from"); + const auto from_line = get_or_throw(from, "line"); + const auto from_column = get_or_throw(from, "column"); + + const auto to = get_or_throw(range, "to"); + const auto to_line = get_or_throw(to, "line"); + const auto to_column = get_or_throw(to, "column"); + + const auto text = get_or_throw(suggestion, "text"); + + const Text::Position cursor_pos = {one_based(position_line), position_column}; + const Text::Position from_pos = {one_based(from_line), from_column}; + const Text::Position to_pos = {one_based(to_line), to_column}; + + return {Text::Range{from_pos, to_pos}, cursor_pos, text}; }); result.new_usertype( diff --git a/src/plugins/lua/meta/texteditor.lua b/src/plugins/lua/meta/texteditor.lua index efab5ef8c41..d4575c14cae 100644 --- a/src/plugins/lua/meta/texteditor.lua +++ b/src/plugins/lua/meta/texteditor.lua @@ -52,13 +52,16 @@ function MultiTextCursor:cursors() end ---@class Suggestion local Suggestion = {} ----@param startLine integer Start position line where to apply the suggestion. ----@param startCharacter integer Start position character where to apply the suggestion. ----@param endLine integer End position line where to apply the suggestion. ----@param endCharacter integer End position character where to apply the suggestion. ----@param text string Suggestions text. +---@class SuggestionParams +---@field text string The text of the suggestion. +---@field position Position The cursor position where the suggestion should be inserted. +---@field range Range The range of the text preceding the suggestion. +SuggestionParams = {} + +---Creates Suggestion. +---@param params SuggestionParams Parameters for creating the suggestion. ---@return Suggestion suggestion The created suggestion. -function Suggestion:create(startLine, startCharacter, endLine, endCharacter, text) end +function Suggestion:create(params) end ---@class TextDocument local TextDocument = {}