Created
February 22, 2015 03:17
-
-
Save jlattimer/a097dab6dfb3ad4c232e to your computer and use it in GitHub Desktop.
Simple Dynamics CRM code fix using the .NET Compiler Platform ("Roslyn")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed override async Task ComputeFixesAsync(CodeFixContext context) | |
{ | |
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | |
var diagnostic = context.Diagnostics.First(); | |
var diagnosticSpan = diagnostic.Location.SourceSpan; | |
// Find the type declaration identified by the diagnostic. | |
var declaration = root.FindToken(diagnosticSpan.Start).Parent; | |
// Register a code action that will invoke the fix. | |
context.RegisterFix( | |
CodeAction.Create("Make lowercase", c => MakeLowercaseAsync(context.Document, declaration, c)), | |
diagnostic); | |
} | |
private async Task<Document> MakeLowercaseAsync(Document document, SyntaxNode node, CancellationToken cancellationToken) | |
{ | |
var root = await document.GetSyntaxRootAsync(cancellationToken); | |
BracketedArgumentListSyntax bal = (BracketedArgumentListSyntax)node; | |
var newFieldName = bal.Arguments.FirstOrDefault().ToString().ToLowerInvariant(); | |
var newNode = SyntaxFactory.BracketedArgumentList(SyntaxFactory.SeparatedList<ArgumentSyntax>().Add( | |
SyntaxFactory.Argument(null, SyntaxFactory.Token(SyntaxKind.None), | |
SyntaxFactory.IdentifierName(newFieldName)))); | |
var newRoot = root.ReplaceNode(node, newNode); | |
return document.WithSyntaxRoot(newRoot); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment