-
Notifications
You must be signed in to change notification settings - Fork 886
Clamp LSP position conversions #3365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ package lsconv | |
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/url" | ||
| "slices" | ||
| "strings" | ||
|
|
@@ -149,19 +148,31 @@ func (c *Converters) LineAndCharacterToPosition(script Script, lineAndCharacter | |
| line := core.TextPos(lineAndCharacter.Line) | ||
| char := core.TextPos(lineAndCharacter.Character) | ||
|
|
||
| if line < 0 || int(line) >= len(lineMap.LineStarts) { | ||
| panic(fmt.Sprintf("bad line number. Line: %d, lineMap length: %d", line, len(lineMap.LineStarts))) | ||
| textLen := core.TextPos(len(script.Text())) | ||
|
|
||
| // Clamp line to valid range. | ||
| if int(line) >= len(lineMap.LineStarts) { | ||
| return textLen | ||
| } | ||
|
|
||
| start := lineMap.LineStarts[line] | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Determine the end of this line (start of next line, or end of text). | ||
| var lineEnd core.TextPos | ||
| if int(line)+1 < len(lineMap.LineStarts) { | ||
| lineEnd = lineMap.LineStarts[int(line)+1] | ||
| } else { | ||
| lineEnd = textLen | ||
| } | ||
|
|
||
| if lineMap.AsciiOnly || c.positionEncoding == lsproto.PositionEncodingKindUTF8 { | ||
| return start + char | ||
| return max(start, min(start+char, lineEnd)) | ||
| } | ||
|
|
||
| var utf8Char core.TextPos | ||
| var utf16Char core.TextPos | ||
|
|
||
| for i, r := range script.Text()[start:] { | ||
| for i, r := range script.Text()[start:lineEnd] { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was actually a preexisting bug; we were not stopping at the end of lines. Oops...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you do this you may technically break the fuzzer. 😅
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how so?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| u16Len := core.TextPos(utf16.RuneLen(r)) | ||
| if utf16Char+u16Len > char { | ||
| break | ||
|
|
@@ -176,15 +187,15 @@ func (c *Converters) LineAndCharacterToPosition(script Script, lineAndCharacter | |
| func (c *Converters) PositionToLineAndCharacter(script Script, position core.TextPos) lsproto.Position { | ||
| // UTF-8 offset to UTF-8/16 0-indexed line and character | ||
|
|
||
| position = min(position, core.TextPos(len(script.Text()))) | ||
| position = max(0, min(position, core.TextPos(len(script.Text())))) | ||
|
|
||
| lineMap := c.getLineMap(script.FileName()) | ||
|
|
||
| line, isLineStart := slices.BinarySearch(lineMap.LineStarts, position) | ||
| if !isLineStart { | ||
| line-- | ||
| } | ||
| line = max(0, line) | ||
| line = max(0, min(line, len(lineMap.LineStarts)-1)) | ||
|
|
||
| // The current line ranges from lineMap.LineStarts[line] (or 0) to lineMap.LineStarts[line+1] (or len(text)). | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we errored in debug builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have that concept anymore. Only "debug builds" that are deoptimized for better delve debugging.