Skip to content

Commit

Permalink
fix: improve rope_test
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 2, 2024
1 parent 84d9154 commit 8cfd287
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/types/rope.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ func (r *Rope) OffsetFromPosition(position lsp.Position) int {

// Ensure character is within valid range
if position.Character >= uint32(len(line)) {
position.Character = uint32(len(line) - 1)
position.Character = uint32(len(line))
}

// Calculate byte offset
offset := 0
for i := 0; i < int(position.Line); i++ {
offset += len(lines[i]) + 1 // Add 1 for the newline character
}

offset += int(position.Character)

return offset
Expand Down
14 changes: 13 additions & 1 deletion server/types/rope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func TestOffsetFromPosition(t *testing.T) {
if offset != expected {
t.Errorf("Expected offset: %d, Got offset: %d", expected, offset)
}

if r.text[:offset] != "Hello, World!\nThis i" {
t.Errorf("Expected text: %s, Got text: %s", "Hello, World!\nThis ", r.text[:offset])
}
})

t.Run("Position Exceeds Line Length", func(t *testing.T) {
Expand All @@ -91,10 +95,14 @@ func TestOffsetFromPosition(t *testing.T) {

offset := r.OffsetFromPosition(position)

expected := 28 // Maximum offset is 28 (end of text)
expected := 29 // Maximum offset is 29 (end of text)
if offset != expected {
t.Errorf("Expected offset: %d, Got offset: %d", expected, offset)
}

if r.text[:offset] != "Hello, World!\nThis is a test." {
t.Errorf("Expected text: %s, Got text: %s", "Hello, World!\nThis is a test.", r.text[:offset])
}
})

t.Run("Position Exceeds Line Count", func(t *testing.T) {
Expand All @@ -107,5 +115,9 @@ func TestOffsetFromPosition(t *testing.T) {
if offset != expected {
t.Errorf("Expected offset: %d, Got offset: %d", expected, offset)
}

if r.text[:offset] != "Hello, World!\nThis i" {
t.Errorf("Expected text: %s, Got text: %s", "Hello, World!\nThis i", r.text[:offset])
}
})
}

0 comments on commit 8cfd287

Please sign in to comment.