Skip to content

Commit 6e4c150

Browse files
Andy HansonRyanCavanaugh
Andy Hanson
authored andcommitted
When function parameters span multiple lines, make the function span start at the ( (#26907)
* When function parameters span multiple lines, make the function span start at the `(` * Undo unnecessary change
1 parent 583edce commit 6e4c150

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/compiler/utilities.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4330,6 +4330,10 @@ namespace ts {
43304330
return positionsAreOnSameLine(range1.end, getStartPositionOfRange(range2, sourceFile), sourceFile);
43314331
}
43324332

4333+
export function isNodeArrayMultiLine(list: NodeArray<Node>, sourceFile: SourceFile): boolean {
4334+
return !positionsAreOnSameLine(list.pos, list.end, sourceFile);
4335+
}
4336+
43334337
export function positionsAreOnSameLine(pos1: number, pos2: number, sourceFile: SourceFile) {
43344338
return pos1 === pos2 ||
43354339
getLineOfLocalPosition(sourceFile, pos1) === getLineOfLocalPosition(sourceFile, pos2);

src/services/outliningElementsCollector.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ namespace ts.OutliningElementsCollector {
153153
function getOutliningSpanForNode(n: Node, sourceFile: SourceFile): OutliningSpan | undefined {
154154
switch (n.kind) {
155155
case SyntaxKind.Block:
156-
if (isFunctionBlock(n)) {
157-
return spanForNode(n.parent, /*autoCollapse*/ n.parent.kind !== SyntaxKind.ArrowFunction);
156+
if (isFunctionLike(n.parent)) {
157+
return functionSpan(n.parent, n as Block, sourceFile);
158158
}
159159
// Check if the block is standalone, or 'attached' to some parent statement.
160160
// If the latter, we want to collapse the block, but consider its hint span
@@ -225,18 +225,26 @@ namespace ts.OutliningElementsCollector {
225225
return spanForNode(node, /*autoCollapse*/ false, /*useFullStart*/ !isArrayLiteralExpression(node.parent) && !isCallExpression(node.parent), open);
226226
}
227227

228-
function spanForNode(hintSpanNode: Node, autoCollapse = false, useFullStart = true, open: SyntaxKind.OpenBraceToken | SyntaxKind.OpenBracketToken = SyntaxKind.OpenBraceToken): OutliningSpan | undefined {
228+
function spanForNode(hintSpanNode: Node, autoCollapse = false, useFullStart = true, open: SyntaxKind.OpenBraceToken | SyntaxKind.OpenBracketToken = SyntaxKind.OpenBraceToken, close: SyntaxKind = open === SyntaxKind.OpenBraceToken ? SyntaxKind.CloseBraceToken : SyntaxKind.CloseBracketToken): OutliningSpan | undefined {
229229
const openToken = findChildOfKind(n, open, sourceFile);
230-
const close = open === SyntaxKind.OpenBraceToken ? SyntaxKind.CloseBraceToken : SyntaxKind.CloseBracketToken;
231230
const closeToken = findChildOfKind(n, close, sourceFile);
232-
if (!openToken || !closeToken) {
233-
return undefined;
234-
}
235-
const textSpan = createTextSpanFromBounds(useFullStart ? openToken.getFullStart() : openToken.getStart(sourceFile), closeToken.getEnd());
236-
return createOutliningSpan(textSpan, OutliningSpanKind.Code, createTextSpanFromNode(hintSpanNode, sourceFile), autoCollapse);
231+
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, hintSpanNode, sourceFile, autoCollapse, useFullStart);
237232
}
238233
}
239234

235+
function functionSpan(node: FunctionLike, body: Block, sourceFile: SourceFile): OutliningSpan | undefined {
236+
const openToken = isNodeArrayMultiLine(node.parameters, sourceFile)
237+
? findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile)
238+
: findChildOfKind(body, SyntaxKind.OpenBraceToken, sourceFile);
239+
const closeToken = findChildOfKind(body, SyntaxKind.CloseBraceToken, sourceFile);
240+
return openToken && closeToken && spanBetweenTokens(openToken, closeToken, node.parent, sourceFile, /*autoCollapse*/ node.parent.kind !== SyntaxKind.ArrowFunction);
241+
}
242+
243+
function spanBetweenTokens(openToken: Node, closeToken: Node, hintSpanNode: Node, sourceFile: SourceFile, autoCollapse = false, useFullStart = true): OutliningSpan {
244+
const textSpan = createTextSpanFromBounds(useFullStart ? openToken.getFullStart() : openToken.getStart(sourceFile), closeToken.getEnd());
245+
return createOutliningSpan(textSpan, OutliningSpanKind.Code, createTextSpanFromNode(hintSpanNode, sourceFile), autoCollapse);
246+
}
247+
240248
function createOutliningSpan(textSpan: TextSpan, kind: OutliningSpanKind, hintSpan: TextSpan = textSpan, autoCollapse = false, bannerText = "..."): OutliningSpan {
241249
return { textSpan, kind, hintSpan, bannerText, autoCollapse };
242250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////function f(x: number, y: number)[| {
4+
//// return x + y;
5+
////}|]
6+
////
7+
////function g[|(
8+
//// x: number,
9+
//// y: number,
10+
////): number {
11+
//// return x + y;
12+
////}|]
13+
14+
verify.outliningSpansInCurrentFile(test.ranges());

0 commit comments

Comments
 (0)