Skip to content

Commit 350621e

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 350621e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: resolve_library.go

+16
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/arbovm/levenshtein"
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,17 @@ 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+
distance := 10000
262+
var tempLib *types.Library
263+
for _, library := range libraries {
264+
if levenshtein.Distance(simplifyName(library.Name), simplifyName(name)) < distance {
265+
tempLib = library
266+
}
267+
}
268+
return tempLib
269+
}
270+
255271
func simplifyName(name string) string {
256272
return strings.ToLower(strings.Replace(name, "_", " ", -1))
257273
}

0 commit comments

Comments
 (0)