Skip to content

Commit

Permalink
fix(yaml): ending comment lossing issue
Browse files Browse the repository at this point in the history
many thanks to eemeli/yaml#576
  • Loading branch information
longkai committed Oct 24, 2024
1 parent a734664 commit d387722
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 79 deletions.
122 changes: 58 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "kubernetes-yaml-formatter",
"displayName": "Kubernetes YAML Formatter",
"displayName": "Better YAML",
"icon": "images/icon.png",
"description": "A better YAML formatter for DevOps like Kubernetes, Ansible, CI/CD, etc.",
"description": "A better YAML formatter",
"version": "2.0.0",
"publisher": "kennylong",
"repository": {
Expand Down Expand Up @@ -47,10 +47,15 @@
"configuration": {
"title": "Kubernetes Style YAML Formatter",
"properties": {
"kubernetes-yaml-formatter.compactSequenceIndent": {
"kubernetes-yaml-formatter.indentSeq": {
"type": "boolean",
"default": true,
"description": "Use compact sequence indent, i.e. no indent"
"default": false,
"description": "Whether block sequences should be indented."
},
"kubernetes-yaml-formatter.directives": {
"type": ["boolean", "null"],
"default": null,
"description": "Include directives in the output. If true, at least the document-start marker --- is always included. If false, no directives or marker is ever included. If null, directives and marker may be included if required."
}
}
},
Expand All @@ -72,14 +77,15 @@
"devDependencies": {
"@types/mocha": "^10.0.7",
"@types/node": "20.x",
"@types/vscode": "^1.61.0",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.11.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.4.0"
"@vscode/test-electron": "^2.4.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
"dependencies": {
"yaml": "^2.5.1"
"yaml": "^2.6.0"
}
}
19 changes: 13 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function activate(context: vscode.ExtensionContext) {
document.positionAt(txt.length),
);

let fmtTxt = format(txt, makeFormattingOptions(vscode.workspace.getConfiguration(), options));
let fmtTxt = format(txt, makeFormattingOptions(vscode.workspace.getConfiguration(), options, false));

return [vscode.TextEdit.replace(fullRange, fmtTxt)];
}
Expand All @@ -35,7 +35,7 @@ export function activate(context: vscode.ExtensionContext) {
provideDocumentRangeFormattingEdits: function (document: vscode.TextDocument, range: vscode.Range, options: vscode.FormattingOptions, token: vscode.CancellationToken): vscode.ProviderResult<vscode.TextEdit[]> {
const txt = document.getText(range);

let fmtTxt = format(txt, makeFormattingOptions(vscode.workspace.getConfiguration(), options));
let fmtTxt = format(txt, makeFormattingOptions(vscode.workspace.getConfiguration(), options, true));

if (txt.slice(-1) !== '\n') {
fmtTxt = fmtTxt.slice(0, -1); // remove last `\n` or it may break the range
Expand All @@ -46,17 +46,24 @@ export function activate(context: vscode.ExtensionContext) {
})
}

function makeFormattingOptions(conf:vscode.WorkspaceConfiguration, options: vscode.FormattingOptions): YAML.ToStringOptions {
return {
function makeFormattingOptions(conf: vscode.WorkspaceConfiguration, options: vscode.FormattingOptions, rangeFormatting: Boolean): YAML.ToStringOptions {
let op: YAML.ToStringOptions = {
indent: options.tabSize,
indentSeq: !conf.get('kubernetes-yaml-formatter.compactSequenceIndent', true),
indentSeq: conf.get('kubernetes-yaml-formatter.indentSeq'),
directives: conf.get('kubernetes-yaml-formatter.directives'),
}

if (rangeFormatting) {
op.directives = null;
}

return op;
}

function format(text: string, options: YAML.ToStringOptions): string {
return YAML.parseAllDocuments(text)
.map(doc => YAML.stringify(doc, options))
.join("\n---\n");
.join("");
}

// this method is called when your extension is deactivated
Expand Down

0 comments on commit d387722

Please sign in to comment.