From 049e5514c211d5eebc69f45c7f6a5feda8139e4f Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Tue, 28 Feb 2023 22:43:29 +0100 Subject: [PATCH] Keep block sequences with BlockProcessor (#62) --- README.md | 46 +++++++++++++------ package.json | 5 ++ src/controller/processor-controller.ts | 14 ++++++ src/processor/block-processor.ts | 7 +++ src/settings.ts | 1 + .../suite/processor/block-processor.test.ts | 44 ++++++++++++++++++ src/test/suite/util/yaml-util.test.ts | 28 ----------- 7 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 src/processor/block-processor.ts create mode 100644 src/test/suite/processor/block-processor.test.ts diff --git a/README.md b/README.md index 3848f4b..930ccd7 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,9 @@ The ArrayProcessor will not add linebreaks to single-line array structures. - - - + + + @@ -80,14 +80,30 @@ The ArrayProcessor will not add linebreaks to single-line array structures.
Original DocumentSorted with
useArrayProcessor: true
Sorted with
useArrayProcessor: false
Original DocumentSorted with
useArrayProcessor: true
Sorted with
useArrayProcessor: false
test: [ "CMD", "pg_isready"]
+### BlockProcessor +The BlockProcessor will keep block sequences. + + + + + + + + + + + + +
Original DocumentSorted with
useBlockProcessor: true
Sorted with
useBlockProcessor: false
hello: |-
  World
hello: |-
  World
hello: World
+ ### Comment Processor An activated Comment Processor will keep comments while sorting. - - - + + + @@ -101,9 +117,9 @@ The Helm Processor makes the extension compatible with Helm charts.
Original DocumentSorted with
useCommentProcessor: true
Sorted with
useCommentProcessor: false
Original DocumentSorted with
useCommentProcessor: true
Sorted with
useCommentProcessor: false
# comment
foo: bar
- - - + + + @@ -117,9 +133,9 @@ The Spacing Processor will add spacing between keywords.
Original DocumentSorted with
useHelmProcessor: true
Sorted with
useHelmProcessor: false
Original DocumentSorted with
useHelmProcessor: true
Sorted with
useHelmProcessor: false
foo: {{ .value }}
- - - + + + @@ -133,9 +149,9 @@ The Octal Processor makes the extension keeping octal value like 0744.
Original DocumentSorted with
emptyLinesUntilLevel: 0
Sorted with
emptyLinesUntilLevel: 1
Original DocumentSorted with
emptyLinesUntilLevel: 0
Sorted with
emptyLinesUntilLevel: 1
foo: bar
baz: bar
- - - + + + diff --git a/package.json b/package.json index 0068da4..9cdd949 100644 --- a/package.json +++ b/package.json @@ -177,6 +177,11 @@ "default": true, "description": "When `true`, will activate ArrayProcessor" }, + "vscode-yaml-sort.useBlockProcessor": { + "type": "boolean", + "default": true, + "description": "When `true`, will activate BlockProcessor" + }, "vscode-yaml-sort.useCommentProcessor": { "type": "boolean", "default": true, diff --git a/src/controller/processor-controller.ts b/src/controller/processor-controller.ts index 87c285b..ebf0f9b 100644 --- a/src/controller/processor-controller.ts +++ b/src/controller/processor-controller.ts @@ -1,4 +1,5 @@ import { ArrayProcessor } from "../processor/array-processor" +import { BlockProcessor } from "../processor/block-processor" import { CommentProcessor } from "../processor/comment-processor" import { HelmProcessor } from "../processor/helm-processor" import { OctalProcessor } from "../processor/octal-processor" @@ -8,6 +9,7 @@ import { Settings } from "../settings" export class ProcessorController { arrayprocessor!: ArrayProcessor + blockprocessor!: BlockProcessor commentprocessor!: CommentProcessor helmprocessor!: HelmProcessor octalprocessor!: OctalProcessor @@ -26,6 +28,12 @@ export class ProcessorController { this.tabstospacespreprocessor.preprocess() this.text = this.tabstospacespreprocessor.text + if (this.settings.useBlockProcessor) { + this.blockprocessor = new BlockProcessor(this.text) + this.blockprocessor.preprocess() + this.text = this.blockprocessor.text + } + if (this.settings.useArrayProcessor) { this.arrayprocessor = new ArrayProcessor(this.text) this.arrayprocessor.preprocess() @@ -76,6 +84,12 @@ export class ProcessorController { this.text = this.arrayprocessor.text } + if (this.settings.useBlockProcessor) { + this.blockprocessor.text = this.text + this.blockprocessor.postprocess() + this.text = this.blockprocessor.text + } + this.spacingprocessor = new SpacingProcessor(this.text) this.spacingprocessor.postprocess() this.text = this.spacingprocessor.text diff --git a/src/processor/block-processor.ts b/src/processor/block-processor.ts new file mode 100644 index 0000000..3082f1e --- /dev/null +++ b/src/processor/block-processor.ts @@ -0,0 +1,7 @@ +import { GenericProcessor } from "./generic-processor" + +export class BlockProcessor extends GenericProcessor { + constructor(text: string) { + super("block", /(\||>)((?!.*:).|[\r\n])*/g, text) + } +} diff --git a/src/settings.ts b/src/settings.ts index 0b72179..469555a 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -53,6 +53,7 @@ export class Settings { useCustomSortRecursively = this.getBoolean("useCustomSortRecursively") useLeadingDashes = this.getBoolean("useLeadingDashes") useArrayProcessor = this.getBoolean("useArrayProcessor") + useBlockProcessor = this.getBoolean("useBlockProcessor") useCommentProcessor = this.getBoolean("useCommentProcessor") useHelmProcessor = this.getBoolean("useHelmProcessor") useOctalProcessor = this.getBoolean("useOctalProcessor") diff --git a/src/test/suite/processor/block-processor.test.ts b/src/test/suite/processor/block-processor.test.ts new file mode 100644 index 0000000..bfcc01a --- /dev/null +++ b/src/test/suite/processor/block-processor.test.ts @@ -0,0 +1,44 @@ +import { equal } from "assert" + +import { BlockProcessor } from "../../../processor/block-processor" + +suite("Test BlockProcessor - preprocess()", () => { + test("should find all block values", () => { + // + }) + test("input string with block sequence > should be the same after pre and postprocessing", () => { + const text = + "foo:\n" + + " bar: >\n" + + " This is a long sentence\n" + + "baz: bar" + + const blockprocessor = new BlockProcessor(text) + + blockprocessor.preprocess() + + equal(blockprocessor.store.size, 1) + + blockprocessor.postprocess() + + equal(blockprocessor.text, text) + }) + + test("input string with block sequence | should be the same after pre and postprocessing", () => { + const text = + "foo:\n" + + " bar: |\n" + + " This is a long sentence\n" + + "baz: bar" + + const blockprocessor = new BlockProcessor(text) + + blockprocessor.preprocess() + + equal(blockprocessor.store.size, 1) + + blockprocessor.postprocess() + + equal(blockprocessor.text, text) + }) +}) \ No newline at end of file diff --git a/src/test/suite/util/yaml-util.test.ts b/src/test/suite/util/yaml-util.test.ts index 03a6bce..695814a 100644 --- a/src/test/suite/util/yaml-util.test.ts +++ b/src/test/suite/util/yaml-util.test.ts @@ -199,34 +199,6 @@ suite("Test dumpYaml", () => { suite("Test sortYaml", () => { - test("should sort a given yaml document", () => { - const actual = - "persons:\n" + - " bob:\n" + - " place: Germany\n" + - " age: 23\n" + - " key: >\n" + - " This is a very long sentence\n" + - " that spans several lines in the YAML\n" + - "animals:\n" + - " kitty:\n" + - " age: 3\n" - - const expected = - "animals:\n" + - " kitty:\n" + - " age: 3\n" + - "persons:\n" + - " bob:\n" + - " age: 23\n" + - " place: Germany\n" + - " key: |\n" + - " This is a very long sentence that spans several lines in the YAML" - - const yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 1), expected) - }) - test("should put top level keyword `spec` before `data` when passing customsort=1", () => { let actual = "data: data\n" +
Original DocumentSorted with
useOctalProcessor: true
Sorted with
useOctalProcessor: false
Original DocumentSorted with
useOctalProcessor: true
Sorted with
useOctalProcessor: false
foo: 0744