fix(44812): add outlining spans for comments inside blocks (#44847)

This commit is contained in:
Oleksandr T
2021-07-31 01:56:32 +03:00
committed by GitHub
parent 76d754329e
commit fcf7bafd57
2 changed files with 199 additions and 5 deletions

View File

@@ -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<OutliningSpan>): void {
const comments = getLeadingCommentRangesOfNode(n, sourceFile);
function addOutliningForLeadingCommentsForPos(pos: number, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): 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<OutliningSpan>): 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);
}

View File

@@ -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());