@@ -15,12 +15,23 @@ public class FileScriptPosition : IScriptPosition, IFilePosition
15
15
16
16
public static FileScriptPosition FromPosition ( FileContext file , int lineNumber , int columnNumber )
17
17
{
18
+ if ( file is null )
19
+ {
20
+ throw new ArgumentNullException ( nameof ( file ) ) ;
21
+ }
22
+
18
23
int offset = 0 ;
19
24
int currLine = 1 ;
20
25
string fileText = file . Ast . Extent . Text ;
21
26
while ( offset < fileText . Length && currLine < lineNumber )
22
27
{
23
- offset = fileText . IndexOf ( '\n ' , offset ) ;
28
+ offset = fileText . IndexOf ( '\n ' , offset ) + 1 ;
29
+ if ( offset is 0 )
30
+ {
31
+ // Line and column passed were not valid and the offset can not be determined.
32
+ return new FileScriptPosition ( file , lineNumber , columnNumber , offset ) ;
33
+ }
34
+
24
35
currLine ++ ;
25
36
}
26
37
@@ -31,6 +42,11 @@ public static FileScriptPosition FromPosition(FileContext file, int lineNumber,
31
42
32
43
public static FileScriptPosition FromOffset ( FileContext file , int offset )
33
44
{
45
+ if ( file is null )
46
+ {
47
+ throw new ArgumentNullException ( nameof ( file ) ) ;
48
+ }
49
+
34
50
int line = 1 ;
35
51
string fileText = file . Ast . Extent . Text ;
36
52
@@ -59,7 +75,7 @@ public static FileScriptPosition FromOffset(FileContext file, int offset)
59
75
internal FileScriptPosition ( FileContext file , int lineNumber , int columnNumber , int offset )
60
76
{
61
77
_file = file ;
62
- Line = file . GetTextLines ( ) [ lineNumber - 1 ] ;
78
+ Line = file ? . GetTextLines ( ) ? [ lineNumber - 1 ] ?? string . Empty ;
63
79
ColumnNumber = columnNumber ;
64
80
LineNumber = lineNumber ;
65
81
Offset = offset ;
@@ -79,7 +95,7 @@ internal FileScriptPosition(FileContext file, int lineNumber, int columnNumber,
79
95
80
96
int IFilePosition . Line => LineNumber ;
81
97
82
- public string GetFullScript ( ) => _file . GetText ( ) ;
98
+ public string GetFullScript ( ) => _file ? . GetText ( ) ?? string . Empty ;
83
99
}
84
100
85
101
public class FileScriptExtent : IScriptExtent , IFileRange
@@ -94,6 +110,11 @@ public static bool IsEmpty(FileScriptExtent extent)
94
110
95
111
public static FileScriptExtent FromOffsets ( FileContext file , int startOffset , int endOffset )
96
112
{
113
+ if ( file is null )
114
+ {
115
+ throw new ArgumentNullException ( nameof ( file ) ) ;
116
+ }
117
+
97
118
return new FileScriptExtent (
98
119
file ,
99
120
FileScriptPosition . FromOffset ( file , startOffset ) ,
@@ -102,6 +123,11 @@ public static FileScriptExtent FromOffsets(FileContext file, int startOffset, in
102
123
103
124
public static FileScriptExtent FromPositions ( FileContext file , int startLine , int startColumn , int endLine , int endColumn )
104
125
{
126
+ if ( file is null )
127
+ {
128
+ throw new ArgumentNullException ( nameof ( file ) ) ;
129
+ }
130
+
105
131
return new FileScriptExtent (
106
132
file ,
107
133
FileScriptPosition . FromPosition ( file , startLine , startColumn ) ,
@@ -127,7 +153,7 @@ public FileScriptExtent(FileContext file, FileScriptPosition start, FileScriptPo
127
153
128
154
public IScriptPosition EndScriptPosition => _end ;
129
155
130
- public string File => _file . Path ;
156
+ public string File => _file ? . Path ?? string . Empty ;
131
157
132
158
public int StartColumnNumber => _start . ColumnNumber ;
133
159
@@ -137,7 +163,7 @@ public FileScriptExtent(FileContext file, FileScriptPosition start, FileScriptPo
137
163
138
164
public IScriptPosition StartScriptPosition => _start ;
139
165
140
- public string Text => _file . GetText ( ) . Substring ( _start . Offset , _end . Offset - _start . Offset ) ;
166
+ public string Text => _file ? . GetText ( ) ? . Substring ( _start . Offset , _end . Offset - _start . Offset ) ?? string . Empty ;
141
167
142
168
IFilePosition IFileRange . Start => _start ;
143
169
0 commit comments