Skip to content

Commit 619658b

Browse files
Merge pull request microsoft#39109 from Kingwl/lambda_span
Add outlining spans for call expression or arrow body
2 parents 784396c + 4d57d59 commit 619658b

6 files changed

+143
-85
lines changed

src/services/outliningElementsCollector.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,31 @@ namespace ts.OutliningElementsCollector {
227227
return spanForTemplateLiteral(<TemplateExpression | NoSubstitutionTemplateLiteral>n);
228228
case SyntaxKind.ArrayBindingPattern:
229229
return spanForNode(n, /*autoCollapse*/ false, /*useFullStart*/ !isBindingElement(n.parent), SyntaxKind.OpenBracketToken);
230+
case SyntaxKind.ArrowFunction:
231+
return spanForArrowFunction(<ArrowFunction>n);
232+
case SyntaxKind.CallExpression:
233+
return spanForCallExpression(<CallExpression>n);
234+
}
235+
236+
function spanForCallExpression(node: CallExpression): OutliningSpan | undefined {
237+
if (!node.arguments.length) {
238+
return undefined;
239+
}
240+
const openToken = findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile);
241+
const closeToken = findChildOfKind(node, SyntaxKind.CloseParenToken, sourceFile);
242+
if (!openToken || !closeToken || positionsAreOnSameLine(openToken.pos, closeToken.pos, sourceFile)) {
243+
return undefined;
244+
}
245+
246+
return spanBetweenTokens(openToken, closeToken, node, sourceFile, /*autoCollapse*/ false, /*useFullStart*/ true);
247+
}
248+
249+
function spanForArrowFunction(node: ArrowFunction): OutliningSpan | undefined {
250+
if (isBlock(node.body) || positionsAreOnSameLine(node.body.getFullStart(), node.body.getEnd(), sourceFile)) {
251+
return undefined;
252+
}
253+
const textSpan = createTextSpanFromBounds(node.body.getFullStart(), node.body.getEnd());
254+
return createOutliningSpan(textSpan, OutliningSpanKind.Code, createTextSpanFromNode(node));
230255
}
231256

232257
function spanForJSXElement(node: JsxElement): OutliningSpan | undefined {

tests/cases/fourslash/getOutliningSpans.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@
106106
////function f(x: number[], y: number[])[| {
107107
//// return 3;
108108
////}|]
109-
////f(
109+
////f[|(
110110
////// single line array literal span won't render in VS
111111
//// [|[0]|],
112112
//// [|[
113113
//// 1,
114114
//// 2
115115
//// ]|]
116-
////);
116+
////)|];
117117

118118

119119
verify.outliningSpansInCurrentFile(test.ranges(), "code");

tests/cases/fourslash/getOutliningSpansDepthChainedCalls.ts

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,113 +4,113 @@
44

55
////declare var router: any;
66
////router
7-
//// .get("/", async(ctx) =>[|{
7+
//// .get[|("/", async(ctx) =>[|{
88
//// ctx.body = "base";
9-
//// }|])
10-
//// .post("/a", async(ctx) =>[|{
9+
//// }|])|]
10+
//// .post[|("/a", async(ctx) =>[|{
1111
//// //a
12-
//// }|])
13-
//// .get("/", async(ctx) =>[|{
12+
//// }|])|]
13+
//// .get[|("/", async(ctx) =>[|{
1414
//// ctx.body = "base";
15-
//// }|])
16-
//// .post("/a", async(ctx) =>[|{
15+
//// }|])|]
16+
//// .post[|("/a", async(ctx) =>[|{
1717
//// //a
18-
//// }|])
19-
//// .get("/", async(ctx) =>[|{
18+
//// }|])|]
19+
//// .get[|("/", async(ctx) =>[|{
2020
//// ctx.body = "base";
21-
//// }|])
22-
//// .post("/a", async(ctx) =>[|{
21+
//// }|])|]
22+
//// .post[|("/a", async(ctx) =>[|{
2323
//// //a
24-
//// }|])
25-
//// .get("/", async(ctx) =>[|{
24+
//// }|])|]
25+
//// .get[|("/", async(ctx) =>[|{
2626
//// ctx.body = "base";
27-
//// }|])
28-
//// .post("/a", async(ctx) =>[|{
27+
//// }|])|]
28+
//// .post[|("/a", async(ctx) =>[|{
2929
//// //a
30-
//// }|])
31-
//// .get("/", async(ctx) =>[|{
30+
//// }|])|]
31+
//// .get[|("/", async(ctx) =>[|{
3232
//// ctx.body = "base";
33-
//// }|])
34-
//// .post("/a", async(ctx) =>[|{
33+
//// }|])|]
34+
//// .post[|("/a", async(ctx) =>[|{
3535
//// //a
36-
//// }|])
37-
//// .get("/", async(ctx) =>[|{
36+
//// }|])|]
37+
//// .get[|("/", async(ctx) =>[|{
3838
//// ctx.body = "base";
39-
//// }|])
40-
//// .post("/a", async(ctx) =>[|{
39+
//// }|])|]
40+
//// .post[|("/a", async(ctx) =>[|{
4141
//// //a
42-
//// }|])
43-
//// .get("/", async(ctx) =>[|{
42+
//// }|])|]
43+
//// .get[|("/", async(ctx) =>[|{
4444
//// ctx.body = "base";
45-
//// }|])
46-
//// .post("/a", async(ctx) =>[|{
45+
//// }|])|]
46+
//// .post[|("/a", async(ctx) =>[|{
4747
//// //a
48-
//// }|])
49-
//// .get("/", async(ctx) =>[|{
48+
//// }|])|]
49+
//// .get[|("/", async(ctx) =>[|{
5050
//// ctx.body = "base";
51-
//// }|])
52-
//// .post("/a", async(ctx) =>[|{
51+
//// }|])|]
52+
//// .post[|("/a", async(ctx) =>[|{
5353
//// //a
54-
//// }|])
55-
//// .get("/", async(ctx) =>[|{
54+
//// }|])|]
55+
//// .get[|("/", async(ctx) =>[|{
5656
//// ctx.body = "base";
57-
//// }|])
58-
//// .post("/a", async(ctx) =>[|{
57+
//// }|])|]
58+
//// .post[|("/a", async(ctx) =>[|{
5959
//// //a
60-
//// }|])
61-
//// .get("/", async(ctx) =>[|{
60+
//// }|])|]
61+
//// .get[|("/", async(ctx) =>[|{
6262
//// ctx.body = "base";
63-
//// }|])
64-
//// .post("/a", async(ctx) =>[|{
63+
//// }|])|]
64+
//// .post[|("/a", async(ctx) =>[|{
6565
//// //a
66-
//// }|])
67-
//// .get("/", async(ctx) =>[|{
66+
//// }|])|]
67+
//// .get[|("/", async(ctx) =>[|{
6868
//// ctx.body = "base";
69-
//// }|])
70-
//// .post("/a", async(ctx) =>[|{
69+
//// }|])|]
70+
//// .post[|("/a", async(ctx) =>[|{
7171
//// //a
72-
//// }|])
73-
//// .get("/", async(ctx) =>[|{
72+
//// }|])|]
73+
//// .get[|("/", async(ctx) =>[|{
7474
//// ctx.body = "base";
75-
//// }|])
76-
//// .post("/a", async(ctx) =>[|{
75+
//// }|])|]
76+
//// .post[|("/a", async(ctx) =>[|{
7777
//// //a
78-
//// }|])
79-
//// .get("/", async(ctx) =>[|{
78+
//// }|])|]
79+
//// .get[|("/", async(ctx) =>[|{
8080
//// ctx.body = "base";
81-
//// }|])
82-
//// .post("/a", async(ctx) =>[|{
81+
//// }|])|]
82+
//// .post[|("/a", async(ctx) =>[|{
8383
//// //a
84-
//// }|])
85-
//// .get("/", async(ctx) =>[|{
84+
//// }|])|]
85+
//// .get[|("/", async(ctx) =>[|{
8686
//// ctx.body = "base";
87-
//// }|])
88-
//// .post("/a", async(ctx) =>[|{
87+
//// }|])|]
88+
//// .post[|("/a", async(ctx) =>[|{
8989
//// //a
90-
//// }|])
91-
//// .get("/", async(ctx) =>[|{
90+
//// }|])|]
91+
//// .get[|("/", async(ctx) =>[|{
9292
//// ctx.body = "base";
93-
//// }|])
94-
//// .post("/a", async(ctx) =>[|{
93+
//// }|])|]
94+
//// .post[|("/a", async(ctx) =>[|{
9595
//// //a
96-
//// }|])
97-
//// .get("/", async(ctx) =>[|{
96+
//// }|])|]
97+
//// .get[|("/", async(ctx) =>[|{
9898
//// ctx.body = "base";
99-
//// }|])
100-
//// .post("/a", async(ctx) =>[|{
99+
//// }|])|]
100+
//// .post[|("/a", async(ctx) =>[|{
101101
//// //a
102-
//// }|])
103-
//// .get("/", async(ctx) =>[|{
102+
//// }|])|]
103+
//// .get[|("/", async(ctx) =>[|{
104104
//// ctx.body = "base";
105-
//// }|])
106-
//// .post("/a", async(ctx) =>[|{
105+
//// }|])|]
106+
//// .post[|("/a", async(ctx) =>[|{
107107
//// //a
108-
//// }|])
109-
//// .get("/", async(ctx) =>[|{
108+
//// }|])|]
109+
//// .get[|("/", async(ctx) =>[|{
110110
//// ctx.body = "base";
111-
//// }|])
112-
//// .post("/a", async(ctx) =>[|{
111+
//// }|])|]
112+
//// .post[|("/a", async(ctx) =>[|{
113113
//// //a
114-
//// }|])
114+
//// }|])|]
115115

116116
verify.outliningSpansInCurrentFile(test.ranges());
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
//// console.log(123, 456)l;
4+
//// console.log(
5+
//// );
6+
//// console.log[|(
7+
//// 123, 456
8+
//// )|];
9+
//// console.log[|(
10+
//// 123,
11+
//// 456
12+
//// )|];
13+
//// () =>[| console.log[|(
14+
//// 123,
15+
//// 456
16+
//// )|]|];
17+
18+
verify.outliningSpansInCurrentFile(test.ranges());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
//// () => 42;
4+
//// () => ( 42 );
5+
//// () =>[| {
6+
//// 42
7+
//// }|];
8+
//// () =>[| (
9+
//// 42
10+
//// )|];
11+
//// () =>[| "foo" +
12+
//// "bar" +
13+
//// "baz"|];
14+
15+
verify.outliningSpansInCurrentFile(test.ranges());

tests/cases/fourslash/outliningSpansForFunction.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
//// b: number
66
////) => {
77
//// return a + b;
8-
////}|]
9-
/////
10-
////(a: number, b: number) => [|{
8+
////}|];
9+
////
10+
////(a: number, b: number) =>[| {
1111
//// return a + b;
1212
////}|]
1313
////
@@ -55,30 +55,30 @@
5555
////}|]
5656
////
5757
////declare function foo(props: any): void;
58-
////foo(
58+
////foo[|(
5959
//// a =>[| {
6060
////
6161
//// }|]
62-
////)
62+
////)|]
6363
////
64-
////foo(
64+
////foo[|(
6565
//// (a) =>[| {
6666
////
6767
//// }|]
68-
////)
68+
////)|]
6969
////
70-
////foo(
70+
////foo[|(
7171
//// (a, b, c) =>[| {
7272
////
7373
//// }|]
74-
////)
74+
////)|]
7575
////
76-
////foo([|
76+
////foo[|([|
7777
//// (a,
7878
//// b,
7979
//// c) => {
8080
////
8181
//// }|]
82-
////)
82+
////)|]
8383

8484
verify.outliningSpansInCurrentFile(test.ranges());

0 commit comments

Comments
 (0)