Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Add configuration for jsonnetfmt flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Enin committed Dec 17, 2019
1 parent af3a241 commit 2c7e90b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
50 changes: 49 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,55 @@
".libsonnet"
]
}
]
],
"configuration": {
"title": "jsonnet-formatter",
"properties": {
"jsonnet-formatter.indent": {
"type": "integer",
"description": "Number of spaces to indent by (0 will inherit from VS Code Formatting Options)",
"default": 2
},
"jsonnet-formatter.maxBlankLines": {
"type": "integer",
"description": "Max vertical spacing, 0 means no change",
"default": 2
},
"jsonnet-formatter.stringStyle": {
"type": "string",
"enum": ["double", "single", "leave"],
"description": "Enforce double, single quotes or 'leave'",
"default": "single"
},
"jsonnet-formatter.commentStyle": {
"type": "string",
"enum": ["hash", "slashes", "leave"],
"enumDescriptions": ["hash sign (#)", "slashes (//)", "leave without change"],
"description": "# (h), // (s), or 'leave'; never changes she-bang",
"default": "slashes"
},
"jsonnet-formatter.prettyFieldNames": {
"type": "boolean",
"description": "Use syntax sugar for fields and indexing",
"default": true
},
"jsonnet-formatter.padArrays": {
"type":"boolean",
"description": "[ 1, 2, 3 ] instead of [1, 2, 3]",
"default": false
},
"jsonnet-formatter.padObjects": {
"type":"boolean",
"description": "{ x: 1, y: 2 } instead of {x: 1, y: 2}",
"default":true
},
"jsonnet-formatter.sortImports": {
"type":"boolean",
"description": "Sorting of imports",
"default": true
}
}
}
},
"scripts": {
"vscode:prepublish": "rm -rf ./out; yarn run compile",
Expand Down
25 changes: 20 additions & 5 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import { Buffer } from 'buffer';
import { getEdits } from './diffUtils';

export class Formatter {
private format(data: string): Promise<string> {
private makeFormatterFlags(options: vscode.FormattingOptions): string[] {
let extConfig = vscode.workspace.getConfiguration('jsonnet-formatter');
return [
'--indent', extConfig.indent == 0 ? options.tabSize : extConfig.indent,
'--max-blank-lines', extConfig.maxBlankLines,
'--string-style', extConfig.stringStyle[0],
'--comment-style', extConfig.commentStyle[0],
extConfig.prettyFieldNames ? '--pretty-field-names' : '--no-pretty-field-names',
extConfig.padArrays ? '--pad-arrays' : '--no-sort-imports',
extConfig.padObjects ? '--pad-objects' : '--no-sort-imports',
extConfig.sortImports ? '--sort-imports' : '--no-sort-imports',
'-'
];
}

private format(data: string, options: vscode.FormattingOptions): Promise<string> {
return new Promise<string>((resolve, reject) => {
const bin = 'jsonnetfmt';
// This uses the Google internal config per https://github.com/google/jsonnet/issues/359.
const args = ['--indent', '2', '--max-blank-lines', '2', '--sort-imports', '--string-style', 's', '--comment-style', 's', '-'];
const args = this.makeFormatterFlags(options);
let p = cp.spawn(bin, args);
let stdout_: Buffer[] = [];
let stderr_: Buffer[] = [];
Expand All @@ -28,9 +43,9 @@ export class Formatter {
});
}

public async getFormatEdits(document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
public async getFormatEdits(document: vscode.TextDocument, options: vscode.FormattingOptions): Promise<vscode.TextEdit[]> {
let oldCode = document.getText();
let newCode = await this.format(oldCode);
let newCode = await this.format(oldCode, options);
let filePatch = getEdits(document.fileName, oldCode, newCode);
return filePatch.edits.map(edit => edit.apply());
}
Expand All @@ -42,6 +57,6 @@ export class JsonnetDocumentFormattingEditProvider implements vscode.DocumentFor
this.formatter = new Formatter();
}
public provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<vscode.TextEdit[]> {
return this.formatter.getFormatEdits(document);
return this.formatter.getFormatEdits(document, options);
}
}

0 comments on commit 2c7e90b

Please sign in to comment.