Skip to content

Commit 1021e76

Browse files
authoredOct 4, 2022
Made core search results more similar to Arduino IDE 1.8.x search dialog (#1904)
* Factored function to get search terms * Made 'core search' logic identical to Arduino IDE 1.8.x search dialog * Drastically simplified search algorithm for libs * Fixed SearchTermsFromQueryString function * Moving SearchTermsFromQueryString into proper place
1 parent ce7b062 commit 1021e76

File tree

3 files changed

+38
-51
lines changed

3 files changed

+38
-51
lines changed
 

‎arduino/utils/search.go

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func removeDiatrics(s string) (string, error) {
4343
return s, nil
4444
}
4545

46+
// SearchTermsFromQueryString returns the terms inside the query string.
47+
// All non alphanumeric characters (expect ':') are considered separators.
48+
// All search terms are converted to lowercase.
49+
func SearchTermsFromQueryString(query string) []string {
50+
// Split on anything but 0-9, a-z or :
51+
return strings.FieldsFunc(strings.ToLower(query), func(r rune) bool {
52+
return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != ':'
53+
})
54+
}
55+
4656
// Match returns true if all substrings are contained in str.
4757
// Both str and substrings are transforms to lower case and have their
4858
// accents and other unicode diatrics removed.

‎commands/core/search.go

+17-21
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,54 @@ import (
2929

3030
// PlatformSearch FIXMEDOC
3131
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
32-
searchArgs := strings.TrimSpace(req.SearchArgs)
33-
allVersions := req.AllVersions
3432
pme, release := commands.GetPackageManagerExplorer(req)
3533
if pme == nil {
3634
return nil, &arduino.InvalidInstanceError{}
3735
}
3836
defer release()
3937

4038
res := []*cores.PlatformRelease{}
41-
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", searchArgs); isUsb {
42-
vid, pid := searchArgs[:4], searchArgs[5:]
39+
if isUsb, _ := regexp.MatchString("[0-9a-f]{4}:[0-9a-f]{4}", req.SearchArgs); isUsb {
40+
vid, pid := req.SearchArgs[:4], req.SearchArgs[5:]
4341
res = pme.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
4442
} else {
45-
43+
searchArgs := utils.SearchTermsFromQueryString(req.SearchArgs)
44+
allVersions := req.AllVersions
4645
for _, targetPackage := range pme.GetPackages() {
4746
for _, platform := range targetPackage.Platforms {
48-
// discard invalid platforms
4947
// Users can install platforms manually in the Sketchbook hardware folder,
5048
// the core search command must operate only on platforms installed through
5149
// the PlatformManager, thus we skip the manually installed ones.
5250
if platform == nil || platform.Name == "" || platform.ManuallyInstalled {
5351
continue
5452
}
5553

56-
// discard invalid releases
57-
platformRelease := platform.GetLatestRelease()
58-
if platformRelease == nil {
54+
// Discard platforms with no releases
55+
latestRelease := platform.GetLatestRelease()
56+
if latestRelease == nil {
5957
continue
6058
}
6159

6260
// Gather all strings that can be used for searching
63-
toTest := []string{
64-
platform.String(),
65-
platform.Name,
66-
platform.Architecture,
67-
targetPackage.Name,
68-
targetPackage.Maintainer,
69-
targetPackage.WebsiteURL,
70-
}
71-
for _, board := range platformRelease.BoardsManifest {
72-
toTest = append(toTest, board.Name)
61+
toTest := platform.String() + " " +
62+
platform.Name + " " +
63+
platform.Architecture + " " +
64+
targetPackage.Name + " " +
65+
targetPackage.Maintainer + " " +
66+
targetPackage.WebsiteURL
67+
for _, board := range latestRelease.BoardsManifest {
68+
toTest += board.Name + " "
7369
}
7470

7571
// Search
76-
if !utils.MatchAny(searchArgs, toTest) {
72+
if !utils.Match(toTest, searchArgs) {
7773
continue
7874
}
7975

8076
if allVersions {
8177
res = append(res, platform.GetAllReleases()...)
8278
} else {
83-
res = append(res, platformRelease)
79+
res = append(res, latestRelease)
8480
}
8581
}
8682
}

‎commands/lib/search.go

+11-30
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,42 +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 :
44-
queryTerms := strings.FieldsFunc(strings.ToLower(req.GetQuery()), func(r rune) bool {
45-
return !((r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || r == ':')
46-
})
41+
queryTerms := utils.SearchTermsFromQueryString(req.GetQuery())
4742

4843
for _, lib := range lm.Index.Libraries {
49-
matchTerm := func(x string) bool {
50-
if strings.Contains(strings.ToLower(lib.Name), x) ||
51-
strings.Contains(strings.ToLower(lib.Latest.Paragraph), x) ||
52-
strings.Contains(strings.ToLower(lib.Latest.Sentence), x) ||
53-
strings.Contains(strings.ToLower(lib.Latest.Author), x) {
54-
return true
55-
}
56-
for _, include := range lib.Latest.ProvidesIncludes {
57-
if strings.Contains(strings.ToLower(include), x) {
58-
return true
59-
}
60-
}
61-
return false
62-
}
63-
match := func() bool {
64-
for _, term := range queryTerms {
65-
if !matchTerm(term) {
66-
return false
67-
}
68-
}
69-
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 + " "
7050
}
71-
if match() {
51+
52+
if utils.Match(toTest, queryTerms) {
7253
res = append(res, indexLibraryToRPCSearchLibrary(lib))
7354
}
7455
}
7556

76-
return &rpc.LibrarySearchResponse{Libraries: res, Status: status}
57+
return &rpc.LibrarySearchResponse{Libraries: res, Status: rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS}
7758
}
7859

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

0 commit comments

Comments
 (0)
Please sign in to comment.