static async Task ProcessUsingsAsync(string path)
        {
            const string searchPattern = "*.cs";
            var          collector     = new UsingCollector();

            IEnumerable <string> fileNames = Directory.EnumerateFiles(path, searchPattern, SearchOption.AllDirectories).Where(fileName => !fileName.EndsWith(".g.i.cs") && !fileName.EndsWith(".g.cs"));

            foreach (var fileName in fileNames)
            {
                string     code = File.ReadAllText(fileName);
                SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
                SyntaxNode root = await tree.GetRootAsync();

                collector.Visit(root);
            }

            var usings        = collector.UsingDirectives;
            var usingStatics  = usings.Select(n => n.ToString()).Distinct().Where(u => u.StartsWith("using static")).OrderBy(u => u);
            var orderedUsings = usings.Select(n => n.ToString()).Distinct().Except(usingStatics).OrderBy(u => u.Substring(0, u.Length - 1));

            foreach (var item in orderedUsings.Union(usingStatics))
            {
                WriteLine(item);
            }
        }
Example #2
0
        // </Snippet1>

        static void Main(string[] args)
        {
            // <Snippet2>
            SyntaxTree            tree = CSharpSyntaxTree.ParseText(programText);
            CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
            // </Snippet2>

            // <Snippet6>
            var collector = new UsingCollector();

            collector.Visit(root);
            foreach (var directive in collector.Usings)
            {
                WriteLine(directive.Name);
            }
            // </Snippet6>
        }
Example #3
0
        static void Main(string[] args)
        {
            SyntaxTree tree = SyntaxTree.ParseText(
                @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
 
namespace TopLevel
{
    using Microsoft;
    using System.ComponentModel;
 
    namespace Child1
    {
        using Microsoft.Win32;
        using System.Runtime.InteropServices;
 
        class Foo { }
    }
 
    namespace Child2
    {
        using System.CodeDom;
        using Microsoft.CSharp;
 
        class Bar { }
    }
}");
            var collector = new UsingCollector();

            foreach (var node in tree.GetRoot().DescendantNodesAndSelf())
            {
                collector.Visit(node);
            }

            foreach (var directive in collector.Usings)
            {
                Console.WriteLine(directive.Name);
            }
        }
Example #4
0
        public static void Main()
        {
            const string programText =
                @"using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using Microsoft.CodeAnalysis;
                using Microsoft.CodeAnalysis.CSharp;

                namespace TopLevel
                {
                    using Microsoft;
                    using System.ComponentModel;

                    namespace Child1
                    {
                        using Microsoft.Win32;
                        using System.Runtime.InteropServices;

                        class Foo { }
                    }

                    namespace Child2
                    {
                        using System.CodeDom;
                        using Microsoft.CSharp;

                        class Bar { }
                    }
                }";

            SyntaxTree            tree = CSharpSyntaxTree.ParseText(programText);
            CompilationUnitSyntax root = tree.GetCompilationUnitRoot();

            var collector = new UsingCollector();

            collector.Visit(root);
            foreach (var directive in collector.Usings)
            {
                Console.WriteLine(directive.Name);
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            SyntaxTree tree = SyntaxTree.ParseText(
            @"using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using Roslyn.Compilers;
            using Roslyn.Compilers.CSharp;

            namespace TopLevel
            {
                using Microsoft;
                using System.ComponentModel;

                namespace Child1
                {
                    using Microsoft.Win32;
                    using System.Runtime.InteropServices;

                    class Foo { }
                }

                namespace Child2
                {
                    using System.CodeDom;
                    using Microsoft.CSharp;

                    class Bar { }
                }
            }");
            var collector = new UsingCollector();

            foreach (var node in tree.GetRoot().DescendantNodesAndSelf())
            {
                collector.Visit(node);
            }

            foreach (var directive in collector.Usings)
            {
                Console.WriteLine(directive.Name);
            }
        }
        static async Task ProcessUsingsAsync(string path)
        {
            const string searchPattern = "*.cs";
            var collector = new UsingCollector();

            IEnumerable<string> fileNames = Directory.EnumerateFiles(path, searchPattern, SearchOption.AllDirectories).Where(fileName => !fileName.EndsWith(".g.i.cs") && !fileName.EndsWith(".g.cs"));
            foreach (var fileName in fileNames)
            {
                string code = File.ReadAllText(fileName);
                SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
                SyntaxNode root = await tree.GetRootAsync();
                collector.Visit(root);
            }

            var usings = collector.UsingDirectives;
            var usingStatics = usings.Select(n => n.ToString()).Distinct().Where(u => u.StartsWith("using static")).OrderBy(u => u);
            var orderedUsings = usings.Select(n => n.ToString()).Distinct().Except(usingStatics).OrderBy(u => u.Substring(0, u.Length - 1));
            foreach (var item in orderedUsings.Union(usingStatics))
            {
                WriteLine(item);
            }
        }