Skip to content

Commit a81e55f

Browse files
authored
Splitting BlockComments into lines only once per run. (#1483)
* Splitting BlockComments into lines only once per run. * small rebase to organise readability * avoid trimStart twice * small refactor * iterate only once over lines and exit early if not successful
1 parent 58f10b8 commit a81e55f

5 files changed

Lines changed: 46 additions & 47 deletions

File tree

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
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

@@ -21,9 +17,6 @@ export class MultiLineComment extends CommentNode {
2117
}
2218

2319
print(): Doc {
24-
if (isIndentableBlockComment(this)) {
25-
return printIndentableBlockComment(this);
26-
}
27-
return join(literalline, this.value.split('\n'));
20+
return printBlockComment(this);
2821
}
2922
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
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

@@ -21,9 +17,6 @@ export class MultiLineNatSpecComment extends CommentNode {
2117
}
2218

2319
print(): Doc {
24-
if (isIndentableBlockComment(this)) {
25-
return printIndentableBlockComment(this);
26-
}
27-
return join(literalline, this.value.split('\n'));
20+
return printBlockComment(this);
2821
}
2922
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 trimmedIndentableLines(lines: string[]): string[] | undefined {
9+
// If the comment has multiple lines and every line starts with a star
10+
// we can fix the indentation of each line.
11+
if (lines.length > 1) {
12+
const trimmedLines = [];
13+
for (let line of lines) {
14+
line = line.trimStart();
15+
if (!line.startsWith('*')) {
16+
return;
17+
}
18+
trimmedLines.push(line);
19+
}
20+
return trimmedLines;
21+
}
22+
}
23+
24+
function printIndentableBlockComment(lines: string[]): Doc {
25+
return join(
26+
hardline,
27+
lines.map((line, index) => `${index === 0 ? '' : ' '}${line.trimEnd()}`)
28+
);
29+
}
30+
31+
export function printBlockComment(comment: BlockComment): Doc {
32+
// We remove the initial `/` to check if every line starts with `*`
33+
const lines = comment.value.slice(1).split('\n');
34+
const trimmedLines = trimmedIndentableLines(lines);
35+
36+
return [
37+
'/',
38+
trimmedLines
39+
? printIndentableBlockComment(trimmedLines)
40+
: join(literalline, lines)
41+
];
42+
}

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

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

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

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

0 commit comments

Comments
 (0)