Skip to content

Commit baab326

Browse files
committed
Splitting BlockComments into lines only once per run.
1 parent f644748 commit baab326

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/slang-nodes/MultiLineComment.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ export class MultiLineComment extends CommentNode {
1414

1515
value: string;
1616

17+
lines: string[];
18+
1719
constructor(ast: TerminalNode, offset: number) {
1820
super(ast, offset);
1921

2022
this.value = ast.unparse();
23+
this.lines = this.value.slice(1).split('\n');
2124
}
2225

2326
print(): Doc {
2427
if (isIndentableBlockComment(this)) {
2528
return printIndentableBlockComment(this);
2629
}
27-
return join(literalline, this.value.split('\n'));
30+
return ['/', join(literalline, this.lines)];
2831
}
2932
}

src/slang-nodes/MultiLineNatSpecComment.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ export class MultiLineNatSpecComment extends CommentNode {
1414

1515
value: string;
1616

17+
lines: string[];
18+
1719
constructor(ast: TerminalNode, offset: number) {
1820
super(ast, offset);
1921

2022
this.value = ast.unparse();
23+
this.lines = this.value.slice(1).split('\n');
2124
}
2225

2326
print(): Doc {
2427
if (isIndentableBlockComment(this)) {
2528
return printIndentableBlockComment(this);
2629
}
27-
return join(literalline, this.value.split('\n'));
30+
return ['/', join(literalline, this.lines)];
2831
}
2932
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import type { BlockComment } from '../slang-nodes/types.d.ts';
55

66
const { hardline, join } = doc.builders;
77

8-
export function printIndentableBlockComment(comment: BlockComment): Doc {
9-
const lines = comment.value.split('\n');
10-
8+
export function printIndentableBlockComment({ lines }: BlockComment): Doc {
119
return join(
1210
hardline,
1311
lines.map((line, index) =>
1412
index === 0
15-
? line.trimEnd()
13+
? `/${line.trimEnd()}`
1614
: ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}`
1715
)
1816
);
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { BlockComment } from '../slang-nodes/types.d.ts';
22

3-
export function isIndentableBlockComment(comment: BlockComment): boolean {
3+
export function isIndentableBlockComment({ lines }: BlockComment): boolean {
44
// If the comment has multiple lines and every line starts with a star
55
// we can fix the indentation of each line. The stars in the `/*` and
66
// `*/` delimiters are not included in the comment value, so add them
77
// back first.
8-
const lines = comment.value.slice(1, -1).split('\n');
98
return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*');
109
}

0 commit comments

Comments
 (0)