Skip to content

Commit

Permalink
Merge pull request #124 from pascalre/62-sort-removed-blockfolded-str…
Browse files Browse the repository at this point in the history
…ing-style

Keep block sequences with BlockProcessor (#62)
  • Loading branch information
pascalre committed Feb 28, 2023
2 parents c2e38ec + 049e551 commit f6169e1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 43 deletions.
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ The ArrayProcessor will not add linebreaks to single-line array structures.

<table width="100%">
<tr>
<th>Original Document</th>
<th>Sorted with<br><code>useArrayProcessor: true</code></th>
<th>Sorted with<br><code>useArrayProcessor: false</code></th>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>useArrayProcessor: true</code></th>
<th width="33.3%">Sorted with<br><code>useArrayProcessor: false</code></th>
</tr>
<tr>
<td><code>test: [ "CMD", "pg_isready"]</code></td>
Expand All @@ -80,14 +80,30 @@ The ArrayProcessor will not add linebreaks to single-line array structures.
</tr>
</table>

### BlockProcessor
The BlockProcessor will keep block sequences.

<table width="100%">
<tr>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>useBlockProcessor: true</code></th>
<th width="33.3%">Sorted with<br><code>useBlockProcessor: false</code></th>
</tr>
<tr>
<td><code>hello: |-<br>&nbsp;&nbsp;World</code></td>
<td><code>hello: |-<br>&nbsp;&nbsp;World</code></td>
<td><code>hello: World</code></td>
</tr>
</table>

### Comment Processor
An activated Comment Processor will keep comments while sorting.

<table width="100%">
<tr>
<th>Original Document</th>
<th>Sorted with<br><code>useCommentProcessor: true</code></th>
<th>Sorted with<br><code>useCommentProcessor: false</code></th>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>useCommentProcessor: true</code></th>
<th width="33.3%">Sorted with<br><code>useCommentProcessor: false</code></th>
</tr>
<tr>
<td><code># comment<br>foo: bar</code></td>
Expand All @@ -101,9 +117,9 @@ The Helm Processor makes the extension compatible with Helm charts.

<table width="100%">
<tr>
<th>Original Document</th>
<th>Sorted with<br><code>useHelmProcessor: true</code></th>
<th>Sorted with<br><code>useHelmProcessor: false</code></th>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>useHelmProcessor: true</code></th>
<th width="33.3%">Sorted with<br><code>useHelmProcessor: false</code></th>
</tr>
<tr>
<td><code>foo: {{ .value }}</code></td>
Expand All @@ -117,9 +133,9 @@ The Spacing Processor will add spacing between keywords.

<table width="100%">
<tr>
<th>Original Document</th>
<th>Sorted with<br><code>emptyLinesUntilLevel: 0</code></th>
<th>Sorted with<br><code>emptyLinesUntilLevel: 1</code></th>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>emptyLinesUntilLevel: 0</code></th>
<th width="33.3%">Sorted with<br><code>emptyLinesUntilLevel: 1</code></th>
</tr>
<tr>
<td><code>foo: bar<br>baz: bar</code></td>
Expand All @@ -133,9 +149,9 @@ The Octal Processor makes the extension keeping octal value like 0744.

<table width="100%">
<tr>
<th>Original Document</th>
<th>Sorted with<br><code>useOctalProcessor: true</code></th>
<th>Sorted with<br><code>useOctalProcessor: false</code></th>
<th width="33.3%">Original Document</th>
<th width="33.3%">Sorted with<br><code>useOctalProcessor: true</code></th>
<th width="33.3%">Sorted with<br><code>useOctalProcessor: false</code></th>
</tr>
<tr>
<td><code>foo: 0744</code></td>
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/controller/processor-controller.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -8,6 +9,7 @@ import { Settings } from "../settings"

export class ProcessorController {
arrayprocessor!: ArrayProcessor
blockprocessor!: BlockProcessor
commentprocessor!: CommentProcessor
helmprocessor!: HelmProcessor
octalprocessor!: OctalProcessor
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/processor/block-processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { GenericProcessor } from "./generic-processor"

export class BlockProcessor extends GenericProcessor {
constructor(text: string) {
super("block", /(\||>)((?!.*:).|[\r\n])*/g, text)
}
}
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 44 additions & 0 deletions src/test/suite/processor/block-processor.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
28 changes: 0 additions & 28 deletions src/test/suite/util/yaml-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down

0 comments on commit f6169e1

Please sign in to comment.