Luca Morelli
Luca Morelli

Reputation: 2560

how to create a new class in a CodeFixProvider

I'm using Visual Studio Preview and I would like create a simple Diagnostic with Code Fix:
I would like to check every function and if more than n parameters are present, replace them with a class that I create and contains those parameters as members.
Where I can find more infos on how to do this?

The first part is very easy, but I don't have any idea how to do this in the code fixer

Upvotes: 1

Views: 178

Answers (1)

SLaks
SLaks

Reputation: 887195

Override GetChangedDocumentAsync in your CodeAction to return document.WithSyntaxRoot(...), passing a new syntax tree with your new class inserted.

To create the syntax tree, use document.GetSyntaxRootAsync().InsertNodesAfter(...), inserting a ClassDeclarationSyntax at some point in the document.

Upvotes: 2

Related Questions