diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 3bd53f86301..e79175b4b24 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -12,6 +12,7 @@ namespace ts.OutliningElementsCollector { const regionEnd = new RegExp("// #endregion *"); walk(sourceFile); + gatherRegions(); return elements; /** If useFullStart is true, then the collapsing span includes leading whitespace, including linebreaks. */ @@ -127,7 +128,10 @@ namespace ts.OutliningElementsCollector { } function addRegionsNearNode(n: Node) { - const comments = ts.getLeadingCommentRangesOfNode(n, sourceFile); + const precedingToken = ts.findPrecedingToken(n.pos, sourceFile); + const trailingComments = precedingToken && ts.getTrailingCommentRanges(sourceFile.text, precedingToken.end); + const leadingComments = ts.getLeadingCommentRangesOfNode(n, sourceFile); + const comments = concatenate(trailingComments, leadingComments); if (n.kind !== SyntaxKind.SourceFile && comments) { for (const currentComment of comments) { @@ -148,7 +152,7 @@ namespace ts.OutliningElementsCollector { if (region) { region.end = currentComment.end; - addOutliningSpanRegions(region); + // addOutliningSpanRegions(region); } } } @@ -156,6 +160,53 @@ namespace ts.OutliningElementsCollector { } } + function isRegionStartBoundaries(start: number, end: number) { + const comment = sourceFile.text.substring(start, end); + const result = comment.match(regionStart); + + if (result && result.length > 0) { + const name = result[0].substring(10).trim(); + if (name) { + return name; + } + else { + return regionText; + } + } + return ""; + } + + function isRegionEndBoundaries(start: number, end: number) { + const comment = sourceFile.text.substring(start, end); + return comment.match(regionEnd); + } + + function gatherRegions(): void { + const lineStarts = sourceFile.getLineStarts(); + + for (const currentLineStart of lineStarts) { + const lineEnd = sourceFile.getLineEndOfPosition(currentLineStart); + + const name = isRegionStartBoundaries(currentLineStart, lineEnd); + if (name) { + const region: RegionRange = { + pos: currentLineStart, + end: lineEnd, + name, + }; + regions.push(region); + } + else if (isRegionEndBoundaries(currentLineStart, lineEnd)) { + const region = regions.pop(); + + if (region) { + region.end = lineEnd; + addOutliningSpanRegions(region); + } + } + } + } + function walk(n: Node): void { cancellationToken.throwIfCancellationRequested(); if (depth > maxDepth) {