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
1511 value : string ;
1612
17- lines : string [ ] ;
18-
1913 constructor ( ast : TerminalNode , offset : number ) {
2014 super ( ast , offset ) ;
2115
2216 this . value = ast . unparse ( ) ;
23- this . lines = this . value . slice ( 1 ) . split ( '\n' ) ;
2417 }
2518
2619 print ( ) : Doc {
27- if ( isIndentableBlockComment ( this ) ) {
28- return printIndentableBlockComment ( this ) ;
29- }
30- return [ '/' , join ( literalline , this . lines ) ] ;
20+ return printBlockComment ( this ) ;
3121 }
3222}
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
1511 value : string ;
1612
17- lines : string [ ] ;
18-
1913 constructor ( ast : TerminalNode , offset : number ) {
2014 super ( ast , offset ) ;
2115
2216 this . value = ast . unparse ( ) ;
23- this . lines = this . value . slice ( 1 ) . split ( '\n' ) ;
2417 }
2518
2619 print ( ) : Doc {
27- if ( isIndentableBlockComment ( this ) ) {
28- return printIndentableBlockComment ( this ) ;
29- }
30- return [ '/' , join ( literalline , this . lines ) ] ;
20+ return printBlockComment ( this ) ;
3121 }
3222}
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 isIndentableBlockComment ( lines : string [ ] ) : boolean {
9+ // If the comment has multiple lines and every line starts with a star
10+ // we can fix the indentation of each line.
11+ return lines . length > 1 && lines . every ( ( line ) => line . trimStart ( ) [ 0 ] === '*' ) ;
12+ }
13+
14+ function printIndentableBlockComment ( lines : string [ ] ) : Doc {
15+ return join (
16+ hardline ,
17+ lines . map ( ( line , index ) =>
18+ index === 0
19+ ? line . trimEnd ( )
20+ : ` ${ index < lines . length - 1 ? line . trim ( ) : line . trimStart ( ) } `
21+ )
22+ ) ;
23+ }
24+
25+ export function printBlockComment ( comment : BlockComment ) : Doc {
26+ // We remove the initial `/` to check if every line starts with `*`
27+ const lines = comment . value . slice ( 1 ) . split ( '\n' ) ;
28+
29+ return [
30+ '/' ,
31+ isIndentableBlockComment ( lines )
32+ ? printIndentableBlockComment ( lines )
33+ : join ( literalline , lines )
34+ ] ;
35+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments