1- using System . Linq ;
1+ using System . IO . Abstractions . Analyzers . RoslynToken ;
22using System . Threading ;
33using System . Threading . Tasks ;
44using Microsoft . CodeAnalysis ;
@@ -34,151 +34,64 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
3434 {
3535 var editor = await DocumentEditor . CreateAsync ( _document , cancellationToken ) . ConfigureAwait ( false ) ;
3636
37- if ( ! HasFileSystemProperty ( _class ) )
37+ if ( ! RoslynClassFyleSystem . HasFileSystemProperty ( _class ) )
3838 {
3939 editor . InsertMembers ( _class ,
4040 0 ,
4141 new SyntaxNode [ ]
4242 {
43- CreateFileSystemPropertyDeclaration ( )
43+ RoslynClassFyleSystem . CreateFileSystemPropertyDeclaration ( )
4444 } ) ;
4545 }
4646
4747 ConstructorAddParameter ( _class , editor ) ;
4848
49- var compilationUnitSyntax = GetCompilationUnit ( _class ) ;
49+ var compilationUnitSyntax = RoslynClassFyleSystem . GetCompilationUnit ( _class ) ;
5050
5151 if ( compilationUnitSyntax . Usings . Any ( ) )
5252 {
53- editor . ReplaceNode ( GetSystemIoUsing ( compilationUnitSyntax ) , GetFileSystemUsing ( ) ) ;
53+ editor . ReplaceNode ( RoslynClassFyleSystem . GetSystemIoUsing ( compilationUnitSyntax ) ,
54+ RoslynClassFyleSystem . GetFileSystemUsing ( ) ) ;
5455 }
5556
5657 return editor . GetChangedDocument ( ) ;
5758 }
5859
59- private static UsingDirectiveSyntax GetFileSystemUsing ( )
60- {
61- return SF . UsingDirective ( SF . ParseName ( Constants . FileSystemNameSpace ) ) ;
62- }
63-
64- private static UsingDirectiveSyntax GetSystemIoUsing ( CompilationUnitSyntax unit )
65- {
66- return unit . Usings . FirstOrDefault ( x =>
67- x . Name . NormalizeWhitespace ( ) . ToFullString ( ) . Equals ( typeof ( Path ) . Namespace ) ) ;
68- }
69-
70- private static FieldDeclarationSyntax CreateFileSystemPropertyDeclaration ( )
71- {
72- return SF . FieldDeclaration ( SF . VariableDeclaration ( GetFileSystemType ( ) )
73- . WithVariables ( SF . SingletonSeparatedList ( SF . VariableDeclarator ( SF . Identifier ( Constants . FieldFileSystemName ) ) ) ) )
74- . WithModifiers ( SF . TokenList ( SF . Token ( SyntaxKind . PrivateKeyword ) ,
75- SF . Token ( SyntaxKind . ReadOnlyKeyword ) ) ) ;
76- }
77-
78- private static ParameterSyntax CreateFileSystemParameterDeclaration ( )
79- {
80- return SF . Parameter ( SF . Identifier ( Constants . ParameterFileSystemName ) )
81- . WithType ( GetFileSystemType ( ) )
82- . WithAdditionalAnnotations ( Formatter . Annotation , Simplifier . SpecialTypeAnnotation )
83- . NormalizeWhitespace ( ) ;
84- }
85-
86- private static TypeSyntax GetFileSystemType ( )
87- {
88- return SF . ParseTypeName ( Constants . FileSystemInterfaceName ) ;
89- }
90-
9160 private static ExpressionStatementSyntax CreateAssignmentExpression ( )
9261 {
9362 return SF . ExpressionStatement ( SF . AssignmentExpression ( SyntaxKind . SimpleAssignmentExpression ,
9463 SF . IdentifierName ( Constants . FieldFileSystemName ) ,
9564 SF . IdentifierName ( Constants . ParameterFileSystemName ) ) ) ;
9665 }
9766
98- private static CompilationUnitSyntax GetCompilationUnit ( SyntaxNode node )
99- {
100- switch ( node )
101- {
102- case null :
103-
104- return null ;
105- case CompilationUnitSyntax compilationUnitSyntax :
106-
107- return compilationUnitSyntax ;
108- default :
109-
110- return GetCompilationUnit ( node . Parent ) ;
111- }
112- }
113-
114- private static bool HasFileSystemProperty ( TypeDeclarationSyntax classDeclaration )
115- {
116- return classDeclaration . Members . OfType < PropertyDeclarationSyntax > ( )
117- . Any ( x => x . Identifier . Text == Constants . FieldFileSystemName && x . Type == GetFileSystemType ( ) ) ;
118- }
119-
120- private static ConstructorDeclarationSyntax GetConstructor ( SyntaxNode classDeclaration )
121- {
122- return classDeclaration . ChildNodes ( ) . OfType < ConstructorDeclarationSyntax > ( ) . FirstOrDefault ( ) ;
123- }
124-
125- private static bool ConstructorHasFileSystemParameter ( BaseMethodDeclarationSyntax constructor )
126- {
127- return constructor . ParameterList . Parameters
128- . Any ( x => x . Identifier . Text == Constants . ParameterFileSystemName && x . Type == GetFileSystemType ( ) ) ;
129- }
130-
131- private static bool ConstructorHasAssignmentExpression ( BaseMethodDeclarationSyntax constructor )
132- {
133- if ( constructor . Body == null )
134- {
135- return false ;
136- }
137-
138- return constructor . Body . Statements . OfType < ExpressionStatementSyntax > ( )
139- . Any ( x => x . IsKind ( SyntaxKind . SimpleAssignmentExpression )
140- && x . Expression . Contains ( SF . IdentifierName ( Constants . FieldFileSystemName ) )
141- && x . Expression . Contains ( SF . IdentifierName ( Constants . ParameterFileSystemName ) ) ) ;
142- }
143-
144- private static bool HasConstructor ( SyntaxNode classDeclaration )
145- {
146- return classDeclaration . ChildNodes ( ) . OfType < ConstructorDeclarationSyntax > ( ) . Any ( ) ;
147- }
148-
14967 private static void ConstructorAddParameter ( ClassDeclarationSyntax classDeclaration , SyntaxEditor editor )
15068 {
151- var constructor = HasConstructor ( classDeclaration )
152- ? GetConstructor ( classDeclaration )
69+ var constructor = RoslynClassFyleSystem . HasConstructor ( classDeclaration )
70+ ? RoslynClassFyleSystem . GetConstructor ( classDeclaration )
15371 : SF . ConstructorDeclaration ( classDeclaration . Identifier )
15472 . WithModifiers ( SyntaxTokenList . Create ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) ) ) ;
15573
15674 var newConstructor = constructor . WithAdditionalAnnotations ( Formatter . Annotation , Simplifier . Annotation )
15775 . NormalizeWhitespace ( ) ;
15876
159- if ( ! ConstructorHasAssignmentExpression ( newConstructor ) )
77+ if ( ! RoslynClassFyleSystem . ConstructorHasAssignmentExpression ( newConstructor ) )
16078 {
16179 newConstructor = newConstructor . AddBodyStatements ( CreateAssignmentExpression ( ) ) ;
16280 }
16381
164- if ( ! ConstructorHasFileSystemParameter ( newConstructor ) )
82+ if ( ! RoslynClassFyleSystem . ConstructorHasFileSystemParameter ( newConstructor ) )
16583 {
166- var parameter = CreateFileSystemParameterDeclaration ( ) ;
84+ var parameter = RoslynClassFyleSystem . CreateFileSystemParameterDeclaration ( ) ;
16785 newConstructor = newConstructor . AddParameterListParameters ( parameter ) ;
16886 }
16987
170- if ( HasConstructor ( classDeclaration ) )
88+ if ( RoslynClassFyleSystem . HasConstructor ( classDeclaration ) )
17189 {
17290 editor . ReplaceNode ( constructor , newConstructor ) ;
17391 } else
17492 {
175- editor . InsertBefore ( GetMethod ( classDeclaration ) , newConstructor ) ;
93+ editor . InsertBefore ( RoslynClassFyleSystem . GetMethod ( classDeclaration ) , newConstructor ) ;
17694 }
17795 }
178-
179- private static MethodDeclarationSyntax GetMethod ( ClassDeclarationSyntax classDeclaration )
180- {
181- return classDeclaration . ChildNodes ( ) . OfType < MethodDeclarationSyntax > ( ) . FirstOrDefault ( ) ;
182- }
18396 }
18497}
0 commit comments