From 61ce3d653b7b9e4c50bb3bda908e462c1ac164dd Mon Sep 17 00:00:00 2001 From: Klaus Date: Thu, 26 Mar 2026 12:03:25 -0300 Subject: [PATCH 1/5] Splitting BlockComments into lines only once per run. --- src/slang-nodes/MultiLineComment.ts | 5 ++++- src/slang-nodes/MultiLineNatSpecComment.ts | 5 ++++- src/slang-printers/print-indentable-block-comment.ts | 6 ++---- src/slang-utils/is-indentable-block-comment.ts | 3 +-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/slang-nodes/MultiLineComment.ts b/src/slang-nodes/MultiLineComment.ts index e08df19b6..a1aba90a5 100644 --- a/src/slang-nodes/MultiLineComment.ts +++ b/src/slang-nodes/MultiLineComment.ts @@ -14,16 +14,19 @@ export class MultiLineComment extends CommentNode { value: string; + lines: string[]; + constructor(ast: TerminalNode, offset: number) { super(ast, offset); this.value = ast.unparse(); + this.lines = this.value.slice(1).split('\n'); } print(): Doc { if (isIndentableBlockComment(this)) { return printIndentableBlockComment(this); } - return join(literalline, this.value.split('\n')); + return ['/', join(literalline, this.lines)]; } } diff --git a/src/slang-nodes/MultiLineNatSpecComment.ts b/src/slang-nodes/MultiLineNatSpecComment.ts index 368522c68..95c053aa5 100644 --- a/src/slang-nodes/MultiLineNatSpecComment.ts +++ b/src/slang-nodes/MultiLineNatSpecComment.ts @@ -14,16 +14,19 @@ export class MultiLineNatSpecComment extends CommentNode { value: string; + lines: string[]; + constructor(ast: TerminalNode, offset: number) { super(ast, offset); this.value = ast.unparse(); + this.lines = this.value.slice(1).split('\n'); } print(): Doc { if (isIndentableBlockComment(this)) { return printIndentableBlockComment(this); } - return join(literalline, this.value.split('\n')); + return ['/', join(literalline, this.lines)]; } } diff --git a/src/slang-printers/print-indentable-block-comment.ts b/src/slang-printers/print-indentable-block-comment.ts index cc5598eec..e59f96582 100644 --- a/src/slang-printers/print-indentable-block-comment.ts +++ b/src/slang-printers/print-indentable-block-comment.ts @@ -5,14 +5,12 @@ import type { BlockComment } from '../slang-nodes/types.d.ts'; const { hardline, join } = doc.builders; -export function printIndentableBlockComment(comment: BlockComment): Doc { - const lines = comment.value.split('\n'); - +export function printIndentableBlockComment({ lines }: BlockComment): Doc { return join( hardline, lines.map((line, index) => index === 0 - ? line.trimEnd() + ? `/${line.trimEnd()}` : ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}` ) ); diff --git a/src/slang-utils/is-indentable-block-comment.ts b/src/slang-utils/is-indentable-block-comment.ts index 7c6842a49..8f6953e0a 100644 --- a/src/slang-utils/is-indentable-block-comment.ts +++ b/src/slang-utils/is-indentable-block-comment.ts @@ -1,10 +1,9 @@ import type { BlockComment } from '../slang-nodes/types.d.ts'; -export function isIndentableBlockComment(comment: BlockComment): boolean { +export function isIndentableBlockComment({ lines }: BlockComment): boolean { // If the comment has multiple lines and every line starts with a star // we can fix the indentation of each line. The stars in the `/*` and // `*/` delimiters are not included in the comment value, so add them // back first. - const lines = comment.value.slice(1, -1).split('\n'); return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*'); } From 29a513d075c3b3c062c76d55cbbf19b4141edbab Mon Sep 17 00:00:00 2001 From: Klaus Date: Fri, 27 Mar 2026 14:07:20 -0300 Subject: [PATCH 2/5] small rebase to organise readability --- src/slang-nodes/MultiLineComment.ts | 14 ++------ src/slang-nodes/MultiLineNatSpecComment.ts | 14 ++------ src/slang-printers/print-block-comment.ts | 35 +++++++++++++++++++ .../print-indentable-block-comment.ts | 17 --------- .../is-indentable-block-comment.ts | 9 ----- 5 files changed, 39 insertions(+), 50 deletions(-) create mode 100644 src/slang-printers/print-block-comment.ts delete mode 100644 src/slang-printers/print-indentable-block-comment.ts delete mode 100644 src/slang-utils/is-indentable-block-comment.ts diff --git a/src/slang-nodes/MultiLineComment.ts b/src/slang-nodes/MultiLineComment.ts index a1aba90a5..976c4cae7 100644 --- a/src/slang-nodes/MultiLineComment.ts +++ b/src/slang-nodes/MultiLineComment.ts @@ -1,32 +1,22 @@ import { TerminalKind } from '@nomicfoundation/slang/cst'; -import { doc } from 'prettier'; -import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js'; -import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js'; +import { printBlockComment } from '../slang-printers/print-block-comment.js'; import { CommentNode } from './CommentNode.js'; import type { TerminalNode } from '@nomicfoundation/slang/cst'; import type { Doc } from 'prettier'; -const { join, literalline } = doc.builders; - export class MultiLineComment extends CommentNode { readonly kind = TerminalKind.MultiLineComment; value: string; - lines: string[]; - constructor(ast: TerminalNode, offset: number) { super(ast, offset); this.value = ast.unparse(); - this.lines = this.value.slice(1).split('\n'); } print(): Doc { - if (isIndentableBlockComment(this)) { - return printIndentableBlockComment(this); - } - return ['/', join(literalline, this.lines)]; + return printBlockComment(this); } } diff --git a/src/slang-nodes/MultiLineNatSpecComment.ts b/src/slang-nodes/MultiLineNatSpecComment.ts index 95c053aa5..c33d3c859 100644 --- a/src/slang-nodes/MultiLineNatSpecComment.ts +++ b/src/slang-nodes/MultiLineNatSpecComment.ts @@ -1,32 +1,22 @@ import { TerminalKind } from '@nomicfoundation/slang/cst'; -import { doc } from 'prettier'; -import { isIndentableBlockComment } from '../slang-utils/is-indentable-block-comment.js'; -import { printIndentableBlockComment } from '../slang-printers/print-indentable-block-comment.js'; +import { printBlockComment } from '../slang-printers/print-block-comment.js'; import { CommentNode } from './CommentNode.js'; import type { TerminalNode } from '@nomicfoundation/slang/cst'; import type { Doc } from 'prettier'; -const { join, literalline } = doc.builders; - export class MultiLineNatSpecComment extends CommentNode { readonly kind = TerminalKind.MultiLineNatSpecComment; value: string; - lines: string[]; - constructor(ast: TerminalNode, offset: number) { super(ast, offset); this.value = ast.unparse(); - this.lines = this.value.slice(1).split('\n'); } print(): Doc { - if (isIndentableBlockComment(this)) { - return printIndentableBlockComment(this); - } - return ['/', join(literalline, this.lines)]; + return printBlockComment(this); } } diff --git a/src/slang-printers/print-block-comment.ts b/src/slang-printers/print-block-comment.ts new file mode 100644 index 000000000..c30daf5a7 --- /dev/null +++ b/src/slang-printers/print-block-comment.ts @@ -0,0 +1,35 @@ +import { doc } from 'prettier'; + +import type { Doc } from 'prettier'; +import type { BlockComment } from '../slang-nodes/types.d.ts'; + +const { hardline, join, literalline } = doc.builders; + +function isIndentableBlockComment(lines: string[]): boolean { + // If the comment has multiple lines and every line starts with a star + // we can fix the indentation of each line. + return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*'); +} + +function printIndentableBlockComment(lines: string[]): Doc { + return join( + hardline, + lines.map((line, index) => + index === 0 + ? line.trimEnd() + : ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}` + ) + ); +} + +export function printBlockComment(comment: BlockComment): Doc { + // We remove the initial `/` to check if every line starts with `*` + const lines = comment.value.slice(1).split('\n'); + + return [ + '/', + isIndentableBlockComment(lines) + ? printIndentableBlockComment(lines) + : join(literalline, lines) + ]; +} diff --git a/src/slang-printers/print-indentable-block-comment.ts b/src/slang-printers/print-indentable-block-comment.ts deleted file mode 100644 index e59f96582..000000000 --- a/src/slang-printers/print-indentable-block-comment.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { doc } from 'prettier'; - -import type { Doc } from 'prettier'; -import type { BlockComment } from '../slang-nodes/types.d.ts'; - -const { hardline, join } = doc.builders; - -export function printIndentableBlockComment({ lines }: BlockComment): Doc { - return join( - hardline, - lines.map((line, index) => - index === 0 - ? `/${line.trimEnd()}` - : ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}` - ) - ); -} diff --git a/src/slang-utils/is-indentable-block-comment.ts b/src/slang-utils/is-indentable-block-comment.ts deleted file mode 100644 index 8f6953e0a..000000000 --- a/src/slang-utils/is-indentable-block-comment.ts +++ /dev/null @@ -1,9 +0,0 @@ -import type { BlockComment } from '../slang-nodes/types.d.ts'; - -export function isIndentableBlockComment({ lines }: BlockComment): boolean { - // If the comment has multiple lines and every line starts with a star - // we can fix the indentation of each line. The stars in the `/*` and - // `*/` delimiters are not included in the comment value, so add them - // back first. - return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*'); -} From 827a74115f513a24c942dd54f12025b3db478a78 Mon Sep 17 00:00:00 2001 From: Klaus Date: Fri, 27 Mar 2026 15:00:07 -0300 Subject: [PATCH 3/5] avoid trimStart twice --- src/slang-printers/print-block-comment.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/slang-printers/print-block-comment.ts b/src/slang-printers/print-block-comment.ts index c30daf5a7..d8a519987 100644 --- a/src/slang-printers/print-block-comment.ts +++ b/src/slang-printers/print-block-comment.ts @@ -5,19 +5,11 @@ import type { BlockComment } from '../slang-nodes/types.d.ts'; const { hardline, join, literalline } = doc.builders; -function isIndentableBlockComment(lines: string[]): boolean { - // If the comment has multiple lines and every line starts with a star - // we can fix the indentation of each line. - return lines.length > 1 && lines.every((line) => line.trimStart()[0] === '*'); -} - function printIndentableBlockComment(lines: string[]): Doc { return join( hardline, lines.map((line, index) => - index === 0 - ? line.trimEnd() - : ` ${index < lines.length - 1 ? line.trim() : line.trimStart()}` + index === 0 ? line.trimEnd() : ` ${line.trimEnd()}` ) ); } @@ -25,11 +17,20 @@ function printIndentableBlockComment(lines: string[]): Doc { export function printBlockComment(comment: BlockComment): Doc { // We remove the initial `/` to check if every line starts with `*` const lines = comment.value.slice(1).split('\n'); + let trimmedLines; + + // Only process lines for possible indentation if the block has multiple + // lines + if (lines.length > 1) { + trimmedLines = lines.map((line) => line.trimStart()); + } return [ '/', - isIndentableBlockComment(lines) - ? printIndentableBlockComment(lines) + // If the comment has multiple lines and every line starts with a star + // we can fix the indentation of each line. + trimmedLines?.every((line) => line.startsWith('*')) + ? printIndentableBlockComment(trimmedLines) : join(literalline, lines) ]; } From 9f59120dece09c9e589f57dbe3e02014940fe358 Mon Sep 17 00:00:00 2001 From: Klaus Date: Sat, 28 Mar 2026 18:46:17 -0300 Subject: [PATCH 4/5] small refactor --- src/slang-printers/print-block-comment.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/slang-printers/print-block-comment.ts b/src/slang-printers/print-block-comment.ts index d8a519987..5db0d9a07 100644 --- a/src/slang-printers/print-block-comment.ts +++ b/src/slang-printers/print-block-comment.ts @@ -8,9 +8,7 @@ const { hardline, join, literalline } = doc.builders; function printIndentableBlockComment(lines: string[]): Doc { return join( hardline, - lines.map((line, index) => - index === 0 ? line.trimEnd() : ` ${line.trimEnd()}` - ) + lines.map((line, index) => `${index === 0 ? '' : ' '}${line.trimEnd()}`) ); } From 759ce6f50c0a53108f9f42640d2a8714907a4137 Mon Sep 17 00:00:00 2001 From: Klaus Date: Sun, 29 Mar 2026 13:03:03 -0300 Subject: [PATCH 5/5] iterate only once over lines and exit early if not successful --- src/slang-printers/print-block-comment.ts | 28 +++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/slang-printers/print-block-comment.ts b/src/slang-printers/print-block-comment.ts index 5db0d9a07..dfc8ce38d 100644 --- a/src/slang-printers/print-block-comment.ts +++ b/src/slang-printers/print-block-comment.ts @@ -5,6 +5,22 @@ import type { BlockComment } from '../slang-nodes/types.d.ts'; const { hardline, join, literalline } = doc.builders; +function trimmedIndentableLines(lines: string[]): string[] | undefined { + // If the comment has multiple lines and every line starts with a star + // we can fix the indentation of each line. + if (lines.length > 1) { + const trimmedLines = []; + for (let line of lines) { + line = line.trimStart(); + if (!line.startsWith('*')) { + return; + } + trimmedLines.push(line); + } + return trimmedLines; + } +} + function printIndentableBlockComment(lines: string[]): Doc { return join( hardline, @@ -15,19 +31,11 @@ function printIndentableBlockComment(lines: string[]): Doc { export function printBlockComment(comment: BlockComment): Doc { // We remove the initial `/` to check if every line starts with `*` const lines = comment.value.slice(1).split('\n'); - let trimmedLines; - - // Only process lines for possible indentation if the block has multiple - // lines - if (lines.length > 1) { - trimmedLines = lines.map((line) => line.trimStart()); - } + const trimmedLines = trimmedIndentableLines(lines); return [ '/', - // If the comment has multiple lines and every line starts with a star - // we can fix the indentation of each line. - trimmedLines?.every((line) => line.startsWith('*')) + trimmedLines ? printIndentableBlockComment(trimmedLines) : join(literalline, lines) ];