From 699186083b6006a14f789d842d1c01fe0bc215cb Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Fri, 29 Jun 2018 11:16:37 -0700 Subject: [PATCH 1/3] Stop including region delimiter comments in comment fold regions --- src/services/outliningElementsCollector.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 91754e2ac76..88a74e1aec6 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -63,7 +63,7 @@ namespace ts.OutliningElementsCollector { const currentLineStart = lineStarts[i]; const lineEnd = i + 1 === lineStarts.length ? sourceFile.getEnd() : lineStarts[i + 1] - 1; const lineText = sourceFile.text.substring(currentLineStart, lineEnd); - const result = lineText.match(/^\s*\/\/\s*#(end)?region(?:\s+(.*))?(?:\r)?$/); + const result = isRegionDelimiter(lineText); if (!result || isInComment(sourceFile, currentLineStart)) { continue; } @@ -83,16 +83,29 @@ namespace ts.OutliningElementsCollector { } } + function isRegionDelimiter(lineText: string) { + return lineText.match(/^\s*\/\/\s*#(end)?region(?:\s+(.*))?(?:\r)?$/); + } + function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push): void { const comments = getLeadingCommentRangesOfNode(n, sourceFile); if (!comments) return; let firstSingleLineCommentStart = -1; let lastSingleLineCommentEnd = -1; let singleLineCommentCount = 0; + const sourceText = sourceFile.getFullText(); for (const { kind, pos, end } of comments) { cancellationToken.throwIfCancellationRequested(); switch (kind) { case SyntaxKind.SingleLineCommentTrivia: + // never fold region delimiters into single-line comment regions + const commentText = sourceText.slice(pos, end); + if (isRegionDelimiter(commentText)) { + combineAndAddMultipleSingleLineComments(); + singleLineCommentCount = 0; + break; + } + // For single line comments, combine consecutive ones (2 or more) into // a single span from the start of the first till the end of the last if (singleLineCommentCount === 0) { From f0e5056063752d9a9abb3a0fcf722caded105b93 Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Fri, 29 Jun 2018 11:33:16 -0700 Subject: [PATCH 2/3] Add test --- ...OutliningSpansForRegionsNoSingleLineFolds.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/cases/fourslash/server/getOutliningSpansForRegionsNoSingleLineFolds.ts diff --git a/tests/cases/fourslash/server/getOutliningSpansForRegionsNoSingleLineFolds.ts b/tests/cases/fourslash/server/getOutliningSpansForRegionsNoSingleLineFolds.ts new file mode 100644 index 00000000000..c2ee165e919 --- /dev/null +++ b/tests/cases/fourslash/server/getOutliningSpansForRegionsNoSingleLineFolds.ts @@ -0,0 +1,17 @@ +////[|//#region +////function foo()[| { +//// +////}|] +////[|//these +//////should|] +//////#endregion not you|] +////[|// be +////// together|] +//// +////[|//#region bla bla bla +//// +////function bar()[| { }|] +//// +//////#endregion|] + +verify.outliningSpansInCurrentFile(test.ranges()); \ No newline at end of file From ba8bc559073a3f9659fdf95b177cdf7370ceb22a Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Fri, 29 Jun 2018 11:51:56 -0700 Subject: [PATCH 3/3] Make regexp a constant --- src/services/outliningElementsCollector.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 88a74e1aec6..bae6bc7222c 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -83,8 +83,9 @@ namespace ts.OutliningElementsCollector { } } + const regionDelimiterRegExp = /^\s*\/\/\s*#(end)?region(?:\s+(.*))?(?:\r)?$/; function isRegionDelimiter(lineText: string) { - return lineText.match(/^\s*\/\/\s*#(end)?region(?:\s+(.*))?(?:\r)?$/); + return regionDelimiterRegExp.exec(lineText); } function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push): void {