File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { 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' ;
53import { CommentNode } from './CommentNode.js' ;
64
75import type { TerminalNode } from '@nomicfoundation/slang/cst' ;
86import type { Doc } from 'prettier' ;
97
10- const { join, literalline } = doc . builders ;
11-
128export 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}
Original file line number Diff line number Diff line change 11import { 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' ;
53import { CommentNode } from './CommentNode.js' ;
64
75import type { TerminalNode } from '@nomicfoundation/slang/cst' ;
86import type { Doc } from 'prettier' ;
97
10- const { join, literalline } = doc . builders ;
11-
128export 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}
Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments