From fcf7bafd57b7c0077e05f234653ab4c78f220212 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sat, 31 Jul 2021 01:56:32 +0300 Subject: [PATCH] fix(44812): add outlining spans for comments inside blocks (#44847) --- src/services/outliningElementsCollector.ts | 20 +- .../fourslash/getOutliningForBlockComments.ts | 184 +++++++++++++++++- 2 files changed, 199 insertions(+), 5 deletions(-) diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 53ad23c69d0..be6755e880c 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -34,7 +34,7 @@ namespace ts.OutliningElementsCollector { if (depthRemaining === 0) return; cancellationToken.throwIfCancellationRequested(); - if (isDeclaration(n) || isVariableStatement(n) || n.kind === SyntaxKind.EndOfFileToken) { + if (isDeclaration(n) || isVariableStatement(n) || isReturnStatement(n) || n.kind === SyntaxKind.EndOfFileToken) { addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out); } @@ -42,6 +42,14 @@ namespace ts.OutliningElementsCollector { addOutliningForLeadingCommentsForNode(n.parent.left, sourceFile, cancellationToken, out); } + if (isBlock(n) || isModuleBlock(n)) { + addOutliningForLeadingCommentsForPos(n.statements.end, sourceFile, cancellationToken, out); + } + + if (isClassLike(n) || isInterfaceDeclaration(n)) { + addOutliningForLeadingCommentsForPos(n.members.end, sourceFile, cancellationToken, out); + } + const span = getOutliningSpanForNode(n, sourceFile); if (span) out.push(span); @@ -106,9 +114,10 @@ namespace ts.OutliningElementsCollector { return regionDelimiterRegExp.exec(lineText); } - function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push): void { - const comments = getLeadingCommentRangesOfNode(n, sourceFile); + function addOutliningForLeadingCommentsForPos(pos: number, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push): void { + const comments = getLeadingCommentRanges(sourceFile.text, pos); if (!comments) return; + let firstSingleLineCommentStart = -1; let lastSingleLineCommentEnd = -1; let singleLineCommentCount = 0; @@ -152,6 +161,11 @@ namespace ts.OutliningElementsCollector { } } + function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push): void { + if (isJsxText(n)) return; + addOutliningForLeadingCommentsForPos(n.pos, sourceFile, cancellationToken, out); + } + function createOutliningSpanFromBounds(pos: number, end: number, kind: OutliningSpanKind): OutliningSpan { return createOutliningSpan(createTextSpanFromBounds(pos, end), kind); } diff --git a/tests/cases/fourslash/getOutliningForBlockComments.ts b/tests/cases/fourslash/getOutliningForBlockComments.ts index 82f4648bcbb..59e11451b3b 100644 --- a/tests/cases/fourslash/getOutliningForBlockComments.ts +++ b/tests/cases/fourslash/getOutliningForBlockComments.ts @@ -136,7 +136,187 @@ //// function method(param)[| { //// }|] ////}|] +//// +////function fn1()[| { +//// [|/** +//// * comment +//// */|] +////}|] +////function fn2()[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////function fn3()[| { +//// const x = 1; +//// +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////function fn4()[| { +//// [|/** +//// * comment +//// */|] +//// const x = 1; +//// +//// [|/** +//// * comment +//// */|] +////}|] +////function fn5()[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +//// return 1; +////}|] +////function fn6()[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +//// const x = 1; +////}|] +////class C1[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////class C2[| { +//// private prop = 1; +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////class C3[| { +//// [|/** +//// * comment +//// */|] +//// +//// private prop = 1; +//// [|/** +//// * comment +//// */|] +////}|] +////class C4[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +//// private prop = 1; +////}|] +////module M1[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////module M2[| { +//// export const a = 1; +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////module M3[| { +//// [|/** +//// * comment +//// */|] +//// export const a = 1; +//// +//// [|/** +//// * comment +//// */|] +////}|] +////module M4[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +//// export const a = 1; +////}|] +////interface I1[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////interface I2[| { +//// x: number; +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] +////interface I3[| { +//// [|/** +//// * comment +//// */|] +//// x: number; +//// +//// [|/** +//// * comment +//// */|] +////}|] +////interface I4[| { +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +//// x: number; +////}|] +////[|{ +//// [|/** +//// * comment +//// */|] +//// +//// [|/** +//// * comment +//// */|] +////}|] verify.outliningSpansInCurrentFile(test.ranges()); - -