public static void Invoke(TargetAction action, CleanupOptions cleanupOptions) { try { var projects = DteServiceProvider.Instance.ActiveSolutionProjects as Array; var currentProject = projects.GetValue(0) as Project; if (currentProject.ProjectItems == null) { return; } if (currentProject.FullName.EndsWith(".shproj", StringComparison.OrdinalIgnoreCase)) { System.Windows.MessageBox .Show("Clean up can't be called direlctly on Shared Project", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } for (var i = 1; i <= currentProject.ProjectItems.Count; i++) { ActionCSharpOnProjectItem.Action(currentProject.ProjectItems.Item(i), action, cleanupOptions); } } catch (Exception e) { ErrorNotification.WriteErrorToOutputWindow(e); ErrorNotification.WriteErrorToFile(e); ProcessActions.GeeksProductivityToolsProcess(); } }
protected virtual async Task RunAsync(ProjectItem item) { ProjectItemDetails = new ProjectItemDetailsType(item); if (IsReportOnlyMode) { await RefreshResultAsync(item.ToSyntaxNode()); } UNModifiedProjectItemDetails = ProjectItemDetails; var initialSourceNode = CleanUpAsync(ProjectItemDetails.InitialSourceNode); if (!IsReportOnlyMode) { try { var result = await initialSourceNode; await SaveResultAsync(result); } catch (Exception ex) { ErrorNotification.WriteErrorToFile(ex); } } }
// public static string GetSolutionName(DTE2 app) // { // if (app == null || app.Solution == null || string.IsNullOrEmpty(app.Solution.FullName)) return ""; // return Path.GetFileNameWithoutExtension(app.Solution.FullName); // } //public static string[] FindSolutionDirectories(DTE2 app) //{ // ThreadHelper.JoinableTaskFactory // .Run(async delegate // { // await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // }); // var basePaths = new List<string>(); // if (app.Solution != null) // { // for (var i = 1; i <= app.Solution.Projects.Count; i++) // { // var projectItem = app.Solution.Projects.Item(i); // AddPathFromProjectItem(basePaths, projectItem); // } // return basePaths.ToArray(); // } // app.StatusBar.Text = "No solution or project is identified. app.Solution is " + // (app.Solution?.GetType().Name).Or("NULL"); // App.DTE = (DTE2)TidyCSharpPackage.GetGlobalService(typeof(DTE2)); // return null; //} static void AddPathFromProjectItem(List <string> basePaths, Project projectItem) { if (projectItem == null) { return; } try { // Project var projectFileName = projectItem.FileName; if (!string.IsNullOrWhiteSpace(projectFileName)) { if (projectItem.Properties.Item("FullPath").Value is string fullPath) { basePaths.Add(fullPath); } } else { // Folder for (var i = 1; i <= projectItem.ProjectItems.Count; i++) { AddPathFromProjectItem(basePaths, projectItem.ProjectItems.Item(i).Object as Project); } } } catch (Exception err) { ErrorNotification.WriteErrorToFile(err); ErrorNotification.WriteErrorToOutputWindow(err); } }
public static void Invoke(TargetAction action, CleanupOptions cleanupOptions) { try { var projects = SolutionActions.FindProjects(DteServiceProvider.Instance); for (var i = 0; i < projects.Count; i++) { var currentProject = projects[i]; if (currentProject.ProjectItems == null) { continue; } if (currentProject.FullName.EndsWith(".shproj", StringComparison.OrdinalIgnoreCase)) { continue; } for (var j = 1; j < currentProject.ProjectItems.Count; j++) { ActionCSharpOnProjectItem.Action(currentProject.ProjectItems.Item(j), action, cleanupOptions); } } } catch (Exception e) { ErrorNotification.WriteErrorToFile(e); ErrorNotification.WriteErrorToOutputWindow(e); ProcessActions.GeeksProductivityToolsProcess(); } }
public override async Task <SyntaxNode> CleanUpAsync(SyntaxNode initialSourceNode) { var item = ProjectItemDetails.ProjectItem; try { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); item.Open(Constants.vsViewKindCode); var document = item.Document; document.Activate(); try { document.DTE.ExecuteCommand(UsingsCommands.RemoveAndSortCommandName); } catch (Exception ex) { if (ex.Message != "Command \"Edit.RemoveAndSort\" is not available.") { throw; } document.Activate(); document.DTE.ExecuteCommand(UsingsCommands.RemoveAndSortCommandName); } var doc = (EnvDTE.TextDocument)(document.Object("TextDocument")); var p = doc.StartPoint.CreateEditPoint(); var s = p.GetText(doc.EndPoint); var modified = SyntaxFactory.ParseSyntaxTree(s); if (IsReportOnlyMode && !IsEquivalentToUNModified(await modified.GetRootAsync())) { CollectMessages(new ChangesReport(initialSourceNode) { LineNumber = 1, Column = 1, Message = "Your Using usage is not good", Generator = nameof(UsingDirectiveOrganizer) }); document.Undo(); return(initialSourceNode); } document.Save(); } catch (Exception e) { ErrorNotification.WriteErrorToFile(e, initialSourceNode.GetFilePath()); ErrorNotification.WriteErrorToOutputWindow(e, initialSourceNode.GetFilePath()); ProcessActions.GeeksProductivityToolsProcess(); } return(item.ToSyntaxNode()); }
static IEnumerable <Project> GetSolutionFolderProjects(Project solutionFolder) { try { if (solutionFolder.ProjectItems == null) { return(null); } var result = new List <Project>(); for (var i = 1; i <= solutionFolder.ProjectItems.Count; i++) { var subProject = solutionFolder.ProjectItems.Item(i).SubProject; if (subProject == null) { continue; } // another solution folder if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder) { var solutionFolderProjects = GetSolutionFolderProjects(subProject); if (solutionFolder != null) { result.AddRange(GetSolutionFolderProjects(subProject)); } } else { result.Add(subProject); } } return(result); } catch (Exception e) { ErrorNotification.WriteErrorToFile(e); ErrorNotification.WriteErrorToOutputWindow(e); ProcessActions.GeeksProductivityToolsProcess(); return(null); } }
public static void Invoke(TargetAction action, CleanupOptions cleanupOptions) { try { var ideSelectedItems = DteServiceProvider.Instance.SelectedItems; for (int itemIndex = 1; itemIndex <= ideSelectedItems.Count; itemIndex++) { var selectItem = ideSelectedItems.Item(itemIndex); var selectedProjectItem = selectItem.ProjectItem; if (selectedProjectItem != null) { if (selectedProjectItem.ProjectItems == null || selectedProjectItem.ProjectItems.Count == 0 && action != null) { action(selectedProjectItem, cleanupOptions, true); } else { ActionCSharpOnProjectItem.Action(selectedProjectItem, action, cleanupOptions); } } else if (selectItem.Project != null) { ActionCSharpOnProject.Invoke(action, cleanupOptions); } else { ActionCSharpOnSolution.Invoke(action, cleanupOptions); } } } catch (Exception e) { ErrorNotification.WriteErrorToFile(e); ErrorNotification.WriteErrorToOutputWindow(e); ProcessActions.GeeksProductivityToolsProcess(); } }
public static void DoCleanup(ProjectItem item, CleanupOptions cleanupOptions, bool fileWindowMustBeOpend = false) { if (!item.IsCsharpFile() || item.IsCSharpDesignerFile()) { return; } try { var path = item.Properties.Item("FullPath").Value.ToString(); if (path.EndsWithAny(new[] { "AssemblyInfo.cs", "TheApplication.cs" })) { return; } var documentText = item.ToSyntaxNode().SyntaxTree.GetText().ToString(); if (documentText.Contains("[EscapeGCop(\"Auto generated code.\")]")) { return; } if (item.ToSyntaxNode() .DescendantNodesOfType <AttributeSyntax>() .Any(x => x.Name.ToString() == "EscapeGCop" && x.ArgumentList != null && x.ArgumentList.Arguments.FirstOrDefault().ToString() == "\"Auto generated code.\"")) { return; } var window = item.Open(Constants.vsViewKindCode); window.Activate(); foreach (var actionTypeItem in cleanupOptions.ActionTypes) { if (actionTypeItem == VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces) { continue; } if (actionTypeItem == VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives) { continue; } if (actionTypeItem == VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods) { continue; } CodeCleanerHost.Run(item, actionTypeItem, cleanupOptions); } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces)) { CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces, cleanupOptions); } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives)) { window.Document.Close(vsSaveChanges.vsSaveChangesYes); CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives, cleanupOptions); if (fileWindowMustBeOpend == false) { window = item.Open(Constants.vsViewKindCode); window.Activate(); } } else { window.Document.Save(); } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods)) { CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods, cleanupOptions); } if (fileWindowMustBeOpend == false) { window.Close(vsSaveChanges.vsSaveChangesYes); } } catch (Exception e) { ErrorNotification.WriteErrorToFile(e, item.Properties.Item("FullPath").Value.ToString()); ErrorNotification.WriteErrorToOutputWindow(e, item.Properties.Item("FullPath").Value.ToString()); ProcessActions.GeeksProductivityToolsProcess(); } }
public static void ReportOnlyDoNotCleanup(ProjectItem item, CleanupOptions cleanupOptions, bool fileWindowMustBeOpend = false) { if (!item.IsCsharpFile() || item.IsCSharpDesignerFile()) { return; } try { ThreadHelper.JoinableTaskFactory .Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); }); // Sometimes cannot find document's file try { var documentText = item.ToSyntaxNode().SyntaxTree.GetText().ToString(); if (documentText.Contains("[EscapeGCop(\"Auto generated code.\")]")) { return; } } catch { return; } var path = item.Properties.Item("FullPath").Value.ToString(); if (path.EndsWithAny(new[] { "AssemblyInfo.cs", "TheApplication.cs" })) { return; } using (var tidyruntimelog = new StreamWriter(Path.Combine(Path.GetTempPath(), "TidyCurrentfilelog.txt"), true)) tidyruntimelog.WriteLine(path); foreach (var actionTypeItem in cleanupOptions.ActionTypes) { if (actionTypeItem != VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces && actionTypeItem != VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives && actionTypeItem != VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods) { var watch = System.Diagnostics.Stopwatch.StartNew(); CodeCleanerHost.Run(item, actionTypeItem, cleanupOptions, true); watch.Stop(); using (var tidyruntimelog = new StreamWriter(Path.Combine(Path.GetTempPath(), "TidyCurrentActionslog.txt"), true)) { tidyruntimelog.WriteLine("Phase1-" + actionTypeItem.ToString() + "-" + watch.ElapsedMilliseconds + " ms"); } } } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces)) { var watch = System.Diagnostics.Stopwatch.StartNew(); CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.NormalizeWhiteSpaces, cleanupOptions, true); watch.Stop(); using (var tidyruntimelog = new StreamWriter(Path.Combine(Path.GetTempPath(), "TidyCurrentActionslog.txt"), true)) { tidyruntimelog.WriteLine("Phase2-" + "NormalizeWhiteSpaces" + "-" + watch.ElapsedMilliseconds + " ms"); } } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives)) { var watch = System.Diagnostics.Stopwatch.StartNew(); CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.OrganizeUsingDirectives, cleanupOptions, true); watch.Stop(); using (var tidyruntimelog = new StreamWriter(Path.Combine(Path.GetTempPath(), "TidyCurrentActionslog.txt"), true)) { tidyruntimelog.WriteLine("Phase3-" + "OrganizeUsingDirectives" + "-" + watch.ElapsedMilliseconds + " ms"); } } if (cleanupOptions.ActionTypes.Contains(VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods)) { var watch = System.Diagnostics.Stopwatch.StartNew(); CodeCleanerHost.Run(item, VSIX.TidyCSharp.Cleanup.CodeCleanerType.ConvertMsharpGeneralMethods, cleanupOptions, true); watch.Stop(); using (var tidyruntimelog = new StreamWriter(Path.Combine(Path.GetTempPath(), "TidyCurrentActionslog.txt"), true)) { tidyruntimelog.WriteLine("Phase4-" + "ConvertMsharpGeneralMethods" + "-" + watch.ElapsedMilliseconds + " ms"); } } } catch (Exception e) { ErrorNotification.WriteErrorToFile(e, item.Properties.Item("FullPath").Value.ToString()); ErrorNotification.WriteErrorToOutputWindow(e, item.Properties.Item("FullPath").Value.ToString()); ProcessActions.GeeksProductivityToolsProcess(); } }