Skip to content

Commit

Permalink
Merge pull request #2743 from comintern/next
Browse files Browse the repository at this point in the history
EncapsulateFieldDialog fixes, refactor.
  • Loading branch information
retailcoder committed Feb 27, 2017
2 parents 1349239 + 6d17b95 commit 127ad15
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 156 deletions.
@@ -1,7 +1,9 @@
using System.Linq;
using System.Windows.Forms;
using Antlr4.Runtime;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Refactorings.EncapsulateField
{
Expand All @@ -21,42 +23,20 @@ public EncapsulateFieldPresenter(IEncapsulateFieldDialog view, EncapsulateFieldM
_model = model;
}

private static readonly string[] PrimitiveTypes =
{
Tokens.Boolean,
Tokens.Byte,
Tokens.Date,
Tokens.Decimal,
Tokens.Double,
Tokens.Long,
Tokens.LongLong,
Tokens.LongPtr,
Tokens.Integer,
Tokens.Single,
Tokens.String,
Tokens.StrPtr
};

public EncapsulateFieldModel Show()
{
if (_model.TargetDeclaration == null) { return null; }

_view.TargetDeclaration = _model.TargetDeclaration;
_view.NewPropertyName = _model.TargetDeclaration.IdentifierName;

var isVariant = _model.TargetDeclaration.AsTypeName.Equals(Tokens.Variant);
var isValueType = !isVariant && (SymbolList.ValueTypes.Contains(_model.TargetDeclaration.AsTypeName) ||
_model.TargetDeclaration.DeclarationType == DeclarationType.Enumeration);

if (_model.TargetDeclaration.References.Any(r => r.IsAssignment))
{
if (PrimitiveTypes.Contains(_model.TargetDeclaration.AsTypeName))
{
_view.MustImplementLetSetterType = true;
_view.CanImplementSetSetterType = false;
}
else if (_model.TargetDeclaration.AsTypeName != Tokens.Variant)
{
_view.MustImplementSetSetterType = true;
_view.CanImplementLetSetterType = false;
}
else
if (isVariant)
{
RuleContext node = _model.TargetDeclaration.References.First(r => r.IsAssignment).Context;
while (!(node is VBAParser.LetStmtContext) && !(node is VBAParser.SetStmtContext))
Expand All @@ -66,25 +46,36 @@ public EncapsulateFieldModel Show()

if (node is VBAParser.LetStmtContext)
{
_view.MustImplementLetSetterType = true;
_view.CanImplementSetSetterType = false;
_view.CanImplementLetSetterType = true;
}
else
{
_view.MustImplementSetSetterType = true;
_view.CanImplementLetSetterType = false;
}
_view.CanImplementSetSetterType = true;
}
}
else if (isValueType)
{
_view.CanImplementLetSetterType = true;
}
else
{
_view.CanImplementSetSetterType = true;
}
}
else
{
if (PrimitiveTypes.Contains(_model.TargetDeclaration.AsTypeName))
if (isValueType)
{
_view.CanImplementLetSetterType = true;
}
else if (!isVariant)
{
_view.CanImplementSetSetterType = false;
_view.CanImplementSetSetterType = true;
}
else if (_model.TargetDeclaration.AsTypeName != Tokens.Variant)
else
{
_view.CanImplementLetSetterType = false;
_view.CanImplementLetSetterType = true;
_view.CanImplementSetSetterType = true;
}
}

Expand All @@ -94,9 +85,9 @@ public EncapsulateFieldModel Show()
}

_model.PropertyName = _view.NewPropertyName;
_model.ImplementLetSetterType = _view.MustImplementLetSetterType;
_model.ImplementSetSetterType = _view.MustImplementSetSetterType;
_model.CanImplementLet = _view.CanImplementLetSetterType;
_model.ImplementLetSetterType = _view.CanImplementLetSetterType;
_model.ImplementSetSetterType = _view.CanImplementSetSetterType;
_model.CanImplementLet = !_view.MustImplementSetSetterType;

_model.ParameterName = _view.ParameterName;
return _model;
Expand Down
Expand Up @@ -79,7 +79,7 @@ private void AddProperty()
var module = _model.TargetDeclaration.QualifiedName.QualifiedModuleName.Component.CodeModule;
SetFieldToPrivate(module);

module.InsertLines(module.CountOfDeclarationLines + 1, GetPropertyText());
module.InsertLines(module.CountOfDeclarationLines + 1, Environment.NewLine + GetPropertyText());
}

private void UpdateReferences()
Expand Down Expand Up @@ -207,30 +207,17 @@ private string RemoveExtraComma(string str, int numParams, int indexRemoved)

private string GetPropertyText()
{
var getterText = string.Join(Environment.NewLine,
string.Format(Environment.NewLine + "Public Property Get {0}() As {1}", _model.PropertyName,
_model.TargetDeclaration.AsTypeName),
string.Format(" {0}{1} = {2}", !_model.CanImplementLet || _model.ImplementSetSetterType ? "Set " : string.Empty, _model.PropertyName, _model.TargetDeclaration.IdentifierName),
"End Property" + Environment.NewLine);

var letterText = string.Join(Environment.NewLine,
string.Format(Environment.NewLine + "Public Property Let {0}(ByVal {1} As {2})",
_model.PropertyName, _model.ParameterName, _model.TargetDeclaration.AsTypeName),
string.Format(" {0} = {1}", _model.TargetDeclaration.IdentifierName, _model.ParameterName),
"End Property" + Environment.NewLine);

var setterText = string.Join(Environment.NewLine,
string.Format(Environment.NewLine + "Public Property Set {0}(ByVal {1} As {2})",
_model.PropertyName, _model.ParameterName, _model.TargetDeclaration.AsTypeName),
string.Format(" Set {0} = {1}", _model.TargetDeclaration.IdentifierName, _model.ParameterName),
"End Property" + Environment.NewLine);

var propertyText = string.Join(string.Empty,
getterText,
(_model.ImplementLetSetterType ? letterText : string.Empty),
(_model.ImplementSetSetterType ? setterText : string.Empty)).TrimEnd() + Environment.NewLine;

var propertyTextLines = propertyText.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
var generator = new PropertyGenerator
{
PropertyName = _model.PropertyName,
AsTypeName = _model.TargetDeclaration.AsTypeName,
BackingField = _model.TargetDeclaration.IdentifierName,
ParameterName = _model.ParameterName,
GenerateSetter = _model.ImplementSetSetterType,
GenerateLetter = _model.ImplementLetSetterType
};

var propertyTextLines = generator.AllPropertyCode.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
return string.Join(Environment.NewLine, _indenter.Indent(propertyTextLines, true));
}
}
Expand Down
Expand Up @@ -10,9 +10,10 @@ public interface IEncapsulateFieldDialog :IDialogView
string NewPropertyName { get; set; }
bool CanImplementLetSetterType { get; set; }
bool CanImplementSetSetterType { get; set; }

bool MustImplementLetSetterType { get; set; }
bool MustImplementSetSetterType { get; set; }
bool LetSetterSelected { get; }
bool SetSetterSelected { get; }
bool MustImplementLetSetterType { get; }
bool MustImplementSetSetterType { get; }

string ParameterName { get; set; }
}
Expand Down
81 changes: 81 additions & 0 deletions RetailCoder.VBE/Refactorings/EncapsulateField/PropertyGenerator.cs
@@ -0,0 +1,81 @@
using System;

namespace Rubberduck.Refactorings.EncapsulateField
{
public class PropertyGenerator
{
public string PropertyName { get; set; }
public string BackingField { get; set; }
public string AsTypeName { get; set; }
public string ParameterName { get; set; }
public bool GenerateLetter { get; set; }
public bool GenerateSetter { get; set; }

public string AllPropertyCode
{
get
{
return GetterCode +
(GenerateLetter ? LetterCode : string.Empty) +
(GenerateSetter ? SetterCode : string.Empty);
}
}

public string GetterCode
{
get
{
if (GenerateSetter && GenerateLetter)
{
return string.Join(Environment.NewLine,
string.Format("Public Property Get {0}() As {1}", PropertyName, AsTypeName),
string.Format(" If IsObject({0}) Then", BackingField),
string.Format(" Set {0} = {1}", PropertyName, BackingField),
" Else",
string.Format(" {0} = {1}", PropertyName, BackingField),
" End If",
"End Property",
Environment.NewLine);
}

return string.Join(Environment.NewLine,
string.Format("Public Property Get {0}() As {1}", PropertyName, AsTypeName),
string.Format(" {0}{1} = {2}", GenerateSetter ? "Set " : string.Empty, PropertyName, BackingField),
"End Property",
Environment.NewLine);
}
}

public string SetterCode
{
get
{
if (!GenerateSetter)
{
return string.Empty;
}
return string.Join(Environment.NewLine,
string.Format("Public Property Set {0}(ByVal {1} As {2})", PropertyName, ParameterName, AsTypeName),
string.Format(" Set {0} = {1}", BackingField, ParameterName),
"End Property",
Environment.NewLine);
}
}

public string LetterCode
{
get
{
if (!GenerateLetter)
{
return string.Empty;
}
return string.Join(Environment.NewLine,
string.Format("Public Property Let {0}(ByVal {1} As {2})", PropertyName, ParameterName, AsTypeName),
string.Format(" {0} = {1}", BackingField, ParameterName),
"End Property",
Environment.NewLine);
}
}
}
}
1 change: 1 addition & 0 deletions RetailCoder.VBE/Rubberduck.csproj
Expand Up @@ -413,6 +413,7 @@
<Compile Include="Inspections\VariableNameValidator.cs" />
<Compile Include="Navigation\CodeExplorer\ICodeExplorerDeclarationViewModel.cs" />
<Compile Include="Navigation\Folders\FolderHelper.cs" />
<Compile Include="Refactorings\EncapsulateField\PropertyGenerator.cs" />
<Compile Include="Refactorings\ExtractMethod\ExtractedMethod.cs" />
<Compile Include="Refactorings\ExtractMethod\ExtractMethodParameterClassification.cs" />
<Compile Include="Refactorings\ExtractMethod\ExtractMethodSelectionValidation.cs" />
Expand Down

0 comments on commit 127ad15

Please sign in to comment.