Skip to content

Commit

Permalink
upgrade thrift-fmt-ts, support align by each part of field (#22)
Browse files Browse the repository at this point in the history
* upgrade thrift-fmt-ts, support align by each part of field

* remove package lock

* fix option

* add test case

* rename option

* rename options

* update docs

* fix test
  • Loading branch information
alingse committed Mar 25, 2023
1 parent 7a6b85a commit 4a5f7c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
26 changes: 19 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "thirft-formatter",
"displayName": "Thrift Formatter",
"description": "Thrift file formatter, keeping and align comment",
"version": "1.4.0",
"version": "1.5.0-a0",
"engines": {
"vscode": "^1.67.0"
},
Expand Down Expand Up @@ -42,23 +42,35 @@
"type": "object",
"title": "Thrift Formatter",
"properties": {
"thriftFormatter.patch": {
"thriftFormatter.patchRequired": {
"type": "boolean",
"scope": "resource",
"default": true,
"description": "auto patch the miss field(like required/comma)"
"description": "auto patch the miss 'required' of field\nsupport field in struct/union/exception "
},
"thriftFormatter.patchSeparator": {
"type": "boolean",
"scope": "resource",
"default": true,
"description": "auto patch the missed or useless separator\nseparator use comma ','"
},
"thriftFormatter.indent": {
"type": "number",
"scope": "resource",
"default": 4,
"description": "indent for field or method"
"description": "indent for field or function in struct enum union exception service"
},
"thriftFormatter.assignAlign": {
"thriftFormatter.alignByAssign": {
"type": "boolean",
"scope": "resource",
"default": true,
"description": "align by field's assgin in struct/enum/union/exception"
"description": "align by field's assgin '=' \nsupport field in the struct enum union exception"
},
"thriftFormatter.alignByField": {
"type": "boolean",
"scope": "resource",
"default": false,
"description": "align by each part of field. if true, this will ingore 'Align By Assign' option\nsupport field in struct enum union exception"
}
}
}
Expand Down Expand Up @@ -90,6 +102,6 @@
"webpack-cli": "^4.9.2"
},
"dependencies": {
"thrift-fmt-ts": "^1.2.0"
"thrift-fmt-ts": "^1.3.0-a0"
}
}
7 changes: 5 additions & 2 deletions src/web/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ export function deactivate() {}
export function editDocument(document: vscode.TextDocument): [string, vscode.TextEdit|undefined] {
const config = vscode.workspace.getConfiguration('thriftFormatter');
const option = newOption({
patch: config.get<boolean>('patch'),
patchRequired: config.get<boolean>('patchRequired'), // 兼容
patchSeparator: config.get<boolean>('patchSeparator'),
indent: config.get<number>('indent'),
assignAlign: config.get<boolean>('assignAlign'),
alignByAssign: config.get<boolean>('alignByAssign'), // 兼容
alignByField: config.get<boolean>('alignByField'),
keepComment: true,
})

const content = document.getText();
Expand Down
29 changes: 26 additions & 3 deletions src/web/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ suite('Web Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
test('Sample test', () => {
const rawContent = `include "shared.thrift"`;
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({patch:true, indent: 4}));
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({indent: 4}));
assert.ok(ok);
assert.equal(content, 'include "shared.thrift"');
});

test('Sample test', () => {
const rawContent = `const string default_user = "\\'default_user\\'" ;
const string default_name = '"abc\\'s"';`
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({patch:true, indent: 4}));
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({indent: 4}));
assert.ok(ok);
assert.equal(content.length, 89);
});
Expand All @@ -27,7 +27,7 @@ suite('Web Extension Test Suite', () => {
TWO,
SEVEN = 7, // seven
}`
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({patch:true, indent:4, assignAlign:true}));
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({indent:4, alignByAssign:true}));
//assert.ok(ok);
assert.equal(content,
`enum Numbers {
Expand All @@ -36,4 +36,27 @@ suite('Web Extension Test Suite', () => {
SEVEN = 7, // seven
}`);
});

test('test align by field', () => {
const rawContent = `
struct Person {
1: list<string> tags = ["A"],
2: optional list<string> opt_tags = ["1", "2"], // dogs
3: required list<string> req_tags = [],
4: string name = "hello"; // wtf
5: optional string opt_name,
16: required string req_name,
}`
const [content, ok] = fmtExt.formatThrift(rawContent, newOption({indent:4, alignByField:true}));
console.log(content);
assert.equal(content.trim(),
`struct Person {
1: required list<string> tags = [ "A" ] ,
2: optional list<string> opt_tags = [ "1", "2" ], // dogs
3: required list<string> req_tags = [ ] ,
4: required string name = "hello" , // wtf
5: optional string opt_name ,
16: required string req_name ,
}`.trim());
});
});

0 comments on commit 4a5f7c7

Please sign in to comment.