forked from qt-creator/qt-creator
Lua: Implement LUA to get a selection text range
Change-Id: If2c24cc8c2063617fa378fed158ae65000c00c91 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
#include "sol/sol.hpp"
|
#include "sol/sol.hpp"
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
using namespace Text;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -166,6 +167,23 @@ void setupTextEditorModule()
|
|||||||
"cursors",
|
"cursors",
|
||||||
[](MultiTextCursor *self) { return sol::as_table(self->cursors()); });
|
[](MultiTextCursor *self) { return sol::as_table(self->cursors()); });
|
||||||
|
|
||||||
|
result.new_usertype<Position>(
|
||||||
|
"Position",
|
||||||
|
sol::no_constructor,
|
||||||
|
"line",
|
||||||
|
sol::property([](Position &pos) { return pos.line; }),
|
||||||
|
"column",
|
||||||
|
sol::property([](Position &pos) { return pos.column; }));
|
||||||
|
|
||||||
|
// In range can't use begin/end as "end" is a reserved word for LUA scripts
|
||||||
|
result.new_usertype<Range>(
|
||||||
|
"Range",
|
||||||
|
sol::no_constructor,
|
||||||
|
"from",
|
||||||
|
sol::property([](Range &range) { return range.begin; }),
|
||||||
|
"to",
|
||||||
|
sol::property([](Range &range) { return range.end; }));
|
||||||
|
|
||||||
result.new_usertype<QTextCursor>(
|
result.new_usertype<QTextCursor>(
|
||||||
"TextCursor",
|
"TextCursor",
|
||||||
sol::no_constructor,
|
sol::no_constructor,
|
||||||
@@ -180,6 +198,29 @@ void setupTextEditorModule()
|
|||||||
"selectedText",
|
"selectedText",
|
||||||
[](QTextCursor *cursor) {
|
[](QTextCursor *cursor) {
|
||||||
return cursor->selectedText().replace(QChar::ParagraphSeparator, '\n');
|
return cursor->selectedText().replace(QChar::ParagraphSeparator, '\n');
|
||||||
|
},
|
||||||
|
"selectionRange",
|
||||||
|
[](const QTextCursor &textCursor) -> Range {
|
||||||
|
Range ret;
|
||||||
|
if (!textCursor.hasSelection())
|
||||||
|
throw sol::error("Cursor has no selection");
|
||||||
|
|
||||||
|
int startPos = textCursor.selectionStart();
|
||||||
|
int endPos = textCursor.selectionEnd();
|
||||||
|
|
||||||
|
QTextDocument *doc = textCursor.document();
|
||||||
|
if (!doc)
|
||||||
|
throw sol::error("Cursor has no document");
|
||||||
|
|
||||||
|
QTextBlock startBlock = doc->findBlock(startPos);
|
||||||
|
QTextBlock endBlock = doc->findBlock(endPos);
|
||||||
|
|
||||||
|
ret.begin.line = startBlock.blockNumber();
|
||||||
|
ret.begin.column = startPos - startBlock.position() - 1;
|
||||||
|
|
||||||
|
ret.end.line = endBlock.blockNumber();
|
||||||
|
ret.end.column = endPos - endBlock.position() - 1;
|
||||||
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
result.new_usertype<TextEditor::BaseTextEditor>(
|
result.new_usertype<TextEditor::BaseTextEditor>(
|
||||||
|
@@ -1,6 +1,16 @@
|
|||||||
---@meta TextEditor
|
---@meta TextEditor
|
||||||
local textEditor = {}
|
local textEditor = {}
|
||||||
|
|
||||||
|
---@class Position
|
||||||
|
---@field line integer The line number.
|
||||||
|
---@field column integer The column number.
|
||||||
|
local Position = {}
|
||||||
|
|
||||||
|
---@class Range
|
||||||
|
---@field from Position The beginning position of the range.
|
||||||
|
---@field to Position The end position of the range.
|
||||||
|
local Range = {}
|
||||||
|
|
||||||
---@class TextCursor
|
---@class TextCursor
|
||||||
local TextCursor = {}
|
local TextCursor = {}
|
||||||
|
|
||||||
@@ -24,6 +34,10 @@ function TextCursor:hasSelection() end
|
|||||||
---@return string selectedText The selected text of the cursor.
|
---@return string selectedText The selected text of the cursor.
|
||||||
function TextCursor:selectedText() end
|
function TextCursor:selectedText() end
|
||||||
|
|
||||||
|
---Returns the range of selected text of the cursor.
|
||||||
|
---@return Range selectionRange The range of selected text of the cursor.
|
||||||
|
function TextCursor:selectionRange() end
|
||||||
|
|
||||||
---@class MultiTextCursor
|
---@class MultiTextCursor
|
||||||
local MultiTextCursor = {}
|
local MultiTextCursor = {}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user