Skip to content

Commit 25abf8a

Browse files
committed
respond ot comments
1 parent 60b78c6 commit 25abf8a

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/services/formatting/formatting.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,30 @@ namespace ts.formatting {
103103

104104
export function formatOnOpeningCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
105105
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);
109111

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+
};
114128

115-
return formatSpan(textRange, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnOpeningCurlyBrace);
116-
}
117-
return [];
129+
return formatSpan(textRange, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnOpeningCurlyBrace);
118130
}
119131

120132
export function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {

0 commit comments

Comments
 (0)