diff --git a/common/lib_tree_model.cpp b/common/lib_tree_model.cpp index 6ec5d9d4e0..29e79ea8ce 100644 --- a/common/lib_tree_model.cpp +++ b/common/lib_tree_model.cpp @@ -267,10 +267,34 @@ void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) { Score = 0; - for( auto& child: Children ) + // We need to score leaf nodes, which are usually (but not always) children. + + if( Children.size() ) { - child->UpdateScore( aMatcher ); - Score = std::max( Score, child->Score ); + for( auto& child: Children ) + { + child->UpdateScore( aMatcher ); + Score = std::max( Score, child->Score ); + } + } + else + { + // No children; we are a leaf. + int found_pos = EDA_PATTERN_NOT_FOUND; + int matchers_fired = 0; + + if( aMatcher.GetPattern() == MatchName ) + { + Score += 1000; // exact match. High score :) + } + else if( aMatcher.Find( MatchName, matchers_fired, found_pos ) ) + { + // Substring match. The earlier in the string the better. + Score += matchPosScore( found_pos, 20 ) + 20; + } + + // More matchers = better match + Score += 2 * matchers_fired; } } diff --git a/common/lib_tree_model_adapter.cpp b/common/lib_tree_model_adapter.cpp index 90e152663f..45758a5453 100644 --- a/common/lib_tree_model_adapter.cpp +++ b/common/lib_tree_model_adapter.cpp @@ -369,10 +369,8 @@ void LIB_TREE_MODEL_ADAPTER::FindAndExpand( LIB_TREE_NODE& aNode, if( !(*aHighScore) || node->Score > (*aHighScore)->Score ) (*aHighScore) = &*node; } - else - { - FindAndExpand( *node, aFunc, aHighScore ); - } + + FindAndExpand( *node, aFunc, aHighScore ); } } @@ -384,7 +382,7 @@ LIB_TREE_NODE* LIB_TREE_MODEL_ADAPTER::ShowResults() FindAndExpand( m_tree, []( LIB_TREE_NODE const* n ) { - return n->Type == LIB_TREE_NODE::TYPE::LIBID && n->Score > 1; + return /*n->Type == LIB_TREE_NODE::TYPE::LIBID &&*/ n->Score > 1; }, &highScore );