4
4
*--------------------------------------------------------------------------------------------*/
5
5
'use strict' ;
6
6
7
- import { Range , FormattingOptions , Edit , SyntaxKind , ScanError } from '../main' ;
7
+ import { Range , FormattingOptions , Edit , SyntaxKind , ScanError } from '../main' ;
8
8
import { createScanner } from './scanner' ;
9
9
10
10
export function format ( documentText : string , range : Range | undefined , options : FormattingOptions ) : Edit [ ] {
@@ -81,12 +81,14 @@ export function format(documentText: string, range: Range | undefined, options:
81
81
let secondToken = scanNext ( ) ;
82
82
83
83
let replaceContent = '' ;
84
+ let needsLineBreak = false ;
84
85
while ( ! lineBreak && ( secondToken === SyntaxKind . LineCommentTrivia || secondToken === SyntaxKind . BlockCommentTrivia ) ) {
85
86
// comments on the same line: keep them on the same line, but ignore them otherwise
86
87
let commentTokenStart = scanner . getTokenOffset ( ) + formatTextStart ;
87
88
addEdit ( ' ' , firstTokenEnd , commentTokenStart ) ;
88
89
firstTokenEnd = scanner . getTokenOffset ( ) + scanner . getTokenLength ( ) + formatTextStart ;
89
- replaceContent = secondToken === SyntaxKind . LineCommentTrivia ? newLineAndIndent ( ) : '' ;
90
+ needsLineBreak = secondToken === SyntaxKind . LineCommentTrivia ;
91
+ replaceContent = needsLineBreak ? newLineAndIndent ( ) : '' ;
90
92
secondToken = scanNext ( ) ;
91
93
}
92
94
@@ -114,17 +116,21 @@ export function format(documentText: string, range: Range | undefined, options:
114
116
case SyntaxKind . BlockCommentTrivia :
115
117
if ( lineBreak ) {
116
118
replaceContent = newLineAndIndent ( ) ;
117
- } else {
119
+ } else if ( ! needsLineBreak ) {
118
120
// symbol following comment on the same line: keep on same line, separate with ' '
119
121
replaceContent = ' ' ;
120
122
}
121
123
break ;
122
124
case SyntaxKind . ColonToken :
123
- replaceContent = ' ' ;
125
+ if ( ! needsLineBreak ) {
126
+ replaceContent = ' ' ;
127
+ }
124
128
break ;
125
129
case SyntaxKind . StringLiteral :
126
130
if ( secondToken === SyntaxKind . ColonToken ) {
127
- replaceContent = '' ;
131
+ if ( ! needsLineBreak ) {
132
+ replaceContent = '' ;
133
+ }
128
134
break ;
129
135
}
130
136
// fall through
@@ -135,7 +141,9 @@ export function format(documentText: string, range: Range | undefined, options:
135
141
case SyntaxKind . CloseBraceToken :
136
142
case SyntaxKind . CloseBracketToken :
137
143
if ( secondToken === SyntaxKind . LineCommentTrivia || secondToken === SyntaxKind . BlockCommentTrivia ) {
138
- replaceContent = ' ' ;
144
+ if ( ! needsLineBreak ) {
145
+ replaceContent = ' ' ;
146
+ }
139
147
} else if ( secondToken !== SyntaxKind . CommaToken && secondToken !== SyntaxKind . EOF ) {
140
148
hasError = true ;
141
149
}
0 commit comments