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

Respect subtasks in fuzzy search #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
58 changes: 48 additions & 10 deletions lib/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sman
import (
"github.com/renstrom/fuzzysearch/fuzzy"
"sort"
"strings"
)

// topsFromRanks iterates through fuzzy.Ranks and returns results
Expand Down Expand Up @@ -31,20 +32,57 @@ func fSearchFileName(pattern string, dir string) (matched []string) {
}

// fSearchSnippet matches pattern to snippet name in SnippetSlice
// returnes SnippetSlice of best matched snippets.
// returns SnippetSlice of best matched snippets.
func fSearchSnippet(snippets SnippetSlice, pattern string) (matched SnippetSlice) {
// special case handling if pattern == snippet name
wholeNameMatchTest := func(s Snippet) bool { return s.Name == pattern }
wholeNameMatch := snippets.FilterView(wholeNameMatchTest)
if wholeNameMatch.Len() == 1 {
return wholeNameMatch
}

topRank := -1
for _, s := range snippets {
r := fuzzy.RankMatch(pattern, s.Name)
switch {
case r == -1:
continue
case topRank == -1 || r < topRank:
matched = SnippetSlice{s}
topRank = r
case r == topRank:
matched = append(matched, s)
for _, part := range nameCombinations(s.Name) {
r := fuzzy.RankMatch(pattern, part)
switch {
case r == -1:
continue
case topRank == -1 || r < topRank:
matched = SnippetSlice{s}
topRank = r
case r == topRank:
matched = append(matched, s)
}
}
}
return matched
}

// construct name combinations using ':' as separator of name parts
// ex: 1:2:3 -> [1, 2, 3, 1:2, 2:3, 1:2:3]
func nameCombinations(pattern string) (combinations []string) {
if len(pattern) > 0 {
if strings.Contains(pattern, ":") {
singleNames := strings.Split(pattern, ":")

// single names
combinations = append(combinations, singleNames...)

// combinations with length 2 to n-1
for combLength := 2; combLength < len(singleNames); combLength++ {
for startAt := 0; startAt < len(singleNames)-combLength+1; startAt++ {
var combination []string
for idx := startAt; idx < startAt+combLength; idx++ {
combination = append(combination, singleNames[idx])
}
combinations = append(combinations, strings.Join(combination, ":"))
}
}
}

// complete name
combinations = append(combinations, pattern)
}
return
}
87 changes: 87 additions & 0 deletions lib/fuzzy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,97 @@ func TestFSearchSnippet(t *testing.T) {
Snippet{Name: "firbe"},
},
},
{"multiple matched subtasks",
SnippetSlice{
Snippet{Name: "user:add"},
Snippet{Name: "alias:add"},
Snippet{Name: "non:match"},
}, "add",
SnippetSlice{
Snippet{Name: "user:add"},
Snippet{Name: "alias:add"},
},
},
{"single matched fully qualified",
SnippetSlice{
Snippet{Name: "user:add"},
Snippet{Name: "alias:add"},
Snippet{Name: "non:match"},
}, "alias:add",
SnippetSlice{
Snippet{Name: "alias:add"},
},
},
{"single matched fuzzy fully qualified",
SnippetSlice{
Snippet{Name: "user:add"},
Snippet{Name: "alias:add"},
Snippet{Name: "non:match"},
}, "als:dd",
SnippetSlice{
Snippet{Name: "alias:add"},
},
},
{"multiple matched subtasks partly qualified",
SnippetSlice{
Snippet{Name: "mysql:user:add"},
Snippet{Name: "system:user:add"},
Snippet{Name: "alias:add"},
Snippet{Name: "non:match"},
}, "user:add",
SnippetSlice{
Snippet{Name: "mysql:user:add"},
Snippet{Name: "system:user:add"},
},
},
{"single matched fully qualified special case when whole match applies",
SnippetSlice{
Snippet{Name: "project:build"},
Snippet{Name: "project:build:full"},
Snippet{Name: "non:match"},
}, "project:build",
SnippetSlice{
Snippet{Name: "project:build"},
},
},
}
for _, tt := range tests {
if gotMatched := fSearchSnippet(tt.snippets, tt.pattern); !reflect.DeepEqual(gotMatched, tt.wantMatched) {
t.Errorf("%q. fSearchSnippet() = %#v, want %#v", tt.name, gotMatched, tt.wantMatched)
}
}
}

func TestNameCombinations(t *testing.T) {
tests := []struct {
name string
pattern string
wantCombinations []string
}{
{"empty name",
"",
[]string(nil),
},
{"single string",
"test",
[]string{"test"},
},
{"one level subtask",
"one:two",
[]string{"one", "two", "one:two"},
},
{"two level subtask",
"one:two:three",
[]string{"one", "two", "three", "one:two", "two:three", "one:two:three"},
},
{"three level subtask",
"one:two:three:four",
[]string{"one", "two", "three", "four", "one:two", "two:three", "three:four", "one:two:three", "two:three:four", "one:two:three:four"},
},
}
for _, tt := range tests {
if gotCombinations := nameCombinations(tt.pattern); !reflect.DeepEqual(gotCombinations, tt.wantCombinations) {
t.Errorf("%q. nameCombinations() = %v, want %v", tt.name, gotCombinations, tt.wantCombinations)
}
}
}
10 changes: 10 additions & 0 deletions lib/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,13 @@ func (s SnippetSlice) Less(a, b int) bool {
func (s SnippetSlice) Swap(a, b int) {
s[a], s[b] = s[b], s[a]
}

// filter input by test function and return new slice with matched snippets
func (ss SnippetSlice) FilterView(test func(Snippet) bool) (ret SnippetSlice) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}