-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cs
More file actions
205 lines (164 loc) · 6.25 KB
/
Copy pathHost.cs
File metadata and controls
205 lines (164 loc) · 6.25 KB
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
using System;
using System.Collections.Generic;
using CnSharp.VisualStudio.Extensions.Commands;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace CnSharp.VisualStudio.Extensions
{
public class Host
{
private static Host _host;
private bool _closingLast;
private DocumentEvents _documentEvents;
private _DTE _dte;
private DTEEvents _dteEvents;
private SolutionEvents _solutionEvents;
static Host()
{
Plugins = new List<Plugin>();
}
public static Host Instance => _host ?? (_host = new Host());
//public Package Package { get; set; }
//public AsyncPackage AsyncPackage { get; set; }
public T GetService<T>() where T : class
{
return AsyncPackage.GetGlobalService(typeof(T)) as T;
}
public IToolWindowAccessor ToolWindowAccessor { get; set; }
//public Assembly Assembly { get; set; }
public string Location { get; set; }
public string FileName { get; set; }
public string Version { get; set; }
public _DTE DTE
{
get => _dte;
set
{
_dte = value;
if (_dte != null)
{
_documentEvents = _dte.Events.DocumentEvents;
_documentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
_documentEvents.DocumentClosing += DocumentEvents_DocumentClosing;
_solutionEvents = _dte.Events.SolutionEvents;
_solutionEvents.Opened += SolutionEvents_Opened;
_solutionEvents.AfterClosing += SolutionEvents_AfterClosing;
_dteEvents = _dte.Events.DTEEvents;
_dteEvents.OnStartupComplete += DteEvents_OnStartupComplete;
_dteEvents.OnBeginShutdown += DteEvents_OnBeginShutdown;
}
}
}
public DTE2 Dte2 => (DTE2) DTE;
public Windows2 Windows => Dte2.Windows as Windows2;
public Solution Solution => DTE.Solution;
public Solution2 Solution2 => Solution as Solution2;
public IVsStatusbar Statusbar { get; set; }
//public IVsOutputWindow OutputWindow { get; set; }
public Action SolutionOpenedAction { get; set; }
public Action AfterSolutionClosingAction { get; set; }
public Action StartupCompleteAction { get; set; }
public Action BeginShutdownAction { get; set; }
public Action<Document> DocumentOpenedAction { get; set; }
public Action<Document> DocumentClosingAction { get; set; }
//public ResourceManager ResourceManager { get; set; }
public static List<Plugin> Plugins { get; set; }
public void ShowOutputWindow()
{
var outputWindow = (OutputWindow)Windows.Item("Output").Object;
outputWindow.Parent.AutoHides = false;
outputWindow.Parent.Activate();
}
/// <summary>
/// output message to Output Window
/// </summary>
/// <param name="paneName"></param>
/// <param name="message"></param>
public void WriteToOutputWindow(string paneName, string message)
{
var outputWindow = (OutputWindow)Windows.Item("Output").Object;
OutputWindowPane pane;
try
{
pane = outputWindow.OutputWindowPanes.Item(paneName);
}
catch
{
pane = outputWindow.OutputWindowPanes.Add(paneName);
}
pane.Activate();
// Add a line of text to the new pane.
pane.OutputString(message + Environment.NewLine);
}
private void DteEvents_OnStartupComplete()
{
StartupCompleteAction?.Invoke();
}
private void DteEvents_OnBeginShutdown()
{
BeginShutdownAction?.Invoke();
}
private void SolutionEvents_AfterClosing()
{
foreach (var plugin in Plugins)
{
plugin.CommandManager.ApplyDependencies(DependentItems.SolutionProject, false);
}
AfterSolutionClosingAction?.Invoke();
}
private void SolutionEvents_Opened()
{
foreach (var plugin in Plugins)
{
plugin.CommandManager.ApplyDependencies(DependentItems.SolutionProject, true);
}
SolutionOpenedAction?.Invoke();
}
private void DocumentEvents_DocumentClosing(Document document)
{
if (_dte.Documents.Count == 1) //the last one
{
_closingLast = true;
foreach (var plugin in Plugins)
{
plugin.CommandManager.ApplyDependencies(DependentItems.Document, false);
}
_closingLast = false;
}
DocumentClosingAction?.Invoke(document);
}
private void DocumentEvents_DocumentOpened(Document document)
{
foreach (var plugin in Plugins)
{
plugin.CommandManager.ApplyDependencies(DependentItems.Document, true);
}
DocumentOpenedAction?.Invoke(document);
}
public bool IsDependencySatisfied(DependentItems dependentItems)
{
var dependencies = dependentItems.ToString()
.Split(new[] {" ,"}, StringSplitOptions.RemoveEmptyEntries);
foreach (var dependency in dependencies)
{
var item = (DependentItems) Enum.Parse(typeof (DependentItems), dependency);
if (!CheckDependency(item))
return false;
}
return true;
}
private bool CheckDependency(DependentItems dependentItems)
{
switch (dependentItems)
{
case DependentItems.Document:
return _dte.Documents.Count > 0 && !_closingLast;
case DependentItems.SolutionProject:
return _dte.Solution != null && _dte.Solution.Projects.Count > 0;
}
return true;
}
}
}