From 724182abe410bb586da5219f9921685160b6fe99 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 14 Dec 2021 15:18:11 +0000 Subject: [PATCH] Fix lib tree searches to handle searching on library names. Fixes https://gitlab.com/kicad/code/kicad/issues/9981 --- common/lib_tree_model.cpp | 23 ++++++++++++++++++----- common/lib_tree_model_adapter.cpp | 12 ++++++++++-- include/lib_tree_model.h | 10 +++++----- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/common/lib_tree_model.cpp b/common/lib_tree_model.cpp index 1d9978c0cf..107040e19e 100644 --- a/common/lib_tree_model.cpp +++ b/common/lib_tree_model.cpp @@ -206,7 +206,7 @@ void LIB_TREE_NODE_LIB_ID::Update( LIB_TREE_ITEM* aItem ) } -void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) +void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) { if( m_Score <= 0 ) return; // Leaf nodes without scores are out of the game. @@ -218,6 +218,12 @@ void LIB_TREE_NODE_LIB_ID::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) m_Normalized = true; } + if( !aLib.IsEmpty() && m_Parent->m_MatchName != aLib ) + { + m_Score = 0; + return; + } + // Keywords and description we only count if the match string is at // least two characters long. That avoids spurious, low quality // matches. Most abbreviations are at three characters long. @@ -281,7 +287,7 @@ LIB_TREE_NODE_LIB_ID& LIB_TREE_NODE_LIB::AddItem( LIB_TREE_ITEM* aItem ) } -void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) +void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) { m_Score = 0; @@ -291,13 +297,20 @@ void LIB_TREE_NODE_LIB::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) { for( std::unique_ptr& child: m_Children ) { - child->UpdateScore( aMatcher ); + child->UpdateScore( aMatcher, aLib ); m_Score = std::max( m_Score, child->m_Score ); } } else { // No children; we are a leaf. + + if( !aLib.IsEmpty() ) + { + m_Score = m_MatchName == aLib ? 1000 : 0; + return; + } + int found_pos = EDA_PATTERN_NOT_FOUND; int matchers_fired = 0; @@ -331,9 +344,9 @@ LIB_TREE_NODE_LIB& LIB_TREE_NODE_ROOT::AddLib( wxString const& aName, wxString c } -void LIB_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) +void LIB_TREE_NODE_ROOT::UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) { for( std::unique_ptr& child: m_Children ) - child->UpdateScore( aMatcher ); + child->UpdateScore( aMatcher, aLib ); } diff --git a/common/lib_tree_model_adapter.cpp b/common/lib_tree_model_adapter.cpp index 31c2473f39..f65448b886 100644 --- a/common/lib_tree_model_adapter.cpp +++ b/common/lib_tree_model_adapter.cpp @@ -224,10 +224,18 @@ void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( const wxString& aSearch, bool a while( tokenizer.HasMoreTokens() ) { - const wxString term = tokenizer.GetNextToken().Lower(); + wxString lib; + wxString term = tokenizer.GetNextToken().Lower(); + + if( term.Contains( ":" ) ) + { + lib = term.BeforeFirst( ':' ); + term = term.AfterFirst( ':' ); + } + EDA_COMBINED_MATCHER matcher( term ); - m_tree.UpdateScore( matcher ); + m_tree.UpdateScore( matcher, lib ); } m_tree.SortNodes(); diff --git a/include/lib_tree_model.h b/include/lib_tree_model.h index b31726151f..9e797f2943 100644 --- a/include/lib_tree_model.h +++ b/include/lib_tree_model.h @@ -81,7 +81,7 @@ public: * * @param aMatcher an EDA_COMBINED_MATCHER initialized with the search term */ - virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) = 0; + virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) = 0; /** * Initialize score to kLowestDefaultScore, recursively. @@ -170,7 +170,7 @@ public: /** * Do nothing, units just take the parent's score */ - virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override {} + virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override {} }; @@ -209,7 +209,7 @@ public: /** * Perform the actual search. */ - virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override; + virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override; protected: /** @@ -250,7 +250,7 @@ public: */ LIB_TREE_NODE_LIB_ID& AddItem( LIB_TREE_ITEM* aItem ); - virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override; + virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override; }; @@ -277,7 +277,7 @@ public: */ LIB_TREE_NODE_LIB& AddLib( wxString const& aName, wxString const& aDesc ); - virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher ) override; + virtual void UpdateScore( EDA_COMBINED_MATCHER& aMatcher, const wxString& aLib ) override; };