forked from qt-creator/qt-creator
Lua: simplify Editor suggestion
Using Text::Range for the suggestion start and end pos simplifies the code and allows us to remove the duplicated toSelection helper and replace with Text::Range::toTextCursor. Change-Id: Id90545fe59453d3feb5cd7d434945388d2bbd232 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -23,21 +23,18 @@ namespace {
|
|||||||
class Suggestion
|
class Suggestion
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Suggestion(Text::Position start, Text::Position end, Text::Position position, const QString &text)
|
Suggestion(Text::Range range, Text::Position position, const QString &text)
|
||||||
: m_start(start)
|
: m_range(range)
|
||||||
, m_end(end)
|
|
||||||
, m_position(position)
|
, m_position(position)
|
||||||
, m_text(text)
|
, m_text(text)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Text::Position start() const { return m_start; }
|
Text::Range range() const { return m_range; }
|
||||||
Text::Position end() const { return m_end; }
|
|
||||||
Text::Position position() const { return m_position; }
|
Text::Position position() const { return m_position; }
|
||||||
QString text() const { return m_text; }
|
QString text() const { return m_text; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Text::Position m_start;
|
Text::Range m_range;
|
||||||
Text::Position m_end;
|
|
||||||
Text::Position m_position;
|
Text::Position m_position;
|
||||||
QString m_text;
|
QString m_text;
|
||||||
};
|
};
|
||||||
@@ -49,15 +46,6 @@ QTextCursor toTextCursor(QTextDocument *doc, const Text::Position &position)
|
|||||||
return cursor;
|
return cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextCursor toSelection(QTextDocument *doc, const Text::Position &start, const Text::Position &end)
|
|
||||||
{
|
|
||||||
QTC_ASSERT(doc, return {});
|
|
||||||
QTextCursor cursor = toTextCursor(doc, start);
|
|
||||||
cursor.setPosition(end.toPositionInDocument(doc), QTextCursor::KeepAnchor);
|
|
||||||
|
|
||||||
return cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
class CyclicSuggestion : public QObject, public TextEditor::TextSuggestion
|
class CyclicSuggestion : public QObject, public TextEditor::TextSuggestion
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -75,8 +63,8 @@ public:
|
|||||||
{
|
{
|
||||||
QTC_ASSERT(current_suggestion < m_suggestions.size(), return);
|
QTC_ASSERT(current_suggestion < m_suggestions.size(), return);
|
||||||
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
||||||
const auto start = suggestion.start();
|
const auto start = suggestion.range().begin;
|
||||||
const auto end = suggestion.end();
|
const auto end = suggestion.range().end;
|
||||||
|
|
||||||
QString text = toTextCursor(origin_document->document(), start).block().text();
|
QString text = toTextCursor(origin_document->document(), start).block().text();
|
||||||
int length = text.length() - start.column;
|
int length = text.length() - start.column;
|
||||||
@@ -102,7 +90,7 @@ public:
|
|||||||
QTC_ASSERT(m_currentSuggestion < m_suggestions.size(), return false);
|
QTC_ASSERT(m_currentSuggestion < m_suggestions.size(), return false);
|
||||||
reset();
|
reset();
|
||||||
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
||||||
QTextCursor cursor = toSelection(m_start.document(), suggestion.start(), suggestion.end());
|
QTextCursor cursor = suggestion.range().toTextCursor(m_start.document());
|
||||||
cursor.insertText(suggestion.text());
|
cursor.insertText(suggestion.text());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -114,7 +102,7 @@ public:
|
|||||||
|
|
||||||
lockCurrentSuggestion();
|
lockCurrentSuggestion();
|
||||||
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
const auto &suggestion = m_suggestions.at(m_currentSuggestion);
|
||||||
QTextCursor cursor = toSelection(m_start.document(), suggestion.start(), suggestion.end());
|
QTextCursor cursor = suggestion.range().toTextCursor(m_start.document());
|
||||||
QTextCursor currentCursor = widget->textCursor();
|
QTextCursor currentCursor = widget->textCursor();
|
||||||
const QString text = suggestion.text();
|
const QString text = suggestion.text();
|
||||||
const int startPos = currentCursor.positionInBlock() - cursor.positionInBlock()
|
const int startPos = currentCursor.positionInBlock() - cursor.positionInBlock()
|
||||||
@@ -571,7 +559,7 @@ void setupTextEditorModule()
|
|||||||
auto one_based = [](int zero_based) { return zero_based + 1; };
|
auto one_based = [](int zero_based) { return zero_based + 1; };
|
||||||
Text::Position start_pos = {one_based(start_line), start_character};
|
Text::Position start_pos = {one_based(start_line), start_character};
|
||||||
Text::Position end_pos = {one_based(end_line), end_character};
|
Text::Position end_pos = {one_based(end_line), end_character};
|
||||||
return {start_pos, end_pos, start_pos, text};
|
return {Text::Range(start_pos, end_pos), start_pos, text};
|
||||||
});
|
});
|
||||||
|
|
||||||
result.new_usertype<TextEditor::TextDocument>(
|
result.new_usertype<TextEditor::TextDocument>(
|
||||||
|
Reference in New Issue
Block a user