private async Task <Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { //Remove the leading trivia from the local declaration. var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); //Create a const token with the leading trivia var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); //Insert the const token into the modifiers list, creating a new modifiers list. var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); //Produce the new local declaration. var newLocal = trimmedLocal.WithModifiers(newModifiers).WithDeclaration(localDeclaration.Declaration); //Add an annotation to format the new local declarartion. var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(localDeclaration, formattedLocal); //Return document with transformed tree return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { // remove leading trivia var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken( firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); // create const token with leading trivia var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); // insert const token as first modifier var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); // infer type for `var` declaration var variableDeclaration = localDeclaration.Declaration; var variableTypeName = variableDeclaration.Type; if (variableTypeName.IsVar) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); // ensure `var` is not an alias, as in `using var = Type;` var aliasInfo = semanticModel.GetAliasInfo(variableTypeName); if (aliasInfo == null) { // infer type for `var` var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType; if (type.Name != "var") { var typeName = SyntaxFactory.ParseTypeName(type.ToDisplayString()) .WithLeadingTrivia(variableTypeName.GetLeadingTrivia()) .WithTrailingTrivia(variableTypeName.GetTrailingTrivia()); // simplify type name var simplifiedTypeName = typeName.WithAdditionalAnnotations(Simplifier.Annotation); variableDeclaration = variableDeclaration.WithType(simplifiedTypeName); } } } // production new declaration var newLocal = trimmedLocal.WithModifiers(newModifiers) .WithDeclaration(variableDeclaration); // add annotation to reformat the declaration var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); // replace old tree with new local declaration var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(localDeclaration, formattedLocal); return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); var variableDeclaration = localDeclaration.Declaration; var variableTypeName = variableDeclaration.Type; if (variableTypeName.IsVar) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var aliasInfo = semanticModel.GetAliasInfo(variableTypeName); if (aliasInfo == null) { var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType; if (type.Name != "var") { var typeName = SyntaxFactory.ParseTypeName(type.ToDisplayString()) .WithLeadingTrivia(variableTypeName.GetLeadingTrivia()) .WithTrailingTrivia(variableTypeName.GetTrailingTrivia()); var simplifiedTypeName = typeName.WithAdditionalAnnotations(Simplifier.Annotation); variableDeclaration = variableDeclaration.WithType(simplifiedTypeName); } } } var newLocal = trimmedLocal.WithModifiers(newModifiers) .WithDeclaration(variableDeclaration); var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(localDeclaration, formattedLocal); return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> MakeConstAsync(Document contextDocument, LocalDeclarationStatementSyntax declaration, CancellationToken cancellationToken) { var firstToken = declaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimLocal = declaration.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); var constToken = SyntaxFactory.Token( leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); var newModifiers = trimLocal.Modifiers.Insert(0, constToken); var newLocal = trimLocal.WithModifiers(newModifiers); var formatLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); var root = await contextDocument.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(declaration, formatLocal); return(contextDocument.WithSyntaxRoot(newRoot)); }
public async Task <Document> RenameLowerCaseLocalMember(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken( firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.SealedKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); var newLocal = trimmedLocal.WithModifiers(newModifiers); var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(localDeclaration, formattedLocal); return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> ConvertToDateTimeOffsetTypeAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { // #1 Replace DateTime x = new DateTime(); // #2 Replace DateTime x = DateTime.Now; // #3 var x = DateTime.Now; // #4 var x = new DateTime(); // #5 var x = new DateTime(); // #6 var x = new DateTime(); var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken( firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); SyntaxNode finalResultSyntaxNode = DateTimeOffSetHelper.FixCode(localDeclaration); var newLocalDecl = finalResultSyntaxNode.WithAdditionalAnnotations(Formatter.Annotation); var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(localDeclaration, newLocalDecl); return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { // Remove the leading trivia from the local declaration. var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken( firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); // Create a const token with the leading trivia. var constToken = SyntaxFactory.Token(leadingTrivia, SyntaxKind.ConstKeyword, SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker)); // Insert the const token into the modifiers list, creating a new modifiers list. var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); // If the type of declaration is 'var', create a new type name for the // type inferred for 'var'. var variableDeclaration = localDeclaration.Declaration; var variableTypeName = variableDeclaration.Type; if (variableTypeName.IsVar) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); // Special case: Ensure that 'var' isn't actually an alias to another type // (e.g. using var = System.String). var aliasInfo = semanticModel.GetAliasInfo(variableTypeName); if (aliasInfo == null) { // Retrieve the type inferred for var. var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType; // Special case: Ensure that 'var' isn't actually a type named 'var'. if (type.Name != "var") { // Create a new TypeSyntax for the inferred type. Be careful // to keep any leading and trailing trivia from the var keyword. var typeName = SyntaxFactory.ParseTypeName(type.ToDisplayString()) .WithLeadingTrivia(variableTypeName.GetLeadingTrivia()) .WithTrailingTrivia(variableTypeName.GetTrailingTrivia()); // Add an annotation to simplify the type name. var simplifiedTypeName = typeName.WithAdditionalAnnotations(Simplifier.Annotation); // Replace the type in the variable declaration. variableDeclaration = variableDeclaration.WithType(simplifiedTypeName); } } } // Produce the new local declaration. var newLocal = trimmedLocal.WithModifiers(newModifiers) .WithDeclaration(variableDeclaration); // Add an annotation to format the new local declaration. var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation); // Replace the old local declaration with the new local declaration. var root = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(localDeclaration, formattedLocal); // Return document with transformed tree. return(document.WithSyntaxRoot(newRoot)); }
public CodeActionEdit GetEdit(CancellationToken cancellationToken) { // Remove the leading trivia from the local declaration. var firstToken = localDeclaration.GetFirstToken(); var leadingTrivia = firstToken.LeadingTrivia; var trimmedLocal = localDeclaration.ReplaceToken(firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty)); // Create a const token with the original leading trivia. var constToken = Syntax.Token(leadingTrivia, SyntaxKind.ConstKeyword); // Insert the const token into the modifiers list, creating a new modifiers list. var newModifiers = trimmedLocal.Modifiers.Insert(0, constToken); // If the type of the declaration is 'var', create a new type name // for the inferred type. var variableDeclaration = localDeclaration.Declaration; var variableTypeName = variableDeclaration.Type; if (variableTypeName.IsVar) { var semanticModel = document.GetSemanticModel(); // Special case: Ensure that 'var' isn't actually an alias to another type // (e.g. using var = System.String). var aliasInfo = semanticModel.GetAliasInfo(variableTypeName); if (aliasInfo == null) { // Retrieve the type inferred for var. var type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType; // Special case: Ensure that 'var' isn't actually a type named 'var'. if (type.Name != "var") { // Create a new TypeSyntax for the inferred type. Be careful // to keep any leading and trailing trivia from the var keyword. var typeName = Syntax.ParseTypeName(type.ToDisplayString()) .WithLeadingTrivia(variableTypeName.GetLeadingTrivia()) .WithTrailingTrivia(variableTypeName.GetTrailingTrivia()); // Add an annotation to simplify the type name. var simplifiedTypeName = CodeAnnotations.Simplify .AddAnnotationTo(typeName); // Replace the type in the variable declaration. variableDeclaration = variableDeclaration.WithType(simplifiedTypeName); } } } // Produce the new local declaration. var newLocal = trimmedLocal.WithModifiers(newModifiers).WithDeclaration(variableDeclaration); // Add an annotation to format the new local declaration. var formattedLocal = CodeAnnotations.Formatting.AddAnnotationTo(newLocal); // Replace the old local declaration with the new local declaration. var oldRoot = document.GetSyntaxRoot(cancellationToken); var newRoot = oldRoot.ReplaceNode(localDeclaration, formattedLocal); // Create and return a new CodeActionEdit for the transformed tree. return(new CodeActionEdit(document.UpdateSyntaxRoot(newRoot))); }