Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,19 @@ func (m *Model) InsertItem(index int, item Item) tea.Cmd {
func (m *Model) RemoveItem(index int) {
m.items = removeItemFromSlice(m.items, index)
if m.filterState != Unfiltered {
m.filteredItems = removeFilterMatchFromSlice(m.filteredItems, index)
// Rebuild filtered list: drop the entry matching the removed global
// index and shift down all subsequent global indices by one.
filtered := m.filteredItems[:0]
for _, fi := range m.filteredItems {
if fi.index == index {
continue
}
if fi.index > index {
fi.index--
}
filtered = append(filtered, fi)
}
m.filteredItems = filtered
if len(m.filteredItems) == 0 {
m.resetFiltering()
}
Expand Down Expand Up @@ -1300,15 +1312,6 @@ func removeItemFromSlice(i []Item, index int) []Item {
return i[:len(i)-1]
}

func removeFilterMatchFromSlice(i []filteredItem, index int) []filteredItem {
if index >= len(i) {
return i // noop
}
copy(i[index:], i[index+1:])
i[len(i)-1] = filteredItem{}
return i[:len(i)-1]
}

func countEnabledBindings(groups [][]key.Binding) (agg int) {
for _, group := range groups {
for _, kb := range group {
Expand Down
23 changes: 23 additions & 0 deletions list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ func TestSetFilterText(t *testing.T) {
}
}

func TestRemoveItemWithFilter(t *testing.T) {
items := []Item{item("foo"), item("bar"), item("baz")}
l := New(items, itemDelegate{}, 10, 10)
l.SetFilterText("ba")
l.SetFilterState(FilterApplied)

// filtered list contains ["bar", "baz"] at global indices 1 and 2.
// Remove "foo" (global index 0) — filtered list should be unchanged.
l.RemoveItem(0)
visible := l.VisibleItems()
if !reflect.DeepEqual(visible, []Item{item("bar"), item("baz")}) {
t.Fatalf("after removing unfiltered item: got %v, want [bar baz]", visible)
}

// The global list is now ["bar", "baz"]. Remove "bar" (global index 0).
// Filtered list should shrink to ["baz"].
l.RemoveItem(0)
visible = l.VisibleItems()
if !reflect.DeepEqual(visible, []Item{item("baz")}) {
t.Fatalf("after removing filtered item: got %v, want [baz]", visible)
}
}

func TestSetFilterState(t *testing.T) {
tc := []Item{item("foo"), item("bar"), item("baz")}

Expand Down