Skip to content

Commit 11c836c

Browse files
committed
Refactor - create extension methods
1 parent b493b64 commit 11c836c

File tree

9 files changed

+111
-276
lines changed

9 files changed

+111
-276
lines changed

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Rubberduck.Parsing.Grammar;
99
using Rubberduck.Parsing.Symbols;
1010
using Rubberduck.VBEditor;
11+
// ReSharper disable LocalizableElement
1112

1213
namespace Rubberduck.Common
1314
{
@@ -20,6 +21,47 @@ public static BitmapImage BitmapImage(this Declaration declaration)
2021
return Cache[declaration];
2122
}
2223

24+
public static Selection GetVariableStmtContextSelection(this Declaration target)
25+
{
26+
if (target.DeclarationType != DeclarationType.Variable)
27+
{
28+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
29+
}
30+
31+
var statement = GetVariableStmtContext(target);
32+
33+
return new Selection(statement.Start.Line, statement.Start.Column,
34+
statement.Stop.Line, statement.Stop.Column);
35+
}
36+
37+
public static VBAParser.VariableStmtContext GetVariableStmtContext(this Declaration target)
38+
{
39+
if (target.DeclarationType != DeclarationType.Variable)
40+
{
41+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
42+
}
43+
44+
var statement = target.Context.Parent.Parent as VBAParser.VariableStmtContext;
45+
if (statement == null)
46+
{
47+
throw new MissingMemberException("Statement not found");
48+
}
49+
50+
return statement;
51+
}
52+
53+
public static bool HasMultipleDeclarationsInStatement(this Declaration target)
54+
{
55+
if (target.DeclarationType != DeclarationType.Variable)
56+
{
57+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
58+
}
59+
60+
var statement = target.Context.Parent as VBAParser.VariableListStmtContext;
61+
62+
return statement != null && statement.children.Count(i => i is VBAParser.VariableSubStmtContext) > 1;
63+
}
64+
2365
public static readonly DeclarationType[] ProcedureTypes =
2466
{
2567
DeclarationType.Procedure,
@@ -332,5 +374,40 @@ public static Declaration FindSelection(this IEnumerable<Declaration> declaratio
332374
}
333375
return target;
334376
}
377+
378+
public static Declaration FindVariable(this IEnumerable<Declaration> declarations, QualifiedSelection selection)
379+
{
380+
var items = declarations.Where(d => !d.IsBuiltIn && d.DeclarationType == DeclarationType.Variable).ToList();
381+
382+
var target = items
383+
.FirstOrDefault(item => item.IsSelected(selection) || item.References.Any(r => r.IsSelected(selection)));
384+
385+
if (target != null) { return target; }
386+
387+
var targets = items.Where(item => item.ComponentName == selection.QualifiedName.ComponentName);
388+
389+
foreach (var declaration in targets)
390+
{
391+
var declarationSelection = new Selection(declaration.Context.Start.Line,
392+
declaration.Context.Start.Column,
393+
declaration.Context.Stop.Line,
394+
declaration.Context.Stop.Column + declaration.Context.Stop.Text.Length);
395+
396+
if (declarationSelection.Contains(selection.Selection) ||
397+
!HasMultipleDeclarationsInStatement(declaration) && GetVariableStmtContextSelection(declaration).Contains(selection.Selection))
398+
{
399+
return declaration;
400+
}
401+
402+
var reference =
403+
declaration.References.FirstOrDefault(r => r.Selection.Contains(selection.Selection));
404+
405+
if (reference != null)
406+
{
407+
return reference.Declaration;
408+
}
409+
}
410+
return null;
411+
}
335412
}
336413
}
Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
4-
using Rubberduck.Parsing.Grammar;
3+
using Rubberduck.Common;
54
using Rubberduck.Parsing.Symbols;
65
using Rubberduck.Parsing.VBA;
76
using Rubberduck.VBEditor;
@@ -10,8 +9,6 @@ namespace Rubberduck.Refactorings.EncapsulateField
109
{
1110
public class EncapsulateFieldModel
1211
{
13-
private readonly IList<Declaration> _declarations;
14-
1512
public Declaration TargetDeclaration { get; private set; }
1613

1714
public string PropertyName { get; set; }
@@ -21,68 +18,11 @@ public class EncapsulateFieldModel
2118

2219
public EncapsulateFieldModel(RubberduckParserState parseResult, QualifiedSelection selection)
2320
{
24-
_declarations = parseResult.AllDeclarations.Where(d => !d.IsBuiltIn && d.DeclarationType == DeclarationType.Variable).ToList();
25-
26-
TargetDeclaration = FindSelection(selection);
27-
}
28-
29-
public Selection GetVariableStmtContextSelection(Declaration target)
30-
{
31-
var statement = GetVariableStmtContext(target);
32-
33-
return new Selection(statement.Start.Line, statement.Start.Column,
34-
statement.Stop.Line, statement.Stop.Column);
35-
}
36-
37-
public VBAParser.VariableStmtContext GetVariableStmtContext(Declaration target)
38-
{
39-
var statement = target.Context.Parent.Parent as VBAParser.VariableStmtContext;
40-
if (statement == null)
41-
{
42-
throw new NullReferenceException("Statement not found");
43-
}
44-
45-
return statement;
46-
}
47-
48-
public bool HasMultipleDeclarationsInStatement(Declaration target)
49-
{
50-
var statement = target.Context.Parent as VBAParser.VariableListStmtContext;
51-
52-
return statement != null && statement.children.Count(i => i is VBAParser.VariableSubStmtContext) > 1;
53-
}
54-
55-
private Declaration FindSelection(QualifiedSelection selection)
56-
{
57-
var target = _declarations
58-
.FirstOrDefault(item => item.IsSelected(selection) || item.References.Any(r => r.IsSelected(selection)));
59-
60-
if (target != null) { return target; }
61-
62-
var targets = _declarations.Where(item => item.ComponentName == selection.QualifiedName.ComponentName);
63-
64-
foreach (var declaration in targets)
65-
{
66-
var declarationSelection = new Selection(declaration.Context.Start.Line,
67-
declaration.Context.Start.Column,
68-
declaration.Context.Stop.Line,
69-
declaration.Context.Stop.Column + declaration.Context.Stop.Text.Length);
70-
71-
if (declarationSelection.Contains(selection.Selection) ||
72-
!HasMultipleDeclarationsInStatement(declaration) && GetVariableStmtContextSelection(declaration).Contains(selection.Selection))
73-
{
74-
return declaration;
75-
}
76-
77-
var reference =
78-
declaration.References.FirstOrDefault(r => r.Selection.Contains(selection.Selection));
21+
IList<Declaration> declarations = parseResult.AllDeclarations
22+
.Where(d => !d.IsBuiltIn && d.DeclarationType == DeclarationType.Variable)
23+
.ToList();
7924

80-
if (reference != null)
81-
{
82-
return reference.Declaration;
83-
}
84-
}
85-
return null;
25+
TargetDeclaration = declarations.FindVariable(selection);
8626
}
8727
}
8828
}

RetailCoder.VBE/Refactorings/EncapsulateField/EncapsulateFieldRefactoring.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using Microsoft.Vbe.Interop;
4+
using Rubberduck.Common;
45
using Rubberduck.Parsing.Symbols;
56
using Rubberduck.VBEditor;
67

@@ -95,14 +96,14 @@ private void RemoveField(Declaration target)
9596
{
9697
Selection selection;
9798
var declarationText = target.Context.GetText();
98-
var multipleDeclarations = _model.HasMultipleDeclarationsInStatement(target);
99+
var multipleDeclarations = target.HasMultipleDeclarationsInStatement();
99100

100-
var variableStmtContext = _model.GetVariableStmtContext(target);
101+
var variableStmtContext = target.GetVariableStmtContext();
101102

102103
if (!multipleDeclarations)
103104
{
104105
declarationText = variableStmtContext.GetText();
105-
selection = _model.GetVariableStmtContextSelection(target);
106+
selection = target.GetVariableStmtContextSelection();
106107
}
107108
else
108109
{
@@ -117,7 +118,7 @@ private void RemoveField(Declaration target)
117118

118119
if (multipleDeclarations)
119120
{
120-
selection = _model.GetVariableStmtContextSelection(target);
121+
selection = target.GetVariableStmtContextSelection();
121122
newLines = RemoveExtraComma(_editor.GetLines(selection).Replace(oldLines, newLines));
122123
}
123124

RetailCoder.VBE/Refactorings/IntroduceField/IntroduceFieldRefactoring.cs

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Rubberduck.Parsing.Grammar;
4+
using Rubberduck.Common;
55
using Rubberduck.Parsing.Symbols;
66
using Rubberduck.Parsing.VBA;
77
using Rubberduck.UI;
@@ -40,7 +40,7 @@ public void Refactor()
4040

4141
public void Refactor(QualifiedSelection selection)
4242
{
43-
var target = FindSelection(selection);
43+
var target = _declarations.FindVariable(selection);
4444

4545
PromoteVariable(target);
4646
}
@@ -71,14 +71,14 @@ private void RemoveVariable(Declaration target)
7171
{
7272
Selection selection;
7373
var declarationText = target.Context.GetText();
74-
var multipleDeclarations = HasMultipleDeclarationsInStatement(target);
74+
var multipleDeclarations = target.HasMultipleDeclarationsInStatement();
7575

76-
var variableStmtContext = GetVariableStmtContext(target);
76+
var variableStmtContext = target.GetVariableStmtContext();
7777

7878
if (!multipleDeclarations)
7979
{
8080
declarationText = variableStmtContext.GetText();
81-
selection = GetVariableStmtContextSelection(target);
81+
selection = target.GetVariableStmtContextSelection();
8282
}
8383
else
8484
{
@@ -93,33 +93,14 @@ private void RemoveVariable(Declaration target)
9393

9494
if (multipleDeclarations)
9595
{
96-
selection = GetVariableStmtContextSelection(target);
96+
selection = target.GetVariableStmtContextSelection();
9797
newLines = RemoveExtraComma(_editor.GetLines(selection).Replace(oldLines, newLines));
9898
}
9999

100100
_editor.DeleteLines(selection);
101101
_editor.InsertLines(selection.StartLine, newLines);
102102
}
103103

104-
private Selection GetVariableStmtContextSelection(Declaration target)
105-
{
106-
var statement = GetVariableStmtContext(target);
107-
108-
return new Selection(statement.Start.Line, statement.Start.Column,
109-
statement.Stop.Line, statement.Stop.Column);
110-
}
111-
112-
private VBAParser.VariableStmtContext GetVariableStmtContext(Declaration target)
113-
{
114-
var statement = target.Context.Parent.Parent as VBAParser.VariableStmtContext;
115-
if (statement == null)
116-
{
117-
throw new NullReferenceException("Statement not found");
118-
}
119-
120-
return statement;
121-
}
122-
123104
private string RemoveExtraComma(string str)
124105
{
125106
if (str.Count(c => c == ',') == 1)
@@ -150,51 +131,11 @@ private string RemoveExtraComma(string str)
150131
return str.Remove(str.LastIndexOf(','), 1);
151132
}
152133

153-
private bool HasMultipleDeclarationsInStatement(Declaration target)
154-
{
155-
var statement = target.Context.Parent as VBAParser.VariableListStmtContext;
156-
157-
return statement != null && statement.children.Count(i => i is VBAParser.VariableSubStmtContext) > 1;
158-
}
159-
160134
private string GetFieldDefinition(Declaration target)
161135
{
162136
if (target == null) { return null; }
163137

164138
return "Private " + target.IdentifierName + " As " + target.AsTypeName;
165139
}
166-
167-
private Declaration FindSelection(QualifiedSelection selection)
168-
{
169-
var target = _declarations
170-
.FirstOrDefault(item => item.IsSelected(selection) || item.References.Any(r => r.IsSelected(selection)));
171-
172-
if (target != null) { return target; }
173-
174-
var targets = _declarations.Where(item => item.ComponentName == selection.QualifiedName.ComponentName);
175-
176-
foreach (var declaration in targets)
177-
{
178-
var declarationSelection = new Selection(declaration.Context.Start.Line,
179-
declaration.Context.Start.Column,
180-
declaration.Context.Stop.Line,
181-
declaration.Context.Stop.Column + declaration.Context.Stop.Text.Length);
182-
183-
if (declarationSelection.Contains(selection.Selection) ||
184-
!HasMultipleDeclarationsInStatement(declaration) && GetVariableStmtContextSelection(declaration).Contains(selection.Selection))
185-
{
186-
return declaration;
187-
}
188-
189-
var reference =
190-
declaration.References.FirstOrDefault(r => r.Selection.Contains(selection.Selection));
191-
192-
if (reference != null)
193-
{
194-
return reference.Declaration;
195-
}
196-
}
197-
return null;
198-
}
199140
}
200141
}

0 commit comments

Comments
 (0)