@@ -15,6 +15,7 @@ interface Options {
1515 readonly lineWidth ?: number ;
1616 readonly columnWidth ?: number ;
1717 readonly insertFinalNewline ?: boolean ;
18+ readonly preserveMultiline ?: boolean ;
1819}
1920
2021export function talonFormatter ( node : Node , options : Options = { } ) : string {
@@ -27,6 +28,7 @@ export function talonFormatter(node: Node, options: Options = {}): string {
2728 options . lineWidth ?? DEFAULT_LINE_WIDTH ,
2829 columnWidth ,
2930 options . insertFinalNewline ?? DEFAULT_INSERT_FINAL_NEWLINE ,
31+ options . preserveMultiline ?? false ,
3032 ) ;
3133 return formatter . getText ( node ) ;
3234}
@@ -40,6 +42,7 @@ class TalonFormatter {
4042 private lineWidth : number ,
4143 private columnWidth : number | undefined ,
4244 private insertFinalNewline : boolean ,
45+ private preserveMultiline : boolean ,
4346 ) { }
4447
4548 getText ( node : Node ) : string {
@@ -56,20 +59,30 @@ class TalonFormatter {
5659 return nodeText ;
5760 }
5861
59- private getLeftRightText ( node : Node ) : string {
62+ private getLeftRightText ( node : Node , forceMultiline : boolean ) : string {
6063 const [ leftNode , _colonNode , ...rightNodes ] = node . children ;
6164 const left = this . getNodeText ( leftNode ) ;
6265
63- if ( rightNodes . length === 1 ) {
64- if ( isLeftRightSingleLine ( leftNode , rightNodes ) ) {
66+ if ( ! forceMultiline && rightNodes . length === 1 ) {
67+ if (
68+ ! this . preserveMultiline ||
69+ isLeftRightSingleLine ( leftNode , rightNodes )
70+ ) {
71+ const lastRow = this . lastRow ;
6572 const right = this . getNodeText ( rightNodes [ 0 ] ) ;
66- const leftWithPadding =
67- this . columnWidth != null
68- ? `${ left } : ` . padEnd ( this . columnWidth )
69- : `${ left } : ` ;
70- if ( leftWithPadding . length + right . length <= this . lineWidth ) {
71- return leftWithPadding + right ;
73+ if ( ! right . includes ( this . eol ) ) {
74+ const leftWithPadding =
75+ this . columnWidth != null
76+ ? `${ left } : ` . padEnd ( this . columnWidth )
77+ : `${ left } : ` ;
78+ if (
79+ leftWithPadding . length + right . length <=
80+ this . lineWidth
81+ ) {
82+ return leftWithPadding + right ;
83+ }
7284 }
85+ this . lastRow = lastRow ;
7386 }
7487 }
7588
@@ -135,8 +148,10 @@ class TalonFormatter {
135148 case "face_declaration" :
136149 case "gamepad_declaration" :
137150 case "deck_declaration" :
151+ return this . getLeftRightText ( node , false ) ;
152+
138153 case "settings_declaration" :
139- return this . getLeftRightText ( node ) ;
154+ return this . getLeftRightText ( node , true ) ;
140155
141156 case "comment" : {
142157 // When using crlf eol comments have a trailing `\r`
0 commit comments