Skip to content

Commit

Permalink
feat: add skipParents parameter to @blocksort marker
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Dec 4, 2024
1 parent 8503dbd commit 11969e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ To enable auto Sorting, you must set the `editor.codeActionsOnSave` in your `set

This will enable auto-sorting for blocks following a `@blocksort` marker.
The marker can additionally be followed by the options `asc` or `desc` to control the sorting order,
as well as a number for the sorting depth:
as well as optional numbers for the depth and skipping parent blocks (`depth:skip`):

```js
// @blocksort asc
Expand All @@ -99,6 +99,23 @@ some:
- up to
- any level)
```
```jsonc
// @blocksort inf:1
{
"keep2": [
"sort",
"only",
"inner",
"values",
],
"keep1": {
"first": "level",
"items": "will",
"be": "kept",
}
}
```
</details>

***
Expand All @@ -110,7 +127,11 @@ some:

- `defaultMultilevelDepth`: Default depth used for deep sorting.
- Default: `-1` (infinite)
- `askForMultilevelDepth`: Skip asking for multilevel depth and always use `defaultMultilevelDepth`.
- `askForMultilevelDepth`: Skip asking for multilevel depth and always use `defaultSkipParents`.
- Default: `true`
- `defaultSkipParents`: Default depth of parent blocks to skip sorting
- Default: `0`
- `askForSkipParents`: Skip asking for depth of parent blocks to skip and always use `defaultSkipParents`.
- Default: `true`
- `indentIgnoreMarkers`: List of regex markers that when matched will result in ignoring the indentation of the current line. This is for example used for c-style `{` in a new line. The markers are always assumed to be at teh start of the line, but can be preceded by spaces and comments.
- Default:
Expand Down
7 changes: 4 additions & 3 deletions src/providers/BlockSortFormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export default class BlockSortFormattingProvider

public static getBlockSortMarkerOptions(document: TextDocument, position: Position): BlockSortOptions {
const line = document.lineAt(position.line).text;
const matches = line.match(/@blocksort ?(asc|desc)? ?(\d+|inf(?:inite)?)?/) ?? [];
const [_, direction = "asc", depth = "0"] = matches as [string, "asc" | "desc", string];
const matches = line.match(/@blocksort ?(asc|desc)? ?(\d+|inf(?:inite)?)?:?(\d+|inf(?:inite)?)?/) ?? [];
const [_, direction = "asc", depth = "0", skip = "0"] = matches as [string, "asc" | "desc", string];
const collator = ConfigurationProvider.getCollatorOptions();

return {
collator,
direction,
sortChildren: depth.includes("inf") ? Infinity : parseInt(depth, 10),
sortChildren: depth.startsWith("inf") ? Infinity : parseInt(depth, 10),
skipParents: skip.startsWith("inf") ? Infinity : parseInt(skip, 10),
expandSelection: true,
};
}
Expand Down

0 comments on commit 11969e3

Please # to comment.