From 11969e30d845adfcc5f2a5bb2c0242f159f8dd23 Mon Sep 17 00:00:00 2001 From: Aram Becker Date: Wed, 4 Dec 2024 16:22:56 +0100 Subject: [PATCH] feat: add skipParents parameter to @blocksort marker --- README.md | 25 ++++++++++++++++++-- src/providers/BlockSortFormattingProvider.ts | 7 +++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 233a64e..33be99f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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", + } +} +``` *** @@ -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: diff --git a/src/providers/BlockSortFormattingProvider.ts b/src/providers/BlockSortFormattingProvider.ts index e1d603c..dcf01cb 100644 --- a/src/providers/BlockSortFormattingProvider.ts +++ b/src/providers/BlockSortFormattingProvider.ts @@ -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, }; }