Beispiel #1
0
 public ApiCall(string parentPropertyName,
                string factoryMethodName,
                ArgList arguments,
                bool useCurliesInsteadOfParentheses = false) : this(parentPropertyName, new MethodCall(factoryMethodName, arguments))
 {
     useCurly = useCurliesInsteadOfParentheses;
 }
Beispiel #2
0
        private ApiCall QuoteTrivia(SyntaxTrivia syntaxTrivia)
        {
            string factoryMethodName = "SyntaxFactory.Trivia";
            string text = syntaxTrivia.ToString();

            if (syntaxTrivia.FullSpan.Length == 0 || (syntaxTrivia.IsKind(SyntaxKind.WhitespaceTrivia) && UseDefaultFormatting))
            {
                return(null);
            }

            FieldInfo field = null;

            if (triviaFactoryFields.TryGetValue(syntaxTrivia.ToString( ), out field) &&
                ((SyntaxTrivia)field.GetValue(null)).CSharpKind( ) == syntaxTrivia.CSharpKind( ))
            {
                return(UseDefaultFormatting ? null : new ApiCall(null, "SyntaxFactory." + field.Name));
            }

            if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text) && syntaxTrivia.IsKind(SyntaxKind.WhitespaceTrivia))
            {
                if (UseDefaultFormatting)
                {
                    return(null);
                }
                factoryMethodName = "SyntaxFactory.Whitespace";
            }

            if (syntaxTrivia.IsTriviaAny(SyntaxKind.SingleLineCommentTrivia,
                                         SyntaxKind.MultiLineCommentTrivia))
            {
                factoryMethodName = "SyntaxFactory.Comment";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
            {
                factoryMethodName = "SyntaxFactory.PreprocessingMessage";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.DisabledTextTrivia))
            {
                factoryMethodName = "SyntaxFactory.DisabledText";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.DocumentationCommentExteriorTrivia))
            {
                factoryMethodName = "SyntaxFactory.DocumentationCommentExterior";
            }

            object argument = "@\"" + Escape(syntaxTrivia.ToString()) + "\"";

            if (syntaxTrivia.HasStructure)
            {
                argument = QuoteNode(syntaxTrivia.GetStructure( ), "Structure");
            }

            return(new ApiCall(null, factoryMethodName, ArgList.Create(argument)));
        }
Beispiel #3
0
        /// <summary>
        /// Adds information about subsequent modifying fluent interface style calls on an object (like
        /// foo.With(...).With(...))
        /// </summary>
        public static void AddModifyingCalls(object treeElement, ApiCall apiCall, APIList values)
        {
            var methods = treeElement.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var value in values)
            {
                var methodName = "With" + value.Name.ProperCase();
                if (!methods.Any(m => m.Name == methodName))
                {
                    throw new NotSupportedException( );
                }
                methodName = "." + methodName;
                apiCall.AddModifyingCall(new MethodCall(methodName, ArgList.Create(value)));
            }
        }
Beispiel #4
0
        private ApiCall QuoteList(IEnumerable syntaxList, string name)
        {
            IEnumerable <object> sourceList = syntaxList.Cast <object>();
            string mName    = "SyntaxFactory.List";
            string listType = null;
            var    pType    = syntaxList.GetType();

            if (pType.IsGenericType)
            {
                var mType = pType.GetGenericArguments()[0].Name;
                listType = mType;

                if (pType.GetGenericTypeDefinition( ) == typeof(SeparatedSyntaxList <>))
                {
                    listType   = "SyntaxNodeOrToken";
                    mName      = "SyntaxFactory.SeparatedList";
                    sourceList = ((SyntaxNodeOrTokenList)syntaxList.GetType( ).GetMethod("GetWithSeparators").Invoke(syntaxList, null)).Cast <object>( ).ToArray( );
                }
                mName += "<" + mType + ">";
            }
            if (pType.Name == "SyntaxTokenList")
            {
                mName = "SyntaxFactory.TokenList";
            }
            if (pType.Name == "SyntaxTriviaList")
            {
                mName = "SyntaxFactory.TriviaList";
            }
            var elements = ArgList.Create(sourceList.Select(o => Quote(o)).Where(cb => cb != null).ToArray());

            if (elements.Count == 0)
            {
                return(null);
            }
            if (elements.Count == 1)
            {
                mName = mName.Replace(".List", ".SingletonList").Replace(".SeparatedList", ".SingletonSeparatedList");
            }
            else
            {
                elements = ArgList.Create(new ApiCall("methodName", "new " + listType + "[]", elements, useCurliesInsteadOfParentheses: true));
            }
            var codeBlock = new ApiCall(name, mName, elements);

            return(codeBlock);
        }
Beispiel #5
0
 public MethodCall(string methodName, ArgList argList) : this( methodName ) { Arguments = argList; }
Beispiel #6
0
        private ApiCall QuoteToken(SyntaxToken value, string name)
        {
            if (value == default(SyntaxToken) || value.IsKind(SyntaxKind.None))
            {
                return(null);
            }
            var    args  = ArgList.Create();
            string mName = "SyntaxFactory.Token";
            string escapedTokenValueText = "@\"" + Escape(value.ToString()) + "\"";
            object head = GetLeadingTrivia(value);
            object tail = GetTrailingTrivia(value);
            object actualValue;

            if (head != null || tail != null)
            {
                head = head ?? GetEmptyTrivia("LeadingTrivia"); tail = tail ?? GetEmptyTrivia("TrailingTrivia");
            }
            if (value.IsKind(SyntaxKind.IdentifierToken))
            {
                mName = "SyntaxFactory.Identifier";
                if (value.IsMissing)
                {
                    mName = "SyntaxFactory.MissingToken";
                }
                if (value.IsMissing)
                {
                    actualValue = value.CSharpKind( );
                }
                else
                {
                    actualValue = escapedTokenValueText;
                }
                args.AddIfNotNull(head);
                args.Add(actualValue);
                args.AddIfNotNull(tail);
            }
            else if (value.IsKindAny(SyntaxKind.XmlTextLiteralToken,
                                     SyntaxKind.XmlTextLiteralNewLineToken,
                                     SyntaxKind.XmlEntityLiteralToken))
            {
                mName = "SyntaxFactory.XmlTextLiteral";
                if (value.IsKind(SyntaxKind.XmlTextLiteralNewLineToken))
                {
                    mName = "SyntaxFactory.XmlTextNewLine";
                }
                else if (value.IsKind(SyntaxKind.XmlEntityLiteralToken))
                {
                    mName = "SyntaxFactory.XmlEntity";
                }
                args.Add(head ?? GetEmptyTrivia("LeadingTrivia"), escapedTokenValueText, escapedTokenValueText, tail ?? GetEmptyTrivia("TrailingTrivia"));
            }
            else if ((value.Parent is LiteralExpressionSyntax ||
                      value.IsKindAny(SyntaxKind.StringLiteralToken, SyntaxKind.NumericLiteralToken)) &&
                     value.IsKindNoneOf(SyntaxKind.TrueKeyword, SyntaxKind.FalseKeyword,
                                        SyntaxKind.NullKeyword, SyntaxKind.ArgListKeyword))
            {
                mName = "SyntaxFactory.Literal";
                args.Add(head ?? GetEmptyTrivia("LeadingTrivia"), escapedTokenValueText);
                string escapedValue = value.ToString();
                if (value.IsKind(SyntaxKind.StringLiteralToken))
                {
                    escapedValue = escapedTokenValueText;
                }
                args.Add(escapedValue, tail ?? GetEmptyTrivia("TrailingTrivia"));
            }
            else
            {
                if (value.IsMissing)
                {
                    mName = "SyntaxFactory.MissingToken";
                }
                if (value.IsKind(SyntaxKind.BadToken))
                {
                    mName = "SyntaxFactory.BadToken";
                    head  = head ?? GetEmptyTrivia("LeadingTrivia");
                    tail  = tail ?? GetEmptyTrivia("TrailingTrivia");
                }
                object tokenValue = value.CSharpKind();
                if (value.IsKind(SyntaxKind.BadToken))
                {
                    tokenValue = escapedTokenValueText;
                }
                args.AddIfNotNull(head);
                args.Add(tokenValue);
                args.AddIfNotNull(tail);
            }
            return(new ApiCall(name, mName, args));
        }