Attempt to fix lib search again

The new bits will drop search results that
don't match all tokens in the search string,
which seems desirable.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16974


(cherry picked from commit 589e21cd3b)
This commit is contained in:
Jon Evans 2024-02-20 21:21:06 -05:00
parent adae4de438
commit ad311c33c6
1 changed files with 22 additions and 10 deletions

View File

@ -242,13 +242,16 @@ void LIB_TREE_NODE_ITEM::Update( LIB_TREE_ITEM* aItem )
void LIB_TREE_NODE_ITEM::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib, void LIB_TREE_NODE_ITEM::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
{ {
// aMatcher test is additive // aMatcher test is additive, but if we don't match the given term at all, it nulls out
if( aMatcher ) if( aMatcher )
m_Score += aMatcher->ScoreTerms( m_SearchTerms ); {
int currentScore = aMatcher->ScoreTerms( m_SearchTerms );
// aLib test is additive if( m_Score >= 0 && currentScore > 0 )
if( !aLib.IsEmpty() && m_Parent->m_Name.Lower().Matches( aLib ) ) m_Score += currentScore;
m_Score += 1; else
m_Score = -1; // Item has failed to match this term, rule it out
}
// aFilter test is subtractive // aFilter test is subtractive
if( aFilter && !(*aFilter)(*this) ) if( aFilter && !(*aFilter)(*this) )
@ -287,18 +290,27 @@ LIB_TREE_NODE_ITEM& LIB_TREE_NODE_LIBRARY::AddItem( LIB_TREE_ITEM* aItem )
void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib, void LIB_TREE_NODE_LIBRARY::UpdateScore( EDA_COMBINED_MATCHER* aMatcher, const wxString& aLib,
std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) std::function<bool( LIB_TREE_NODE& aNode )>* aFilter )
{ {
int newScore = 0;
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children ) for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
{ {
child->UpdateScore( aMatcher, aLib, aFilter ); child->UpdateScore( aMatcher, aLib, aFilter );
m_Score = std::max( m_Score, child->m_Score ); newScore = std::max( newScore, child->m_Score );
} }
// aLib test is additive // Each time UpdateScore is called for a library, child (item) scores may go up or down.
if( !aLib.IsEmpty() && m_Name.Lower().Matches( aLib ) ) // If the all go down to zero, we need to make sure to drop the library from the list.
if( newScore > 0 )
m_Score = std::max( m_Score, newScore );
else
m_Score = 0;
// aLib test is additive, but only when we've already accumulated some score from children
if( !aLib.IsEmpty() && m_Name.Lower().Matches( aLib ) && m_Score > 0 )
m_Score += 1; m_Score += 1;
// aMatcher test is additive // aMatcher test is additive, but only when we've already accumulated some score from children
if( aMatcher ) if( aMatcher && m_Score > 0 )
m_Score += aMatcher->ScoreTerms( m_SearchTerms ); m_Score += aMatcher->ScoreTerms( m_SearchTerms );
// show all nodes if no search/filter/etc. criteria are given // show all nodes if no search/filter/etc. criteria are given