@@ -34,10 +34,18 @@ func (c *converters) fromLspRange(textRange lsproto.Range, fileName string) (cor
34
34
if scriptInfo == nil {
35
35
return core.TextRange {}, fmt .Errorf ("no script info found for %s" , fileName )
36
36
}
37
- return core .NewTextRange (
38
- lineAndCharacterToPosition (textRange .Start , scriptInfo .LineMap ()),
39
- lineAndCharacterToPosition (textRange .End , scriptInfo .LineMap ()),
40
- ), nil
37
+
38
+ startPos , err := lineAndCharacterToPosition (textRange .Start , scriptInfo .LineMap ())
39
+ if err != nil {
40
+ return core.TextRange {}, fmt .Errorf ("error converting start position: %w" , err )
41
+ }
42
+
43
+ endPos , err := lineAndCharacterToPosition (textRange .End , scriptInfo .LineMap ())
44
+ if err != nil {
45
+ return core.TextRange {}, fmt .Errorf ("error converting end position: %w" , err )
46
+ }
47
+
48
+ return core .NewTextRange (startPos , endPos ), nil
41
49
}
42
50
43
51
func (c * converters ) fromLspTextChange (change * lsproto.TextDocumentContentChangePartial , fileName string ) (ls.TextChange , error ) {
@@ -124,7 +132,7 @@ func (c *converters) lineAndCharacterToPosition(lineAndCharacter lsproto.Positio
124
132
if scriptInfo == nil {
125
133
return 0 , fmt .Errorf ("no script info found for %s" , fileName )
126
134
}
127
- return lineAndCharacterToPosition (lineAndCharacter , scriptInfo .LineMap ()), nil
135
+ return lineAndCharacterToPosition (lineAndCharacter , scriptInfo .LineMap ())
128
136
}
129
137
130
138
func languageKindToScriptKind (languageID lsproto.LanguageKind ) core.ScriptKind {
@@ -188,19 +196,20 @@ func fileNameToDocumentUri(fileName string) lsproto.DocumentUri {
188
196
return lsproto .DocumentUri ("file://" + fileName )
189
197
}
190
198
191
- func lineAndCharacterToPosition (lineAndCharacter lsproto.Position , lineMap []core.TextPos ) int {
199
+ func lineAndCharacterToPosition (lineAndCharacter lsproto.Position , lineMap []core.TextPos ) ( int , error ) {
192
200
line := int (lineAndCharacter .Line )
193
201
offset := int (lineAndCharacter .Character )
194
202
195
203
if line < 0 || line >= len (lineMap ) {
196
- panic ( fmt .Sprintf ("bad line number. Line: %d, lineMap length: %d" , line , len (lineMap ) ))
204
+ return 0 , fmt .Errorf ("bad line number. Line: %d, lineMap length: %d" , line , len (lineMap ))
197
205
}
198
206
199
207
res := int (lineMap [line ]) + offset
200
208
if line < len (lineMap )- 1 && res >= int (lineMap [line + 1 ]) {
201
- panic ("resulting position is out of bounds" )
209
+ return 0 , fmt . Errorf ("resulting position is out of bounds" )
202
210
}
203
- return res
211
+
212
+ return res , nil
204
213
}
205
214
206
215
func positionToLineAndCharacter (position int , lineMap []core.TextPos ) lsproto.Position {
0 commit comments