Skip to content

Commit

Permalink
Improve indentation API (#231)
Browse files Browse the repository at this point in the history
* refactor: Simplify indenation implementation

* feat: Add TextDocument::indentationAtLine

* feat: TextDocument::setIndentation

This function allows setting the *absolute* indentation for the current
line/selection.

* feat: Line-based indentation API

Add API for indenting at a given line number, not just at the current
selection/position.

* refactor!: indentationAt API now returns int

The `indentationAtLine` and `...AtPosition` methods now return `int`, in
accordance to the `indent` and `setIndentation` methods.

The old behavior is still accessible via the `indentTextAtLine` and
`indentTextAtPosition` functions.
It turned out to still be useful, as there are a few situation where you
just want to copy the indentation from another line.
Getting access to the original indentation is useful there, as it can
deal better with cases where Knuts indentation settings don't match up
with the actual indentation in the file.

* refactor!: Remove TextDocument::removeIndent

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.

* chore: Bump photonwidgets to new indentation API

Note: This uses a yet-unmerged commit in photonwidgets
Ideally this commit should be updated to a stable version of
photonwidgets before merging

* fix: Mark indentation getter API as Q_INVOKABLE

Clazy in the CI is right, that these should indeed not be slots, but
rather Q_INVOKABLE.
  • Loading branch information
LeonMatthesKDAB authored Dec 13, 2024
1 parent 2d893d4 commit 67d7a97
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 116 deletions.
39 changes: 0 additions & 39 deletions .reuse/dep5

This file was deleted.

2 changes: 1 addition & 1 deletion 3rdparty-kdab/photonwidgets
Submodule photonwidgets updated from 3ea1a6 to f6fe20
46 changes: 46 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version = 1
SPDX-PackageName = "knut"
SPDX-PackageSupplier = "<[email protected]>"
SPDX-PackageDownloadLocation = "https://github.com/KDAB/knut/"

[[annotations]]
path = [".pre-commit-config.yaml", ".codespellrc", ".clangd", "**.clang-format", ".clazy", ".gitattributes", ".github/workflows/documentation.yml", ".gitignore", ".gitmodules", ".krazy", "knut.code-workspace", ".github/**.yml", "mkdocs.yml"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>"
SPDX-License-Identifier = "BSD-3-Clause"

[[annotations]]
path = ["**.md", "docs/API/mapping.txt", "docs/**.png", "docs/**.puml", "docs/**.gif", "docs/**.svg", "docs/**.css"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>"
SPDX-License-Identifier = "GPL-3.0-only"

[[annotations]]
path = ["**.json", "**.ui", "**.qrc", "**.rc", "**.xml", "src/**.png", "src/**.ico", "version.txt"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>"
SPDX-License-Identifier = "GPL-2.0-only OR GPL-3.0-only"

[[annotations]]
path = "test_data/**"
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>"
SPDX-License-Identifier = "GPL-2.0-only OR GPL-3.0-only"

[[annotations]]
path = ["test_data/rcfiles/cryEdit/Resource.h", "test_data/rcfiles/luaDebugger/resource.h"]
precedence = "aggregate"
SPDX-FileCopyrightText = "Amazon.com, Inc. or its affiliates or its licensors"
SPDX-License-Identifier = "Apache-2.0 OR MIT"

[[annotations]]
path = "qt6.natvis"
precedence = "aggregate"
SPDX-FileCopyrightText = "2022 The Qt Company Ltd."
SPDX-License-Identifier = "GPL-3.0-only OR LicenseRef-Qt-Commercial"

[[annotations]]
path = "src/gui/gui/**.png"
precedence = "aggregate"
SPDX-FileCopyrightText = "The Pictogrammers"
SPDX-License-Identifier = "Apache-2.0"
62 changes: 54 additions & 8 deletions docs/API/knut/textdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[gotoStartOfWord](#gotoStartOfWord)**()|
|bool |**[hasSelection](#hasSelection)**()|
||**[indent](#indent)**(int count)|
||**[indentationAtPosition](#indentationAtPosition)**(int pos)|
||**[indentLine](#indentLine)**(int count, int line)|
|string |**[indentTextAtLine](#indentTextAtLine)**(int line = -1)|
|string |**[indentTextAtPosition](#indentTextAtPosition)**(int pos)|
|int |**[indentationAtLine](#indentationAtLine)**(int line = -1)|
|int |**[indentationAtPosition](#indentationAtPosition)**(int pos)|
||**[insert](#insert)**(string text)|
||**[insertAtLine](#insertAtLine)**(string text, int line = -1)|
||**[insertAtPosition](#insertAtPosition)**(string text, int pos)|
Expand All @@ -72,7 +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)|
||**[replace](#replace)**(int length, string text)|
||**[replace](#replace)**([RangeMark](../knut/rangemark.md) range, string text)|
||**[replace](#replace)**(int from, int to, string text)|
Expand All @@ -96,6 +99,8 @@ Inherited properties: [Document properties](../knut/document.md#properties)
||**[selectStartOfWord](#selectStartOfWord)**()|
||**[selectTo](#selectTo)**(int pos)|
||**[selectToMark](#selectToMark)**([Mark](../knut/mark.md) mark)|
||**[setIndentation](#setIndentation)**(int indent)|
||**[setIndentationAtLine](#setIndentationAtLine)**(int indent, int line)|
||**[undo](#undo)**(int count)|
||**[unselect](#unselect)**()|

Expand Down Expand Up @@ -309,9 +314,41 @@ 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.

#### <a name="indentationAtPosition"></a>**indentationAtPosition**(int pos)
The `count` can be negative to reduce the existing indentation.

Returns the indentation at the given position.
See also: [`setIndentation`](#setIndentation).

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

Indents the `line` `count` times.

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

#### <a name="indentTextAtLine"></a>string **indentTextAtLine**(int line = -1)

Returns the indentation text at the given line.

If `line` is -1 it will return the indentation at the current line.
If `line` is larger than the number of lines in the document, it will return an empty string

Note: To get the level of indentation, use [`indentationAtLine`](#indentationAtLine).

#### <a name="indentTextAtPosition"></a>string **indentTextAtPosition**(int pos)

Returns the indentation text at the given position.

Note: To get the level of indentation, use [`indentationAtPosition`](#indentationAtPosition).

#### <a name="indentationAtLine"></a>int **indentationAtLine**(int line = -1)

Returns the indentation level at the given line.

If `line` is -1 it will return the indentation at the current line.
If `line` is larger than the number of lines in the document, it will return 0

#### <a name="indentationAtPosition"></a>int **indentationAtPosition**(int pos)

Returns the indentation level at the given position.

#### <a name="insert"></a>**insert**(string text)

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

Remove `length` character from the current position.

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

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

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

Replaces `length` characters from the current position with the string `text`.
Expand Down Expand Up @@ -495,6 +528,19 @@ Selects the text from the current position to `pos`.

Selects the text from the cursor position to the `mark`.

#### <a name="setIndentation"></a>**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 [`indentLine`](#indentLine).

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

Sets the absolute indentation of the `line` to `indent` indentations.

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

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

Undo `count` times the last actions.
Expand Down
6 changes: 3 additions & 3 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,12 @@ CppDocument::addMemberOrMethod(const QString &memberInfo, const QString &classNa
const auto fields = match.getAll("field");
if (!fields.isEmpty()) {
const auto &pos = fields.last();
const auto indent = indentationAtPosition(pos.end());
const auto indent = indentTextAtPosition(pos.end());
insertAtPosition("\n" + indent + memberText, pos.end());
} else {
const auto access = match.getAll("access");
const auto &pos = access.last();
const auto indent = indentationAtPosition(pos.end());
const auto indent = indentTextAtPosition(pos.end());
insertAtPosition("\n" + indent + memberText, pos.end());
}
} else {
Expand Down Expand Up @@ -1519,7 +1519,7 @@ bool CppDocument::addSpecifierSection(const QString &memberText, const QString &
if (!result.isEmpty()) {
const auto &match = result.last();
const auto pos = match.get("pos");
const auto indent = indentationAtPosition(pos.end());
const auto indent = indentTextAtPosition(pos.end());

const QString newSpecifier = QString("\n\n%1:").arg(accessSpecifierMap.value(specifier));

Expand Down
Loading

0 comments on commit 67d7a97

Please sign in to comment.