Ejemplo n.º 1
0
        private async Task <Solution> UseImplicitTyping(
            Document document, VariableDeclarationSyntax declaration, CancellationToken cancellationToken)
        {
            var typeSyntax = SyntaxFactory.ParseTypeName("var");

            var newDeclaration = declaration
                                 .WithType(typeSyntax)
                                 .WithLeadingTrivia(declaration.GetLeadingTrivia())
                                 .WithTrailingTrivia(declaration.GetTrailingTrivia());

            var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            syntaxRoot = syntaxRoot.ReplaceNode(declaration, newDeclaration);

            syntaxRoot = syntaxRoot.Format();

            return(document.WithSyntaxRoot(syntaxRoot).Project.Solution);
        }
Ejemplo n.º 2
0
        private async Task <Solution> UseExplicitTyping(
            Document document,
            VariableDeclarationSyntax declaration, ITypeSymbol variableType,
            CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var typeName = variableType.ToMinimalDisplayString(semanticModel, declaration.SpanStart);

            var typeSyntax = SyntaxFactory.ParseTypeName(typeName);

            var newDeclaration = declaration
                                 .WithType(typeSyntax)
                                 .WithLeadingTrivia(declaration.GetLeadingTrivia())
                                 .WithTrailingTrivia(declaration.GetTrailingTrivia());

            var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            syntaxRoot = syntaxRoot.ReplaceNode(declaration, newDeclaration);

            syntaxRoot = syntaxRoot.Format();

            return(document.WithSyntaxRoot(syntaxRoot).Project.Solution);
        }