-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ArrayProcessor to keep single line arrays (#36)
- Loading branch information
Showing
25 changed files
with
406 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { ArrayProcessor } from "../processor/array-processor" | ||
import { CommentProcessor } from "../processor/comment-processor" | ||
import { HelmProcessor } from "../processor/helm-processor" | ||
import { Settings } from "../settings" | ||
|
||
export class ProcessorController { | ||
arrayprocessor!: ArrayProcessor | ||
commentprocessor!: CommentProcessor | ||
helmprocessor!: HelmProcessor | ||
text: string | ||
settings: Settings | ||
|
||
constructor(text: string, settings = new Settings()) { | ||
this.text = text | ||
this.settings = settings | ||
} | ||
|
||
preprocess() { | ||
if (this.settings.useArrayProcessor) { | ||
this.arrayprocessor = new ArrayProcessor(this.text) | ||
this.arrayprocessor.preprocess() | ||
this.text = this.arrayprocessor.text | ||
} | ||
|
||
if (this.settings.useHelmProcessor) { | ||
this.helmprocessor = new HelmProcessor(this.text) | ||
this.helmprocessor.preprocess() | ||
this.text = this.helmprocessor.text | ||
} | ||
|
||
if (this.settings.useCommentProcessor) { | ||
this.commentprocessor = new CommentProcessor(this.text) | ||
this.commentprocessor.findComments() | ||
this.text = this.commentprocessor.text | ||
} | ||
} | ||
|
||
postprocess() { | ||
if (this.settings.useCommentProcessor) { | ||
this.commentprocessor.text = this.text | ||
this.commentprocessor.postprocess() | ||
this.text = this.commentprocessor.text | ||
} | ||
|
||
if (this.settings.useHelmProcessor) { | ||
this.helmprocessor.text = this.text | ||
this.helmprocessor.postprocess() | ||
this.text = this.helmprocessor.text | ||
} | ||
|
||
if (this.settings.useArrayProcessor) { | ||
this.arrayprocessor.text = this.text | ||
this.arrayprocessor.postprocess() | ||
this.text = this.arrayprocessor.text | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GenericProcessor } from "./generic-processor"; | ||
|
||
export class ArrayProcessor extends GenericProcessor { | ||
constructor(text: string) { | ||
super("array", /\[([^[]*)\]/g, text) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export abstract class GenericProcessor { | ||
filter: string | ||
matcher: RegExp | ||
store: Map<string, string> = new Map() | ||
text: string | ||
|
||
constructor(filter: string, matcher: RegExp, text: string) { | ||
this.filter = filter | ||
this.matcher = matcher | ||
this.text = text | ||
} | ||
|
||
preprocess() { | ||
const matches = this.findMatches() | ||
if (matches) { | ||
for (const match of matches) { | ||
this.replaceValueWithSubstitue(match) | ||
} | ||
} | ||
} | ||
|
||
findMatches() { | ||
return this.text.match(this.matcher) | ||
} | ||
|
||
replaceValueWithSubstitue(value: string) { | ||
const substitue = `vscode-yaml-sort.${this.filter}.${this.store.size}` | ||
this.store.set(substitue, value) | ||
this.text = this.text.replace(value, substitue) | ||
} | ||
|
||
postprocess() { | ||
this.store.forEach((value: string, key: string) => { | ||
this.replaceSubstituteWithValue(key, value) | ||
}) | ||
} | ||
|
||
replaceSubstituteWithValue(substitue: string, value: string) { | ||
const match = new RegExp(`('|")?${substitue}('|")?`) | ||
this.text = this.text.replace(match, value) | ||
} | ||
} |
Oops, something went wrong.