|
| 1 | +using EncodingNormalizerVsx.View; |
| 2 | + |
| 3 | +using EnvDTE; |
| 4 | + |
| 5 | +using EnvDTE80; |
| 6 | + |
| 7 | +using Microsoft.VisualStudio.Shell; |
| 8 | +using Microsoft.VisualStudio.Shell.Interop; |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.ComponentModel.Design; |
| 13 | +using System.Globalization; |
| 14 | +using System.IO; |
| 15 | +using System.Linq; |
| 16 | +using System.Threading; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using System.Windows; |
| 19 | + |
| 20 | +using Task = System.Threading.Tasks.Task; |
| 21 | +using Window = System.Windows.Window; |
| 22 | +#pragma warning disable IDE0090 // 忽略警告 |
| 23 | + |
| 24 | +namespace EncodingNormalizerVsix; |
| 25 | + |
| 26 | +/// <summary> |
| 27 | +/// Command handler |
| 28 | +/// </summary> |
| 29 | +internal sealed class EncodingNormalizer |
| 30 | +{ |
| 31 | + /// <summary> |
| 32 | + /// Command ID. |
| 33 | + /// </summary> |
| 34 | + public const int CommandId = 0x0100; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Command menu group (command set GUID). |
| 38 | + /// </summary> |
| 39 | + public static readonly Guid CommandSet = new Guid("0640f5ce-e6bc-43ba-b45e-497d70819a20"); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// VS Package that provides this command, not null. |
| 43 | + /// </summary> |
| 44 | + private readonly AsyncPackage _package; |
| 45 | + |
| 46 | + private Window _conformWindow; |
| 47 | + private Window _definitionWindow; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Initializes a new instance of the <see cref="EncodingNormalizer"/> class. |
| 51 | + /// Adds our command handlers for menu (commands must exist in the command table file) |
| 52 | + /// </summary> |
| 53 | + /// <param name="package">Owner package, not null.</param> |
| 54 | + /// <param name="commandService">Command service to add command to, not null.</param> |
| 55 | + private EncodingNormalizer(AsyncPackage package, OleMenuCommandService commandService) |
| 56 | + { |
| 57 | + _package = package ?? throw new ArgumentNullException(nameof(package)); |
| 58 | + commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); |
| 59 | + |
| 60 | + CommandID menuCommandID = new CommandID(CommandSet, CommandId); |
| 61 | + MenuCommand menuItem = new MenuCommand(EncodingNormalizerCallback, menuCommandID); |
| 62 | + commandService.AddCommand(menuItem); |
| 63 | + |
| 64 | + CommandID convertCurrentFileSaveEncodingCommand = new CommandID(CommandSet, 0x0103); |
| 65 | + MenuCommand convertCurrentEncodingMenuCommand = new MenuCommand(ConvertCurrentFileEncoding, convertCurrentFileSaveEncodingCommand); |
| 66 | + commandService.AddCommand(convertCurrentEncodingMenuCommand); |
| 67 | + |
| 68 | + menuCommandID = new CommandID(CommandSet, 0x0101); |
| 69 | + menuItem = new MenuCommand(MenuItemCallback, menuCommandID); |
| 70 | + commandService.AddCommand(menuItem); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// 修改用户打开的文件的编码 |
| 75 | + /// </summary> |
| 76 | + /// <param name="sender"></param> |
| 77 | + /// <param name="e"></param> |
| 78 | + private async void ConvertCurrentFileEncoding(object sender, EventArgs e) |
| 79 | + { |
| 80 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(_package.DisposalToken); |
| 81 | + // 修改用户打开的文件的编码 |
| 82 | + DTE dte = (DTE) await ServiceProvider.GetServiceAsync(typeof(DTE)); |
| 83 | + Document document = dte.ActiveDocument; |
| 84 | + |
| 85 | + if (document is null) |
| 86 | + { |
| 87 | + IServiceProvider serviceProvider = (IServiceProvider) ServiceProvider; |
| 88 | + VsShellUtilities.ShowMessageBox(serviceProvider, "Can not find any file opened", "Fail to convert current file encoding", OLEMSGICON.OLEMSGICON_WARNING, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); |
| 89 | + return; |
| 90 | + } |
| 91 | + string str = document.FullName; |
| 92 | + |
| 93 | + new ConvertFileEncodingPage(new FileInfo(str)).Show(); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Gets the instance of the command. |
| 98 | + /// </summary> |
| 99 | + public static EncodingNormalizer Instance |
| 100 | + { |
| 101 | + get; |
| 102 | + private set; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets the service provider from the owner package. |
| 107 | + /// </summary> |
| 108 | + private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider |
| 109 | + { |
| 110 | + get |
| 111 | + { |
| 112 | + return this._package; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private async void EncodingNormalizerCallback(object sender, EventArgs e) |
| 117 | + { |
| 118 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(_package.DisposalToken); |
| 119 | + DTE dte = (DTE) await ServiceProvider.GetServiceAsync(typeof(DTE)); |
| 120 | + string file = dte.Solution.FullName; |
| 121 | + List<string> project = new List<string>(); |
| 122 | + if (dte.Solution.Projects.Count > 0) |
| 123 | + { |
| 124 | + int noLoadProjectCount = TryParseProject(dte, project); |
| 125 | + |
| 126 | + if (noLoadProjectCount > 0) |
| 127 | + { |
| 128 | + if (project.Count == 0) |
| 129 | + { |
| 130 | + MessageBox.Show("All project not loaded.", "全部项目都没有加载完成"); |
| 131 | + return; |
| 132 | + } |
| 133 | + MessageBox.Show("存在" + noLoadProjectCount + "个工程没有加载"); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + if (project.Count == 0) |
| 138 | + { |
| 139 | + MessageBox.Show("Cant find any project.", "没有发现工程"); |
| 140 | + return; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + MessageBox.Show("Cant find the solution.", "少年,听说你没有打开工程"); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + ConformWindow(file, project); |
| 151 | + } |
| 152 | + |
| 153 | + private static int TryParseProject(DTE dte, List<string> project) |
| 154 | + { |
| 155 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 156 | + int noLoadProjectCount = 0; |
| 157 | + |
| 158 | + foreach (object temp in dte.Solution.Projects) |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + if (temp is Project) |
| 163 | + { |
| 164 | + if (((Project) temp).Kind == ProjectKinds.vsProjectKindSolutionFolder) |
| 165 | + { |
| 166 | + project.AddRange(GetSolutionFolderProjects((Project) temp).Select(ParseProjectFolder)); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + project.Add(ParseProjectFolder((Project) temp)); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + catch (NotImplementedException) |
| 175 | + { |
| 176 | + noLoadProjectCount++; |
| 177 | + } |
| 178 | + } |
| 179 | + return noLoadProjectCount; |
| 180 | + } |
| 181 | + |
| 182 | + private static string ParseProjectFolder(Project project) |
| 183 | + { |
| 184 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 185 | + string file = project.FullName; |
| 186 | + if (!string.IsNullOrEmpty(file)) |
| 187 | + { |
| 188 | + return new FileInfo(file).Directory?.FullName; |
| 189 | + } |
| 190 | + return ""; |
| 191 | + } |
| 192 | + |
| 193 | + private static List<Project> GetSolutionFolderProjects(Project solutionFolder) |
| 194 | + { |
| 195 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 196 | + List<Project> project = new List<Project>(); |
| 197 | + for (int i = 1; i <= solutionFolder.ProjectItems.Count; i++) |
| 198 | + { |
| 199 | + Project subProject = solutionFolder.ProjectItems.Item(i).SubProject; |
| 200 | + if (subProject == null) |
| 201 | + { |
| 202 | + continue; |
| 203 | + } |
| 204 | + |
| 205 | + // If this is another solution folder, do a recursive call, otherwise add |
| 206 | + if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder) |
| 207 | + { |
| 208 | + project.AddRange(GetSolutionFolderProjects(subProject)); |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + project.Add(subProject); |
| 213 | + } |
| 214 | + } |
| 215 | + return project; |
| 216 | + } |
| 217 | + |
| 218 | + private void ConformWindow(string file, List<string> project) |
| 219 | + { |
| 220 | + if (_conformWindow != null) |
| 221 | + { |
| 222 | + _conformWindow.Focus(); |
| 223 | + _conformWindow.Show(); |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + string folder = ""; |
| 228 | + if (!string.IsNullOrEmpty(file)) |
| 229 | + { |
| 230 | + folder = new FileInfo(file).Directory?.FullName; |
| 231 | + } |
| 232 | + Window window = new Window() |
| 233 | + { |
| 234 | + Width = 500, |
| 235 | + Height = 500 |
| 236 | + }; |
| 237 | + ConformPage conformPage = new ConformPage(); |
| 238 | + window.Content = conformPage; |
| 239 | + window.Title = "编码规范工具"; |
| 240 | + conformPage.Closing += (_s, _e) => |
| 241 | + { |
| 242 | + window.Close(); |
| 243 | + _conformWindow = null; |
| 244 | + }; |
| 245 | + window.Closed += (_s, _e) => { _conformWindow = null; }; |
| 246 | + conformPage.SolutionFolder = folder; |
| 247 | + conformPage.Project = project; |
| 248 | + window.Show(); |
| 249 | + conformPage.InspectFolderEncoding(); |
| 250 | + _conformWindow = window; |
| 251 | + } |
| 252 | + |
| 253 | + /// <summary> |
| 254 | + /// Initializes the singleton instance of the command. |
| 255 | + /// </summary> |
| 256 | + /// <param name="package">Owner package, not null.</param> |
| 257 | + public static async Task InitializeAsync(AsyncPackage package) |
| 258 | + { |
| 259 | + // Switch to the main thread - the call to AddCommand in HelloCommand's constructor requires |
| 260 | + // the UI thread. |
| 261 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); |
| 262 | + |
| 263 | + OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; |
| 264 | + Instance = new EncodingNormalizer(package, commandService); |
| 265 | + } |
| 266 | + |
| 267 | + /// <summary> |
| 268 | + /// This function is the callback used to execute the command when the menu item is clicked. |
| 269 | + /// See the constructor to see how the menu item is associated with this function using |
| 270 | + /// OleMenuCommandService service and MenuCommand class. |
| 271 | + /// </summary> |
| 272 | + /// <param name="sender">Event sender.</param> |
| 273 | + /// <param name="e">Event args.</param> |
| 274 | + private void MenuItemCallback(object sender, EventArgs e) |
| 275 | + { |
| 276 | + if (_definitionWindow != null) |
| 277 | + { |
| 278 | + _definitionWindow.Focus(); |
| 279 | + _definitionWindow.Show(); |
| 280 | + return; |
| 281 | + } |
| 282 | + Window window = new Window(); |
| 283 | + DefinitionPage definitionPage = new DefinitionPage(); |
| 284 | + definitionPage.Closing += (_s, _e) => |
| 285 | + { |
| 286 | + window.Close(); |
| 287 | + _definitionWindow = null; |
| 288 | + }; |
| 289 | + window.Closed += (_s, _e) => { _definitionWindow = null; }; |
| 290 | + window.Title = "编码规范工具设置"; |
| 291 | + window.Content = definitionPage; |
| 292 | + window.Show(); |
| 293 | + |
| 294 | + _definitionWindow = window; |
| 295 | + } |
| 296 | +} |
0 commit comments