Ich fange an, Roslyn endlich zu erkunden und jetzt möchte ich eine CodeRefactoringProvider
schreiben, die eine neue Datei erstellt, anstatt Code in einem aktuellen zu ersetzen. Meine aktuellen Code einfach umbenennt eine Klasse wie folgt:CodeRefactoringProvider sollte neue Datei generieren
[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = nameof(TransportModelCodeRefactoringProvider)), Shared]
internal class TransportModelCodeRefactoringProvider : CodeRefactoringProvider
{
private const string Title = "Make TransportModel class";
public override sealed async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);
var classDeclaration = node as ClassDeclarationSyntax;
if (classDeclaration == null)
{
// current node is not a class declaration
return;
}
var action = CodeAction.Create(Title, token => MakeTransportModelAsync(context.Document, classDeclaration, token), Title);
context.RegisterRefactoring(action);
}
private async Task<Document> MakeTransportModelAsync(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = (CompilationUnitSyntax)root;
var newName = classDeclaration.Identifier + "TransportModel";
var newId = SyntaxFactory.Identifier(classDeclaration.Identifier.LeadingTrivia, SyntaxKind.IdentifierToken, newName, newName, classDeclaration.Identifier.TrailingTrivia);
var newClass = classDeclaration.ReplaceToken(classDeclaration.Identifier, newId);
newRoot = newRoot.ReplaceNode(classDeclaration, newClass).NormalizeWhitespace();
var result = document.WithSyntaxRoot(newRoot);
return result;
}
}
ich eine Überlastung zu CodeAction.Create
gefunden, die eine Task<Solution>
akzeptiert, die ich interessant gefunden, aber dann weiß ich nicht, wie die Lösung zu erhalten und/oder ein Projekt für den aktuellen Dokument.