-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathCodeSearchToolWindow.cs
225 lines (199 loc) · 8.67 KB
/
CodeSearchToolWindow.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
// 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 EnvDTE;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.TextManager.Interop;
using VsChromium.Commands;
using VsChromium.Features.AutoUpdate;
using VsChromium.Package.CommandHandler;
using VsChromium.Wpf;
namespace VsChromium.Features.ToolWindows.CodeSearch {
/// <summary>
/// This class implements the tool window exposed by this package and hosts a user control.
///
/// In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane,
/// usually implemented by the package implementer.
///
/// This class derives from the ToolWindowPane class provided from the MPF in order to use its
/// implementation of the IVsUIElementPane interface.
/// </summary>
[Guid(GuidList.GuidCodeSearchToolWindowString)]
public class CodeSearchToolWindow : ToolWindowPane, IOleCommandTarget {
private VsWindowFrameNotifyHandler _frameNotify;
private IVsTextManager _txtMgr;
private IComponentModel _componentModel;
private IVsEditorAdaptersFactoryService _editorAdaptersFactoryService;
private EnvDTE.WindowEvents WindowEvents { get; set; }
/// <summary>
/// Standard constructor for the tool window.
/// </summary>
public CodeSearchToolWindow()
: base(null) {
// Set the window title reading it from the resources.
Caption = Resources.CodeSearchToolWindowTitle;
// Set the image that will appear on the tab of the window frame
// when docked with an other window
// The resource ID correspond to the one defined in the resx file
// while the Index is the offset in the bitmap strip. Each image in
// the strip being 16x16.
BitmapResourceID = 301;
BitmapIndex = 1;
// This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
// we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
// the object returned by the Content property.
ExplorerControl = new CodeSearchControl();
}
private string GetSelectedOrWord()
{
string selection = string.Empty;
IVsTextView vTextView = null;
int mustHaveFocus = 1;
_txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
if (vTextView == null)
return selection;
vTextView.GetSelectedText(out selection);
if (!selection.Equals(""))
return selection;
var textView = _editorAdaptersFactoryService.GetWpfTextView(vTextView);
var viewadatper = _editorAdaptersFactoryService.GetViewAdapter(textView);
if (textView == null || viewadatper == null)
return selection;
var position = textView.Caret.Position.BufferPosition;
position = position.TranslateTo(textView.TextSnapshot, PointTrackingMode.Positive);
TextSpan[] spans = new TextSpan[1];
var containingline = position.GetContainingLine();
if (ErrorHandler.Failed(viewadatper.GetWordExtent(containingline.LineNumber, position - containingline.Start, (uint)WORDEXTFLAGS.WORDEXT_CURRENT, spans)))
return selection;
var wordstring = string.Empty;
var span = spans[0];
vTextView.GetTextStream(span.iStartLine, span.iStartIndex, span.iEndLine, span.iEndIndex, out wordstring);
return wordstring;
}
public override void OnToolWindowCreated() {
base.OnToolWindowCreated();
ExplorerControl.OnVsToolWindowCreated(this);
_txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
_componentModel = (IComponentModel)GetService(typeof(SComponentModel));
_editorAdaptersFactoryService = (IVsEditorAdaptersFactoryService)_componentModel.GetService<IVsEditorAdaptersFactoryService>();
EnvDTE.DTE dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
EnvDTE80.Events2 events = (EnvDTE80.Events2)dte.Events;
this.WindowEvents = (EnvDTE.WindowEvents)events.get_WindowEvents(null);
this.WindowEvents.WindowActivated += (Window GotFocus, Window LostFocus) => {
if (GotFocus.ObjectKind.ToString().ToLower().Equals("{" + GuidList.GuidCodeSearchToolWindowString + "}"))
{
var word = GetSelectedOrWord();
if (word.Equals(""))
return;
ExplorerControl.SearchCodeCombo.Text = word;
}
};
// Advise IVsWindowFrameNotify so we know when we get hidden, etc.
var frame = Frame as IVsWindowFrame2;
if (frame != null) {
_frameNotify = new VsWindowFrameNotifyHandler(frame);
_frameNotify.Advise();
}
// Hookup command handlers
var commands = new List<IPackageCommandHandler> {
new PreviousLocationCommandHandler(this),
new NextLocationCommandHandler(this),
new CancelSearchCommandHandler(this),
//new CancelSearchToolWindowCommandHandler(this),
// Add more here...
};
var commandService = (IMenuCommandService)GetService(typeof(IMenuCommandService));
commands.ForEach(handler =>
commandService.AddCommand(handler.ToOleMenuCommand()));
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
if (ExplorerControl.Controller != null) {
ExplorerControl.Controller.Dispose();
}
}
}
public CodeSearchControl ExplorerControl {
get { return Content as CodeSearchControl; }
set { Content = value; }
}
public bool IsVisible {
get {
return _frameNotify != null &&
_frameNotify.IsVisible;
}
}
public bool IsCancelSearchEnabled {
get {
switch (ExplorerControl.ViewModel.ActiveDisplay) {
case CodeSearchViewModel.DisplayKind.SearchFilePathsResult:
case CodeSearchViewModel.DisplayKind.SearchCodeResult:
return true;
default:
return false;
}
}
}
public void FocusSearchCodeBox(CommandID commandId) {
switch (commandId.ID) {
case (int)PkgCmdIdList.CmdidSearchFilePaths:
ExplorerControl.SearchFilePathsCombo.Focus();
break;
case (int)PkgCmdIdList.CmdidSearchCode:
ExplorerControl.SearchCodeCombo.Focus();
break;
}
}
public bool HasNextLocation() {
return ExplorerControl.Controller.HasNextLocation();
}
public bool HasPreviousLocation() {
return ExplorerControl.Controller.HasPreviousLocation();
}
public void NavigateToNextLocation() {
ExplorerControl.Controller.NavigateToNextLocation();
}
public void NavigateToPreviousLocation() {
ExplorerControl.Controller.NavigateToPreviousLocation();
}
public void NotifyPackageUpdate(UpdateInfo updateInfo) {
WpfUtilities.Post(ExplorerControl, () => {
ExplorerControl.UpdateInfo = updateInfo;
});
}
int IOleCommandTarget.QueryStatus(ref System.Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, System.IntPtr pCmdText) {
var impl = GetService(typeof(IMenuCommandService)) as IOleCommandTarget;
return OleCommandTargetSpy.WrapQueryStatus(this, impl, ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
int IOleCommandTarget.Exec(ref System.Guid pguidCmdGroup, uint nCmdId, uint nCmdexecopt, System.IntPtr pvaIn, System.IntPtr pvaOut) {
var impl = GetService(typeof (IMenuCommandService)) as IOleCommandTarget;
return OleCommandTargetSpy.WrapExec(this, impl, ref pguidCmdGroup, nCmdId, nCmdexecopt, pvaIn, pvaOut);
}
public void CancelSearch() {
ExplorerControl.Controller.CancelSearch();
}
public void QuickSearchCode(string searchPattern) {
ExplorerControl.Controller.QuickSearchCode(searchPattern);
}
public void QuickSearchFilePaths(string searchPattern) {
ExplorerControl.Controller.QuickFilePaths(searchPattern);
}
public void FocusQuickSearchCode() {
ExplorerControl.Controller.FocusQuickSearchCode();
}
public void FocusQuickSearchFilePaths() {
ExplorerControl.Controller.FocusQuickSearchFilePaths();
}
}
}