Skip to content

Commit 8c554f6

Browse files
committed
Merge pull request #885 from Hosch250/BugBlipper
Change field accessibility to private.
2 parents dcddec4 + ff6a31b commit 8c554f6

File tree

4 files changed

+116
-21
lines changed

4 files changed

+116
-21
lines changed

RetailCoder.VBE/Refactorings/EncapsulateField/EncapsulateFieldModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public EncapsulateFieldModel(RubberduckParserState parseResult, QualifiedSelecti
2626
TargetDeclaration = FindSelection(selection);
2727
}
2828

29-
private Selection GetVariableStmtContextSelection(Declaration target)
29+
public Selection GetVariableStmtContextSelection(Declaration target)
3030
{
3131
var statement = GetVariableStmtContext(target);
3232

3333
return new Selection(statement.Start.Line, statement.Start.Column,
3434
statement.Stop.Line, statement.Stop.Column);
3535
}
3636

37-
private VBAParser.VariableStmtContext GetVariableStmtContext(Declaration target)
37+
public VBAParser.VariableStmtContext GetVariableStmtContext(Declaration target)
3838
{
3939
var statement = target.Context.Parent.Parent as VBAParser.VariableStmtContext;
4040
if (statement == null)
@@ -45,7 +45,7 @@ private VBAParser.VariableStmtContext GetVariableStmtContext(Declaration target)
4545
return statement;
4646
}
4747

48-
private bool HasMultipleDeclarationsInStatement(Declaration target)
48+
public bool HasMultipleDeclarationsInStatement(Declaration target)
4949
{
5050
var statement = target.Context.Parent as VBAParser.VariableListStmtContext;
5151

RetailCoder.VBE/Refactorings/EncapsulateField/EncapsulateFieldRefactoring.cs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Linq;
3+
using Microsoft.Vbe.Interop;
24
using Rubberduck.Parsing.Symbols;
35
using Rubberduck.VBEditor;
46

@@ -7,11 +9,13 @@ namespace Rubberduck.Refactorings.EncapsulateField
79
class EncapsulateFieldRefactoring : IRefactoring
810
{
911
private readonly IRefactoringPresenterFactory<IEncapsulateFieldPresenter> _factory;
12+
private readonly IActiveCodePaneEditor _editor;
1013
private EncapsulateFieldModel _model;
1114

12-
public EncapsulateFieldRefactoring(IRefactoringPresenterFactory<IEncapsulateFieldPresenter> factory)
15+
public EncapsulateFieldRefactoring(IRefactoringPresenterFactory<IEncapsulateFieldPresenter> factory, IActiveCodePaneEditor editor)
1316
{
1417
_factory = factory;
18+
_editor = editor;
1519
}
1620

1721
public void Refactor()
@@ -44,6 +48,8 @@ private void AddProperty()
4448
UpdateReferences();
4549

4650
var module = _model.TargetDeclaration.QualifiedName.QualifiedModuleName.Component.CodeModule;
51+
SetFieldToPrivate(module);
52+
4753
module.InsertLines(module.CountOfDeclarationLines + 1, GetPropertyText());
4854
}
4955

@@ -61,6 +67,98 @@ private void UpdateReferences()
6167
}
6268
}
6369

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+
64162
private string GetPropertyText()
65163
{
66164
var getterText = string.Join(Environment.NewLine,
@@ -84,7 +182,7 @@ private string GetPropertyText()
84182
return string.Join(Environment.NewLine,
85183
getterText,
86184
(_model.ImplementLetSetterType ? letterText : string.Empty),
87-
(_model.ImplementSetSetterType ? setterText : string.Empty));
185+
(_model.ImplementSetSetterType ? setterText : string.Empty)).TrimEnd();
88186
}
89187
}
90188
}

RetailCoder.VBE/UI/Command/Refactorings/RefactorEncapsulateFieldCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override void Execute(object parameter)
2828
using (var view = new EncapsulateFieldDialog())
2929
{
3030
var factory = new EncapsulateFieldPresenterFactory(_state, Editor, view);
31-
var refactoring = new EncapsulateFieldRefactoring(factory);
31+
var refactoring = new EncapsulateFieldRefactoring(factory, Editor);
3232
refactoring.Refactor();
3333
}
3434
}

RetailCoder.VBE/UI/Refactorings/EncapsulateFieldDialog.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,28 @@ private void UpdatePreview()
119119

120120
private void ValidatePropertyName()
121121
{
122-
if (TargetDeclaration == null) { return; }
123-
124-
var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
125-
126-
InvalidPropertyNameIcon.Visible = NewPropertyName == TargetDeclaration.IdentifierName
127-
|| !char.IsLetter(NewPropertyName.FirstOrDefault())
128-
|| tokenValues.Contains(NewPropertyName, StringComparer.InvariantCultureIgnoreCase)
129-
|| NewPropertyName.Any(c => !char.IsLetterOrDigit(c) && c != '_');
122+
InvalidPropertyNameIcon.Visible = ValidateName(NewPropertyName, ParameterName);
130123

131124
SetOkButtonEnabledState();
132125
}
133126

134127
private void ValidateVariableName()
135128
{
136-
if (TargetDeclaration == null) { return; }
129+
InvalidVariableNameIcon.Visible = ValidateName(ParameterName, NewPropertyName);
137130

131+
SetOkButtonEnabledState();
132+
}
133+
134+
private bool ValidateName(string changedName, string otherName)
135+
{
138136
var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
139137

140-
InvalidVariableNameIcon.Visible = ParameterName == TargetDeclaration.IdentifierName
141-
|| ParameterName == NewPropertyName
142-
|| !char.IsLetter(ParameterName.FirstOrDefault())
138+
return TargetDeclaration == null
139+
|| changedName == TargetDeclaration.IdentifierName
140+
|| changedName == otherName
141+
|| !char.IsLetter(changedName.FirstOrDefault())
143142
|| tokenValues.Contains(ParameterName, StringComparer.InvariantCultureIgnoreCase)
144-
|| ParameterName.Any(c => !char.IsLetterOrDigit(c) && c != '_');
145-
146-
SetOkButtonEnabledState();
143+
|| changedName.Any(c => !char.IsLetterOrDigit(c) && c != '_');
147144
}
148145

149146
private void SetOkButtonEnabledState()

0 commit comments

Comments
 (0)