-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathCodeSearchControl.xaml.cs
337 lines (290 loc) · 13.1 KB
/
CodeSearchControl.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using VsChromium.Core.Logging;
using VsChromium.Core.Utility;
using VsChromium.Features.AutoUpdate;
using VsChromium.Features.BuildOutputAnalyzer;
using VsChromium.Features.IndexServerInfo;
using VsChromium.Package;
using VsChromium.ServerProxy;
using VsChromium.Settings;
using VsChromium.Threads;
using VsChromium.Views;
using VsChromium.Wpf;
namespace VsChromium.Features.ToolWindows.CodeSearch {
/// <summary>
/// Interaction logic for CodeSearchControl.xaml
/// </summary>
public partial class CodeSearchControl {
// For controlling scrolling inside tree view.
private double _treeViewHorizScrollPos;
private bool _treeViewResetHorizScroll;
private ScrollViewer _treeViewScrollViewer;
private readonly IProgressBarTracker _progressBarTracker;
private IDispatchThreadServerRequestExecutor _dispatchThreadServerRequestExecutor;
private bool _swallowsRequestBringIntoView = true;
private CodeSearchController _controller;
public CodeSearchControl() {
InitializeComponent();
// Add the "VsColors" brushes to the WPF resources of the control, so that the
// resource keys used on the XAML file can be resolved dynamically.
Resources.MergedDictionaries.Add(VsResources.BuildResourceDictionary());
DataContext = new CodeSearchViewModel();
_progressBarTracker = new ProgressBarTracker(ProgressBar);
InitComboBox(SearchCodeCombo, new ComboBoxInfo {
TextChanged = text => { ViewModel.SearchCodeValue = text; },
SearchFunction = RefreshSearchResults,
NextElement = SearchFilePathsCombo,
});
InitComboBox(SearchFilePathsCombo, new ComboBoxInfo {
TextChanged = text => { ViewModel.SearchFilePathsValue = text; },
SearchFunction = RefreshSearchResults,
PreviousElement = SearchCodeCombo,
NextElement = FileTreeView,
InitialItems = {
"*",
"*.c;*.cpp;*.cxx;*.cc;*.tli;*.tlh;*.h;*.hh;*.hpp;*.hxx;*.hh;*.inl;*.rc;*.resx;*.idl;*.asm;*.inc",
"*.htm;*.html;*.xml;*.gif;*.jpg;*.png;*.css;*.disco;*.js;*.srf",
"*.xml;*.xsl;*.xslt;*.xsd;*.dtd",
"*.txt",
"*.cs;*.resx;*.resw;*.xsd;*.wsdl;*.xaml;*.xml;*.htm;*.html;*.css",
"*.vb;*.resx;*.resw;*.xsd;*.wsdl;*.xaml;*.xml;*.htm;*.html;*.css",
"*.*",
}
});
}
/// <summary>
/// Called when Visual Studio creates our container ToolWindow.
/// </summary>
public void OnVsToolWindowCreated(IServiceProvider serviceProvider) {
var componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));
_dispatchThreadServerRequestExecutor = componentModel.DefaultExportProvider.GetExportedValue<IDispatchThreadServerRequestExecutor>();
var standarImageSourceFactory = componentModel.DefaultExportProvider.GetExportedValue<IStandarImageSourceFactory>();
_controller = new CodeSearchController(
this,
_dispatchThreadServerRequestExecutor,
componentModel.DefaultExportProvider.GetExportedValue<IDispatchThreadDelayedOperationExecutor>(),
componentModel.DefaultExportProvider.GetExportedValue<IFileSystemTreeSource>(),
componentModel.DefaultExportProvider.GetExportedValue<ITypedRequestProcessProxy>(),
_progressBarTracker,
standarImageSourceFactory,
componentModel.DefaultExportProvider.GetExportedValue<IWindowsExplorer>(),
componentModel.DefaultExportProvider.GetExportedValue<IClipboard>(),
componentModel.DefaultExportProvider.GetExportedValue<ISynchronizationContextProvider>(),
componentModel.DefaultExportProvider.GetExportedValue<IOpenDocumentHelper>(),
componentModel.DefaultExportProvider.GetExportedValue<ITextDocumentTable>(),
componentModel.DefaultExportProvider.GetExportedValue<IEventBus>(),
componentModel.DefaultExportProvider.GetExportedValue<IGlobalSettingsProvider>(),
componentModel.DefaultExportProvider.GetExportedValue<IBuildOutputParser>(),
componentModel.DefaultExportProvider.GetExportedValue<IVsEditorAdaptersFactoryService>(),
componentModel.DefaultExportProvider.GetExportedValue<IShowServerInfoService>());
_controller.Start();
// TODO(rpaquay): leaky abstraction. We need this because the ViewModel
// exposes pictures from Visual Studio resources.
ViewModel.ImageSourceFactory = standarImageSourceFactory;
// Hookup property changed notifier
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
ViewModel.RootNodesChanged += ViewModelOnRootNodesChanged;
}
private void ViewModelOnRootNodesChanged(object sender, EventArgs eventArgs) {
//FileTreeView.Items.Refresh();
//FileTreeView.UpdateLayout();
}
public CodeSearchViewModel ViewModel {
get {
return (CodeSearchViewModel)DataContext;
}
}
public UpdateInfo UpdateInfo {
get { return ViewModel.UpdateInfo; }
set { ViewModel.UpdateInfo = value; }
}
public ICodeSearchController Controller {
get { return _controller; }
}
private void InitComboBox(EditableComboBox comboBox, ComboBoxInfo info) {
comboBox.DataContext = new StringListViewModel(info.InitialItems);
comboBox.TextChanged += (s, e) => {
info.TextChanged(comboBox.Text);
info.SearchFunction(false);
};
comboBox.KeyDown += (s, e) => {
if ((e.KeyboardDevice.Modifiers == ModifierKeys.None) &&
(e.Key == Key.Return || e.Key == Key.Enter)) {
info.SearchFunction(true);
}
};
if (info.PreviousElement != null) {
comboBox.PrePreviewKeyDown += (s, e) => {
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Up) {
if (!comboBox.IsDropDownOpen) {
info.PreviousElement.Focus();
e.Handled = true;
}
}
};
}
if (info.NextElement != null) {
comboBox.PrePreviewKeyDown += (s, e) => {
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Down) {
if (!comboBox.IsDropDownOpen) {
info.NextElement.Focus();
e.Handled = true;
}
}
};
}
}
private class ComboBoxInfo {
public ComboBoxInfo() {
InitialItems = new List<string>();
}
public Action<string> TextChanged { get; set; }
public Action<bool> SearchFunction { get; set; }
public UIElement PreviousElement { get; set; }
public UIElement NextElement { get; set; }
public List<string> InitialItems { get; }
}
public void SwallowsRequestBringIntoView(bool value) {
_swallowsRequestBringIntoView = value;
}
#region WPF Event handlers
void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
// Handle property change for the ViewModel.
if (e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.MatchCase) ||
e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.MatchWholeWord) ||
e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.UseRegex) ||
e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.IncludeSymLinks) ||
e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.UnderstandBuildOutputPaths) ||
e.PropertyName == ReflectionUtils.GetPropertyName(ViewModel, x => x.ExpandAll)) {
RefreshSearchResults(true);
}
}
private void RefreshSearchResults(bool immediate) {
Controller.PerformSearch(immediate);
}
private void TreeViewItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) {
var tvi = sender as TreeViewItem;
if (tvi == null)
return;
if (!tvi.IsSelected)
return;
if (Controller.ExecuteOpenCommandForItem(tvi.DataContext as TreeViewItemViewModel))
e.Handled = true;
}
private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) {
if (_swallowsRequestBringIntoView) {
// This prevents the tree view for scrolling horizontally to make the
// selected item as visibile as possible. This is useful for
// "SearchCode", as text extracts are usually wide enough to make tree
// view navigation annoying when they are selected.
e.Handled = true;
return;
}
// Find the scroll viewer and hook up scroll changed event handler.
if (_treeViewScrollViewer == null) {
_treeViewScrollViewer = FileTreeView.Template.FindName("_tv_scrollviewer_", FileTreeView) as ScrollViewer;
if (_treeViewScrollViewer != null) {
_treeViewScrollViewer.ScrollChanged += TreeViewScrollViewer_ScrollChanged;
}
}
// If we got a scroll viewer, remember the horizontal offset so we can
// restore it in the scroll changed event.
if (_treeViewScrollViewer != null) {
_treeViewResetHorizScroll = true;
_treeViewHorizScrollPos = _treeViewScrollViewer.HorizontalOffset;
}
e.Handled = false;
}
private void TreeViewScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) {
Invariants.Assert(_treeViewScrollViewer != null);
if (_treeViewResetHorizScroll) {
_treeViewScrollViewer.ScrollToHorizontalOffset(_treeViewHorizScrollPos);
_treeViewResetHorizScroll = false;
}
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {
// Open the default web browser to the update URL.
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
private void FileTreeView_OnPreviewKeyDown(object sender, KeyEventArgs e) {
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Return) {
e.Handled = Controller.ExecuteOpenCommandForItem(
FileTreeView.SelectedItem as TreeViewItemViewModel);
}
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Up) {
// If topmost item is selected, move selection to bottom combo box
var item = FileTreeView.SelectedItem as TreeViewItemViewModel;
if (item != null) {
var parent = item.ParentViewModel as RootTreeViewItemViewModel;
if (parent != null) {
if (item == parent.Children.FirstOrDefault()) {
SearchFilePathsCombo.Focus();
e.Handled = true;
}
}
}
}
}
/// <summary>
/// Ensures the item right-clicked on is selected before showing the context
/// menu.
/// </summary>
private void FileTreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) {
var source = e.OriginalSource as DependencyObject;
if (source == null)
return;
var treeViewItem = WpfUtilities.VisualTreeGetParentOfType<TreeViewItem>(source);
if (treeViewItem == null)
return;
treeViewItem.Focus();
e.Handled = true;
}
private void RefreshSearchResultsButton_Click(object sender, RoutedEventArgs e) {
Logger.WrapActionInvocation(
() => RefreshSearchResults(true));
}
private void RefreshIndexButton_Click(object sender, RoutedEventArgs e) {
Logger.WrapActionInvocation(
() => Controller.RefreshFileSystemTree());
}
private void GotoPrevious_Click(object sender, RoutedEventArgs e) {
Controller.NavigateToPreviousLocation();
}
private void GotoNext_Click(object sender, RoutedEventArgs e) {
Controller.NavigateToNextLocation();
}
private void Cancel_Click(object sender, RoutedEventArgs e) {
Controller.CancelSearch();
}
private void ClearFilePathsPattern_Click(object sender, RoutedEventArgs e) {
SearchFilePathsCombo.Text = "";
RefreshSearchResults(true);
}
private void ClearSearchCode_Click(object sender, RoutedEventArgs e) {
SearchCodeCombo.Text = "";
RefreshSearchResults(true);
}
#endregion
private void PauseResumeIndexingButton_Click(object sender, RoutedEventArgs e) {
Logger.WrapActionInvocation(() => Controller.PauseResumeIndexing());
}
private void ServerInfo_Click(object sender, RoutedEventArgs e) {
var forceGarbageCollection = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift);
Logger.WrapActionInvocation(() => Controller.ShowServerInfo(forceGarbageCollection));
}
}
}