/// <summary> /// The execute transaction inner. /// </summary> /// <param name="solution"> /// The solution. /// </param> /// <param name="textControl"> /// The text control. /// </param> public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl) { ITreeNode element = Utils.GetElementAtCaret(solution, textControl); if (element == null) { return; } IUsingList usingList = element.GetContainingNode(typeof(IUsingList), false) as IUsingList; if (usingList == null) { return; } ICSharpFile file = Utils.GetCSharpFile(solution, textControl); // This violation will only run if there are some using statements and definately at least 1 namespace // so [0] index will always be OK. ICSharpNamespaceDeclaration firstNamespace = file.NamespaceDeclarations[0]; foreach (IUsingDirective usingDirectiveNode in usingList.Imports) { firstNamespace.AddImportBefore(usingDirectiveNode, null); file.RemoveImport(usingDirectiveNode); } // Now sort the Usings into order. OrderUsingsBulbItem orderUsingsBulbItem = new OrderUsingsBulbItem(); orderUsingsBulbItem.ExecuteTransactionInner(solution, textControl); }
private static void SortImports(ICSharpFile psiFile) { IUsingDirective[] imports = psiFile.Imports.ToArray(); string defaultNamespaceFirstPart = null; IProject project = psiFile.GetSourceFile().ToProjectFile()?.GetProject(); if (project?.ProjectProperties is CSharpProjectProperties projectProperties) { defaultNamespaceFirstPart = projectProperties.CSharpBuildSettings.DefaultNamespace.SubstringBefore("."); } imports.Sort((x, y) => CompareUsingDirectives(x, y, defaultNamespaceFirstPart)); foreach (IUsingDirective import in imports) { psiFile.RemoveImport(import); } foreach (IUsingDirective import in imports) { psiFile.AddImport(import); } }
public void RemoveImport(IUsingDirective param) { _file.RemoveImport(param); }