diff --git a/CHANGELOG.md b/CHANGELOG.md index a9f60a93ad..f1b7e8126f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497 - Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521 +- Fix `index out of bounds` exception thrown in rare cases by `rescript-editor-analysis.exe codeAction` command. https://github.com/rescript-lang/rescript/pull/7523 #### :nail_care: Polish diff --git a/analysis/src/CompletionFrontEnd.ml b/analysis/src/CompletionFrontEnd.ml index 2c2b49380e..9493aa8490 100644 --- a/analysis/src/CompletionFrontEnd.ml +++ b/analysis/src/CompletionFrontEnd.ml @@ -373,7 +373,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor in let posOfDot = Pos.posOfDot text ~pos:posCursor ~offset in let charAtCursor = - if offset < String.length text then text.[offset] else '\n' + if offset >= 0 && offset < String.length text then text.[offset] else '\n' in let posBeforeCursor = Pos.posBeforeCursor posCursor in let charBeforeCursor, blankAfterCursor = @@ -869,7 +869,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor match (Pos.positionToOffset text posStart, Pos.positionToOffset text posEnd) with - | Some offsetStart, Some offsetEnd -> + | Some offsetStart, Some offsetEnd + when offsetStart >= 0 && offsetEnd >= offsetStart -> (* Can't trust the parser's location E.g. @foo. let x... gives as label @foo.let *) let label = diff --git a/analysis/src/Pos.ml b/analysis/src/Pos.ml index e20af11e59..d739c9dae1 100644 --- a/analysis/src/Pos.ml +++ b/analysis/src/Pos.ml @@ -22,8 +22,8 @@ let positionToOffset text (line, character) = match offsetOfLine text line with | None -> None | Some bol -> - if bol + character <= String.length text then Some (bol + character) - else None + let offset = bol + character in + if offset >= 0 && offset <= String.length text then Some offset else None let posBeforeCursor pos = (fst pos, max 0 (snd pos - 1))