44using System . Linq ;
55using Microsoft . CSharp . RuntimeBinder ;
66using Rubberduck . Parsing . Inspections . Abstract ;
7+ using Rubberduck . Parsing . Rewriter ;
78using Rubberduck . Parsing . VBA ;
9+ using Rubberduck . Parsing . VBA . Parsing ;
810using Rubberduck . VBEditor ;
911
1012namespace Rubberduck . Inspections . QuickFixes
@@ -74,17 +76,39 @@ public void Fix(IQuickFix fix, IInspectionResult result)
7476 return ;
7577 }
7678
77- fix . Fix ( result ) ;
79+ var rewriteSession = RewriteSession ( fix . TargetCodeKind ) ;
80+ fix . Fix ( result , rewriteSession ) ;
81+ rewriteSession . Rewrite ( ) ;
82+
7883 _state . RewriteAllModules ( ) ;
7984 _state . OnParseRequested ( this ) ;
8085 }
8186
87+ private IRewriteSession RewriteSession ( CodeKind targetCodeKind )
88+ {
89+ switch ( targetCodeKind )
90+ {
91+ case CodeKind . CodePaneCode :
92+ return _state . RewritingManager . CheckOutCodePaneSession ( ) ;
93+ case CodeKind . AttributesCode :
94+ return _state . RewritingManager . CheckOutAttributesSession ( ) ;
95+ default :
96+ throw new NotSupportedException ( nameof ( targetCodeKind ) ) ;
97+ }
98+ }
99+
82100 public void FixInProcedure ( IQuickFix fix , QualifiedMemberName ? qualifiedMember , Type inspectionType , IEnumerable < IInspectionResult > results )
83101 {
84102 Debug . Assert ( qualifiedMember . HasValue , "Null qualified member." ) ;
85103
86104 var filteredResults = results . Where ( result => result . Inspection . GetType ( ) == inspectionType && result . QualifiedMemberName == qualifiedMember ) . ToList ( ) ;
87105
106+ if ( ! filteredResults . Any ( ) )
107+ {
108+ return ;
109+ }
110+
111+ var rewriteSession = RewriteSession ( fix . TargetCodeKind ) ;
88112 foreach ( var result in filteredResults )
89113 {
90114 if ( ! CanFix ( fix , result ) )
@@ -94,18 +118,22 @@ public void FixInProcedure(IQuickFix fix, QualifiedMemberName? qualifiedMember,
94118
95119 fix . Fix ( result ) ;
96120 }
121+ rewriteSession . Rewrite ( ) ;
97122
98- if ( filteredResults . Any ( ) )
99- {
100- _state . RewriteAllModules ( ) ;
101- _state . OnParseRequested ( this ) ;
102- }
123+ _state . RewriteAllModules ( ) ;
124+ _state . OnParseRequested ( this ) ;
103125 }
104126
105127 public void FixInModule ( IQuickFix fix , QualifiedSelection selection , Type inspectionType , IEnumerable < IInspectionResult > results )
106128 {
107129 var filteredResults = results . Where ( result => result . Inspection . GetType ( ) == inspectionType && result . QualifiedSelection . QualifiedName == selection . QualifiedName ) . ToList ( ) ;
108130
131+ if ( ! filteredResults . Any ( ) )
132+ {
133+ return ;
134+ }
135+
136+ var rewriteSession = RewriteSession ( fix . TargetCodeKind ) ;
109137 foreach ( var result in filteredResults )
110138 {
111139 if ( ! CanFix ( fix , result ) )
@@ -115,18 +143,22 @@ public void FixInModule(IQuickFix fix, QualifiedSelection selection, Type inspec
115143
116144 fix . Fix ( result ) ;
117145 }
146+ rewriteSession . Rewrite ( ) ;
118147
119- if ( filteredResults . Any ( ) )
120- {
121- _state . RewriteAllModules ( ) ;
122- _state . OnParseRequested ( this ) ;
123- }
148+ _state . RewriteAllModules ( ) ;
149+ _state . OnParseRequested ( this ) ;
124150 }
125151
126152 public void FixInProject ( IQuickFix fix , QualifiedSelection selection , Type inspectionType , IEnumerable < IInspectionResult > results )
127153 {
128154 var filteredResults = results . Where ( result => result . Inspection . GetType ( ) == inspectionType && result . QualifiedSelection . QualifiedName . ProjectId == selection . QualifiedName . ProjectId ) . ToList ( ) ;
129155
156+ if ( ! filteredResults . Any ( ) )
157+ {
158+ return ;
159+ }
160+
161+ var rewriteSession = RewriteSession ( fix . TargetCodeKind ) ;
130162 foreach ( var result in filteredResults )
131163 {
132164 if ( ! CanFix ( fix , result ) )
@@ -136,18 +168,22 @@ public void FixInProject(IQuickFix fix, QualifiedSelection selection, Type inspe
136168
137169 fix . Fix ( result ) ;
138170 }
171+ rewriteSession . Rewrite ( ) ;
139172
140- if ( filteredResults . Any ( ) )
141- {
142- _state . RewriteAllModules ( ) ;
143- _state . OnParseRequested ( this ) ;
144- }
173+ _state . RewriteAllModules ( ) ;
174+ _state . OnParseRequested ( this ) ;
145175 }
146176
147177 public void FixAll ( IQuickFix fix , Type inspectionType , IEnumerable < IInspectionResult > results )
148178 {
149179 var filteredResults = results . Where ( result => result . Inspection . GetType ( ) == inspectionType ) . ToArray ( ) ;
150180
181+ if ( ! filteredResults . Any ( ) )
182+ {
183+ return ;
184+ }
185+
186+ var rewriteSession = RewriteSession ( fix . TargetCodeKind ) ;
151187 foreach ( var result in filteredResults )
152188 {
153189 if ( ! CanFix ( fix , result ) )
@@ -157,12 +193,10 @@ public void FixAll(IQuickFix fix, Type inspectionType, IEnumerable<IInspectionRe
157193
158194 fix . Fix ( result ) ;
159195 }
196+ rewriteSession . Rewrite ( ) ;
160197
161- if ( filteredResults . Any ( ) )
162- {
163- _state . RewriteAllModules ( ) ;
164- _state . OnParseRequested ( this ) ;
165- }
198+ _state . RewriteAllModules ( ) ;
199+ _state . OnParseRequested ( this ) ;
166200 }
167201
168202 public bool HasQuickFixes ( IInspectionResult inspectionResult )
0 commit comments