@@ -103,18 +103,30 @@ namespace ts.formatting {
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
- if ( openingCurly ) {
107
- const curlyBraceRange = openingCurly . parent ;
108
- const outermostNode = findOutermostNodeWithinListLevel ( curlyBraceRange ) ;
106
+ if ( ! openingCurly ) {
107
+ return [ ] ;
108
+ }
109
+ const curlyBraceRange = openingCurly . parent ;
110
+ const outermostNode = findOutermostNodeWithinListLevel ( curlyBraceRange ) ;
109
111
110
- const textRange : TextRange = {
111
- pos : getLineStartPositionForPosition ( outermostNode . getStart ( sourceFile ) , sourceFile ) ,
112
- end : position
113
- } ;
112
+ /**
113
+ * We limit the span to end at the opening curly to handle the case where
114
+ * the brace matched to that just typed will be incorrect after further edits.
115
+ * For example, we could type the opening curly for the following method
116
+ * body without brace-matching activated:
117
+ * ```
118
+ * class C {
119
+ * foo()
120
+ * }
121
+ * ```
122
+ * and we wouldn't want to move the closing brace.
123
+ */
124
+ const textRange : TextRange = {
125
+ pos : getLineStartPositionForPosition ( outermostNode . getStart ( sourceFile ) , sourceFile ) ,
126
+ end : position
127
+ } ;
114
128
115
- return formatSpan ( textRange , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnOpeningCurlyBrace ) ;
116
- }
117
- return [ ] ;
129
+ return formatSpan ( textRange , sourceFile , options , rulesProvider , FormattingRequestKind . FormatOnOpeningCurlyBrace ) ;
118
130
}
119
131
120
132
export function formatOnClosingCurly ( position : number , sourceFile : SourceFile , rulesProvider : RulesProvider , options : FormatCodeSettings ) : TextChange [ ] {
0 commit comments