Skip to content

Instantly share code, notes, and snippets.

@jlattimer
Created February 22, 2015 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlattimer/a097dab6dfb3ad4c232e to your computer and use it in GitHub Desktop.
Save jlattimer/a097dab6dfb3ad4c232e to your computer and use it in GitHub Desktop.
Simple Dynamics CRM code fix using the .NET Compiler Platform ("Roslyn")
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