Skip to content

Commit 020a374

Browse files
committed
Use lexicographic distance as last chance to spot the right library
Should solve most "overload" cases in Create. Eg: Library ZZ_Sensor contains ZZ_Sensor.h but the name in library.properties is "ZZ Unified Sensor" A sketch includes ZZ_Sensor.h and the resolve function fails every test (since the library name is not contained in any variation of the "include" string), so it uses another clashing library randomly. Lexicographic distance should help avoiding any library with "very different" name (unless explicitely requested, of course).
1 parent 69b6665 commit 020a374

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: resolve_library.go

+27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/arduino/arduino-builder/constants"
3737
"github.com/arduino/arduino-builder/types"
3838
"github.com/arduino/arduino-builder/utils"
39+
"github.com/schollz/closestmatch"
3940
)
4041

4142
func ResolveLibrary(ctx *types.Context, header string) *types.Library {
@@ -211,6 +212,10 @@ func findBestLibraryWithHeader(header string, libraries []*types.Library) *types
211212
if library != nil {
212213
return library
213214
}
215+
library = findLibWithNameBestDistance(headerName, libraries)
216+
if library != nil {
217+
return library
218+
}
214219
}
215220

216221
return nil
@@ -252,6 +257,28 @@ func findLibWithNameContaining(name string, libraries []*types.Library) *types.L
252257
return nil
253258
}
254259

260+
func findLibWithNameBestDistance(name string, libraries []*types.Library) *types.Library {
261+
// create closestmatch DB
262+
var wordsToTest []string
263+
264+
for _, library := range libraries {
265+
wordsToTest = append(wordsToTest, simplifyName(library.Name))
266+
}
267+
// Choose a set of bag sizes, more is more accurate but slower
268+
bagSizes := []int{2}
269+
// Create a closestmatch object
270+
cm := closestmatch.New(wordsToTest, bagSizes)
271+
closest_name := cm.Closest(name)
272+
273+
for _, library := range libraries {
274+
if (closest_name == simplifyName(library.Name)) {
275+
return library
276+
}
277+
}
278+
279+
return nil
280+
}
281+
255282
func simplifyName(name string) string {
256283
return strings.ToLower(strings.Replace(name, "_", " ", -1))
257284
}

0 commit comments

Comments
 (0)