1
1
using System ;
2
+ using System . Linq ;
3
+ using Microsoft . Vbe . Interop ;
2
4
using Rubberduck . Parsing . Symbols ;
3
5
using Rubberduck . VBEditor ;
4
6
@@ -7,11 +9,13 @@ namespace Rubberduck.Refactorings.EncapsulateField
7
9
class EncapsulateFieldRefactoring : IRefactoring
8
10
{
9
11
private readonly IRefactoringPresenterFactory < IEncapsulateFieldPresenter > _factory ;
12
+ private readonly IActiveCodePaneEditor _editor ;
10
13
private EncapsulateFieldModel _model ;
11
14
12
- public EncapsulateFieldRefactoring ( IRefactoringPresenterFactory < IEncapsulateFieldPresenter > factory )
15
+ public EncapsulateFieldRefactoring ( IRefactoringPresenterFactory < IEncapsulateFieldPresenter > factory , IActiveCodePaneEditor editor )
13
16
{
14
17
_factory = factory ;
18
+ _editor = editor ;
15
19
}
16
20
17
21
public void Refactor ( )
@@ -44,6 +48,8 @@ private void AddProperty()
44
48
UpdateReferences ( ) ;
45
49
46
50
var module = _model . TargetDeclaration . QualifiedName . QualifiedModuleName . Component . CodeModule ;
51
+ SetFieldToPrivate ( module ) ;
52
+
47
53
module . InsertLines ( module . CountOfDeclarationLines + 1 , GetPropertyText ( ) ) ;
48
54
}
49
55
@@ -61,6 +67,98 @@ private void UpdateReferences()
61
67
}
62
68
}
63
69
70
+ private void SetFieldToPrivate ( CodeModule module )
71
+ {
72
+ if ( _model . TargetDeclaration . Accessibility == Accessibility . Private )
73
+ {
74
+ return ;
75
+ }
76
+
77
+ RemoveField ( _model . TargetDeclaration ) ;
78
+
79
+ var newField = "Private " + _model . TargetDeclaration . IdentifierName + " As " +
80
+ _model . TargetDeclaration . AsTypeName ;
81
+
82
+ module . InsertLines ( module . CountOfDeclarationLines + 1 , newField ) ;
83
+
84
+ _editor . SetSelection ( _model . TargetDeclaration . QualifiedSelection ) ;
85
+ for ( var index = 1 ; index <= module . CountOfDeclarationLines ; index ++ )
86
+ {
87
+ if ( module . Lines [ index , 1 ] . Trim ( ) == string . Empty )
88
+ {
89
+ _editor . DeleteLines ( new Selection ( index , 0 , index , 0 ) ) ;
90
+ }
91
+ }
92
+ }
93
+
94
+ private void RemoveField ( Declaration target )
95
+ {
96
+ Selection selection ;
97
+ var declarationText = target . Context . GetText ( ) ;
98
+ var multipleDeclarations = _model . HasMultipleDeclarationsInStatement ( target ) ;
99
+
100
+ var variableStmtContext = _model . GetVariableStmtContext ( target ) ;
101
+
102
+ if ( ! multipleDeclarations )
103
+ {
104
+ declarationText = variableStmtContext . GetText ( ) ;
105
+ selection = _model . GetVariableStmtContextSelection ( target ) ;
106
+ }
107
+ else
108
+ {
109
+ selection = new Selection ( target . Context . Start . Line , target . Context . Start . Column ,
110
+ target . Context . Stop . Line , target . Context . Stop . Column ) ;
111
+ }
112
+
113
+ var oldLines = _editor . GetLines ( selection ) ;
114
+
115
+ var newLines = oldLines . Replace ( " _" + Environment . NewLine , string . Empty )
116
+ . Remove ( selection . StartColumn , declarationText . Length ) ;
117
+
118
+ if ( multipleDeclarations )
119
+ {
120
+ selection = _model . GetVariableStmtContextSelection ( target ) ;
121
+ newLines = RemoveExtraComma ( _editor . GetLines ( selection ) . Replace ( oldLines , newLines ) ) ;
122
+ }
123
+
124
+ _editor . DeleteLines ( selection ) ;
125
+
126
+ if ( newLines . Trim ( ) != string . Empty )
127
+ {
128
+ _editor . InsertLines ( selection . StartLine , newLines ) ;
129
+ }
130
+ }
131
+
132
+ private string RemoveExtraComma ( string str )
133
+ {
134
+ if ( str . Count ( c => c == ',' ) == 1 )
135
+ {
136
+ return str . Remove ( str . IndexOf ( ',' ) , 1 ) ;
137
+ }
138
+
139
+ var significantCharacterAfterComma = false ;
140
+
141
+ for ( var index = str . IndexOf ( "Dim" , StringComparison . Ordinal ) + 3 ; index < str . Length ; index ++ )
142
+ {
143
+ if ( ! significantCharacterAfterComma && str [ index ] == ',' )
144
+ {
145
+ return str . Remove ( index , 1 ) ;
146
+ }
147
+
148
+ if ( ! char . IsWhiteSpace ( str [ index ] ) && str [ index ] != '_' && str [ index ] != ',' )
149
+ {
150
+ significantCharacterAfterComma = true ;
151
+ }
152
+
153
+ if ( str [ index ] == ',' )
154
+ {
155
+ significantCharacterAfterComma = false ;
156
+ }
157
+ }
158
+
159
+ return str . Remove ( str . LastIndexOf ( ',' ) , 1 ) ;
160
+ }
161
+
64
162
private string GetPropertyText ( )
65
163
{
66
164
var getterText = string . Join ( Environment . NewLine ,
@@ -84,7 +182,7 @@ private string GetPropertyText()
84
182
return string . Join ( Environment . NewLine ,
85
183
getterText ,
86
184
( _model . ImplementLetSetterType ? letterText : string . Empty ) ,
87
- ( _model . ImplementSetSetterType ? setterText : string . Empty ) ) ;
185
+ ( _model . ImplementSetSetterType ? setterText : string . Empty ) ) . TrimEnd ( ) ;
88
186
}
89
187
}
90
188
}
0 commit comments