@@ -723,11 +723,67 @@ static public void populateImportToLibraryTable() {
723723 for (String header : headers ) {
724724 Library old = importToLibraryTable .get (header );
725725 if (old != null ) {
726- // If a library was already found with this header, keep
727- // it if the library's name matches the header name.
728- String name = header .substring (0 , header .length () - 2 );
729- if (old .getFolder ().getPath ().endsWith (name ))
730- continue ;
726+ // This is the case where 2 libraries have a .h header
727+ // with the same name. We must decide which library to
728+ // use when a sketch has #include "name.h"
729+ //
730+ // When all other factors are equal, "libName" is
731+ // used in preference to "oldName", because getLibraries()
732+ // gives the library list in order from less specific to
733+ // more specific locations.
734+ //
735+ // But often one library is more clearly the user's
736+ // intention to use. Many cases are tested, always first
737+ // for "libName", then for "oldName".
738+ //
739+ String name = header .substring (0 , header .length () - 2 ); // name without ".h"
740+ String oldName = old .getFolder ().getName (); // just the library folder name
741+ String libName = lib .getFolder ().getName (); // just the library folder name
742+ //System.out.println("name conflict: " + name);
743+ //System.out.println(" old = " + oldName + " -> " + old.getFolder().getPath());
744+ //System.out.println(" new = " + libName + " -> " + lib.getFolder().getPath());
745+ String name_lc = name .toLowerCase ();
746+ String oldName_lc = oldName .toLowerCase ();
747+ String libName_lc = libName .toLowerCase ();
748+ // always favor a perfect name match
749+ if (libName .equals (name )) {
750+ } else if (oldName .equals (name )) {
751+ continue ;
752+ // check for "-master" appended (zip file from github)
753+ } else if (libName .equals (name +"-master" )) {
754+ } else if (oldName .equals (name +"-master" )) {
755+ continue ;
756+ // next, favor a match with other stuff appended
757+ } else if (libName .startsWith (name )) {
758+ } else if (oldName .startsWith (name )) {
759+ continue ;
760+ // otherwise, favor a match with stuff prepended
761+ } else if (libName .endsWith (name )) {
762+ } else if (oldName .endsWith (name )) {
763+ continue ;
764+ // as a last resort, match if stuff prepended and appended
765+ } else if (libName .contains (name )) {
766+ } else if (oldName .contains (name )) {
767+ continue ;
768+ // repeat all the above tests, with case insensitive matching
769+ } else if (libName_lc .equals (name_lc )) {
770+ } else if (oldName_lc .equals (name_lc )) {
771+ continue ;
772+ } else if (libName_lc .equals (name_lc +"-master" )) {
773+ } else if (oldName_lc .equals (name_lc +"-master" )) {
774+ continue ;
775+ } else if (libName_lc .startsWith (name_lc )) {
776+ } else if (oldName_lc .startsWith (name_lc )) {
777+ continue ;
778+ } else if (libName_lc .endsWith (name_lc )) {
779+ } else if (oldName_lc .endsWith (name_lc )) {
780+ continue ;
781+ } else if (libName_lc .contains (name_lc )) {
782+ } else if (oldName_lc .contains (name_lc )) {
783+ continue ;
784+ } else {
785+ // none of these tests matched, so just default to "libName".
786+ }
731787 }
732788 importToLibraryTable .put (header , lib );
733789 }
0 commit comments