Skip to content

Commit 599e36a

Browse files
authored
Decrement line ends if they end with a carriage return. (microsoft#31220)
* Decrement line ends if they end with a carriage return. * Changed handling of newlines and inlined regex operation. * fixed misname of hintSpan * added tests * revert inline of regex match and use getLineEndOfPosition * fixed lint error and changed a silly thing in tests
1 parent 772bee5 commit 599e36a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/services/outliningElementsCollector.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ namespace ts.OutliningElementsCollector {
7171
function addRegionOutliningSpans(sourceFile: SourceFile, out: Push<OutliningSpan>): void {
7272
const regions: OutliningSpan[] = [];
7373
const lineStarts = sourceFile.getLineStarts();
74-
for (let i = 0; i < lineStarts.length; i++) {
75-
const currentLineStart = lineStarts[i];
76-
const lineEnd = i + 1 === lineStarts.length ? sourceFile.getEnd() : lineStarts[i + 1] - 1;
74+
for (const currentLineStart of lineStarts) {
75+
const lineEnd = sourceFile.getLineEndOfPosition(currentLineStart);
7776
const lineText = sourceFile.text.substring(currentLineStart, lineEnd);
7877
const result = isRegionDelimiter(lineText);
7978
if (!result || isInComment(sourceFile, currentLineStart)) {

src/testRunner/unittests/services/hostNewLineSupport.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,34 @@ namespace ts {
3838
verifyNewLines(content, { newLine: NewLineKind.LineFeed });
3939
}
4040

41+
function verifyOutliningSpanNewLines(content: string, options: CompilerOptions) {
42+
const ls = testLSWithFiles(options, [{
43+
content,
44+
fileOptions: {},
45+
unitName: "input.ts"
46+
}]);
47+
const span = ls.getOutliningSpans("input.ts")[0];
48+
const textAfterSpanCollapse = content.substring(span.textSpan.start + span.textSpan.length);
49+
assert(textAfterSpanCollapse.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /\r\n/ : /[^\r]\n/), "expected to find appropriate newlines");
50+
assert(!textAfterSpanCollapse.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /[^\r]\n/ : /\r\n/), "expected not to find inappropriate newlines");
51+
}
52+
4153
it("should exist and respect provided compiler options", () => {
4254
verifyBothNewLines(`
4355
function foo() {
4456
return 2 + 2;
4557
}
4658
`);
4759
});
60+
61+
it("should respect CRLF line endings around outlining spans", () => {
62+
verifyOutliningSpanNewLines("// comment not included\r\n// #region name\r\nlet x: string = \"x\";\r\n// #endregion name\r\n",
63+
{ newLine: NewLineKind.CarriageReturnLineFeed });
64+
});
65+
66+
it("should respect LF line endings around outlining spans", () => {
67+
verifyOutliningSpanNewLines("// comment not included\n// #region name\nlet x: string = \"x\";\n// #endregion name\n\n",
68+
{ newLine: NewLineKind.LineFeed });
69+
});
4870
});
4971
}

0 commit comments

Comments
 (0)