Skip to content

Instantly share code, notes, and snippets.

@miya2000
Created August 5, 2015 16:27
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 miya2000/9804fba1e741ed9906f5 to your computer and use it in GitHub Desktop.
Save miya2000/9804fba1e741ed9906f5 to your computer and use it in GitHub Desktop.
nest ".ValueChanged.cs" file.
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ValueChangedGaneratorCodeFixProvider)), Shared]
public class ValueChangedGaneratorCodeFixProvider : CodeFixProvider
{
// snip.
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().First();
context.RegisterCodeFix(
new ValueChangedGaneratorCodeAction(context.Document.FilePath, "Generate properties", c => GenerateValueChanged(context.Document, declaration, true, c)),
diagnostic);
context.RegisterCodeFix(
new ValueChangedGaneratorCodeAction(context.Document.FilePath, "Generate properties (> C# 5)", c => GenerateValueChanged(context.Document, declaration, false, c)),
diagnostic);
}
public class ValueChangedGaneratorCodeAction : CodeAction
{
public override string Title { get; }
public override string EquivalenceKey { get; }
private Func<CancellationToken, Task<Solution>> CreateChangedSolution { get; }
public string FilePath { get; }
public ValueChangedGaneratorCodeAction(string filePath, string title, Func<CancellationToken, Task<Solution>> createChangedSolution, string equivalenceKey = null)
{
FilePath = filePath;
Title = title;
CreateChangedSolution = createChangedSolution;
EquivalenceKey = equivalenceKey;
}
protected override Task<Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
{
return CreateChangedSolution(cancellationToken);
}
protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperationsAsync(CancellationToken cancellationToken)
{
var result = await base.ComputeOperationsAsync(cancellationToken).ConfigureAwait(false);
return result.Concat(new[] { new ValueChangedGaneratorCodeActionOperation(Title, FilePath) });
}
}
public class ValueChangedGaneratorCodeActionOperation : CodeActionOperation
{
public override string Title { get; }
public string FilePath { get; }
public ValueChangedGaneratorCodeActionOperation(string title, string filePath)
{
Title = title;
FilePath = filePath;
}
public override void Apply(Workspace workspace, CancellationToken cancellationToken)
{
var dte = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE._DTE)) as EnvDTE.DTE;
var projectItem = dte.Solution.FindProjectItem(FilePath);
var generatedFilename = FilePath.Substring(0, FilePath.Length - ".cs".Length) + ".ValueChanged.cs";
projectItem.ProjectItems.AddFromFile(generatedFilename);
}
}
// snip.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment