Skip to content

Commit 751a444

Browse files
committed
Merged with rubberduck/next
2 parents ff22747 + 427ef60 commit 751a444

27 files changed

+512
-305
lines changed

RetailCoder.VBE/Inspections/Abstract/InspectionBase.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,29 @@ protected bool IsIgnoringInspectionResultFor(IVBComponent component, int line)
116116

117117
protected bool IsIgnoringInspectionResultFor(Declaration declaration, string inspectionName)
118118
{
119+
var module = Declaration.GetModuleParent(declaration);
120+
if (module == null) { return false; }
121+
122+
var isIgnoredAtModuleLevel = module.Annotations
123+
.Any(annotation => annotation.AnnotationType == AnnotationType.IgnoreModule
124+
&& ((IgnoreModuleAnnotation) annotation).IsIgnored(inspectionName));
125+
126+
119127
if (declaration.DeclarationType == DeclarationType.Parameter)
120128
{
121-
return declaration.ParentDeclaration.Annotations.Any(annotation =>
129+
return isIgnoredAtModuleLevel || declaration.ParentDeclaration.Annotations.Any(annotation =>
122130
annotation.AnnotationType == AnnotationType.Ignore
123131
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
124132
}
125133

126-
return declaration.Annotations.Any(annotation =>
134+
return isIgnoredAtModuleLevel || declaration.Annotations.Any(annotation =>
127135
annotation.AnnotationType == AnnotationType.Ignore
128136
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
129137
}
130138

131139
protected bool IsIgnoringInspectionResultFor(IdentifierReference reference, string inspectionName)
132140
{
133-
if (reference == null)
134-
{
135-
return false;
136-
}
137-
138-
return reference.Annotations.Any(annotation =>
139-
annotation.AnnotationType == AnnotationType.Ignore
140-
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
141+
return reference != null && reference.IsIgnoringInspectionResultFor(inspectionName);
141142
}
142143

143144
public int CompareTo(IInspection other)

RetailCoder.VBE/Inspections/AssignedByValParameterInspection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Rubberduck.Inspections.Abstract;
44
using Rubberduck.Inspections.Resources;
55
using Rubberduck.Inspections.Results;
6-
using Rubberduck.Parsing.Grammar;
76
using Rubberduck.Parsing.Symbols;
87
using Rubberduck.Parsing.VBA;
98
using Rubberduck.UI.Refactorings;
@@ -12,8 +11,12 @@ namespace Rubberduck.Inspections
1211
{
1312
public sealed class AssignedByValParameterInspection : InspectionBase
1413
{
15-
private IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
14+
//<<<<<<< HEAD
15+
private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
1616
private RubberduckParserState _parserState;
17+
//=======
18+
//private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
19+
//>>>>>>> rubberduck-vba/next
1720
public AssignedByValParameterInspection(RubberduckParserState state, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
1821
: base(state)
1922
{

RetailCoder.VBE/Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Rubberduck.Inspections.Abstract;
22
using System.Linq;
3-
using Rubberduck.Parsing;
43
using Rubberduck.VBEditor;
54
using Rubberduck.Inspections.Resources;
65
using Rubberduck.Parsing.Grammar;
@@ -19,18 +18,27 @@ public class AssignedByValParameterMakeLocalCopyQuickFix : QuickFixBase
1918
{
2019
private readonly Declaration _target;
2120
private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
21+
//<<<<<<< HEAD
2222
private readonly RubberduckParserState _parserState;
23+
// private string[] _variableNamesAccessibleToProcedureContext;
24+
//=======
25+
private readonly IEnumerable<string> _forbiddenNames;
26+
//>>>>>>> rubberduck-vba/next
2327
private string _localCopyVariableName;
24-
private string[] _variableNamesAccessibleToProcedureContext;
2528

2629
public AssignedByValParameterMakeLocalCopyQuickFix(Declaration target, QualifiedSelection selection, RubberduckParserState parserState, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
2730
: base(target.Context, selection, InspectionsUI.AssignedByValParameterMakeLocalCopyQuickFix)
2831
{
2932
_target = target;
3033
_dialogFactory = dialogFactory;
34+
//<<<<<<< HEAD
3135
_parserState = parserState;
32-
_variableNamesAccessibleToProcedureContext = GetUserDefinedNamesAccessibleToProcedureContext(_target.Context.Parent.Parent);
33-
SetValidLocalCopyVariableNameSuggestion();
36+
//_variableNamesAccessibleToProcedureContext = GetUserDefinedNamesAccessibleToProcedureContext(_target.Context.Parent.Parent);
37+
//SetValidLocalCopyVariableNameSuggestion();
38+
//=======
39+
_forbiddenNames = GetIdentifierNamesAccessibleToProcedureContext(target.Context.Parent.Parent);
40+
_localCopyVariableName = ComputeSuggestedName();
41+
//>>>>>>> rubberduck-vba/next
3442
}
3543

3644
public override bool CanFixInModule { get { return false; } }
@@ -52,10 +60,9 @@ public override void Fix()
5260

5361
private void RequestLocalCopyVariableName()
5462
{
55-
using( var view = _dialogFactory.Create(_target.IdentifierName, _target.DeclarationType.ToString()))
63+
using( var view = _dialogFactory.Create(_target.IdentifierName, _target.DeclarationType.ToString(), _forbiddenNames))
5664
{
5765
view.NewName = _localCopyVariableName;
58-
view.IdentifierNamesAlreadyDeclared = _variableNamesAccessibleToProcedureContext;
5966
view.ShowDialog();
6067
IsCancelled = view.DialogResult == DialogResult.Cancel;
6168
if (!IsCancelled)
@@ -65,63 +72,73 @@ private void RequestLocalCopyVariableName()
6572
}
6673
}
6774

68-
private void SetValidLocalCopyVariableNameSuggestion()
75+
private string ComputeSuggestedName()
6976
{
70-
_localCopyVariableName = "x" + _target.IdentifierName.CapitalizeFirstLetter();
71-
if (VariableNameIsValid(_localCopyVariableName)) { return; }
77+
var newName = "local" + _target.IdentifierName.CapitalizeFirstLetter();
78+
if (VariableNameIsValid(newName))
79+
{
80+
return newName;
81+
}
7282

73-
//If the initial suggestion is not valid, keep pre-pending x's until it is
74-
for ( int attempt = 2; attempt < 10; attempt++)
83+
for ( var attempt = 2; attempt < 10; attempt++)
7584
{
76-
_localCopyVariableName = "x" + _localCopyVariableName;
77-
if (VariableNameIsValid(_localCopyVariableName))
85+
var result = newName + attempt;
86+
if (VariableNameIsValid(result))
7887
{
79-
return;
88+
return result;
8089
}
8190
}
82-
//if "xxFoo" to "xxxxxxxxxxFoo" isn't unique, give up and go with the original suggestion.
83-
//The QuickFix will leave the code as-is unless it receives a name that is free of conflicts
84-
_localCopyVariableName = "x" + _target.IdentifierName.CapitalizeFirstLetter();
91+
return newName;
8592
}
8693

8794
private bool VariableNameIsValid(string variableName)
8895
{
89-
var validator = new VariableNameValidator(variableName);
90-
return validator.IsValidName()
91-
&& !_variableNamesAccessibleToProcedureContext
92-
.Any(name => name.Equals(variableName, System.StringComparison.InvariantCultureIgnoreCase));
96+
return VariableNameValidator.IsValidName(variableName)
97+
&& !_forbiddenNames.Any(name => name.Equals(variableName, System.StringComparison.InvariantCultureIgnoreCase));
9398
}
9499

95100
private void ReplaceAssignedByValParameterReferences()
96101
{
97102
var module = Selection.QualifiedName.Component.CodeModule;
98-
foreach (IdentifierReference identifierReference in _target.References)
103+
foreach (var identifierReference in _target.References)
99104
{
100105
module.ReplaceIdentifierReferenceName(identifierReference, _localCopyVariableName);
101106
}
102107
}
103108

104109
private void InsertLocalVariableDeclarationAndAssignment()
105110
{
106-
var blocks = QuickFixHelper.GetBlockStmtContexts(_target.Context.Parent.Parent);
111+
//<<<<<<< HEAD
112+
//var blocks = QuickFixHelper.GetBlockStmtContexts(_target.Context.Parent.Parent);
113+
//=======
114+
var block = QuickFixHelper.GetBlockStmtContexts(_target.Context.Parent.Parent).FirstOrDefault();
115+
if (block == null)
116+
{
117+
return;
118+
}
119+
120+
//>>>>>>> rubberduck-vba/next
107121
string[] lines = { BuildLocalCopyDeclaration(), BuildLocalCopyAssignment() };
108122
var module = Selection.QualifiedName.Component.CodeModule;
109-
module.InsertLines(blocks.FirstOrDefault().Start.Line, lines);
123+
module.InsertLines(block.Start.Line, lines);
110124
}
111125

112126
private string BuildLocalCopyDeclaration()
113127
{
114-
return Tokens.Dim + " " + _localCopyVariableName + " " + Tokens.As
115-
+ " " + _target.AsTypeName;
128+
return Tokens.Dim + " " + _localCopyVariableName + " " + Tokens.As + " " + _target.AsTypeName;
116129
}
117130

118131
private string BuildLocalCopyAssignment()
119132
{
120-
return (SymbolList.ValueTypes.Contains(_target.AsTypeName) ? string.Empty : Tokens.Set + " ")
133+
return (_target.AsTypeDeclaration is ClassModuleDeclaration ? Tokens.Set + " " : string.Empty)
121134
+ _localCopyVariableName + " = " + _target.IdentifierName;
122135
}
123136

124-
private string[] GetUserDefinedNamesAccessibleToProcedureContext(RuleContext ruleContext)
137+
//<<<<<<< HEAD
138+
// private string[] GetUserDefinedNamesAccessibleToProcedureContext(RuleContext ruleContext)
139+
//=======
140+
private IEnumerable<string> GetIdentifierNamesAccessibleToProcedureContext(RuleContext ruleContext)
141+
//>>>>>>> rubberduck-vba/next
125142
{
126143
var allIdentifiers = new HashSet<string>();
127144

@@ -163,6 +180,7 @@ private string[] GetUserDefinedNamesAccessibleToProcedureContext(RuleContext rul
163180
return allIdentifiers.ToArray();
164181
}
165182

183+
//<<<<<<< HEAD
166184
private HashSet<string> GetVariableNamesFromRuleContexts(RuleContext[] ruleContexts)
167185
{
168186
var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
@@ -185,23 +203,50 @@ private HashSet<RuleContext> GetIdentifierContexts(IReadOnlyList<RuleContext> ru
185203
foreach (RuleContext ruleContext in ruleContexts)
186204
{
187205
var identifiersForThisContext = GetIdentifierContexts(ruleContext);
206+
//=======
207+
// private IEnumerable<string> GetIdentifierNames(IEnumerable<RuleContext> ruleContexts)
208+
// {
209+
// var identifiers = new HashSet<string>();
210+
// foreach (var identifiersForThisContext in ruleContexts.Select(GetIdentifierNames))
211+
// {
212+
//>>>>>>> rubberduck-vba/next
188213
identifiers.UnionWith(identifiersForThisContext);
189214
}
190215
return identifiers;
191216
}
192217

218+
//<<<<<<< HEAD
193219
private HashSet<RuleContext> GetIdentifierContexts(RuleContext ruleContext)
220+
//=======
221+
// private static HashSet<string> GetIdentifierNames(RuleContext ruleContext)
222+
//>>>>>>> rubberduck-vba/next
194223
{
224+
// note: this looks like something that's already handled somewhere else...
225+
195226
//Recursively work through the tree to get all IdentifierContexts
227+
//<<<<<<< HEAD
196228
var results = new HashSet<RuleContext>();
229+
//=======
230+
// var results = new HashSet<string>();
231+
// var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item).ToArray();
232+
//>>>>>>> rubberduck-vba/next
197233
var children = GetChildren(ruleContext);
198234

199-
foreach (IParseTree child in children)
235+
foreach (var child in children)
200236
{
201-
if (child is VBAParser.IdentifierContext)
237+
var context = child as VBAParser.IdentifierContext;
238+
if (context != null)
202239
{
240+
//<<<<<<< HEAD
203241
var childName = Identifier.GetName((VBAParser.IdentifierContext)child);
204242
results.Add((RuleContext)child);
243+
//=======
244+
// var childName = Identifier.GetName(context);
245+
// if (!tokenValues.Contains(childName))
246+
// {
247+
// results.Add(childName);
248+
// }
249+
//>>>>>>> rubberduck-vba/next
205250
}
206251
else
207252
{
@@ -214,12 +259,12 @@ private HashSet<RuleContext> GetIdentifierContexts(RuleContext ruleContext)
214259
return results;
215260
}
216261

217-
private static List<IParseTree> GetChildren(RuleContext ruleCtx)
262+
private static IEnumerable<IParseTree> GetChildren(IParseTree tree)
218263
{
219264
var result = new List<IParseTree>();
220-
for (int index = 0; index < ruleCtx.ChildCount; index++)
265+
for (var index = 0; index < tree.ChildCount; index++)
221266
{
222-
result.Add(ruleCtx.GetChild(index));
267+
result.Add(tree.GetChild(index));
223268
}
224269
return result;
225270
}

RetailCoder.VBE/Inspections/Results/AssignedByValParameterInspectionResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ namespace Rubberduck.Inspections.Results
1212
public class AssignedByValParameterInspectionResult : InspectionResultBase
1313
{
1414
private IEnumerable<QuickFixBase> _quickFixes;
15+
//<<<<<<< HEAD
1516
private RubberduckParserState _parserState;
16-
private IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
17+
private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
18+
//=======
19+
// private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
20+
//>>>>>>> rubberduck-vba/next
1721

1822
public AssignedByValParameterInspectionResult(IInspection inspection, Declaration target, RubberduckParserState parserState, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
1923
: base(inspection, target)

RetailCoder.VBE/Inspections/UseMeaningfulNameInspection.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,11 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
5151
!IgnoreDeclarationTypes.Contains(declaration.ParentDeclaration.DeclarationType) &&
5252
!handlers.Contains(declaration.ParentDeclaration)) &&
5353
!whitelistedNames.Contains(declaration.IdentifierName) &&
54-
IsBadIdentifier(declaration.IdentifierName))
54+
!VariableNameValidator.IsMeaningfulName(declaration.IdentifierName))
5555
.Select(issue => new IdentifierNameInspectionResult(this, issue, State, _messageBox, _settings))
5656
.ToList();
5757

5858
return issues;
5959
}
60-
61-
private static bool IsBadIdentifier(string identifier)
62-
{
63-
var validator = new VariableNameValidator(identifier);
64-
return !validator.IsMeaningfulName();
65-
}
6660
}
6761
}

0 commit comments

Comments
 (0)