forked from ishani/ClangVSx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTEHelper.cs
284 lines (243 loc) · 8.32 KB
/
DTEHelper.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
// from http://code.google.com/p/nenhancer/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using Process = System.Diagnostics.Process;
namespace NEnhancer.Common
{
public class DTEHelper
{
private readonly AddIn _addin;
private readonly DTE2 _dte;
public DTEHelper(DTE2 dte, AddIn addin)
{
_dte = dte;
_addin = addin;
}
public UIHierarchy SolutionExplorerNode
{
get { return _dte.ToolWindows.SolutionExplorer; }
}
public string GetCulturedMenuName(string englishName)
{
string result;
try
{
string resourceName;
var resourceManager = new ResourceManager("ClangVSx.CommandBar",
Assembly.GetExecutingAssembly());
var cultureInfo = new CultureInfo(_dte.LocaleID);
if (cultureInfo.TwoLetterISOLanguageName == "zh")
{
CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, englishName);
}
else
{
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, englishName);
}
result = resourceManager.GetString(resourceName);
}
catch
{
result = englishName;
}
return result;
}
public CommandBar GetCommandBarByName(string cmdBarName)
{
return ((CommandBars)_dte.CommandBars)[cmdBarName];
}
public void DumpCommandBars()
{
foreach (CommandBar cb in ((CommandBars)_dte.CommandBars))
{
string sb = cb.Name;
Debug.WriteLine(sb);
}
}
public void AddNamedCommand2(string cmdName, string buttonText, string toolTip,
bool useMsoButton, int iconIndex)
{
// Get commands collection
var commands = (Commands2)_dte.Commands;
var contextGuids = new object[] { };
try
{
// Add command
commands.AddNamedCommand2(_addin, cmdName, buttonText, toolTip,
useMsoButton, iconIndex, ref contextGuids);
}
catch (ArgumentException)
{
// Command already exists, so ignore the exception.
}
}
public CommandBarButton AddButtonToCmdBar(CommandBar cmdBar, int beforeIndex, string caption, string tooltip)
{
var button = cmdBar.Controls.Add(
MsoControlType.msoControlButton,
Type.Missing,
Type.Missing,
beforeIndex,
true) as CommandBarButton;
if (button != null)
{
button.Caption = caption;
button.TooltipText = tooltip;
}
return button;
}
public CommandBarButton AddButtonToPopup(CommandBarPopup popup, int beforeIndex, string caption, string tooltip)
{
var button = popup.Controls.Add(
MsoControlType.msoControlButton,
Type.Missing,
Type.Missing,
beforeIndex,
true) as CommandBarButton;
if (button != null)
{
button.Caption = caption;
button.TooltipText = tooltip;
}
return button;
}
public List<UIHierarchyItem> GetProjectNodes(Solution solution)
{
string solutionName = solution.Properties.Item("Name").Value.ToString();
return GetProjectNodes(SolutionExplorerNode.GetItem(solutionName).UIHierarchyItems);
}
public List<UIHierarchyItem> GetProjectNodes(UIHierarchyItems topLevelItems)
{
var projects = new List<UIHierarchyItem>();
foreach (UIHierarchyItem item in topLevelItems)
{
if (IsProjectNode(item))
{
projects.Add(item);
}
else if (IsSolutionFolder(item))
{
projects.AddRange(GetProjectNodesInSolutionFolder(item));
}
}
return projects;
}
public List<UIHierarchyItem> GetProjectNodesInSolutionFolder(UIHierarchyItem item)
{
var projects = new List<UIHierarchyItem>();
if (IsSolutionFolder(item))
{
projects.AddRange(item.UIHierarchyItems.Cast<UIHierarchyItem>().Where(IsProjectNode));
}
return projects;
}
public bool IsSolutionFolder(UIHierarchyItem item)
{
return ((item.Object is Project) &&
((item.Object as Project).Kind == ProjectKinds.vsProjectKindSolutionFolder));
}
public bool IsProjectNode(UIHierarchyItem item)
{
return IsDirectProjectNode(item) || IsProjectNodeInSolutionFolder(item);
}
public bool IsDirectProjectNode(UIHierarchyItem item)
{
return ((item.Object is Project) &&
((item.Object as Project).Kind != ProjectKinds.vsProjectKindSolutionFolder));
}
public bool IsProjectNodeInSolutionFolder(UIHierarchyItem item)
{
return (item.Object is ProjectItem && ((ProjectItem)item.Object).Object is Project &&
((Project)((ProjectItem)item.Object).Object).Kind != ProjectKinds.vsProjectKindSolutionFolder);
}
public string GetSelectedText()
{
var selectedText = _dte.ActiveDocument.Selection as TextSelection;
return selectedText != null ? selectedText.Text : string.Empty;
}
public string GetSelectedLines()
{
var selectedText = _dte.ActiveDocument.Selection as TextSelection;
if (selectedText != null)
{
TextPoint topPoint = selectedText.TopPoint;
EditPoint bottomPoint = selectedText.BottomPoint.CreateEditPoint();
return bottomPoint.GetLines(topPoint.Line, bottomPoint.Line + 1);
}
return string.Empty;
}
private static bool IsBlank(string input)
{
return string.IsNullOrEmpty(input) || input.All(char.IsWhiteSpace);
}
public string GetCurrentWord()
{
var selectedText = _dte.ActiveDocument.Selection as TextSelection;
if (selectedText != null)
{
EditPoint topPoint = selectedText.TopPoint.CreateEditPoint();
string currentLine = topPoint.GetLines(topPoint.Line, topPoint.Line + 1);
if (IsBlank(currentLine))
{
return string.Empty;
}
string result;
int charIndex = topPoint.LineCharOffset - 1;
if (topPoint.AtStartOfLine ||
(!char.IsWhiteSpace(currentLine[charIndex]) && char.IsWhiteSpace(currentLine[charIndex - 1])))
{
EditPoint rightPoint = topPoint.CreateEditPoint();
rightPoint.WordRight();
result = currentLine.Substring(topPoint.LineCharOffset - 1,
rightPoint.LineCharOffset - topPoint.LineCharOffset).Trim();
}
else if (topPoint.AtEndOfLine ||
(!char.IsWhiteSpace(currentLine[charIndex - 1]) && char.IsWhiteSpace(currentLine[charIndex])))
{
EditPoint leftPoint = topPoint.CreateEditPoint();
leftPoint.WordLeft();
result = currentLine.Substring(leftPoint.LineCharOffset - 1,
topPoint.LineCharOffset - leftPoint.LineCharOffset).Trim();
}
else if (char.IsLetterOrDigit(currentLine[charIndex - 1]) &&
char.IsLetterOrDigit(currentLine[charIndex + 1]))
{
topPoint.WordLeft();
EditPoint rightPoint = topPoint.CreateEditPoint();
rightPoint.WordRight();
result = currentLine.Substring(topPoint.LineCharOffset - 1,
rightPoint.LineCharOffset - topPoint.LineCharOffset);
}
else
{
result = GetSelectedText();
}
return result;
}
return string.Empty;
}
public string GetAddinAssemblyLocation()
{
Assembly asm = Assembly.GetEntryAssembly();
return asm.Location;
}
public void Restart()
{
_dte.Quit();
Process.Start(_dte.FileName);
}
public Project GetProjectByName(Solution2 sln, string projName)
{
return sln.Projects.Cast<Project>().FirstOrDefault(p => p.Name == projName);
}
}
}