@@ -40,26 +40,39 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
4040
4141 if ( ! HasFileSystemProperty ( _class ) )
4242 {
43- var fileSystemPropertyDeclaration = CreateFileSystemPropertyDeclaration ( ) ;
44-
4543 editor . InsertMembers ( _class ,
4644 0 ,
4745 new SyntaxNode [ ]
4846 {
49- fileSystemPropertyDeclaration
47+ CreateFileSystemPropertyDeclaration ( )
5048 } ) ;
5149 }
5250
5351 ConstructorAddParameter ( _class , editor ) ;
5452
5553 var compilationUnitSyntax = GetCompilationUnit ( _class ) ;
54+ var fileSystemUsing = GetFileSystemUsing ( ) ;
5655
57- editor . ReplaceNode ( compilationUnitSyntax . Usings . FirstOrDefault ( ) ,
58- SF . UsingDirective ( SF . ParseName ( Constants . FileSystemNameSpace ) ) ) ;
56+ if ( compilationUnitSyntax . Usings . Any ( ) )
57+ {
58+ editor . ReplaceNode ( GetSystemIoUsing ( compilationUnitSyntax ) ,
59+ fileSystemUsing ) ;
60+ }
5961
6062 return editor . GetChangedDocument ( ) ;
6163 }
6264
65+ private static UsingDirectiveSyntax GetFileSystemUsing ( )
66+ {
67+ return SF . UsingDirective ( SF . ParseName ( Constants . FileSystemNameSpace ) ) ;
68+ }
69+
70+ private static UsingDirectiveSyntax GetSystemIoUsing ( CompilationUnitSyntax unit )
71+ {
72+ return unit . Usings . FirstOrDefault ( x =>
73+ x . Name . NormalizeWhitespace ( ) . ToFullString ( ) . Equals ( typeof ( Path ) . Namespace ) ) ;
74+ }
75+
6376 private static FieldDeclarationSyntax CreateFileSystemPropertyDeclaration ( )
6477 {
6578 return SF . FieldDeclaration ( SF . VariableDeclaration ( GetFileSystemType ( ) )
@@ -123,6 +136,11 @@ private static bool ConstructorHasFileSystemParameter(BaseMethodDeclarationSynta
123136
124137 private static bool ConstructorHasAssignmentExpression ( BaseMethodDeclarationSyntax constructor )
125138 {
139+ if ( constructor . Body == null )
140+ {
141+ return false ;
142+ }
143+
126144 return constructor . Body . Statements . OfType < ExpressionStatementSyntax > ( )
127145 . Any ( x => x . IsKind ( SyntaxKind . SimpleAssignmentExpression )
128146 && x . Expression . Contains ( SF . IdentifierName ( FieldFileSystemName ) )
@@ -134,16 +152,21 @@ private static bool HasConstructor(SyntaxNode classDeclaration)
134152 return classDeclaration . ChildNodes ( ) . OfType < ConstructorDeclarationSyntax > ( ) . Any ( ) ;
135153 }
136154
137- private static void ConstructorAddParameter ( ClassDeclarationSyntax classDeclaration , DocumentEditor editor )
155+ private static void ConstructorAddParameter ( ClassDeclarationSyntax classDeclaration , SyntaxEditor editor )
138156 {
139157 var constructor = HasConstructor ( classDeclaration )
140158 ? GetConstructor ( classDeclaration )
141- : SF . ConstructorDeclaration ( classDeclaration . Identifier ) ;
159+ : SF . ConstructorDeclaration ( classDeclaration . Identifier )
160+ . WithModifiers ( SyntaxTokenList . Create ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) ) ) ;
142161
143- var newConstructor = constructor . WithBody ( constructor . Body ? . AddStatements ( CreateAssignmentExpression ( ) ) )
144- . WithAdditionalAnnotations ( Formatter . Annotation , Simplifier . Annotation )
162+ var newConstructor = constructor . WithAdditionalAnnotations ( Formatter . Annotation , Simplifier . Annotation )
145163 . NormalizeWhitespace ( ) ;
146164
165+ if ( ! ConstructorHasAssignmentExpression ( newConstructor ) )
166+ {
167+ newConstructor = newConstructor . AddBodyStatements ( CreateAssignmentExpression ( ) ) ;
168+ }
169+
147170 if ( ! ConstructorHasFileSystemParameter ( newConstructor ) )
148171 {
149172 var parameter = CreateFileSystemParameterDeclaration ( ) ;
@@ -155,13 +178,13 @@ private static void ConstructorAddParameter(ClassDeclarationSyntax classDeclarat
155178 editor . ReplaceNode ( constructor , newConstructor ) ;
156179 } else
157180 {
158- editor . InsertMembers ( classDeclaration ,
159- 0 ,
160- new SyntaxNode [ ]
161- {
162- newConstructor
163- } ) ;
181+ editor . InsertBefore ( GetMethod ( classDeclaration ) , newConstructor ) ;
164182 }
165183 }
184+
185+ private static MethodDeclarationSyntax GetMethod ( ClassDeclarationSyntax classDeclaration )
186+ {
187+ return classDeclaration . ChildNodes ( ) . OfType < MethodDeclarationSyntax > ( ) . FirstOrDefault ( ) ;
188+ }
166189 }
167190}
0 commit comments