Skip to content

Commit

Permalink
refactor!: Remove TextDocument::removeIndent
Browse files Browse the repository at this point in the history
Calling `indent` with negative values has exactly the same effect, so
remove this duplicate function.
`remove` is also not a great name for what the function did, as it didn't
remove the entire indentation but rather "reduced" it.
  • Loading branch information
LeonMatthesKDAB committed Dec 5, 2024
1 parent 7924cde commit 1755272
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 51 deletions.
21 changes: 4 additions & 17 deletions docs/API/knut/textdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[positionAt](#positionAt)**(int line, int col)|
||**[redo](#redo)**(int count)|
||**[remove](#remove)**(int length)|
||**[removeIndent](#removeIndent)**(int count)|
||**[removeIndentAtLine](#removeIndentAtLine)**(int count, int line)|
||**[replace](#replace)**(int length, string text)|
||**[replace](#replace)**([RangeMark](../knut/rangemark.md) range, string text)|
||**[replace](#replace)**(int from, int to, string text)|
Expand Down Expand Up @@ -316,7 +314,9 @@ Returns true if the editor has a selection.

Indents the current line `count` times. If there's a selection, indent all lines in the selection.

See also: [`removeIndent`](#removeIndent), [`setIndentation`](#setIndentation).
The `count` can be negative to reduce the existing indentation.

See also: [`setIndentation`](#setIndentation).

#### <a name="indentLine"></a>**indentLine**(int count, int line)

Expand Down Expand Up @@ -392,19 +392,6 @@ Redo `count` times the last actions.

Remove `length` character from the current position.

#### <a name="removeIndent"></a>**removeIndent**(int count)

Reduce the indenation of the current line `count` times. If there's a selection, reduce indentation for all lines in
the selection.

See also: [`indent`](#indent), [`setIndentation`](#setIndentation).

#### <a name="removeIndentAtLine"></a>**removeIndentAtLine**(int count, int line)

Reduce the indentation of the `line` by `count` times.

See also: [`removeIndent`](#removeIndent)

#### <a name="replace"></a>**replace**(int length, string text)

Replaces `length` characters from the current position with the string `text`.
Expand Down Expand Up @@ -546,7 +533,7 @@ Selects the text from the cursor position to the `mark`.
Sets the absolute indentation of the current line to `indent` indentations.
If there's a selection, sets the indentation of all lines in the selection.

For relative indentation, see [`indent`](#indent) and [`removeIndent`](#removeIndent).
For relative indentation, see [`indent`](#indent) and [`indentLine`](#indentLine).

#### <a name="setIndentationAtLine"></a>**setIndentationAtLine**(int indent, int line)

Expand Down
34 changes: 5 additions & 29 deletions src/core/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ bool TextDocument::eventFilter(QObject *watched, QEvent *event)
else if (keyEvent == QKeySequence::MoveToPreviousPage)
return false;
else if ((keyEvent->key() == Qt::Key_Backtab))
removeIndent();
indent(-1);
else if (keyEvent->key() == Qt::Key_Tab)
indent();
else if (keyEvent == QKeySequence::Undo)
Expand Down Expand Up @@ -1611,7 +1611,9 @@ void indentTextInTextEdit(QPlainTextEdit *textEdit, int tabCount, bool relative)
* \qmlmethod TextDocument::indent(int count)
* Indents the current line `count` times. If there's a selection, indent all lines in the selection.
*
* See also: [`removeIndent`](#removeIndent), [`setIndentation`](#setIndentation).
* The `count` can be negative to reduce the existing indentation.
*
* See also: [`setIndentation`](#setIndentation).
*/
void TextDocument::indent(int count)
{
Expand All @@ -1632,38 +1634,12 @@ void TextDocument::indentLine(int count, int line)
indentBlocksInTextEdit(m_document, line - 1, line - 1, count, true);
}

/*!
* \qmlmethod TextDocument::removeIndent(int count)
* Reduce the indenation of the current line `count` times. If there's a selection, reduce indentation for all lines in
* the selection.
*
* See also: [`indent`](#indent), [`setIndentation`](#setIndentation).
*/
void TextDocument::removeIndent(int count)
{
LOG_AND_MERGE(count);
indentTextInTextEdit(m_document, -count);
}

/*!
* \qmlmethod TextDocument::removeIndentAtLine(int count, int line)
* Reduce the indentation of the `line` by `count` times.
*
* See also: [`removeIndent`](#removeIndent)
*/
void TextDocument::removeIndentAtLine(int count, int line)
{
LOG(LOG_ARG("count", count), LOG_ARG("line", line));

indentBlocksInTextEdit(m_document, line - 1, line - 1, -count, true);
}

/*!
* \qmlmethod TextDocument::setIndentation(int indent)
* Sets the absolute indentation of the current line to `indent` indentations.
* If there's a selection, sets the indentation of all lines in the selection.
*
* For relative indentation, see [`indent`](#indent) and [`removeIndent`](#removeIndent).
* For relative indentation, see [`indent`](#indent) and [`indentLine`](#indentLine).
*/
void TextDocument::setIndentation(int indent)
{
Expand Down
2 changes: 0 additions & 2 deletions src/core/textdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ public slots:
// Indentation
void indent(int count = 1);
void indentLine(int count, int line);
void removeIndent(int count = 1);
void removeIndentAtLine(int count, int line);
void setIndentation(int indent);
void setIndentationAtLine(int indent, int line);
int indentationAtPosition(int pos) const;
Expand Down
6 changes: 3 additions & 3 deletions tests/tst_textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private slots:
// When we remove 2 levels of indentation, that will result in 2 columns left (aka. 8 spaces).
document.gotoLine(7, 4);
COMPARE_CURRENT_INDENT(" \t\t\t\t", 4)
document.removeIndent(2);
document.indent(-2);
COMPARE_CURRENT_INDENT(spaces(8), 2)

document.gotoLine(10);
Expand All @@ -393,14 +393,14 @@ private slots:

document.gotoLine(16);
document.selectNextLine();
document.removeIndent();
document.indent(-1);

document.gotoLine(19);
document.selectNextLine();
document.setIndentation(1);

document.indentLine(2, 25);
document.removeIndentAtLine(1, 26);
document.indentLine(-1, 26);
document.setIndentationAtLine(1, 28);

document.save();
Expand Down

0 comments on commit 1755272

Please sign in to comment.