-
Notifications
You must be signed in to change notification settings - Fork 681
Fix panic in LSP formatting with multi-byte characters and trailing newlines #1391
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
Draft
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/fix-1389
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0d3f5e
Initial plan
Copilot 41c533d
Fix panic in LSP formatting with multi-byte characters and trailing nβ¦
Copilot 1956a59
Add comprehensive test coverage for issue #1389 fix
Copilot 0f6e9fc
Remove compiler tests - not needed for editor-related PRs
Copilot 444d174
Remove redundant issue1389_test.go file
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ls | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
) | ||
|
||
type mockScript struct { | ||
fileName string | ||
text string | ||
} | ||
|
||
func (m *mockScript) FileName() string { | ||
return m.fileName | ||
} | ||
|
||
func (m *mockScript) Text() string { | ||
return m.text | ||
} | ||
|
||
func TestPositionToLineAndCharacterBoundsCheck(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Test case that reproduces the panic with multi-byte characters and trailing newlines | ||
text := "\"β\" ;\n\n\n" | ||
|
||
lineMap := ComputeLineStarts(text) | ||
|
||
converters := NewConverters(lsproto.PositionEncodingKindUTF16, func(fileName string) *LineMap { | ||
return lineMap | ||
}) | ||
|
||
script := &mockScript{ | ||
fileName: "test.ts", | ||
text: text, | ||
} | ||
|
||
// This should not panic even if position is beyond text length | ||
textLen := len(text) | ||
position := core.TextPos(textLen) // position at end of text | ||
|
||
// This should work | ||
result := converters.PositionToLineAndCharacter(script, position) | ||
|
||
t.Logf("Text length: %d, Position: %d", textLen, position) | ||
t.Logf("Result: line=%d, char=%d", result.Line, result.Character) | ||
|
||
// This should also not panic, even though position is beyond text length | ||
beyondEndPosition := core.TextPos(textLen + 1) | ||
result2 := converters.PositionToLineAndCharacter(script, beyondEndPosition) | ||
|
||
t.Logf("Beyond end position: %d", beyondEndPosition) | ||
t.Logf("Result2: line=%d, char=%d", result2.Line, result2.Character) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ls | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
) | ||
|
||
// TestIssue1389Fix tests the specific scenario from issue #1389 | ||
func TestIssue1389Fix(t *testing.T) { | ||
t.Parallel() | ||
|
||
// This is the exact content from the issue report | ||
content := "\"β\" ;\n\n\n" | ||
|
||
lineMap := ComputeLineStarts(content) | ||
|
||
converters := NewConverters(lsproto.PositionEncodingKindUTF16, func(fileName string) *LineMap { | ||
return lineMap | ||
}) | ||
|
||
script := &mockScript{ | ||
fileName: "test.ts", | ||
text: content, | ||
} | ||
|
||
// Test the exact scenario that was causing the panic | ||
textLen := len(content) | ||
|
||
// These should not panic | ||
result1 := converters.PositionToLineAndCharacter(script, core.TextPos(textLen)) | ||
t.Logf("Position at end of text (%d): line=%d, char=%d", textLen, result1.Line, result1.Character) | ||
|
||
result2 := converters.PositionToLineAndCharacter(script, core.TextPos(textLen+1)) | ||
t.Logf("Position beyond end of text (%d): line=%d, char=%d", textLen+1, result2.Line, result2.Character) | ||
|
||
// Also test converting ranges, which is what the formatter does | ||
textRange := core.NewTextRange(0, textLen) | ||
lspRange := converters.ToLSPRange(script, textRange) | ||
|
||
t.Logf("Range conversion successful: start=(%d,%d), end=(%d,%d)", | ||
lspRange.Start.Line, lspRange.Start.Character, | ||
lspRange.End.Line, lspRange.End.Character) | ||
|
||
// Test with a range that extends beyond the text (this was the original issue) | ||
textRangeBeyondEnd := core.NewTextRange(0, textLen+1) | ||
lspRangeBeyondEnd := converters.ToLSPRange(script, textRangeBeyondEnd) | ||
|
||
t.Logf("Range beyond end conversion successful: start=(%d,%d), end=(%d,%d)", | ||
lspRangeBeyondEnd.Start.Line, lspRangeBeyondEnd.Start.Character, | ||
lspRangeBeyondEnd.End.Line, lspRangeBeyondEnd.End.Character) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.