Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/slang-nodes/MultiLineComment.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
11 changes: 2 additions & 9 deletions src/slang-nodes/MultiLineNatSpecComment.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
42 changes: 42 additions & 0 deletions src/slang-printers/print-block-comment.ts
Original file line number Diff line number Diff line change
@@ -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)
];
}
19 changes: 0 additions & 19 deletions src/slang-printers/print-indentable-block-comment.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/slang-utils/is-indentable-block-comment.ts

This file was deleted.