1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . Linq ;
4
5
using Antlr4 . Runtime ;
5
6
using Rubberduck . Parsing ;
@@ -18,16 +19,46 @@ public static class CodeModuleExtensions
18
19
/// <param name="module">The <see cref="ICodeModule"/> to modify.</param>
19
20
/// <param name="target"></param>
20
21
public static void Remove ( this ICodeModule module , Declaration target )
22
+ {
23
+ if ( ! module . Equals ( target . QualifiedName . QualifiedModuleName . Component . CodeModule ) )
24
+ {
25
+ throw new ArgumentException ( "Target is not declared in specified module." ) ;
26
+ }
27
+
28
+ var sortedItems = target . References
29
+ . Where ( reference => module . Equals ( reference . QualifiedModuleName . Component . CodeModule ) )
30
+ . Select ( reference => Tuple . Create ( ( object ) reference , reference . Selection ) )
31
+ . Concat ( new [ ] { Tuple . Create ( ( object ) target , target . Selection ) } )
32
+ . OrderByDescending ( t => t . Item2 ) ;
33
+
34
+ foreach ( var tuple in sortedItems )
35
+ {
36
+ if ( tuple . Item1 is Declaration )
37
+ {
38
+ RemoveDeclarationOnly ( module , target ) ;
39
+ }
40
+ else
41
+ {
42
+ var reference = ( IdentifierReference ) tuple . Item1 ;
43
+ Remove ( reference . QualifiedModuleName . Component . CodeModule , reference ) ;
44
+ }
45
+ }
46
+ }
47
+
48
+ private static void RemoveDeclarationOnly ( this ICodeModule module , Declaration target )
21
49
{
22
50
var multipleDeclarations = target . DeclarationType == DeclarationType . Variable && target . HasMultipleDeclarationsInStatement ( ) ;
23
51
var context = GetStmtContext ( target ) ;
24
- var declarationText = context . GetText ( ) . Replace ( " _" + Environment . NewLine , string . Empty ) ;
52
+ var declarationText = context . GetText ( ) . Replace ( " _" + Environment . NewLine , Environment . NewLine ) ;
25
53
var selection = GetStmtContextSelection ( target ) ;
54
+ Debug . Assert ( selection . StartColumn > 0 ) ;
26
55
27
56
var oldLines = module . GetLines ( selection ) ;
57
+ var indent = oldLines . IndexOf ( oldLines . FirstOrDefault ( c => c != ' ' ) ) + 1 ;
28
58
29
- var newLines = oldLines . Replace ( " _" + Environment . NewLine , string . Empty )
30
- . Remove ( selection . StartColumn - 1 , declarationText . Length ) ;
59
+ var newLines = oldLines
60
+ . Replace ( " _" + Environment . NewLine , Environment . NewLine )
61
+ . Remove ( selection . StartColumn - 1 , declarationText . Length - selection . StartColumn + indent ) ;
31
62
32
63
if ( multipleDeclarations )
33
64
{
@@ -129,11 +160,6 @@ private static string RemoveExtraComma(string str, int numParams, int indexRemov
129
160
return str . Remove ( str . NthIndexOf ( ',' , commaToRemove ) , 1 ) ;
130
161
}
131
162
132
- public static void Remove ( this ICodeModule module , ConstantDeclaration target )
133
- {
134
-
135
- }
136
-
137
163
public static void Remove ( this ICodeModule module , IdentifierReference target )
138
164
{
139
165
var parent = target . Context . Parent as ParserRuleContext ;
0 commit comments