Skip to content

Commit 29fa35d

Browse files
committed
module labels support
1 parent 9ef8fd3 commit 29fa35d

File tree

10 files changed

+122
-11
lines changed

10 files changed

+122
-11
lines changed

external-crates/move/crates/move-analyzer/prettier-move/src/cst/module/Module.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,43 @@ export enum Module {
4545
*/
4646
export function printModuleDefinition(
4747
path: AstPath<Node>,
48-
options: ParserOptions,
48+
options: ParserOptions & MoveOptions,
4949
print: printFn,
5050
): Doc {
51-
return join(' ', ['module', ...path.map(print, 'nonFormattingChildren')]);
51+
let useLabel = false;
52+
53+
// when option is present we must check that there's only one module per file
54+
if (options.useModuleLabel) {
55+
let modules = path.parent!.nonFormattingChildren.filter(
56+
(node) => node.type === path.node.type,
57+
);
58+
59+
useLabel = modules.length == 1;
60+
}
61+
62+
if (options.useModuleLabel) console.log(useLabel);
63+
64+
let result = [
65+
'module ',
66+
path.call(print, 'nonFormattingChildren', 0),
67+
// path.call(print, 'nonFormattingChildren', 1),
68+
];
69+
70+
if (useLabel) {
71+
result.push(...[';', hardline, path.call(print, 'nonFormattingChildren', 1)]);
72+
} else {
73+
result.push(
74+
...[
75+
' {',
76+
indent(hardline),
77+
indent(path.call(print, 'nonFormattingChildren', 1)),
78+
hardline,
79+
'}',
80+
],
81+
);
82+
}
83+
84+
return result;
5285
}
5386

5487
/**
@@ -112,5 +145,5 @@ function printModuleBody(
112145
return path.call(print);
113146
}, 'namedAndEmptyLineChildren');
114147

115-
return ['{', indent(hardline), indent(join(hardline, printed)), hardline, '}'];
148+
return join(hardline, printed);
116149
}

external-crates/move/crates/move-analyzer/prettier-move/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,20 @@ export const options: Record<string, SupportOption> = {
7878
default: true,
7979
description: 'Always break function body into multiple lines.',
8080
},
81-
alwaysBreakConditionals: {
81+
useModuleLabel: {
8282
type: 'boolean',
8383
category: 'Global',
84-
default: true,
85-
description: 'Always break conditional body into multiple lines.',
84+
default: false,
85+
description:
86+
'Enable module labels instead of module with braces. This option will be ignored if there is more than one module in the file.',
8687
},
8788
};
8889

8990
export const defaultOptions = {
9091
tabWidth: 4,
9192
useTabs: false,
9293
printWidth: 100,
94+
useModuleLabel: false,
9395
};
9496

9597
export default {

external-crates/move/crates/move-analyzer/prettier-move/src/printer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type MoveOptions = {
3131
alwaysBreakFunctions: boolean;
3232
alwaysBreakConditionals: boolean;
3333
alwaysBreakStructDefinition: boolean;
34+
useModuleLabel: boolean;
3435
};
3536

3637
export type printFn = (path: AstPath) => Doc;

external-crates/move/crates/move-analyzer/prettier-move/tests/functions/pattern_matching.exp.move

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module prettier::pattern_matching {
22
fun f(x: MyEnum): u8 {
33
match (x) {
4-
MyEnum::Variant(ERROR) => 1,
4+
MyEnum::Variant(1, true) => 1,
55
MyEnum::Variant(_, _) => 1,
6-
MyEnum::OtherVariant(_, ERROR) => 2,
6+
MyEnum::OtherVariant(_, 3) => 2,
77
// Now exhaustive since this will match all values of MyEnum::OtherVariant
88
MyEnum::OtherVariant(..) => 2,
99
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// options:
2+
// printWidth: 80
3+
// useModuleLabel: true
4+
5+
module prettier::module_label;
6+
use std::string::String;
7+
8+
/// Hey there, this is the first time we meet.
9+
public struct Hey {}
10+
11+
public fun another_public_fun() {}
12+
13+
fun ignored_vec() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// options:
2+
// printWidth: 80
3+
// useModuleLabel: true
4+
5+
module prettier::module_label {
6+
use std::string::String;
7+
/// Hey there, this is the first time we meet.
8+
public struct Hey {}
9+
public fun another_public_fun() {}
10+
fun ignored_vec() {}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// options:
2+
// printWidth: 80
3+
// useModuleLabel: true
4+
5+
module prettier::module_label {
6+
/// Hey there, this is the first time we meet.
7+
public struct Hey {}
8+
9+
public fun another_public_fun() {}
10+
11+
fun ignored_vec() {}
12+
}
13+
14+
module prettier::module_label_v2 {
15+
/// Hey there, this is the first time we meet.
16+
public struct Hey {}
17+
18+
public fun another_public_fun() {}
19+
20+
fun ignored_vec() {}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// options:
2+
// printWidth: 80
3+
// useModuleLabel: true
4+
5+
module prettier::module_label {
6+
/// Hey there, this is the first time we meet.
7+
public struct Hey {}
8+
public fun another_public_fun() {}
9+
fun ignored_vec() {}
10+
}
11+
12+
module prettier::module_label_v2 {
13+
/// Hey there, this is the first time we meet.
14+
public struct Hey {}
15+
public fun another_public_fun() {}
16+
fun ignored_vec() {}
17+
}

external-crates/move/crates/move-analyzer/prettier-move/tests/run.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as path from 'path';
88
import * as plugin from '../';
99
import * as prettier from 'prettier';
1010
import { describe, it } from 'vitest';
11+
import { MoveOptions } from "../src/printer";
1112

1213
const UB = process.env['UB'];
1314
const OPTIONS_HEADER = '// options:';
@@ -36,23 +37,32 @@ function runSpec(dirname: string) {
3637
// allows `// options:` header in the test file to set prettier options
3738
// e.g.
3839
// ```
39-
// // options: printWidth: 80
40+
// // options:
41+
// // printWidth: 80
4042
// // tabWidth: 2
43+
// // useModuleLabel: true
4144
// ```
4245
let config = {
4346
printWidth: 80,
4447
tabWidth: 4,
48+
useModuleLabel: false,
4549
};
4650

4751
if (content.startsWith(OPTIONS_HEADER)) {
4852
let lines = content.split('\n').slice(0, 10);
4953
while (lines.length) {
5054
let line = lines.shift();
5155
if (line?.startsWith('// ')) {
52-
let value = /(printWidth|tabWidth)\:\ ([0-9]+)/.exec(line);
56+
let value = /(printWidth|tabWidth|useModuleLabel)\:\ (true|[0-9]+)/.exec(
57+
line,
58+
);
5359
if (value) {
5460
let [_, key, val] = value || [];
55-
config[key] = parseInt(val);
61+
if (key == 'useModuleLabel') {
62+
config[key] = val == 'true';
63+
} else {
64+
config[key] = parseInt(val);
65+
}
5666
}
5767
}
5868
}
@@ -66,11 +76,14 @@ function runSpec(dirname: string) {
6676
// @ts-ignore
6777
printers: plugin.printers,
6878
defaultOptions: plugin.defaultOptions,
79+
// @ts-ignore
80+
options: plugin.options,
6981
},
7082
],
7183
parser: 'move-parse',
7284
printWidth: config.printWidth,
7385
tabWidth: config.tabWidth,
86+
useModuleLabel: config.useModuleLabel,
7487
});
7588

7689
// user asked to regenerate output
Binary file not shown.

0 commit comments

Comments
 (0)