@@ -23,13 +23,8 @@ public FindSymbolViewModel(IEnumerable<Declaration> declarations, DeclarationIco
23
23
{
24
24
_declarations = declarations ;
25
25
_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 ) ;
33
28
}
34
29
35
30
public event EventHandler < NavigateCodeEventArgs > Navigate ;
@@ -59,47 +54,76 @@ public void OnNavigate()
59
54
60
55
private void Search ( string value )
61
56
{
57
+ if ( string . IsNullOrWhiteSpace ( value ) )
58
+ {
59
+ MatchResults = new ObservableCollection < SearchResult > ( ) ;
60
+ return ;
61
+ }
62
+
62
63
var lower = value . ToLowerInvariant ( ) ;
63
64
var results = _declarations
64
65
. Where ( declaration => ! ExcludedTypes . Contains ( declaration . DeclarationType )
65
- && ( string . IsNullOrEmpty ( value ) || declaration . IdentifierName . ToLowerInvariant ( ) . Contains ( lower ) ) )
66
+ && ( string . IsNullOrEmpty ( value ) || declaration . IdentifierName . ToLowerInvariant ( ) . Contains ( lower ) ) )
66
67
. OrderBy ( declaration => declaration . IdentifierName . ToLowerInvariant ( ) )
67
- . Select ( declaration => new SearchResult ( declaration , _cache [ declaration ] ) )
68
- . ToList ( ) ;
68
+ . Select ( declaration => new SearchResult ( declaration , _cache [ declaration ] ) ) ;
69
69
70
70
MatchResults = new ObservableCollection < SearchResult > ( results ) ;
71
71
}
72
72
73
73
private string _searchString ;
74
-
75
74
public string SearchString
76
75
{
77
76
get { return _searchString ; }
78
77
set
79
78
{
80
- _searchString = value ;
81
- Search ( value ) ;
79
+ if ( _searchString != value )
80
+ {
81
+ _searchString = value ;
82
+ Search ( value ) ;
83
+ }
82
84
}
83
85
}
84
86
85
87
private SearchResult _selectedItem ;
86
-
87
88
public SearchResult SelectedItem
88
89
{
89
90
get { return _selectedItem ; }
90
91
set
91
- {
92
- _selectedItem = value ;
93
- OnPropertyChanged ( ) ;
92
+ {
93
+ if ( _selectedItem != value )
94
+ {
95
+ _selectedItem = value ;
96
+ OnPropertyChanged ( ) ;
97
+ }
94
98
}
95
99
}
96
100
97
101
private ObservableCollection < SearchResult > _matchResults ;
98
-
99
102
public ObservableCollection < SearchResult > MatchResults
100
103
{
101
104
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
+ }
103
127
}
104
128
105
129
public event PropertyChangedEventHandler PropertyChanged ;
0 commit comments