LUA: Improve TextEditor.Suggestion.create() and include position

Change-Id: I4735d68fe031fe8aaf1c6a4da1e843a949a91f22
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Lukasz Papierkowski
2024-09-25 13:20:15 +02:00
committed by lie
parent 7a196b4a0b
commit 9cc4e66ea1
2 changed files with 42 additions and 15 deletions

View File

@@ -21,6 +21,16 @@ using namespace Text;
namespace {
template<typename Return, typename Argument>
Return get_or_throw(const Argument &arg, const char *key)
{
const auto value = arg.template get<sol::optional<Return>>(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<TextEditor::TextSuggestion::Data>(
"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<sol::table>(suggestion, "position");
const auto position_line = get_or_throw<int>(position, "line");
const auto position_column = get_or_throw<int>(position, "column");
const auto range = get_or_throw<sol::table>(suggestion, "range");
const auto from = get_or_throw<sol::table>(range, "from");
const auto from_line = get_or_throw<int>(from, "line");
const auto from_column = get_or_throw<int>(from, "column");
const auto to = get_or_throw<sol::table>(range, "to");
const auto to_line = get_or_throw<int>(to, "line");
const auto to_column = get_or_throw<int>(to, "column");
const auto text = get_or_throw<QString>(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<TextEditor::TextDocument>(

View File

@@ -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 = {}