@@ -16,23 +16,24 @@ import {
16
16
export class MockTextLine implements TextLine {
17
17
readonly lineNumber : number ;
18
18
readonly text : string ;
19
- private eol : string ;
20
19
readonly range : Range ;
21
20
readonly rangeIncludingLineBreak : Range ;
22
21
readonly firstNonWhitespaceCharacterIndex : number ;
23
22
readonly lastNonWhitespaceCharacterIndex : number ;
24
23
readonly isEmptyOrWhitespace : boolean ;
25
24
26
- constructor ( lineNumber : number , text : string , eol : string ) {
25
+ constructor ( lineNumber : number , text : string ) {
27
26
if ( lineNumber < 0 ) {
28
27
throw new Error ( "lineNumber must be non-negative" ) ;
29
28
}
30
29
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 ;
34
36
}
35
- this . eol = eol ;
36
37
this . range = new Range (
37
38
this . lineNumber ,
38
39
0 ,
@@ -43,7 +44,7 @@ export class MockTextLine implements TextLine {
43
44
this . lineNumber ,
44
45
0 ,
45
46
this . lineNumber ,
46
- this . text . length + this . eol . length ,
47
+ this . text . length + eol . length ,
47
48
) ;
48
49
const first = this . text . search ( / \S / ) ;
49
50
this . firstNonWhitespaceCharacterIndex =
@@ -74,17 +75,21 @@ export class MockTextDocument implements TextDocument {
74
75
throw new Error ( "InMemoryTextDocument does not support CRLF (yet?)" ) ;
75
76
}
76
77
this . contents = contents ;
77
- const rawLines = contents . split ( "\n" ) ;
78
+ const rawLines : string [ ] = contents . match ( / [ ^ \n ] * \n | [ ^ \n ] + / g ) ?? [ ] ;
78
79
this . lines = rawLines . map ( ( line , i ) => {
79
- return new MockTextLine ( i , line , "\n" ) ;
80
+ return new MockTextLine ( i , line ) ;
80
81
} ) ;
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
+ }
88
93
this . eol = "LF" ;
89
94
}
90
95
0 commit comments