Skip to content

Commit 90ed76f

Browse files
committed
Drastically simplified search algorithm for libs
1 parent 0e41036 commit 90ed76f

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

Diff for: commands/lib/search.go

+10-27
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ package lib
1717

1818
import (
1919
"context"
20-
"strings"
2120

2221
"github.com/arduino/arduino-cli/arduino"
2322
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2423
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
24+
"github.com/arduino/arduino-cli/arduino/utils"
2525
"github.com/arduino/arduino-cli/commands"
2626
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2727
semver "go.bug.st/relaxed-semver"
@@ -38,40 +38,23 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib
3838

3939
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) *rpc.LibrarySearchResponse {
4040
res := []*rpc.SearchedLibrary{}
41-
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
42-
43-
// Split on anything but 0-9, a-z or :
4441
queryTerms := rpc.SearchTermsFromQueryString(req.GetQuery())
4542

4643
for _, lib := range lm.Index.Libraries {
47-
matchTerm := func(x string) bool {
48-
if strings.Contains(strings.ToLower(lib.Name), x) ||
49-
strings.Contains(strings.ToLower(lib.Latest.Paragraph), x) ||
50-
strings.Contains(strings.ToLower(lib.Latest.Sentence), x) ||
51-
strings.Contains(strings.ToLower(lib.Latest.Author), x) {
52-
return true
53-
}
54-
for _, include := range lib.Latest.ProvidesIncludes {
55-
if strings.Contains(strings.ToLower(include), x) {
56-
return true
57-
}
58-
}
59-
return false
60-
}
61-
match := func() bool {
62-
for _, term := range queryTerms {
63-
if !matchTerm(term) {
64-
return false
65-
}
66-
}
67-
return true
44+
toTest := lib.Name + " " +
45+
lib.Latest.Paragraph + " " +
46+
lib.Latest.Sentence + " " +
47+
lib.Latest.Author + " "
48+
for _, include := range lib.Latest.ProvidesIncludes {
49+
toTest += include + " "
6850
}
69-
if match() {
51+
52+
if utils.Match(toTest, queryTerms) {
7053
res = append(res, indexLibraryToRPCSearchLibrary(lib))
7154
}
7255
}
7356

74-
return &rpc.LibrarySearchResponse{Libraries: res, Status: status}
57+
return &rpc.LibrarySearchResponse{Libraries: res, Status: rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS}
7558
}
7659

7760
// indexLibraryToRPCSearchLibrary converts a librariindex.Library to rpc.SearchLibrary

0 commit comments

Comments
 (0)