Skip to content

Commit 950c66a

Browse files
committed
Merge pull request #891 from Hosch250/BugBlipper
Refactor - create extension methods
2 parents 6598dbb + 951550e commit 950c66a

File tree

13 files changed

+150
-283
lines changed

13 files changed

+150
-283
lines changed

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 110 additions & 1 deletion
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,65 @@ public static BitmapImage BitmapImage(this Declaration declaration)
2021
return Cache[declaration];
2122
}
2223

24+
/// <summary>
25+
/// Returns the Selection of a VariableStmtContext.
26+
/// </summary>
27+
/// <exception cref="ArgumentException">Throws when target's DeclarationType is not Variable.</exception>
28+
/// <param name="target"></param>
29+
/// <returns></returns>
30+
public static Selection GetVariableStmtContextSelection(this Declaration target)
31+
{
32+
if (target.DeclarationType != DeclarationType.Variable)
33+
{
34+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
35+
}
36+
37+
var statement = GetVariableStmtContext(target);
38+
39+
return new Selection(statement.Start.Line, statement.Start.Column,
40+
statement.Stop.Line, statement.Stop.Column);
41+
}
42+
43+
/// <summary>
44+
/// Returns a VariableStmtContext.
45+
/// </summary>
46+
/// <exception cref="ArgumentException">Throws when target's DeclarationType is not Variable.</exception>
47+
/// <param name="target"></param>
48+
/// <returns></returns>
49+
public static VBAParser.VariableStmtContext GetVariableStmtContext(this Declaration target)
50+
{
51+
if (target.DeclarationType != DeclarationType.Variable)
52+
{
53+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
54+
}
55+
56+
var statement = target.Context.Parent.Parent as VBAParser.VariableStmtContext;
57+
if (statement == null)
58+
{
59+
throw new MissingMemberException("Statement not found");
60+
}
61+
62+
return statement;
63+
}
64+
65+
/// <summary>
66+
/// Returns whether a variable declaration statement contains multiple declarations in a single statement.
67+
/// </summary>
68+
/// <exception cref="ArgumentException">Throws when target's DeclarationType is not Variable.</exception>
69+
/// <param name="target"></param>
70+
/// <returns></returns>
71+
public static bool HasMultipleDeclarationsInStatement(this Declaration target)
72+
{
73+
if (target.DeclarationType != DeclarationType.Variable)
74+
{
75+
throw new ArgumentException("Target DeclarationType is not Variable.", "target");
76+
}
77+
78+
var statement = target.Context.Parent as VBAParser.VariableListStmtContext;
79+
80+
return statement != null && statement.children.Count(i => i is VBAParser.VariableSubStmtContext) > 1;
81+
}
82+
2383
public static readonly DeclarationType[] ProcedureTypes =
2484
{
2585
DeclarationType.Procedure,
@@ -270,7 +330,15 @@ public static Declaration FindInterfaceMember(this IEnumerable<Declaration> decl
270330
: matches.First();
271331
}
272332

273-
public static Declaration FindSelection(this IEnumerable<Declaration> declarations, QualifiedSelection selection, DeclarationType[] validDeclarationTypes)
333+
/// <summary>
334+
/// Returns the declaration contained in a qualified selection.
335+
/// To get the selection of a variable or field, use FindVariable(QualifiedSelection)
336+
/// </summary>
337+
/// <param name="declarations"></param>
338+
/// <param name="selection"></param>
339+
/// <param name="validDeclarationTypes"></param>
340+
/// <returns></returns>
341+
public static Declaration FindTarget(this IEnumerable<Declaration> declarations, QualifiedSelection selection, DeclarationType[] validDeclarationTypes)
274342
{
275343
var items = declarations.ToList();
276344

@@ -332,5 +400,46 @@ public static Declaration FindSelection(this IEnumerable<Declaration> declaratio
332400
}
333401
return target;
334402
}
403+
404+
/// <summary>
405+
/// Returns the variable which contains the passed-in QualifiedSelection. Returns null if the selection is not on a variable.
406+
/// </summary>
407+
/// <param name="declarations"></param>
408+
/// <param name="selection"></param>
409+
/// <returns></returns>
410+
public static Declaration FindVariable(this IEnumerable<Declaration> declarations, QualifiedSelection selection)
411+
{
412+
var items = declarations.Where(d => !d.IsBuiltIn && d.DeclarationType == DeclarationType.Variable).ToList();
413+
414+
var target = items
415+
.FirstOrDefault(item => item.IsSelected(selection) || item.References.Any(r => r.IsSelected(selection)));
416+
417+
if (target != null) { return target; }
418+
419+
var targets = items.Where(item => item.ComponentName == selection.QualifiedName.ComponentName);
420+
421+
foreach (var declaration in targets)
422+
{
423+
var declarationSelection = new Selection(declaration.Context.Start.Line,
424+
declaration.Context.Start.Column,
425+
declaration.Context.Stop.Line,
426+
declaration.Context.Stop.Column + declaration.Context.Stop.Text.Length);
427+
428+
if (declarationSelection.Contains(selection.Selection) ||
429+
!HasMultipleDeclarationsInStatement(declaration) && GetVariableStmtContextSelection(declaration).Contains(selection.Selection))
430+
{
431+
return declaration;
432+
}
433+
434+
var reference =
435+
declaration.References.FirstOrDefault(r => r.Selection.Contains(selection.Selection));
436+
437+
if (reference != null)
438+
{
439+
return reference.Declaration;
440+
}
441+
}
442+
return null;
443+
}
335444
}
336445
}

RetailCoder.VBE/Navigation/RegexSearchReplace/RegexSearchReplace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private List<RegexSearchResult> SearchCurrentBlock(string searchPattern)
132132

133133
var wrapper = _codePaneFactory.Create(_vbe.ActiveCodePane);
134134
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(wrapper.CodeModule.Parent), wrapper.Selection);
135-
dynamic block = parseResult.AllDeclarations.FindSelection(qualifiedSelection, declarationTypes).Context.Parent;
135+
dynamic block = parseResult.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context.Parent;
136136
var selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line, block.Stop.Column);
137137
return results.Where(r => selection.Contains(r.Selection)).ToList();
138138
}
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)