This can only occur when a Lexical.Document.Position is created with :character set to 0. There is therefore an implicit invariant that any positions intended to be sent in a response have a positive character.
This PR makes that invariant explicit by adding a guard to Position.new/3 that will raise of any line/character that is not greater-than-or-equal-to 1.
There were a few places where we were using "utility positions" with line and/or character set to 0, but as far as I can tell, replacing these cases with line/character 1 does not change any behavior.
It is likely that there are still position-related bugs in Lexical, but this should help us catch them closer to where the bug is instead of during conversion to LSP where the stacktrace is pretty useless.
There was a report in the Elixir Language Discord of an error that occurred during a completion:
This can only occur when a
Lexical.Document.Position
is created with:character
set to 0. There is therefore an implicit invariant that any positions intended to be sent in a response have a positive character.This PR makes that invariant explicit by adding a guard to
Position.new/3
that will raise of any line/character that is not greater-than-or-equal-to 1.There were a few places where we were using "utility positions" with line and/or character set to 0, but as far as I can tell, replacing these cases with line/character 1 does not change any behavior.
It is likely that there are still position-related bugs in Lexical, but this should help us catch them closer to where the bug is instead of during conversion to LSP where the stacktrace is pretty useless.