Skip to content

Commit 17dba76

Browse files
committed
fix: prune selections when filtering
1 parent 4fe58f3 commit 17dba76

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

internal/tui/explorer/selector.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ func (s *selector) add(n node) error {
3636
return nil
3737
}
3838

39+
func (s *selector) reindex(nodes []node) {
40+
if len(s.selections) == 0 {
41+
return
42+
}
43+
selections := make(map[resource.ID]struct{}, len(s.selections))
44+
for _, n := range nodes {
45+
id, ok := n.ID().(resource.ID)
46+
if !ok {
47+
continue
48+
}
49+
if _, ok := s.selections[id]; ok {
50+
selections[id] = struct{}{}
51+
}
52+
}
53+
s.selections = selections
54+
}
55+
3956
// addAll implements "select all" functionality with a twist: it ensures only
4057
// nodes of the same type of selected; the cursor node must match any existing
4158
// selection type, and then the nodes are filtered to only add those
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package explorer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/leg100/pug/internal/resource"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSelector_isSelected(t *testing.T) {
11+
mod1 := moduleNode{id: resource.NewID(resource.Module)}
12+
mod2 := moduleNode{id: resource.NewID(resource.Module)}
13+
14+
s := selector{selections: make(map[resource.ID]struct{})}
15+
s.add(mod1)
16+
17+
assert.True(t, s.isSelected(mod1))
18+
assert.False(t, s.isSelected(mod2))
19+
}
20+
21+
func TestSelector_reindex(t *testing.T) {
22+
mod1 := moduleNode{id: resource.NewID(resource.Module)}
23+
mod2 := moduleNode{id: resource.NewID(resource.Module)}
24+
25+
s := selector{selections: make(map[resource.ID]struct{})}
26+
s.add(mod1)
27+
s.add(mod2)
28+
29+
assert.True(t, s.isSelected(mod1))
30+
assert.True(t, s.isSelected(mod2))
31+
32+
s.reindex([]node{mod1})
33+
34+
assert.True(t, s.isSelected(mod1))
35+
assert.False(t, s.isSelected(mod2))
36+
}

internal/tui/explorer/tracker.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (t *tracker) reindex(tree *tree, height int) {
3939
t.totalWorkspaces = 0
4040
t.cursorIndex = -1
4141
t.doReindex(tree)
42+
t.selector.reindex(t.nodes)
4243

4344
// When pug first starts up, for the user's convenience we want the cursor
4445
// to be on the first module. Because modules are added asynchronously, a
@@ -74,12 +75,6 @@ func (t *tracker) doReindex(tree *tree) {
7475
if t.cursorNode != nil && t.cursorNode.ID() == tree.value.ID() {
7576
t.cursorIndex = len(t.nodes) - 1
7677
}
77-
// Track indices of selected nodes
78-
if dir, ok := tree.value.(dirNode); ok {
79-
if dir.closed {
80-
return
81-
}
82-
}
8378
for _, child := range tree.children {
8479
t.doReindex(child)
8580
}

0 commit comments

Comments
 (0)