Skip to content

Commit ec25e51

Browse files
committed
Complete FileServiceInterfaceInjectionCodeAction
1 parent 871aeb3 commit ec25e51

2 files changed

Lines changed: 60 additions & 11 deletions

File tree

Roslyn.Testing/CodeFix/CSharpCodeFixProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected void VerifyFix(string oldSource,
7676

7777
if (string.IsNullOrEmpty(result.ErrorMessage))
7878
{
79-
result.NewSource.ShouldBe(result.ActualSource);
79+
result.NewSource.ShouldContainWithoutWhitespace(result.ActualSource);
8080
} else
8181
{
8282
result.Success.ShouldBeTrue(result.ErrorMessage);

System.IO.Abstractions.Analyzers/CodeActions/FileServiceInterfaceInjectionCodeAction.cs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
3434
{
3535
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken).ConfigureAwait(false);
3636

37-
var parameter = SF.Parameter(SF.Identifier("fileSystem"))
38-
.WithType(SF.ParseTypeName(Constants.FileSystemName))
39-
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
40-
.NormalizeWhitespace();
37+
var parameter = CreateFileSystemParameterDeclaration();
4138

4239
if (!(_constructor.Parent is ClassDeclarationSyntax classDeclarationSyntax))
4340
{
@@ -55,19 +52,71 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
5552
return editor.GetChangedDocument();
5653
}
5754

58-
var fileSystemPropertyDeclaration = SF.FieldDeclaration(SF.VariableDeclaration(SF.IdentifierName(Constants.FileSystemName))
59-
.WithVariables(SF.SingletonSeparatedList(SF.VariableDeclarator(SF.Identifier("_fileSystem")))))
60-
.WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword),
61-
SF.Token(SyntaxKind.ReadOnlyKeyword)));
55+
var fileSystemPropertyDeclaration = CreateFileSystemPropertyDeclaration();
6256

6357
editor.InsertMembers(classDeclarationSyntax,
6458
0,
6559
new SyntaxNode[]
66-
{ fileSystemPropertyDeclaration });
60+
{
61+
fileSystemPropertyDeclaration
62+
});
63+
64+
var newConstructor = _constructor.WithBody(_constructor.Body.AddStatements(CreateAssignmentExpression()))
65+
.AddParameterListParameters(parameter)
66+
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation)
67+
.NormalizeWhitespace();
68+
69+
editor.ReplaceNode(_constructor, newConstructor);
70+
var compilationUnitSyntax = GetCompilationUnit(_constructor);
6771

68-
editor.AddParameter(_constructor, parameter);
72+
editor.ReplaceNode(compilationUnitSyntax.Usings.FirstOrDefault(),
73+
SF.UsingDirective(SF.ParseName(Constants.FileSystemNameSpace)));
6974

7075
return editor.GetChangedDocument();
7176
}
77+
78+
private static FieldDeclarationSyntax CreateFileSystemPropertyDeclaration()
79+
{
80+
return SF.FieldDeclaration(SF.VariableDeclaration(CreateFileSystemType())
81+
.WithVariables(SF.SingletonSeparatedList(SF.VariableDeclarator(SF.Identifier("_fileSystem")))))
82+
.WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword),
83+
SF.Token(SyntaxKind.ReadOnlyKeyword)));
84+
}
85+
86+
private static ParameterSyntax CreateFileSystemParameterDeclaration()
87+
{
88+
return SF.Parameter(SF.Identifier("fileSystem"))
89+
.WithType(CreateFileSystemType())
90+
.WithAdditionalAnnotations(Formatter.Annotation, Simplifier.SpecialTypeAnnotation)
91+
.NormalizeWhitespace();
92+
}
93+
94+
private static TypeSyntax CreateFileSystemType()
95+
{
96+
return SF.ParseTypeName(Constants.FileSystemName);
97+
}
98+
99+
private static ExpressionStatementSyntax CreateAssignmentExpression()
100+
{
101+
return SF.ExpressionStatement(SF.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
102+
SF.IdentifierName("_fileSystem"),
103+
SF.IdentifierName("fileSystem")));
104+
}
105+
106+
private CompilationUnitSyntax GetCompilationUnit(SyntaxNode node)
107+
{
108+
switch (node)
109+
{
110+
case null:
111+
112+
return null;
113+
case CompilationUnitSyntax compilationUnitSyntax:
114+
115+
return compilationUnitSyntax;
116+
default:
117+
118+
return GetCompilationUnit(node.Parent);
119+
}
120+
}
72121
}
73122
}

0 commit comments

Comments
 (0)