Beispiel #1
0
        private FileReferenceInfo ParseFile(string path, CSharpParseOptions options)
        {
            var content      = File.ReadAllText(path);
            var contentLines = content.Split('\n');
            var tree         = CSharpSyntaxTree.ParseText(content, options);

            var descendants = tree.GetRoot()
                              .DescendantNodes(node =>
                                               node is CompilationUnitSyntax ||
                                               node is NamespaceDeclarationSyntax ||
                                               node is EnumDeclarationSyntax ||
                                               node is InterfaceDeclarationSyntax ||
                                               node is StructDeclarationSyntax ||
                                               node is ClassDeclarationSyntax)
                              .ToList();

            var namespaces = descendants
                             .OfType <NamespaceDeclarationSyntax>()
                             .Select(x => x.Name.ToString().Split('.'))
                             .SelectMany(x => Enumerable
                                         .Range(1, x.Length)
                                         .Select(n => string.Join(".", x.Take(n))));

            var usings = descendants
                         .OfType <UsingDirectiveSyntax>()
                         .Select(x => x.Name.ToString())
                         .Concat(namespaces)
                         .ToArray();

            var fileInfo = new FileReferenceInfo(path, contentLines, usings);

            var references = new IEnumerable <SymbolReference>[]
            {
                // Methods
                descendants
                .OfType <BaseMethodDeclarationSyntax>()
                .Select(method => new MethodReference(fileInfo, method)),

                // Types
                descendants
                .OfType <BaseTypeDeclarationSyntax>()
                .Select(method => new TypeReference(fileInfo, method)),

                // Properties
                descendants
                .OfType <PropertyDeclarationSyntax>()
                .Select(method => new PropertyReference(fileInfo, method)),
            };

            fileInfo.Symbols.AddRange(references.SelectMany(x => x));

            return(fileInfo);
        }
        public SymbolReference(FileReferenceInfo file, SyntaxNode node)
        {
            Identifier = NameResolver.GetFullName(node);
            File       = file;

            // Get line span
            var loc = node.GetLocation().GetLineSpan();

            StartLine = loc.StartLinePosition.Line;
            EndLine   = loc.EndLinePosition.Line;

            // Expand line span to include xml doc
            while (
                StartLine > 0 &&
                StartLine - 1 < file.Lines.Length &&
                Regex.IsMatch(file.Lines[StartLine - 1], "\\s*///")
                )
            {
                StartLine--;
            }

            // Determine whether the symbol is visible to external assemblies
            IsExported = IsExportedCore(node);
        }
 public TypeReference(FileReferenceInfo file, SyntaxNode node) :
     base(file, node)
 {
 }
Beispiel #4
0
 public MethodReference(FileReferenceInfo file, BaseMethodDeclarationSyntax node) :
     base(file, node)
 {
     this.node = node;
 }
Beispiel #5
0
 public PropertyReference(FileReferenceInfo file, SyntaxNode node) :
     base(file, node)
 {
 }