-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEncodingNormalizer.cs
More file actions
308 lines (270 loc) · 11 KB
/
EncodingNormalizer.cs
File metadata and controls
308 lines (270 loc) · 11 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
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
//------------------------------------------------------------------------------
// <copyright file="EncodingNormalizer.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using EncodingNormalizerVsx.View;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Window = System.Windows.Window;
namespace EncodingNormalizerVsx
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class EncodingNormalizer
{
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("0640f5ce-e6bc-43ba-b45e-497d70819a20");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private AsyncPackage AsyncPackage { get; }
private Window _conformWindow;
private Window _definitionWindow;
/// <summary>
/// Initializes a new instance of the <see cref="EncodingNormalizer" /> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private EncodingNormalizer(AsyncPackage package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
AsyncPackage = package;
OleMenuCommandService commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
CommandID menuCommandID = new CommandID(CommandSet, CommandId);
MenuCommand menuItem = new MenuCommand(EncodingNormalizerCallback, menuCommandID);
commandService.AddCommand(menuItem);
CommandID convertCurrentFileSaveEncodingCommand = new CommandID(CommandSet, 0x0103);
MenuCommand convertCurrentEncodingMenuCommand = new MenuCommand(ConvertCurrentFileEncoding, convertCurrentFileSaveEncodingCommand);
commandService.AddCommand(convertCurrentEncodingMenuCommand);
menuCommandID = new CommandID(CommandSet, 0x0101);
menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
//menuCommandID = new CommandID(CommandSet, 0x0102);
//menuItem = new MenuCommand(SaveEncoding, menuCommandID);
//commandService.AddCommand(menuItem);
}
}
#pragma warning disable VSTHRD100 // 避免使用 Async Void 方法
private async void ConvertCurrentFileEncoding(object sender, EventArgs e)
#pragma warning restore VSTHRD100 // 避免使用 Async Void 方法
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
// 修改用户打开的文件的编码
DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
Document document = dte.ActiveDocument;
string str = document.FullName;
new ConvertFileEncodingPage(new FileInfo(str)).Show();
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static EncodingNormalizer Instance { get; private set; }
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider => AsyncPackage;
#pragma warning disable VSTHRD100 // 避免使用 Async Void 方法
private async void EncodingNormalizerCallback(object sender, EventArgs e)
#pragma warning restore VSTHRD100 // 避免使用 Async Void 方法
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
string file = dte.Solution.FullName;
List<string> project = new List<string>();
if (dte.Solution.Projects.Count > 0)
{
//try
//{
int noLoadProjectCount = await TryParseProjectAsync(dte, project);
//}
// catch (NotImplementedException)
//{
// MessageBox.Show("The project not loaded.", "项目还没有加载完成");
// return;
//}
if (noLoadProjectCount > 0)
{
if (project.Count == 0)
{
MessageBox.Show("All project not loaded.", "全部项目都没有加载完成");
return;
}
MessageBox.Show("存在" + noLoadProjectCount + "个工程没有加载");
}
else
{
if (project.Count == 0)
{
MessageBox.Show("Cant find any project.", "没有发现工程");
return;
}
}
}
else
{
MessageBox.Show("Cant find the solution.", "少年,听说你没有打开工程");
return;
}
ConformWindow(file, project);
}
private async Task<int> TryParseProjectAsync(DTE dte, List<string> project)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
int noLoadProjectCount = 0;
foreach (object temp in dte.Solution.Projects)
{
try
{
if (temp is Project tempProject)
{
if ((tempProject).Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
foreach(var subProject in await GetSolutionFolderProjectsAsync(tempProject))
{
var directory = await ParseProjectFolderAsync(subProject);
if (!string.IsNullOrEmpty(directory))
{
project.Add(directory);
}
}
}
else
{
var directory = await ParseProjectFolderAsync(tempProject);
project.Add(directory);
}
}
}
catch (NotImplementedException)
{
noLoadProjectCount++;
}
}
return noLoadProjectCount;
}
private async Task<string> ParseProjectFolderAsync(Project project)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
string file = project.FullName;
if (!string.IsNullOrEmpty(file))
{
return new FileInfo(file).Directory?.FullName;
}
return "";
}
private async Task<List<Project>> GetSolutionFolderProjectsAsync(Project solutionFolder)
{
await AsyncPackage.JoinableTaskFactory.SwitchToMainThreadAsync();
List<Project> project = new List<Project>();
for (int i = 1; i <= solutionFolder.ProjectItems.Count; i++)
{
Project subProject = solutionFolder.ProjectItems.Item(i).SubProject;
if (subProject == null)
{
continue;
}
// If this is another solution folder, do a recursive call, otherwise add
if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
{
project.AddRange(await GetSolutionFolderProjectsAsync(subProject));
}
else
{
project.Add(subProject);
}
}
return project;
}
private void ConformWindow(string file, List<string> project)
{
if (_conformWindow != null)
{
_conformWindow.Focus();
_conformWindow.Show();
return;
}
string folder = "";
if (!string.IsNullOrEmpty(file))
{
folder = new FileInfo(file).Directory?.FullName;
}
Window window = new Window()
{
Width = 500,
Height = 500
};
ConformPage conformPage = new ConformPage();
window.Content = conformPage;
window.Title = "编码规范工具";
conformPage.Closing += (_s, _e) =>
{
window.Close();
_conformWindow = null;
};
window.Closed += (_s, _e) => { _conformWindow = null; };
conformPage.SolutionFolder = folder;
conformPage.Project = project;
window.Show();
conformPage.InspectFolderEncoding();
_conformWindow = window;
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(AsyncPackage package)
{
Instance = new EncodingNormalizer(package);
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void MenuItemCallback(object sender, EventArgs e)
{
if (_definitionWindow != null)
{
_definitionWindow.Focus();
_definitionWindow.Show();
return;
}
Window window = new Window();
DefinitionPage definitionPage = new DefinitionPage();
definitionPage.Closing += (_s, _e) =>
{
window.Close();
_definitionWindow = null;
};
window.Closed += (_s, _e) => { _definitionWindow = null; };
window.Title = "编码规范工具设置";
window.Content = definitionPage;
window.Show();
_definitionWindow = window;
}
}
}