Skip to content

Commit

Permalink
module labels support
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Jul 28, 2024
1 parent 9ef8fd3 commit 29fa35d
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,43 @@ export enum Module {
*/
export function printModuleDefinition(
path: AstPath<Node>,
options: ParserOptions,
options: ParserOptions & MoveOptions,
print: printFn,
): Doc {
return join(' ', ['module', ...path.map(print, 'nonFormattingChildren')]);
let useLabel = false;

// when option is present we must check that there's only one module per file
if (options.useModuleLabel) {
let modules = path.parent!.nonFormattingChildren.filter(
(node) => node.type === path.node.type,
);

useLabel = modules.length == 1;
}

if (options.useModuleLabel) console.log(useLabel);

let result = [
'module ',
path.call(print, 'nonFormattingChildren', 0),
// path.call(print, 'nonFormattingChildren', 1),
];

if (useLabel) {
result.push(...[';', hardline, path.call(print, 'nonFormattingChildren', 1)]);
} else {
result.push(
...[
' {',
indent(hardline),
indent(path.call(print, 'nonFormattingChildren', 1)),
hardline,
'}',
],
);
}

return result;
}

/**
Expand Down Expand Up @@ -112,5 +145,5 @@ function printModuleBody(
return path.call(print);
}, 'namedAndEmptyLineChildren');

return ['{', indent(hardline), indent(join(hardline, printed)), hardline, '}'];
return join(hardline, printed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ export const options: Record<string, SupportOption> = {
default: true,
description: 'Always break function body into multiple lines.',
},
alwaysBreakConditionals: {
useModuleLabel: {
type: 'boolean',
category: 'Global',
default: true,
description: 'Always break conditional body into multiple lines.',
default: false,
description:
'Enable module labels instead of module with braces. This option will be ignored if there is more than one module in the file.',
},
};

export const defaultOptions = {
tabWidth: 4,
useTabs: false,
printWidth: 100,
useModuleLabel: false,
};

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type MoveOptions = {
alwaysBreakFunctions: boolean;
alwaysBreakConditionals: boolean;
alwaysBreakStructDefinition: boolean;
useModuleLabel: boolean;
};

export type printFn = (path: AstPath) => Doc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module prettier::pattern_matching {
fun f(x: MyEnum): u8 {
match (x) {
MyEnum::Variant(ERROR) => 1,
MyEnum::Variant(1, true) => 1,
MyEnum::Variant(_, _) => 1,
MyEnum::OtherVariant(_, ERROR) => 2,
MyEnum::OtherVariant(_, 3) => 2,
// Now exhaustive since this will match all values of MyEnum::OtherVariant
MyEnum::OtherVariant(..) => 2,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// options:
// printWidth: 80
// useModuleLabel: true

module prettier::module_label;
use std::string::String;

/// Hey there, this is the first time we meet.
public struct Hey {}

public fun another_public_fun() {}

fun ignored_vec() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// options:
// printWidth: 80
// useModuleLabel: true

module prettier::module_label {
use std::string::String;
/// Hey there, this is the first time we meet.
public struct Hey {}
public fun another_public_fun() {}
fun ignored_vec() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// options:
// printWidth: 80
// useModuleLabel: true

module prettier::module_label {
/// Hey there, this is the first time we meet.
public struct Hey {}

public fun another_public_fun() {}

fun ignored_vec() {}
}

module prettier::module_label_v2 {
/// Hey there, this is the first time we meet.
public struct Hey {}

public fun another_public_fun() {}

fun ignored_vec() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// options:
// printWidth: 80
// useModuleLabel: true

module prettier::module_label {
/// Hey there, this is the first time we meet.
public struct Hey {}
public fun another_public_fun() {}
fun ignored_vec() {}
}

module prettier::module_label_v2 {
/// Hey there, this is the first time we meet.
public struct Hey {}
public fun another_public_fun() {}
fun ignored_vec() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as path from 'path';
import * as plugin from '../';
import * as prettier from 'prettier';
import { describe, it } from 'vitest';
import { MoveOptions } from "../src/printer";

const UB = process.env['UB'];
const OPTIONS_HEADER = '// options:';
Expand Down Expand Up @@ -36,23 +37,32 @@ function runSpec(dirname: string) {
// allows `// options:` header in the test file to set prettier options
// e.g.
// ```
// // options: printWidth: 80
// // options:
// // printWidth: 80
// // tabWidth: 2
// // useModuleLabel: true
// ```
let config = {
printWidth: 80,
tabWidth: 4,
useModuleLabel: false,
};

if (content.startsWith(OPTIONS_HEADER)) {
let lines = content.split('\n').slice(0, 10);
while (lines.length) {
let line = lines.shift();
if (line?.startsWith('// ')) {
let value = /(printWidth|tabWidth)\:\ ([0-9]+)/.exec(line);
let value = /(printWidth|tabWidth|useModuleLabel)\:\ (true|[0-9]+)/.exec(
line,
);
if (value) {
let [_, key, val] = value || [];
config[key] = parseInt(val);
if (key == 'useModuleLabel') {
config[key] = val == 'true';
} else {
config[key] = parseInt(val);
}
}
}
}
Expand All @@ -66,11 +76,14 @@ function runSpec(dirname: string) {
// @ts-ignore
printers: plugin.printers,
defaultOptions: plugin.defaultOptions,
// @ts-ignore
options: plugin.options,
},
],
parser: 'move-parse',
printWidth: config.printWidth,
tabWidth: config.tabWidth,
useModuleLabel: config.useModuleLabel,
});

// user asked to regenerate output
Expand Down
Binary file not shown.

0 comments on commit 29fa35d

Please sign in to comment.