/
ExpandDefaultMemberQuickFix.cs
192 lines (169 loc) · 8.03 KB
/
ExpandDefaultMemberQuickFix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.CodeAnalysis.Inspections;
using Rubberduck.CodeAnalysis.Inspections.Concrete;
using Rubberduck.CodeAnalysis.QuickFixes.Abstract;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.VBEditor;
namespace Rubberduck.CodeAnalysis.QuickFixes.Concrete
{
/// <summary>
/// Makes default member calls explicit.
/// </summary>
/// <inspections>
/// <inspection name="ObjectWhereProcedureIsRequiredInspection" />
/// <inspection name="IndexedDefaultMemberAccessInspection" />
/// <inspection name="IndexedRecursiveDefaultMemberAccessInspection" />
/// <inspection name="ImplicitDefaultMemberAccessInspection" />
/// <inspection name="ImplicitRecursiveDefaultMemberAccessInspection" />
/// <inspection name="SuspiciousLetAssignmentInspection" />
/// </inspections>
/// <canfix multiple="true" procedure="true" module="true" project="true" all="true" />
/// <example>
/// <before>
/// <![CDATA[
/// Public Sub DoSomething()
/// Dim values As Dictionary
/// Set values = New Dictionary
/// values("Value1") = 42
/// values("Value2") = 24
/// Debug.Print values("Value1")
/// End Sub
/// ]]>
/// </before>
/// <after>
/// <![CDATA[
/// Public Sub DoSomething()
/// Dim values As Dictionary
/// Set values = New Dictionary
/// values.Item("Value1") = 42
/// values.Item("Value2") = 24
/// Debug.Print values.Item("Value1")
/// End Sub
/// ]]>
/// </after>
/// </example>
internal class ExpandDefaultMemberQuickFix : QuickFixBase
{
private readonly IDeclarationFinderProvider _declarationFinderProvider;
public ExpandDefaultMemberQuickFix(IDeclarationFinderProvider declarationFinderProvider)
: base(
typeof(ObjectWhereProcedureIsRequiredInspection),
typeof(IndexedDefaultMemberAccessInspection),
typeof(IndexedRecursiveDefaultMemberAccessInspection),
typeof(ImplicitDefaultMemberAccessInspection),
typeof(ImplicitRecursiveDefaultMemberAccessInspection),
typeof(SuspiciousLetAssignmentInspection))
{
_declarationFinderProvider = declarationFinderProvider;
}
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
{
var rewriter = rewriteSession.CheckOutModuleRewriter(result.QualifiedSelection.QualifiedName);
var finder = _declarationFinderProvider.DeclarationFinder;
var lExpressionContext = result.Context;
var selection = result.QualifiedSelection;
InsertDefaultMember(lExpressionContext, selection, finder, rewriter);
if (result.Inspection is SuspiciousLetAssignmentInspection)
{
if (!(result is IWithInspectionResultProperties<IdentifierReference> resultProperties))
{
return;
}
var rhsReference = resultProperties.Properties;
var rhsLExpressionContext = rhsReference.Context;
var rhsSelection = rhsReference.QualifiedSelection;
InsertDefaultMember(rhsLExpressionContext, rhsSelection, finder, rewriter);
}
}
private void InsertDefaultMember(ParserRuleContext lExpressionContext, QualifiedSelection selection, DeclarationFinder finder, IModuleRewriter rewriter)
{
var defaultMemberAccessCode = DefaultMemberAccessCode(selection, finder);
rewriter.InsertAfter(lExpressionContext.Stop.TokenIndex, defaultMemberAccessCode);
}
private string DefaultMemberAccessCode(QualifiedSelection selection, DeclarationFinder finder)
{
var defaultMemberAccesses = finder.IdentifierReferences(selection).Where(reference => reference.DefaultMemberRecursionDepth > 0);
var defaultMemberNames = defaultMemberAccesses.Select(DefaultMemberName)
.Select(declarationName => IsNotLegalIdentifierName(declarationName)
? $"[{declarationName}]"
: declarationName);
return $".{string.Join("().", defaultMemberNames)}";
}
private bool IsNotLegalIdentifierName(string declarationName)
{
return string.IsNullOrEmpty(declarationName)
|| NonIdentifierCharacters.Any(character => declarationName.Contains(character))
|| AdditionalNonFirstIdentifierCharacters.Contains(declarationName[0]); ;
}
private static string DefaultMemberName(IdentifierReference defaultMemberReference)
{
var defaultMemberMemberName = defaultMemberReference.Declaration.QualifiedName;
var fullDefaultMemberName = $"{defaultMemberMemberName.QualifiedModuleName.ProjectName}.{defaultMemberMemberName.QualifiedModuleName.ComponentName}.{defaultMemberMemberName.MemberName}";
if (DefaultMemberBaseOverrides.TryGetValue(fullDefaultMemberName, out var baseOverride))
{
if (DefaultMemberArgumentNumberOverrides.TryGetValue(fullDefaultMemberName, out var argumentNumberOverrides))
{
var numberOfArguments = NumberOfArguments(defaultMemberReference);
if (argumentNumberOverrides.TryGetValue(numberOfArguments, out var numberOfArgumentsOverride))
{
return numberOfArgumentsOverride;
}
}
return baseOverride;
}
return defaultMemberMemberName.MemberName;
}
private static int NumberOfArguments(IdentifierReference defaultMemberReference)
{
if (defaultMemberReference.IsNonIndexedDefaultMemberAccess)
{
return 0;
}
var argumentList = ArgumentList(defaultMemberReference);
if (argumentList == null)
{
return -1;
}
var arguments = argumentList.argument();
return arguments?.Length ?? 0;
}
private static VBAParser.ArgumentListContext ArgumentList(IdentifierReference indexedDefaultMemberReference)
{
var defaultMemberReferenceContextWithArguments = indexedDefaultMemberReference.Context.Parent;
switch (defaultMemberReferenceContextWithArguments)
{
case VBAParser.IndexExprContext indexExpression:
return indexExpression.argumentList();
case VBAParser.WhitespaceIndexExprContext whiteSpaceIndexExpression:
return whiteSpaceIndexExpression.argumentList();
default:
return null;
}
}
public override string Description(IInspectionResult result)
{
return Resources.Inspections.QuickFixes.ExpandDefaultMemberQuickFix;
}
public override bool CanFixMultiple => true;
public override bool CanFixInProcedure => true;
public override bool CanFixInModule => true;
public override bool CanFixInProject => true;
public override bool CanFixAll => true;
private string NonIdentifierCharacters = "[](){}\r\n\t.,'\"\\ |!@#$%^&*-+:=; ";
private string AdditionalNonFirstIdentifierCharacters = "0123456789_";
private static readonly Dictionary<string, string> DefaultMemberBaseOverrides = new Dictionary<string, string>
{
["Excel.Range._Default"] = "Item"
};
private static readonly Dictionary<string, Dictionary<int, string>> DefaultMemberArgumentNumberOverrides = new Dictionary<string, Dictionary<int, string>>
{
["Excel.Range._Default"] = new Dictionary<int, string>{[0] = "Value"}
};
}
}