Ejemplo n.º 1
0
        public void GetImportedNamespaces_CS()
        {
            var src =
                @"using System;

namespace Test
{
}
";

            var document      = src.CreateDocument();
            var syntaxTree    = document.GetSyntaxTreeAsync().Result;
            var compilation   = document.Project.GetCompilationAsync().Result;
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var result        = ImportedNamespace.GetImportedNamespaces(syntaxTree, semanticModel);

            Assert.AreEqual(1, result.Count);
        }
Ejemplo n.º 2
0
        public static async Task AddMissingNamespacesAsync(this DocumentEditor editor, NamespaceSet namespaces,
                                                           Compilation compilation, INamespaceSymbol containingNamespace, SyntaxTree syntaxTree, SemanticModel semanticModel, CancellationToken ct)
        {
            namespaces = new NamespaceSet(namespaces); // Make a copy because namespaces is a input parameter

            namespaces.ExceptWith(containingNamespace.GetImplicitlyImportedNamespaces());
            if (namespaces.Count == 0)
            {
                return;
            }

            var globalImports = compilation.GetGlobalImports().ToArray();

            if (globalImports.Length > 0)
            {
                namespaces.RemoveWhere(x => globalImports.Contains(x.ToDisplayString()));
            }

            var importedNamespaces = ImportedNamespace.GetImportedNamespaces(syntaxTree, semanticModel);
            await editor.AddMissingNamespaces(namespaces, importedNamespaces, ImportedNamespace.GetAddFunc(semanticModel.Language), ct);
        }