Skip to content

Commit

Permalink
fix: Small changes to TextDocument after API review
Browse files Browse the repository at this point in the history
- Remove duplicated selectRangeMark (same as selectRange) method
- Tidy up some methods using ranges
  • Loading branch information
narnaud committed Oct 7, 2024
1 parent 180db5b commit 472729d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 38 deletions.
6 changes: 0 additions & 6 deletions docs/API/knut/textdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[selectPreviousLine](#selectPreviousLine)**(int count = 1)|
||**[selectPreviousWord](#selectPreviousWord)**(int count = 1)|
||**[selectRange](#selectRange)**([RangeMark](../knut/rangemark.md) range)|
||**[selectRangeMark](#selectRangeMark)**([RangeMark](../knut/rangemark.md) mark)|
||**[selectRegion](#selectRegion)**(int from, int to)|
||**[selectStartOfLine](#selectStartOfLine)**(int count = 1)|
||**[selectStartOfWord](#selectStartOfWord)**()|
Expand Down Expand Up @@ -475,11 +474,6 @@ Selects the previous word, repeat the operation `count` times.

Selects the range passed in parameter.

#### <a name="selectRangeMark"></a>**selectRangeMark**([RangeMark](../knut/rangemark.md) mark)

Selects the text defined by the range make `mark`.


#### <a name="selectRegion"></a>**selectRegion**(int from, int to)

Selects the text between `from` and `to` positions.
Expand Down
35 changes: 10 additions & 25 deletions src/core/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,11 @@ void TextDocument::selectRegion(int from, int to)
void TextDocument::selectRange(const RangeMark &range)
{
LOG(range);
selectRegion(range.start(), range.end());
if (range.document() != this) {
spdlog::error("{}: Can't use a range mark from another editor.", FUNCTION_NAME);
return;
}
range.select();
}

/*!
Expand Down Expand Up @@ -1017,11 +1021,11 @@ void TextDocument::deleteRegion(int from, int to)
void TextDocument::deleteRange(const RangeMark &range)
{
LOG(range);
QTextCursor cursor(m_document->document());
cursor.setPosition(range.start(), QTextCursor::MoveAnchor);
cursor.setPosition(range.end(), QTextCursor::KeepAnchor);
cursor.removeSelectedText();
m_document->setTextCursor(cursor);
if (range.document() != this) {
spdlog::error("{}: Can't use a range mark from another editor.", FUNCTION_NAME);
return;
}
range.remove();
}

/*!
Expand Down Expand Up @@ -1185,25 +1189,6 @@ Core::RangeMark TextDocument::createRangeMark()
LOG_RETURN("rangeMark", createRangeMark(start, end));
}

/**
* \qmlmethod TextDocument::selectRangeMark(RangeMark mark)
*
* Selects the text defined by the range make `mark`.
*
* \sa RangeMark
*/
void TextDocument::selectRangeMark(const Core::RangeMark &mark)
{
LOG(LOG_ARG("mark", mark));

if (mark.document() != this) {
spdlog::error("{}: Can't use a range mark from another editor.", FUNCTION_NAME);
return;
}

mark.select();
}

/*!
* \qmlmethod bool TextDocument::find(string text, int options = TextDocument.NoFindFlags)
* Searches the string `text` in the editor. Options could be a combination of:
Expand Down
11 changes: 5 additions & 6 deletions src/core/textdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,13 @@ public slots:
void paste();
void cut();

// Text handling
void remove(int length);
// Insertion
void insert(const QString &text);
void insertAtLine(const QString &text, int line = -1);
void insertAtPosition(const QString &text, int pos);
void replace(int length, const QString &text);
void replace(int from, int to, const QString &text);
void replace(const Core::RangeMark &range, const QString &text);

// Deletion
void remove(int length);
void deleteLine(int line = -1);
void deleteSelection();
void deleteEndOfLine();
Expand All @@ -171,14 +168,16 @@ public slots:
// RangeMark
Core::RangeMark createRangeMark(int from, int to);
Core::RangeMark createRangeMark();
void selectRangeMark(const Core::RangeMark &mark);

// Find
bool find(const QString &text, int options = NoFindFlags);
bool findRegexp(const QString &regexp, int options = NoFindFlags);
QString match(const QString &regexp, int options = NoFindFlags);

// Replace
void replace(int length, const QString &text);
void replace(int from, int to, const QString &text);
void replace(const Core::RangeMark &range, const QString &text);
bool replaceOne(const QString &before, const QString &after, int options = NoFindFlags);
int replaceAll(const QString &before, const QString &after, int options = NoFindFlags);
int replaceAllInRange(const QString &before, const QString &after, const Core::RangeMark &range,
Expand Down
2 changes: 1 addition & 1 deletion tests/tst_textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private slots:
// When replacing the text in the rangemark, the rangemark should span the new text afterwards.
const auto newText = QString("Hello World");
mark.replace(newText);
document.selectRangeMark(mark);
document.selectRange(mark);
QCOMPARE(document.selectedText(), newText);

// Delete everything in the range mark.
Expand Down

0 comments on commit 472729d

Please sign in to comment.