@@ -23,13 +23,8 @@ public FindSymbolViewModel(IEnumerable<Declaration> declarations, DeclarationIco
2323 {
2424 _declarations = declarations ;
2525 _cache = cache ;
26- var initialResults = _declarations
27- . Where ( declaration => ! ExcludedTypes . Contains ( declaration . DeclarationType ) )
28- . OrderBy ( declaration => declaration . IdentifierName . ToLowerInvariant ( ) )
29- . Select ( declaration => new SearchResult ( declaration , cache [ declaration ] ) )
30- . ToList ( ) ;
31-
32- MatchResults = new ObservableCollection < SearchResult > ( initialResults ) ;
26+
27+ Search ( string . Empty ) ;
3328 }
3429
3530 public event EventHandler < NavigateCodeEventArgs > Navigate ;
@@ -59,47 +54,76 @@ public void OnNavigate()
5954
6055 private void Search ( string value )
6156 {
57+ if ( string . IsNullOrWhiteSpace ( value ) )
58+ {
59+ MatchResults = new ObservableCollection < SearchResult > ( ) ;
60+ return ;
61+ }
62+
6263 var lower = value . ToLowerInvariant ( ) ;
6364 var results = _declarations
6465 . Where ( declaration => ! ExcludedTypes . Contains ( declaration . DeclarationType )
65- && ( string . IsNullOrEmpty ( value ) || declaration . IdentifierName . ToLowerInvariant ( ) . Contains ( lower ) ) )
66+ && ( string . IsNullOrEmpty ( value ) || declaration . IdentifierName . ToLowerInvariant ( ) . Contains ( lower ) ) )
6667 . OrderBy ( declaration => declaration . IdentifierName . ToLowerInvariant ( ) )
67- . Select ( declaration => new SearchResult ( declaration , _cache [ declaration ] ) )
68- . ToList ( ) ;
68+ . Select ( declaration => new SearchResult ( declaration , _cache [ declaration ] ) ) ;
6969
7070 MatchResults = new ObservableCollection < SearchResult > ( results ) ;
7171 }
7272
7373 private string _searchString ;
74-
7574 public string SearchString
7675 {
7776 get { return _searchString ; }
7877 set
7978 {
80- _searchString = value ;
81- Search ( value ) ;
79+ if ( _searchString != value )
80+ {
81+ _searchString = value ;
82+ Search ( value ) ;
83+ }
8284 }
8385 }
8486
8587 private SearchResult _selectedItem ;
86-
8788 public SearchResult SelectedItem
8889 {
8990 get { return _selectedItem ; }
9091 set
91- {
92- _selectedItem = value ;
93- OnPropertyChanged ( ) ;
92+ {
93+ if ( _selectedItem != value )
94+ {
95+ _selectedItem = value ;
96+ OnPropertyChanged ( ) ;
97+ }
9498 }
9599 }
96100
97101 private ObservableCollection < SearchResult > _matchResults ;
98-
99102 public ObservableCollection < SearchResult > MatchResults
100103 {
101104 get { return _matchResults ; }
102- set { _matchResults = value ; OnPropertyChanged ( ) ; }
105+ set
106+ {
107+ var oldSelectedItem = SelectedItem ;
108+
109+ _matchResults = value ;
110+
111+ // save the selection when the user clicks on one of the drop-down items and the search results are updated
112+ if ( oldSelectedItem != null )
113+ {
114+ var newSelectedItem = value . FirstOrDefault ( s => s . Declaration == oldSelectedItem . Declaration ) ;
115+
116+ if ( newSelectedItem != null )
117+ {
118+ _selectedItem = newSelectedItem ;
119+ _searchString = newSelectedItem . IdentifierName ;
120+
121+ OnPropertyChanged ( "SelectedItem" ) ;
122+ }
123+ }
124+
125+ OnPropertyChanged ( ) ;
126+ }
103127 }
104128
105129 public event PropertyChangedEventHandler PropertyChanged ;
0 commit comments