@@ -98,21 +98,28 @@ namespace ts.formatting {
98
98
99
99
export function formatOnSemicolon ( position : number , sourceFile : SourceFile , rulesProvider : RulesProvider , options : FormatCodeSettings ) : TextChange [ ] {
100
100
const semicolon = findImmediatelyPrecedingTokenOfKind ( position , SyntaxKind . SemicolonToken , sourceFile ) ;
101
- return formatOutermostNodeWithinListLevel ( semicolon , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnSemicolon ) ;
101
+ return formatNodeLines ( findOutermostNodeWithinListLevel ( semicolon ) , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnSemicolon ) ;
102
102
}
103
103
104
104
export function formatOnOpeningCurly ( position : number , sourceFile : SourceFile , rulesProvider : RulesProvider , options : FormatCodeSettings ) : TextChange [ ] {
105
105
const openingCurly = findImmediatelyPrecedingTokenOfKind ( position , SyntaxKind . OpenBraceToken , sourceFile ) ;
106
- const curlyBraceRange = openingCurly && openingCurly . parent ;
107
- const nextToken = curlyBraceRange && findNextToken ( openingCurly , curlyBraceRange ) ;
108
- return nextToken && nextToken . kind === SyntaxKind . CloseBraceToken ?
109
- formatOutermostNodeWithinListLevel ( curlyBraceRange , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnOpeningCurlyBrace ) :
110
- [ ] ;
106
+ if ( openingCurly ) {
107
+ const curlyBraceRange = openingCurly . parent ;
108
+ const outermostNode = findOutermostNodeWithinListLevel ( curlyBraceRange ) ;
109
+
110
+ const textRange : TextRange = {
111
+ pos : getLineStartPositionForPosition ( outermostNode . getStart ( sourceFile ) , sourceFile ) ,
112
+ end : position
113
+ } ;
114
+
115
+ return formatSpan ( textRange , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnOpeningCurlyBrace ) ;
116
+ }
117
+ return [ ] ;
111
118
}
112
119
113
120
export function formatOnClosingCurly ( position : number , sourceFile : SourceFile , rulesProvider : RulesProvider , options : FormatCodeSettings ) : TextChange [ ] {
114
121
const precedingToken = findImmediatelyPrecedingTokenOfKind ( position , SyntaxKind . CloseBraceToken , sourceFile ) ;
115
- return formatOutermostNodeWithinListLevel ( precedingToken , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnClosingCurlyBrace ) ;
122
+ return formatNodeLines ( findOutermostNodeWithinListLevel ( precedingToken ) , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnClosingCurlyBrace ) ;
116
123
}
117
124
118
125
export function formatDocument ( sourceFile : SourceFile , rulesProvider : RulesProvider , options : FormatCodeSettings ) : TextChange [ ] {
@@ -145,8 +152,8 @@ namespace ts.formatting {
145
152
}
146
153
147
154
/**
148
- * Finds and formats the highest node enclosing `node` whose end does not exceed the `node.end `
149
- * and is at the same list level as the token at `node`.
155
+ * Finds the highest node enclosing `node` at the same list level as `node`
156
+ * and whose end does not exceed `node.end `.
150
157
*
151
158
* Consider typing the following
152
159
* ```
@@ -157,9 +164,7 @@ namespace ts.formatting {
157
164
* Upon typing the closing curly, we want to format the entire `while`-statement, but not the preceding
158
165
* variable declaration.
159
166
*/
160
- function formatOutermostNodeWithinListLevel ( node : Node , sourceFile : SourceFile , options : FormatCodeSettings , rulesProvider : RulesProvider , formattingRequestKind : FormattingRequestKind ) {
161
- // If we walk upwards searching for the parent that has the same end value, we'll end up with the whole source file.
162
- // `isListElement` allows to stop on the list element level.
167
+ function findOutermostNodeWithinListLevel ( node : Node ) {
163
168
let current = node ;
164
169
while ( current &&
165
170
current . parent &&
@@ -168,7 +173,7 @@ namespace ts.formatting {
168
173
current = current . parent ;
169
174
}
170
175
171
- return formatNodeLines ( current , sourceFile , options , rulesProvider , formattingRequestKind ) ;
176
+ return current ;
172
177
}
173
178
174
179
// Returns true if node is a element in some list in parent
0 commit comments