SimplificationHelpers.cs 6.2 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
P
Pilchie 已提交
4 5 6 7 8

using System.Linq;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

9 10 11 12 13 14 15 16
#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
using OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions;
#else
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Options;
#endif

P
Pilchie 已提交
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 50 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
namespace Microsoft.CodeAnalysis.Simplification
{
    internal static class SimplificationHelpers
    {
        public static readonly SyntaxAnnotation DontSimplifyAnnotation = new SyntaxAnnotation();
        public static readonly SyntaxAnnotation SimplifyModuleNameAnnotation = new SyntaxAnnotation();

        public static TNode CopyAnnotations<TNode>(SyntaxNode from, TNode to) where TNode : SyntaxNode
        {
            // Because we are removing a node that may have annotations (i.e. formatting), we need
            // to copy those annotations to the new node. However, we can only copy all annotations
            // which will mean that the new node will include a ParenthesesSimplification annotation,
            // even if didn't have one before. That results in potentially removing parentheses that
            // weren't annotated by the user. To address this, we add *another* annotation to indicate
            // that the new node shouldn't be simplified. This is to work around the
            // fact that there is no way to remove an annotation from a node in the current API. If
            // that gets added, we can clean this up.

            var dontSimplifyResult = !to.HasAnnotation(Simplifier.Annotation);

            to = from.CopyAnnotationsTo(to);

            if (dontSimplifyResult)
            {
                to = to.WithAdditionalAnnotations(DontSimplifyAnnotation);
            }

            return to;
        }

        public static SyntaxToken CopyAnnotations(SyntaxToken from, SyntaxToken to)
        {
            // Because we are removing a node that may have annotations (i.e. formatting), we need
            // to copy those annotations to the new node. However, we can only copy all annotations
            // which will mean that the new node will include a ParenthesesSimplification annotation,
            // even if didn't have one before. That results in potentially removing parentheses that
            // weren't annotated by the user. To address this, we add *another* annotation to indicate
            // that the new node shouldn't be simplified. This is to work around the
            // fact that there is no way to remove an annotation from a node in the current API. If
            // that gets added, we can clean this up.

            var dontSimplifyResult = !to.HasAnnotation(Simplifier.Annotation);

            to = from.CopyAnnotationsTo(to);

            if (dontSimplifyResult)
            {
                to = to.WithAdditionalAnnotations(DontSimplifyAnnotation);
            }

            return to;
        }

        internal static ISymbol GetOriginalSymbolInfo(SemanticModel semanticModel, SyntaxNode expression)
        {
            Contract.ThrowIfNull(expression);
            var annotation1 = expression.GetAnnotations(SymbolAnnotation.Kind).FirstOrDefault();
            if (annotation1 != null)
            {
                var typeSymbol = SymbolAnnotation.GetSymbol(annotation1, semanticModel.Compilation);
                if (IsValidSymbolInfo(typeSymbol))
                {
                    return typeSymbol;
                }
            }

            var annotation2 = expression.GetAnnotations(SpecialTypeAnnotation.Kind).FirstOrDefault();
            if (annotation2 != null)
            {
                var specialType = SpecialTypeAnnotation.GetSpecialType(annotation2);
                if (specialType != SpecialType.None)
                {
                    var typeSymbol = semanticModel.Compilation.GetSpecialType(specialType);
                    if (IsValidSymbolInfo(typeSymbol))
                    {
                        return typeSymbol;
                    }
                }
            }

            var symbolInfo = semanticModel.GetSymbolInfo(expression);
            if (!IsValidSymbolInfo(symbolInfo.Symbol))
            {
                return null;
            }

            return symbolInfo.Symbol;
        }

        internal static bool IsValidSymbolInfo(ISymbol symbol)
        {
            // name bound to only one symbol is valid
            return symbol != null && !symbol.IsErrorType();
        }
111

112
        internal static bool ShouldSimplifyThisOrMeMemberAccessExpression(
113
            SemanticModel semanticModel, OptionSet optionSet, ISymbol symbol)
114
        {
115 116 117
            // If we're accessing a static member off of this/me then we should always consider this
            // simplifiable.  Note: in C# this isn't even legal to access a static off of `this`,
            // but in VB it is legal to access a static off of `me`.
118
            if (symbol.IsStatic)
119
                return true;
120 121 122 123 124

            if ((symbol.IsKind(SymbolKind.Field) && optionSet.GetOption(CodeStyleOptions.QualifyFieldAccess, semanticModel.Language).Value ||
                (symbol.IsKind(SymbolKind.Property) && optionSet.GetOption(CodeStyleOptions.QualifyPropertyAccess, semanticModel.Language).Value) ||
                (symbol.IsKind(SymbolKind.Method) && optionSet.GetOption(CodeStyleOptions.QualifyMethodAccess, semanticModel.Language).Value) ||
                (symbol.IsKind(SymbolKind.Event) && optionSet.GetOption(CodeStyleOptions.QualifyEventAccess, semanticModel.Language).Value)))
125 126 127 128 129 130
            {
                return false;
            }

            return true;
        }
131 132 133 134 135 136 137 138 139 140

        internal static bool PreferPredefinedTypeKeywordInDeclarations(OptionSet optionSet, string language)
        {
            return optionSet.GetOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, language).Value;
        }

        internal static bool PreferPredefinedTypeKeywordInMemberAccess(OptionSet optionSet, string language)
        {
            return optionSet.GetOption(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, language).Value;
        }
P
Pilchie 已提交
141 142
    }
}