forked from qt-creator/qt-creator
LUA: Improve TextEditor.Suggestion.create() and include position
Change-Id: I4735d68fe031fe8aaf1c6a4da1e843a949a91f22 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -21,6 +21,16 @@ using namespace Text;
|
|||||||
|
|
||||||
namespace {
|
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)
|
TextEditor::TextEditorWidget *getSuggestionReadyEditorWidget(TextEditor::TextDocument *document)
|
||||||
{
|
{
|
||||||
const auto textEditor = TextEditor::BaseTextEditor::currentTextEditor();
|
const auto textEditor = TextEditor::BaseTextEditor::currentTextEditor();
|
||||||
@@ -249,15 +259,29 @@ void setupTextEditorModule()
|
|||||||
result.new_usertype<TextEditor::TextSuggestion::Data>(
|
result.new_usertype<TextEditor::TextSuggestion::Data>(
|
||||||
"Suggestion",
|
"Suggestion",
|
||||||
"create",
|
"create",
|
||||||
[](int start_line,
|
[](const sol::table &suggestion) -> TextEditor::TextSuggestion::Data {
|
||||||
int start_character,
|
const auto one_based = [](int zero_based) { return zero_based + 1; };
|
||||||
int end_line,
|
const auto position = get_or_throw<sol::table>(suggestion, "position");
|
||||||
int end_character,
|
const auto position_line = get_or_throw<int>(position, "line");
|
||||||
const QString &text) -> TextEditor::TextSuggestion::Data {
|
const auto position_column = get_or_throw<int>(position, "column");
|
||||||
auto one_based = [](int zero_based) { return zero_based + 1; };
|
|
||||||
Text::Position start_pos = {one_based(start_line), start_character};
|
const auto range = get_or_throw<sol::table>(suggestion, "range");
|
||||||
Text::Position end_pos = {one_based(end_line), end_character};
|
|
||||||
return {Text::Range{start_pos, end_pos}, start_pos, text};
|
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>(
|
result.new_usertype<TextEditor::TextDocument>(
|
||||||
|
@@ -52,13 +52,16 @@ function MultiTextCursor:cursors() end
|
|||||||
---@class Suggestion
|
---@class Suggestion
|
||||||
local Suggestion = {}
|
local Suggestion = {}
|
||||||
|
|
||||||
---@param startLine integer Start position line where to apply the suggestion.
|
---@class SuggestionParams
|
||||||
---@param startCharacter integer Start position character where to apply the suggestion.
|
---@field text string The text of the suggestion.
|
||||||
---@param endLine integer End position line where to apply the suggestion.
|
---@field position Position The cursor position where the suggestion should be inserted.
|
||||||
---@param endCharacter integer End position character where to apply the suggestion.
|
---@field range Range The range of the text preceding the suggestion.
|
||||||
---@param text string Suggestions text.
|
SuggestionParams = {}
|
||||||
|
|
||||||
|
---Creates Suggestion.
|
||||||
|
---@param params SuggestionParams Parameters for creating the suggestion.
|
||||||
---@return Suggestion suggestion The created suggestion.
|
---@return Suggestion suggestion The created suggestion.
|
||||||
function Suggestion:create(startLine, startCharacter, endLine, endCharacter, text) end
|
function Suggestion:create(params) end
|
||||||
|
|
||||||
---@class TextDocument
|
---@class TextDocument
|
||||||
local TextDocument = {}
|
local TextDocument = {}
|
||||||
|
Reference in New Issue
Block a user