NodeHelpers.cs 7.2 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

using System;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Test.Utilities
{
    public static partial class NodeHelpers
    {
        // TODO: Having this as a shared property makes this less discoverable. This can also be bad
        // if we ever have an app where multiple threads need to use this with each thread operating
        // on trees from a different language. We will need to fix this if we ever need to write app
        // test that does something like this.
        public static ISyntaxNodeKindProvider KindProvider
        {
            get;
            set;
        }

        public static string GetKind(this SyntaxNodeOrToken n)
        {
            return KindProvider.Kind(n);
        }

        public static string GetKind(this SyntaxNode n)
        {
            return KindProvider.Kind(n);
        }

        public static string GetKind(this SyntaxToken n)
        {
            return KindProvider.Kind(n);
        }

        public static string GetKind(this SyntaxTrivia n)
        {
            return KindProvider.Kind(n);
        }

        public static bool IsIdentifier(this SyntaxToken n)
        {
            return n.GetKind().Contains("Identifier") && n.Parent != null && n.Parent.GetKind().Contains("Name");
        }

        public static bool IsKeyword(this SyntaxToken n)
        {
            var kind = n.GetKind();
50
            return kind.EndsWith("Keyword", StringComparison.Ordinal) || (kind.Contains("Identifier") && n.Parent != null && !n.Parent.GetKind().Contains("Name"));
P
Pilchie 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        }

        public static bool IsLiteral(this SyntaxToken n)
        {
            return n.GetKind().Contains("Literal");
        }

        public static bool IsComment(this SyntaxTrivia n)
        {
            return n.GetKind().Contains("Comment");
        }

        public static bool IsDocumentationComment(this SyntaxTrivia n)
        {
            return n.GetKind().Contains("DocumentationComment");
        }

        public static bool IsDisabledOrSkippedText(this SyntaxTrivia n)
        {
            var kind = n.GetKind();
            return kind.Contains("Disabled") || kind.Contains("Skipped");
        }

        public static SyntaxNode GetRootNode(this SyntaxNodeOrToken node)
        {
            SyntaxNode retVal = null;
            if (node.IsNode)
            {
                retVal = node.AsNode().GetRootNode();
            }
            else
            {
                retVal = node.AsToken().GetRootNode();
            }

            return retVal;
        }

        public static SyntaxNode GetRootNode(this SyntaxToken node)
        {
            SyntaxNode retVal = node.Parent;
            if (retVal != null)
            {
                while (retVal.Parent != null)
                {
                    retVal = retVal.Parent;
                }
            }

            return retVal;
        }

        public static SyntaxNode GetRootNode(this SyntaxNode node)
        {
            var retVal = node.Parent == null ? node : node.Parent;
            while (retVal.Parent != null)
            {
                retVal = retVal.Parent;
            }

            return retVal;
        }

        public static SyntaxNode GetRootNode(this SyntaxTrivia node)
        {
            SyntaxNode retVal = node.Token.Parent;
            if (retVal != null)
            {
                while (retVal != null)
                {
                    retVal = retVal.Parent;
                }
            }

            return retVal;
        }

        public static NodeInfo GetNodeInfo(this SyntaxNodeOrToken nodeOrToken)
        {
            NodeInfo retVal = null;
            if (nodeOrToken.IsNode)
            {
                retVal = GetNodeInfo(nodeOrToken.AsNode());
            }
            else
            {
                retVal = GetNodeInfo(nodeOrToken.AsToken());
            }

            return retVal;
        }

        public static NodeInfo GetNodeInfo(this SyntaxNode node)
        {
            var typeObject = node.GetType();
            var nodeClassName = typeObject.Name;
147
            var properties = typeObject.GetTypeInfo().DeclaredProperties;
P
Pilchie 已提交
148 149 150 151 152 153 154 155 156 157
            return new NodeInfo(typeObject.Name, (
                from p in properties
                where IsField(p)
                select GetFieldInfo(p, node)).ToArray());
        }

        public static NodeInfo GetNodeInfo(this SyntaxToken token)
        {
            var typeObject = token.GetType();
            var nodeClassName = typeObject.Name;
158
            var properties = typeObject.GetTypeInfo().DeclaredProperties;
P
Pilchie 已提交
159 160 161 162 163 164 165 166 167 168
            return new NodeInfo(typeObject.Name, (
                from p in properties
                where IsField(p)
                select GetFieldInfo(p, token)).ToArray());
        }

        public static NodeInfo GetNodeInfo(this SyntaxTrivia trivia)
        {
            var typeObject = trivia.GetType();
            var nodeClassName = typeObject.Name;
169
            var properties = typeObject.GetTypeInfo().DeclaredProperties;
P
Pilchie 已提交
170 171 172 173 174 175 176 177 178 179
            return new NodeInfo(typeObject.Name, (
                from p in properties
                where IsField(p)
                select GetFieldInfo(p, trivia)).ToArray());
        }

        //Does this property refer to a field?
        private static bool IsField(PropertyInfo prop)
        {
            var typeObject = prop.PropertyType;
180 181 182 183 184 185 186 187 188 189 190 191
            if (typeObject == typeof(int) || 
                typeObject == typeof(uint) || 
                typeObject == typeof(long) || 
                typeObject == typeof(ulong) || 
                typeObject == typeof(bool) || 
                typeObject == typeof(string) ||
                typeObject == typeof(float) || 
                typeObject == typeof(double) || 
                typeObject == typeof(char) ||
                typeObject == typeof(DateTime) ||
                typeObject == typeof(decimal) || 
                typeObject.GetTypeInfo().IsEnum)
P
Pilchie 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //Only called if IsField returns true. Get the name/type/value of this field and packages into a FieldInfo.
        private static NodeInfo.FieldInfo GetFieldInfo(PropertyInfo prop, SyntaxNode node)
        {
            return new NodeInfo.FieldInfo(prop.Name, prop.PropertyType, prop.GetValue(node, null));
        }

        //Only called if IsField returns true. Get the name/type/value of this field and packages into a FieldInfo.
        private static NodeInfo.FieldInfo GetFieldInfo(PropertyInfo prop, SyntaxToken token)
        {
            return new NodeInfo.FieldInfo(prop.Name, prop.PropertyType, prop.GetValue(token, null));
        }

        //Only called if IsField returns true. Get the name/type/value of this field and packages into a FieldInfo.
        private static NodeInfo.FieldInfo GetFieldInfo(PropertyInfo prop, SyntaxTrivia trivia)
        {
            return new NodeInfo.FieldInfo(prop.Name, prop.PropertyType, prop.GetValue(trivia, null));
        }
    }
219
}