Skip to content

Commit

Permalink
Merge pull request #3 from lavanyaguru/codefix
Browse files Browse the repository at this point in the history
Add codefix
  • Loading branch information
lavanyaguru authored Jul 31, 2019
2 parents 2b88633 + 69d01c8 commit 400f809
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.GetCapturedVariables;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace PassVariableExplicitlyInLocalStaticFunction
{
//interface IMyService : ILanguageService
//{
// Task<Document> FixDocument(Document document, LocalFunctionStatementSyntax localfunction, CancellationToken cancellationToken);
//}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PassVariableExplicitlyInLocalStaticFunctionCodeFixProvider)), Shared]
internal class PassVariableExplicitlyInLocalStaticFunctionCodeFixProvider : CodeFixProvider
{
private const string Title = "Pass variable explicitly";

public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create("CS8421"); }
}

public sealed override FixAllProvider GetFixAllProvider()
{
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
return WellKnownFixAllProviders.BatchFixer;
}

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

// TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;

// Find the type declaration identified by the diagnostic.
var declaration = root.FindNode(diagnosticSpan).AncestorsAndSelf().OfType<LocalFunctionStatementSyntax>().First();
var document = context.Document;
var cancellationToken = context.CancellationToken;

//var service = context.Document.GetLanguageService<IMyService>();

var service = document.GetLanguageService<GetCaptures>(); //how does it perform what we want if we aren't passing in any parameters inside GetCaptures?




// Register a new code action that will invoke the fix.
context.RegisterCodeFix(
new MyCodeAction(c => service.CreateParameterSymbolAsync(context.Document, declaration, c)),
context.Diagnostics);



}


private class MyCodeAction : CodeAction.SolutionChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Solution>> createChangedSolution)
: base("Pass variable explicitly in local static function", createChangedSolution, "Pass variable explicitly in local static function")
{
}
}

}



}

0 comments on commit 400f809

Please sign in to comment.