Skip to content

Commit c984cf4

Browse files
committed
small rebase to organise readability
1 parent baab326 commit c984cf4

5 files changed

Lines changed: 39 additions & 50 deletions

File tree

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import { TerminalKind } from '@nomicfoundation/slang/cst';
2-
import { doc } from 'prettier';
3-
import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js';
4-
import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js';
2+
import { printBlockComment } from '../slang-printers/print-block-comment.js';
53
import { CommentNode } from './CommentNode.js';
64

75
import type { TerminalNode } from '@nomicfoundation/slang/cst';
86
import type { Doc } from 'prettier';
97

10-
const { join, literalline } = doc.builders;
11-
128
export class MultiLineComment extends CommentNode {
139
readonly kind = TerminalKind.MultiLineComment;
1410

1511
value: string;
1612

17-
lines: string[];
18-
1913
constructor(ast: TerminalNode, offset: number) {
2014
super(ast, offset);
2115

2216
this.value = ast.unparse();
23-
this.lines = this.value.slice(1).split('\n');
2417
}
2518

2619
print(): Doc {
27-
if (isIndentableBlockComment(this)) {
28-
return printIndentableBlockComment(this);
29-
}
30-
return ['/', join(literalline, this.lines)];
20+
return printBlockComment(this);
3121
}
3222
}
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import { TerminalKind } from '@nomicfoundation/slang/cst';
2-
import { doc } from 'prettier';
3-
import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js';
4-
import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js';
2+
import { printBlockComment } from '../slang-printers/print-block-comment.js';
53
import { CommentNode } from './CommentNode.js';
64

75
import type { TerminalNode } from '@nomicfoundation/slang/cst';
86
import type { Doc } from 'prettier';
97

10-
const { join, literalline } = doc.builders;
11-
128
export class MultiLineNatSpecComment extends CommentNode {
139
readonly kind = TerminalKind.MultiLineNatSpecComment;
1410

1511
value: string;
1612

17-
lines: string[];
18-
1913
constructor(ast: TerminalNode, offset: number) {
2014
super(ast, offset);
2115

2216
this.value = ast.unparse();
23-
this.lines = this.value.slice(1).split('\n');
2417
}
2518

2619
print(): Doc {
27-
if (isIndentableBlockComment(this)) {
28-
return printIndentableBlockComment(this);
29-
}
30-
return ['/', join(literalline, this.lines)];
20+
return printBlockComment(this);
3121
}
3222
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { doc } from 'prettier';
2+
3+
import type { Doc } from 'prettier';
4+
import type { BlockComment } from '../slang-nodes/types.d.ts';
5+
6+
const { hardline, join, literalline } = doc.builders;
7+
8+
function isIndentableBlockComment(lines: string[]): boolean {
9+
// If the comment has multiple lines and every line starts with a star
10+
// we can fix the indentation of each line.
11+
return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*');
12+
}
13+
14+
function printIndentableBlockComment(lines: string[]): Doc {
15+
return join(
16+
hardline,
17+
lines.map((line, index) =>
18+
index === 0
19+
? line.trimEnd()
20+
: ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}`
21+
)
22+
);
23+
}
24+
25+
export function printBlockComment(comment: BlockComment): Doc {
26+
// We remove the initial `/` to check if every line starts with `*`
27+
const lines = comment.value.slice(1).split('\n');
28+
29+
return [
30+
'/',
31+
isIndentableBlockComment(lines)
32+
? printIndentableBlockComment(lines)
33+
: join(literalline, lines)
34+
];
35+
}

src/slang-printers/print-indentable-block-comment.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/slang-utils/is-indentable-block-comment.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)