Skip to content

Commit fcf7baf

Browse files
authored
fix(44812): add outlining spans for comments inside blocks (microsoft#44847)
1 parent 76d7543 commit fcf7baf

File tree

2 files changed

+199
-5
lines changed

2 files changed

+199
-5
lines changed

src/services/outliningElementsCollector.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ namespace ts.OutliningElementsCollector {
3434
if (depthRemaining === 0) return;
3535
cancellationToken.throwIfCancellationRequested();
3636

37-
if (isDeclaration(n) || isVariableStatement(n) || n.kind === SyntaxKind.EndOfFileToken) {
37+
if (isDeclaration(n) || isVariableStatement(n) || isReturnStatement(n) || n.kind === SyntaxKind.EndOfFileToken) {
3838
addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out);
3939
}
4040

4141
if (isFunctionLike(n) && isBinaryExpression(n.parent) && isPropertyAccessExpression(n.parent.left)) {
4242
addOutliningForLeadingCommentsForNode(n.parent.left, sourceFile, cancellationToken, out);
4343
}
4444

45+
if (isBlock(n) || isModuleBlock(n)) {
46+
addOutliningForLeadingCommentsForPos(n.statements.end, sourceFile, cancellationToken, out);
47+
}
48+
49+
if (isClassLike(n) || isInterfaceDeclaration(n)) {
50+
addOutliningForLeadingCommentsForPos(n.members.end, sourceFile, cancellationToken, out);
51+
}
52+
4553
const span = getOutliningSpanForNode(n, sourceFile);
4654
if (span) out.push(span);
4755

@@ -106,9 +114,10 @@ namespace ts.OutliningElementsCollector {
106114
return regionDelimiterRegExp.exec(lineText);
107115
}
108116

109-
function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
110-
const comments = getLeadingCommentRangesOfNode(n, sourceFile);
117+
function addOutliningForLeadingCommentsForPos(pos: number, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
118+
const comments = getLeadingCommentRanges(sourceFile.text, pos);
111119
if (!comments) return;
120+
112121
let firstSingleLineCommentStart = -1;
113122
let lastSingleLineCommentEnd = -1;
114123
let singleLineCommentCount = 0;
@@ -152,6 +161,11 @@ namespace ts.OutliningElementsCollector {
152161
}
153162
}
154163

164+
function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
165+
if (isJsxText(n)) return;
166+
addOutliningForLeadingCommentsForPos(n.pos, sourceFile, cancellationToken, out);
167+
}
168+
155169
function createOutliningSpanFromBounds(pos: number, end: number, kind: OutliningSpanKind): OutliningSpan {
156170
return createOutliningSpan(createTextSpanFromBounds(pos, end), kind);
157171
}

tests/cases/fourslash/getOutliningForBlockComments.ts

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,187 @@
136136
//// function method(param)[| {
137137
//// }|]
138138
////}|]
139+
////
140+
////function fn1()[| {
141+
//// [|/**
142+
//// * comment
143+
//// */|]
144+
////}|]
145+
////function fn2()[| {
146+
//// [|/**
147+
//// * comment
148+
//// */|]
149+
////
150+
//// [|/**
151+
//// * comment
152+
//// */|]
153+
////}|]
154+
////function fn3()[| {
155+
//// const x = 1;
156+
////
157+
//// [|/**
158+
//// * comment
159+
//// */|]
160+
////
161+
//// [|/**
162+
//// * comment
163+
//// */|]
164+
////}|]
165+
////function fn4()[| {
166+
//// [|/**
167+
//// * comment
168+
//// */|]
169+
//// const x = 1;
170+
////
171+
//// [|/**
172+
//// * comment
173+
//// */|]
174+
////}|]
175+
////function fn5()[| {
176+
//// [|/**
177+
//// * comment
178+
//// */|]
179+
////
180+
//// [|/**
181+
//// * comment
182+
//// */|]
183+
//// return 1;
184+
////}|]
185+
////function fn6()[| {
186+
//// [|/**
187+
//// * comment
188+
//// */|]
189+
////
190+
//// [|/**
191+
//// * comment
192+
//// */|]
193+
//// const x = 1;
194+
////}|]
195+
////class C1[| {
196+
//// [|/**
197+
//// * comment
198+
//// */|]
199+
////
200+
//// [|/**
201+
//// * comment
202+
//// */|]
203+
////}|]
204+
////class C2[| {
205+
//// private prop = 1;
206+
//// [|/**
207+
//// * comment
208+
//// */|]
209+
////
210+
//// [|/**
211+
//// * comment
212+
//// */|]
213+
////}|]
214+
////class C3[| {
215+
//// [|/**
216+
//// * comment
217+
//// */|]
218+
////
219+
//// private prop = 1;
220+
//// [|/**
221+
//// * comment
222+
//// */|]
223+
////}|]
224+
////class C4[| {
225+
//// [|/**
226+
//// * comment
227+
//// */|]
228+
////
229+
//// [|/**
230+
//// * comment
231+
//// */|]
232+
//// private prop = 1;
233+
////}|]
234+
////module M1[| {
235+
//// [|/**
236+
//// * comment
237+
//// */|]
238+
////
239+
//// [|/**
240+
//// * comment
241+
//// */|]
242+
////}|]
243+
////module M2[| {
244+
//// export const a = 1;
245+
//// [|/**
246+
//// * comment
247+
//// */|]
248+
////
249+
//// [|/**
250+
//// * comment
251+
//// */|]
252+
////}|]
253+
////module M3[| {
254+
//// [|/**
255+
//// * comment
256+
//// */|]
257+
//// export const a = 1;
258+
////
259+
//// [|/**
260+
//// * comment
261+
//// */|]
262+
////}|]
263+
////module M4[| {
264+
//// [|/**
265+
//// * comment
266+
//// */|]
267+
////
268+
//// [|/**
269+
//// * comment
270+
//// */|]
271+
//// export const a = 1;
272+
////}|]
273+
////interface I1[| {
274+
//// [|/**
275+
//// * comment
276+
//// */|]
277+
////
278+
//// [|/**
279+
//// * comment
280+
//// */|]
281+
////}|]
282+
////interface I2[| {
283+
//// x: number;
284+
//// [|/**
285+
//// * comment
286+
//// */|]
287+
////
288+
//// [|/**
289+
//// * comment
290+
//// */|]
291+
////}|]
292+
////interface I3[| {
293+
//// [|/**
294+
//// * comment
295+
//// */|]
296+
//// x: number;
297+
////
298+
//// [|/**
299+
//// * comment
300+
//// */|]
301+
////}|]
302+
////interface I4[| {
303+
//// [|/**
304+
//// * comment
305+
//// */|]
306+
////
307+
//// [|/**
308+
//// * comment
309+
//// */|]
310+
//// x: number;
311+
////}|]
312+
////[|{
313+
//// [|/**
314+
//// * comment
315+
//// */|]
316+
////
317+
//// [|/**
318+
//// * comment
319+
//// */|]
320+
////}|]
139321

140322
verify.outliningSpansInCurrentFile(test.ranges());
141-
142-

0 commit comments

Comments
 (0)