public static void AddUsing(this ICSharpFile file, string namespaceName, [NotNull] CSharpElementFactory factory) { var usingSymbolDirectives = file.ImportsEnumerable.OfType <IUsingSymbolDirective>().ToArray(); var taskUsing = factory.CreateUsingDirective(namespaceName); if (usingSymbolDirectives.All(i => i.ImportedSymbolName.QualifiedName != namespaceName)) { file.AddImport(taskUsing, true); } }
private void AddUsings(MockInfo[] mockInfos, CSharpElementFactory factory) { var usingSymbolDirectives = file.ImportsEnumerable.OfType <IUsingSymbolDirective>().ToArray(); var namespaces = mockInfos.Select(x => x.Type.GetScalarType()?.GetClrName().GetNamespaceName()).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray(); foreach (var namespaceName in namespaces) { var taskUsing = factory.CreateUsingDirective(namespaceName); if (usingSymbolDirectives.All(i => i.ImportedSymbolName.QualifiedName != namespaceName)) { file.AddImport(taskUsing, true); } } }
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 IUsingDirective AddImport(IUsingDirective param, bool saveUsingListPosition = false) { return(_file.AddImport(param, saveUsingListPosition)); }