@@ -22,9 +22,9 @@ import (
22
22
23
23
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
24
24
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
25
+ "github.com/arduino/arduino-cli/arduino/utils"
25
26
"github.com/arduino/arduino-cli/commands"
26
27
rpc "github.com/arduino/arduino-cli/rpc/commands"
27
- "github.com/lithammer/fuzzysearch/fuzzy"
28
28
semver "go.bug.st/relaxed-semver"
29
29
)
30
30
@@ -43,44 +43,33 @@ func searchLibrary(req *rpc.LibrarySearchReq, lm *librariesmanager.LibrariesMana
43
43
res := []* rpc.SearchedLibrary {}
44
44
status := rpc .LibrarySearchStatus_success
45
45
46
- // If the query is empty all libraries are returned
47
- if strings .Trim (query , " " ) == "" {
48
- for _ , lib := range lm .Index .Libraries {
49
- res = append (res , indexLibraryToRPCSearchLibrary (lib ))
46
+ searchArgs := strings .Split (strings .Trim (query , " " ), " " )
47
+
48
+ match := func (toTest []string ) (bool , error ) {
49
+ if len (searchArgs ) == 0 {
50
+ return true , nil
50
51
}
51
- return & rpc.LibrarySearchResp {Libraries : res , Status : status }, nil
52
- }
53
52
54
- // maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
55
- // This value is completely arbitrary and picked randomly.
56
- maximumSearchDistance := 150
57
- // Use a lower distance for shorter query or the user might be flooded with unrelated results
58
- if len (query ) <= 4 {
59
- maximumSearchDistance = 40
53
+ for _ , t := range toTest {
54
+ matches , err := utils .Match (t , searchArgs )
55
+ if err != nil {
56
+ return false , err
57
+ }
58
+ if matches {
59
+ return matches , nil
60
+ }
61
+ }
62
+ return false , nil
60
63
}
61
64
62
- // Removes some chars from query strings to enhance results
63
- cleanQuery := strings .Map (func (r rune ) rune {
64
- switch r {
65
- case '_' :
66
- case '-' :
67
- case ' ' :
68
- return - 1
69
- }
70
- return r
71
- }, query )
72
65
for _ , lib := range lm .Index .Libraries {
73
- // Use both uncleaned and cleaned query
74
- for _ , q := range []string {query , cleanQuery } {
75
- toTest := []string {lib .Name , lib .Latest .Paragraph , lib .Latest .Sentence }
76
- for _ , rank := range fuzzy .RankFindNormalizedFold (q , toTest ) {
77
- if rank .Distance < maximumSearchDistance {
78
- res = append (res , indexLibraryToRPCSearchLibrary (lib ))
79
- goto nextLib
80
- }
81
- }
66
+ toTest := []string {lib .Name , lib .Latest .Paragraph , lib .Latest .Sentence }
67
+ if ok , err := match (toTest ); err != nil {
68
+ return nil , err
69
+ } else if ! ok {
70
+ continue
82
71
}
83
- nextLib:
72
+ res = append ( res , indexLibraryToRPCSearchLibrary ( lib ))
84
73
}
85
74
86
75
return & rpc.LibrarySearchResp {Libraries : res , Status : status }, nil
0 commit comments