diff --git a/src/slang-nodes/MultiLineComment.ts b/src/slang-nodes/MultiLineComment.ts index e08df19b6..976c4cae7 100644 --- a/src/slang-nodes/MultiLineComment.ts +++ b/src/slang-nodes/MultiLineComment.ts @@ -1,14 +1,10 @@ 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; @@ -21,9 +17,6 @@ export class MultiLineComment extends CommentNode { } print(): Doc { - if (isIndentableBlockComment(this)) { - return printIndentableBlockComment(this); - } - return join(literalline, this.value.split('\n')); + return printBlockComment(this); } } diff --git a/src/slang-nodes/MultiLineNatSpecComment.ts b/src/slang-nodes/MultiLineNatSpecComment.ts index 368522c68..c33d3c859 100644 --- a/src/slang-nodes/MultiLineNatSpecComment.ts +++ b/src/slang-nodes/MultiLineNatSpecComment.ts @@ -1,14 +1,10 @@ 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; @@ -21,9 +17,6 @@ export class MultiLineNatSpecComment extends CommentNode { } print(): Doc { - if (isIndentableBlockComment(this)) { - return printIndentableBlockComment(this); - } - return join(literalline, this.value.split('\n')); + 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..dfc8ce38d --- /dev/null +++ b/src/slang-printers/print-block-comment.ts @@ -0,0 +1,42 @@ +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 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, + lines.map((line, index) => `${index === 0 ? '' : ' '}${line.trimEnd()}`) + ); +} + +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'); + const trimmedLines = trimmedIndentableLines(lines); + + return [ + '/', + trimmedLines + ? printIndentableBlockComment(trimmedLines) + : 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 cc5598eec..000000000 --- a/src/slang-printers/print-indentable-block-comment.ts +++ /dev/null @@ -1,19 +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(comment: BlockComment): Doc { - const lines = comment.value.split('\n'); - - 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 7c6842a49..000000000 --- a/src/slang-utils/is-indentable-block-comment.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { BlockComment } from '../slang-nodes/types.d.ts'; - -export function isIndentableBlockComment(comment: 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] === '*'); -}