@@ -15,12 +15,23 @@ public class FileScriptPosition : IScriptPosition, IFilePosition
1515
1616 public static FileScriptPosition FromPosition ( FileContext file , int lineNumber , int columnNumber )
1717 {
18+ if ( file is null )
19+ {
20+ throw new ArgumentNullException ( nameof ( file ) ) ;
21+ }
22+
1823 int offset = 0 ;
1924 int currLine = 1 ;
2025 string fileText = file . Ast . Extent . Text ;
2126 while ( offset < fileText . Length && currLine < lineNumber )
2227 {
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+
2435 currLine ++ ;
2536 }
2637
@@ -31,6 +42,11 @@ public static FileScriptPosition FromPosition(FileContext file, int lineNumber,
3142
3243 public static FileScriptPosition FromOffset ( FileContext file , int offset )
3344 {
45+ if ( file is null )
46+ {
47+ throw new ArgumentNullException ( nameof ( file ) ) ;
48+ }
49+
3450 int line = 1 ;
3551 string fileText = file . Ast . Extent . Text ;
3652
@@ -59,7 +75,7 @@ public static FileScriptPosition FromOffset(FileContext file, int offset)
5975 internal FileScriptPosition ( FileContext file , int lineNumber , int columnNumber , int offset )
6076 {
6177 _file = file ;
62- Line = file . GetTextLines ( ) [ lineNumber - 1 ] ;
78+ Line = file ? . GetTextLines ( ) ? [ lineNumber - 1 ] ?? string . Empty ;
6379 ColumnNumber = columnNumber ;
6480 LineNumber = lineNumber ;
6581 Offset = offset ;
@@ -79,7 +95,7 @@ internal FileScriptPosition(FileContext file, int lineNumber, int columnNumber,
7995
8096 int IFilePosition . Line => LineNumber ;
8197
82- public string GetFullScript ( ) => _file . GetText ( ) ;
98+ public string GetFullScript ( ) => _file ? . GetText ( ) ?? string . Empty ;
8399 }
84100
85101 public class FileScriptExtent : IScriptExtent , IFileRange
@@ -94,6 +110,11 @@ public static bool IsEmpty(FileScriptExtent extent)
94110
95111 public static FileScriptExtent FromOffsets ( FileContext file , int startOffset , int endOffset )
96112 {
113+ if ( file is null )
114+ {
115+ throw new ArgumentNullException ( nameof ( file ) ) ;
116+ }
117+
97118 return new FileScriptExtent (
98119 file ,
99120 FileScriptPosition . FromOffset ( file , startOffset ) ,
@@ -102,6 +123,11 @@ public static FileScriptExtent FromOffsets(FileContext file, int startOffset, in
102123
103124 public static FileScriptExtent FromPositions ( FileContext file , int startLine , int startColumn , int endLine , int endColumn )
104125 {
126+ if ( file is null )
127+ {
128+ throw new ArgumentNullException ( nameof ( file ) ) ;
129+ }
130+
105131 return new FileScriptExtent (
106132 file ,
107133 FileScriptPosition . FromPosition ( file , startLine , startColumn ) ,
@@ -127,7 +153,7 @@ public FileScriptExtent(FileContext file, FileScriptPosition start, FileScriptPo
127153
128154 public IScriptPosition EndScriptPosition => _end ;
129155
130- public string File => _file . Path ;
156+ public string File => _file ? . Path ?? string . Empty ;
131157
132158 public int StartColumnNumber => _start . ColumnNumber ;
133159
@@ -137,7 +163,7 @@ public FileScriptExtent(FileContext file, FileScriptPosition start, FileScriptPo
137163
138164 public IScriptPosition StartScriptPosition => _start ;
139165
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 ;
141167
142168 IFilePosition IFileRange . Start => _start ;
143169
0 commit comments