Skip to content

Commit 60b78c6

Browse files
committed
only format open curly up to the open curly
1 parent eae234c commit 60b78c6

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/services/formatting/formatting.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,28 @@ namespace ts.formatting {
9898

9999
export function formatOnSemicolon(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
100100
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);
102102
}
103103

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

113120
export function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
114121
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);
116123
}
117124

118125
export function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
@@ -145,8 +152,8 @@ namespace ts.formatting {
145152
}
146153

147154
/**
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`.
150157
*
151158
* Consider typing the following
152159
* ```
@@ -157,9 +164,7 @@ namespace ts.formatting {
157164
* Upon typing the closing curly, we want to format the entire `while`-statement, but not the preceding
158165
* variable declaration.
159166
*/
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) {
163168
let current = node;
164169
while (current &&
165170
current.parent &&
@@ -168,7 +173,7 @@ namespace ts.formatting {
168173
current = current.parent;
169174
}
170175

171-
return formatNodeLines(current, sourceFile, options, rulesProvider, formattingRequestKind);
176+
return current;
172177
}
173178

174179
// Returns true if node is a element in some list in parent

0 commit comments

Comments
 (0)