Decrement line ends if they end with a carriage return. (#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
This commit is contained in:
Jesse Trinity 2019-07-25 21:29:12 -07:00 committed by GitHub
parent 772bee5e84
commit 599e36a068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -71,9 +71,8 @@ namespace ts.OutliningElementsCollector {
function addRegionOutliningSpans(sourceFile: SourceFile, out: Push<OutliningSpan>): void {
const regions: OutliningSpan[] = [];
const lineStarts = sourceFile.getLineStarts();
for (let i = 0; i < lineStarts.length; i++) {
const currentLineStart = lineStarts[i];
const lineEnd = i + 1 === lineStarts.length ? sourceFile.getEnd() : lineStarts[i + 1] - 1;
for (const currentLineStart of lineStarts) {
const lineEnd = sourceFile.getLineEndOfPosition(currentLineStart);
const lineText = sourceFile.text.substring(currentLineStart, lineEnd);
const result = isRegionDelimiter(lineText);
if (!result || isInComment(sourceFile, currentLineStart)) {

View File

@ -38,6 +38,18 @@ namespace ts {
verifyNewLines(content, { newLine: NewLineKind.LineFeed });
}
function verifyOutliningSpanNewLines(content: string, options: CompilerOptions) {
const ls = testLSWithFiles(options, [{
content,
fileOptions: {},
unitName: "input.ts"
}]);
const span = ls.getOutliningSpans("input.ts")[0];
const textAfterSpanCollapse = content.substring(span.textSpan.start + span.textSpan.length);
assert(textAfterSpanCollapse.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /\r\n/ : /[^\r]\n/), "expected to find appropriate newlines");
assert(!textAfterSpanCollapse.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /[^\r]\n/ : /\r\n/), "expected not to find inappropriate newlines");
}
it("should exist and respect provided compiler options", () => {
verifyBothNewLines(`
function foo() {
@ -45,5 +57,15 @@ namespace ts {
}
`);
});
it("should respect CRLF line endings around outlining spans", () => {
verifyOutliningSpanNewLines("// comment not included\r\n// #region name\r\nlet x: string = \"x\";\r\n// #endregion name\r\n",
{ newLine: NewLineKind.CarriageReturnLineFeed });
});
it("should respect LF line endings around outlining spans", () => {
verifyOutliningSpanNewLines("// comment not included\n// #region name\nlet x: string = \"x\";\n// #endregion name\n\n",
{ newLine: NewLineKind.LineFeed });
});
});
}