Skip to content

Commit

Permalink
Improve line breaking for nested groups (#56)
Browse files Browse the repository at this point in the history
* Improve line breaking for nested groups

* Further improve group breaking logic

* 0.2.1

* Update mo-fmt version

* Deactivate compiler diff test
  • Loading branch information
rvanasa authored Sep 30, 2022
1 parent 1daa0aa commit 49b83f6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-motoko",
"version": "0.2.0",
"version": "0.2.1",
"description": "A code formatter for the Motoko smart contract language.",
"main": "lib/environments/node.js",
"browser": "lib/environments/web.js",
Expand Down
18 changes: 9 additions & 9 deletions packages/mo-fmt/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/mo-fmt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mo-fmt",
"version": "0.2.0",
"version": "0.2.1",
"description": "An easy-to-use Motoko formatter command.",
"main": "src/cli.js",
"bin": {
Expand All @@ -21,7 +21,7 @@
"commander": "^9.4.0",
"fast-glob": "^3.2.11",
"prettier": "^2.7",
"prettier-plugin-motoko": "^0.2.0"
"prettier-plugin-motoko": "^0.2.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
60 changes: 44 additions & 16 deletions src/printers/motoko-tt-ast/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,39 @@ function printTokenTree(
const leftMap = new Map<TokenTree, TokenTree>();
const rightMap = new Map<TokenTree, TokenTree>();

let shouldBreak = false;
const shouldBreak = (
tt: TokenTree & { _shouldBreak?: boolean },
): boolean => {
if ('_shouldBreak' in tt) {
// use incremental cache to reduce time complexity
return tt._shouldBreak;
}
if (tt.token_tree_type === 'Group') {
const [trees] = tt.data;
tt._shouldBreak =
trees.some((tt) => shouldBreak(tt)) &&
!trees.every((tt) => {
// don't force break if all whitespace and/or nested groups
const token = getToken(tt);
return (
!token || // group
token.token_type === 'Line' ||
token.token_type === 'MultiLine'
);
});
} else {
const token = getToken(tt);
if (token) {
tt._shouldBreak =
token.token_type === 'Line' ||
token.token_type === 'MultiLine' ||
token.token_type === 'LineComment';
}
}
return tt._shouldBreak;
};
let shouldBreakTree = shouldBreak(tree);

const trees = originalTrees.filter((tt, i) => {
const left = originalTrees[i - 1];
if (left) {
Expand All @@ -166,10 +198,6 @@ function printTokenTree(
if (right) {
rightMap.set(tt, right);
}

if (getToken(tt)?.token_type === 'Line') {
shouldBreak = true;
}
return !shouldSkipTokenTree(tt);
});

Expand All @@ -184,10 +212,11 @@ function printTokenTree(
return true;
}
if (groupType === 'Square' || groupType === 'Paren') {
if (hasNestedGroup) {
return false;
}
return results.length <= 1 && !shouldBreak;
// if (hasNestedGroup) {
// return false;
// }
// return results.length <= 1 && !shouldBreakTree;
return !hasNestedGroup && !shouldBreakTree;
}
return false;
};
Expand Down Expand Up @@ -320,7 +349,7 @@ function printTokenTree(
]
: results,
{
shouldBreak,
shouldBreak: shouldBreakTree,
},
);
return shouldKeepSameLine() ? withoutLineBreaks(resultDoc) : resultDoc;
Expand All @@ -338,16 +367,15 @@ function printToken(token: Token): Doc {
case 'Space':
return space;
case 'Line':
// return breakParent;
return hardline;
case 'MultiLine':
// return [breakParent, hardline];
// return [hardline, hardline];
return [breakParent];
case 'LineComment':
// return [token.data, hardline];
return token.data;
// return token.data;
// return token.data;
return ifBreak(
token.data,
`/* ${token.data.substring(2).trim()} */`,
);
// return lineSuffix(token.data);
}
return getTokenText(token);
Expand Down
12 changes: 10 additions & 2 deletions tests/motoko.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ describe('Motoko formatter', () => {
// );
});

test('anonymous function line break', () => {
expect(format("(func() {\na\n})")).toStrictEqual("(\n func() {\n a;\n },\n);\n");
});

test('line comment in single line', () => {
expect(format("a<(b,\n//c\n)>()")).toStrictEqual("a<(b, /* c */)>()\n");
});

test('unclosed quotes in comments', () => {
expect(format("// a'b\n '")).toStrictEqual("// a'b\n';\n");
expect(format('// a"b\n "')).toStrictEqual('// a"b\n";\n');
Expand Down Expand Up @@ -268,11 +276,11 @@ describe('Motoko formatter', () => {
// )} <<<\n\n${formatted}\n\n`;
// }
// writeFileSync(
// join(__dirname, `motoko/_CompilerTests_Before.${extension}`),
// join(__dirname, `generated/_CompilerTests_Before.${extension}_`),
// preOutput,
// );
// writeFileSync(
// join(__dirname, `motoko/_CompilerTests_Formatted.${extension}`),
// join(__dirname, `generated/_CompilerTests_Formatted.${extension}_`),
// postOutput,
// );
// }
Expand Down
1 change: 0 additions & 1 deletion tests/motoko/.gitignore

This file was deleted.

0 comments on commit 49b83f6

Please sign in to comment.