Skip to content

Commit

Permalink
return single snippet with whole name match immediately in fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Nov 18, 2017
1 parent b224324 commit 2121005
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ func fSearchFileName(pattern string, dir string) (matched []string) {
// fSearchSnippet matches pattern to snippet name in SnippetSlice
// 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 {
for _, part := range nameCombinations(s.Name) {
Expand Down
10 changes: 10 additions & 0 deletions lib/fuzzy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func TestFSearchSnippet(t *testing.T) {
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) {
Expand Down
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
}

0 comments on commit 2121005

Please sign in to comment.