Skip to content
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

FEAT: Improve behavior of search / function on tree #124

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
15 changes: 10 additions & 5 deletions components/Tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (tree *Tree) databasesToNodes(children map[string][]string, node *tview.Tre

func (tree *Tree) search(searchText string) {
rootNode := tree.GetRoot()
currNode := tree.GetCurrentNode()
lowerSearchText := strings.ToLower(searchText)
tree.state.searchFoundNodes = []*tview.TreeNode{}

Expand All @@ -339,15 +340,19 @@ func (tree *Tree) search(searchText string) {
return
}

// filteredNodes := make([]*TreeStateNode, 0, len(treeNodes))
var searchStartNode *tview.TreeNode
if currNode != nil && currNode.IsExpanded() {
// search between tables if theres a curentdatabase selected
searchStartNode = currNode
} else {
// if there's no current database selected, search within the database names only
searchStartNode = rootNode
}
Comment on lines +343 to +350
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var searchStartNode *tview.TreeNode
if currNode != nil && currNode.IsExpanded() {
// search between tables if theres a curentdatabase selected
searchStartNode = currNode
} else {
// if there's no current database selected, search within the database names only
searchStartNode = rootNode
}
// search within the database names only by default
searchStartNode := rootNode
if currNode != nil && currNode.IsExpanded() {
// search between tables if there is a database selected
searchStartNode = currNode
}


rootNode.Walk(func(node, parent *tview.TreeNode) bool {
searchStartNode.Walk(func(node, parent *tview.TreeNode) bool {
nodeText := strings.ToLower(node.GetText())

if strings.Contains(nodeText, lowerSearchText) {
if parent != nil {
parent.SetExpanded(true)
}
tree.state.searchFoundNodes = append(tree.state.searchFoundNodes, node)
tree.SetCurrentNode(node)
tree.state.currentFocusFoundNode = node
Expand Down