Skip to content

Commit bec11d6

Browse files
committed
Add declaration inside new method for return parameter if not already in the selected code
1 parent e35c3a2 commit bec11d6

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

Rubberduck.Refactorings/ExtractMethod/ExtractMethodModel.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ private void Setup()
7979
var declarationsInParentMethod = GetDeclarationsInSelection(sourceMethodSelection);
8080

8181
//List of "inbound" variables. Parent procedure parameters + explicit dims which get referenced inside the selection.
82-
//TODO - add case where reference earlier in the same line as where the selection starts (unusual but could exist if using colons to separate multiple statements)
8382
var inboundParameters = sourceMethodParameters.Where(d => d.References.Any(r => QualifiedSelection.Selection.Contains(r.Selection)));
8483
var inboundLocalVariables = declarationsInParentMethod
8584
.Where(d => d.References.Any(r => QualifiedSelection.Selection.Contains(r.Selection)) &&
@@ -161,9 +160,7 @@ private void SetUpParameters(IEnumerable<Declaration> inboundVariables, IEnumera
161160
paramType = ExtractMethodParameterType.ByRefParameter;
162161
canReturn = true;
163162
}
164-
Parameters.Add(new ExtractMethodParameter(declaration.AsTypeNameWithoutArrayDesignator,
165-
paramType, declaration.IdentifierName,
166-
declaration.IsArray, declaration.IsObject, canReturn));
163+
Parameters.Add(new ExtractMethodParameter(declaration, paramType, canReturn));
167164
}
168165
}
169166

@@ -362,10 +359,16 @@ public string NewMethodCode
362359
}({string.Join(", ", _parametersToExtract)}) {returnType}");
363360
foreach (var dec in _declarationsToMoveIn)
364361
{
365-
//go from variableSubStmt to variableListStmt to variableStmt
366-
strings.Add(dec.Context.Parent.Parent.GetText());
362+
strings.Add(dec.Context.GetAncestor<VBAParser.VariableStmtContext>().GetText());
367363
//TODO - handle case of variable list having multiple parts (if not excluded by validator)
368364
}
365+
if (isFunction)
366+
{
367+
if (!QualifiedSelection.Selection.Contains(ReturnParameter.Declaration.Context))
368+
{
369+
strings.Add(ReturnParameter.Declaration.Context.GetAncestor<VBAParser.VariableStmtContext>().GetText());
370+
}
371+
}
369372
strings.AddRange(SelectedCodeToExtract.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
370373
if (isFunction)
371374
{

Rubberduck.Refactorings/ExtractMethod/ExtractMethodParameter.cs

+24-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Rubberduck.Parsing.Grammar;
55
using System.ComponentModel;
66
using System.Collections.ObjectModel;
7+
using Rubberduck.Parsing.Symbols;
8+
using System.Xml.Linq;
79

810
namespace Rubberduck.Refactorings.ExtractMethod
911
{
@@ -27,16 +29,28 @@ public class ExtractMethodParameter : INotifyPropertyChanged
2729

2830
public event PropertyChangedEventHandler PropertyChanged;
2931

30-
public ExtractMethodParameter(string typeName, ExtractMethodParameterType parameterType, string name, bool isArray, bool isObject, bool canReturn)
32+
public ExtractMethodParameter(Declaration declaration, ExtractMethodParameterType parameterType, bool canReturn)
3133
{
32-
Name = name ?? NoneLabel;
33-
TypeName = typeName;
3434
ParameterType = parameterType;
35-
IsArray = isArray;
3635
CanReturn = canReturn;
37-
IsObject = isObject;
36+
Declaration = declaration;
37+
if (declaration == null)
38+
{
39+
Name = NoneLabel;
40+
TypeName = string.Empty;
41+
IsArray = false;
42+
IsObject = false;
43+
}
44+
else
45+
{
46+
Name = declaration.IdentifierName;
47+
TypeName = declaration.AsTypeNameWithoutArrayDesignator;
48+
IsArray = declaration.IsArray;
49+
IsObject = declaration.IsObject;
50+
}
3851
}
3952

53+
public Declaration Declaration { get; }
4054
public string Name { get; set; }
4155

4256
public string TypeName { get; set; }
@@ -111,9 +125,7 @@ public override string ToString()
111125
}
112126

113127

114-
public static ExtractMethodParameter None => new ExtractMethodParameter(string.Empty,
115-
ExtractMethodParameterType.ByValParameter,
116-
RefactoringsUI.ExtractMethod_NoneSelected, false, false, false);
128+
public static ExtractMethodParameter None => new ExtractMethodParameter(null, ExtractMethodParameterType.ByValParameter, false);
117129

118130
public static ObservableCollection<ExtractMethodParameterType> AllowableTypes = new ObservableCollection<ExtractMethodParameterType>
119131
{
@@ -144,43 +156,31 @@ public static Dictionary<ExtractMethodParameterType, string> ParameterTypes
144156
public static bool operator ==(ExtractMethodParameter left, ExtractMethodParameter right)
145157
{
146158
return left?.ParameterType == right?.ParameterType &&
147-
left?.TypeName == right?.TypeName &&
148-
left?.Name == right?.Name &&
149-
left?.IsArray == right?.IsArray &&
150159
left?.CanReturn == right?.CanReturn &&
151-
left?.IsObject == right?.IsObject;
160+
left?.Declaration == right?.Declaration;
152161
}
153162

154163
public static bool operator !=(ExtractMethodParameter left, ExtractMethodParameter right)
155164
{
156165
return !(left?.ParameterType == right?.ParameterType &&
157-
left?.TypeName == right?.TypeName &&
158-
left?.Name == right?.Name &&
159-
left?.IsArray == right?.IsArray &&
160166
left?.CanReturn == right?.CanReturn &&
161-
left?.IsObject == right?.IsObject);
167+
left?.Declaration == right?.Declaration);
162168
}
163169

164170
public override bool Equals(object obj)
165171
{
166172
return obj is ExtractMethodParameter parameter &&
167-
Name == parameter.Name &&
168-
TypeName == parameter.TypeName &&
169173
CanReturn == parameter.CanReturn &&
170174
ParameterType == parameter.ParameterType &&
171-
IsArray == parameter.IsArray &&
172-
IsObject == parameter.IsObject;
175+
Declaration == parameter.Declaration;
173176
}
174177

175178
public override int GetHashCode()
176179
{
177180
int hashCode = 1661774273;
178-
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Name);
179-
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(TypeName);
180181
hashCode = (hashCode * -1521134295) + CanReturn.GetHashCode();
181182
hashCode = (hashCode * -1521134295) + ParameterType.GetHashCode();
182-
hashCode = (hashCode * -1521134295) + IsArray.GetHashCode();
183-
hashCode = (hashCode * -1521134295) + IsObject.GetHashCode();
183+
hashCode = (hashCode * -1521134295) + (Declaration == null ? 0 : Declaration.GetHashCode());
184184
return hashCode;
185185
}
186186
}

RubberduckTests/Refactoring/ExtractMethod/ExtractMethodModelTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Debug.Print a
307307
var selection = new Selection(4, 5, 4, 11);
308308
var expectedNewMethodCode = @"
309309
Private Function NewMethod() As Integer
310+
Dim a As Integer
310311
a = 10
311312
312313
NewMethod = a

0 commit comments

Comments
 (0)