@@ -16,23 +16,24 @@ import {
1616export class MockTextLine implements TextLine {
1717 readonly lineNumber : number ;
1818 readonly text : string ;
19- private eol : string ;
2019 readonly range : Range ;
2120 readonly rangeIncludingLineBreak : Range ;
2221 readonly firstNonWhitespaceCharacterIndex : number ;
2322 readonly lastNonWhitespaceCharacterIndex : number ;
2423 readonly isEmptyOrWhitespace : boolean ;
2524
26- constructor ( lineNumber : number , text : string , eol : string ) {
25+ constructor ( lineNumber : number , text : string ) {
2726 if ( lineNumber < 0 ) {
2827 throw new Error ( "lineNumber must be non-negative" ) ;
2928 }
3029 this . lineNumber = lineNumber ;
31- this . text = text ;
32- if ( eol !== "\n" && eol !== "\r\n" ) {
33- throw new Error ( "eol must be \\n or \\r\\n" ) ;
30+ // capture any trailing \r\n or \n as eol (possibly neither is present)
31+ const eol = text . match ( / ( \r ? \n ) $ / ) ?. [ 1 ] ?? "" ;
32+ if ( eol . length > 0 ) {
33+ this . text = text . slice ( 0 , - eol . length ) ;
34+ } else {
35+ this . text = text ;
3436 }
35- this . eol = eol ;
3637 this . range = new Range (
3738 this . lineNumber ,
3839 0 ,
@@ -43,7 +44,7 @@ export class MockTextLine implements TextLine {
4344 this . lineNumber ,
4445 0 ,
4546 this . lineNumber ,
46- this . text . length + this . eol . length ,
47+ this . text . length + eol . length ,
4748 ) ;
4849 const first = this . text . search ( / \S / ) ;
4950 this . firstNonWhitespaceCharacterIndex =
@@ -74,17 +75,21 @@ export class MockTextDocument implements TextDocument {
7475 throw new Error ( "InMemoryTextDocument does not support CRLF (yet?)" ) ;
7576 }
7677 this . contents = contents ;
77- const rawLines = contents . split ( "\n" ) ;
78+ const rawLines : string [ ] = contents . match ( / [ ^ \n ] * \n | [ ^ \n ] + / g ) ?? [ ] ;
7879 this . lines = rawLines . map ( ( line , i ) => {
79- return new MockTextLine ( i , line , "\n" ) ;
80+ return new MockTextLine ( i , line ) ;
8081 } ) ;
81- const lastLineRange = this . lines [ this . lines . length - 1 ] . range ;
82- this . range = new Range (
83- 0 ,
84- 0 ,
85- lastLineRange . end . line ,
86- lastLineRange . end . character ,
87- ) ;
82+ if ( this . lines . length === 0 ) {
83+ this . range = new Range ( 0 , 0 , 0 , 0 ) ;
84+ } else {
85+ const lastLineRange = this . lines [ this . lines . length - 1 ] . range ;
86+ this . range = new Range (
87+ 0 ,
88+ 0 ,
89+ lastLineRange . end . line ,
90+ lastLineRange . end . character ,
91+ ) ;
92+ }
8893 this . eol = "LF" ;
8994 }
9095
0 commit comments