Skip to content

Commit 1a387f8

Browse files
committed
2 parents fb86e37 + c7d2f67 commit 1a387f8

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

RetailCoder.VBE/Inspections/ObsoleteTypeHintInspection.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
2222

2323
var declarations = from item in results
2424
where item.HasTypeHint()
25-
select new ObsoleteTypeHintInspectionResult(this,
26-
string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat,
27-
InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(),
28-
item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item);
29-
25+
// bug: this inspection result only has one value. Why are we passing two in?
26+
select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item);
27+
// todo: localize this InspectionResultFormat properly
3028
var references = from item in results.SelectMany(d => d.References)
3129
where item.HasTypeHint()
32-
select new ObsoleteTypeHintInspectionResult(this,
33-
string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat,
34-
InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(),
35-
item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context),
36-
item.Declaration);
30+
select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context), item.Declaration);
3731

3832
return declarations.Union(references);
3933
}

RetailCoder.VBE/Inspections/UntypedFunctionUsageInspectionResult.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Linq;
24
using Antlr4.Runtime;
5+
using Rubberduck.Parsing.Grammar;
36
using Rubberduck.Parsing.Symbols;
47
using Rubberduck.VBEditor;
58

@@ -16,7 +19,7 @@ public UntypedFunctionUsageInspectionResult(IInspection inspection, IdentifierRe
1619
_reference = reference;
1720
_quickFixes = new CodeInspectionQuickFix[]
1821
{
19-
new UntypedFunctionUsageQuickFix(Context, QualifiedSelection),
22+
new UntypedFunctionUsageQuickFix((ParserRuleContext)GetFirst(typeof(VBAParser.IdentifierContext)).Parent, QualifiedSelection),
2023
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName),
2124
};
2225
}
@@ -27,29 +30,53 @@ public override string Description
2730
{
2831
get { return string.Format(Inspection.Description, _reference.Declaration.IdentifierName); }
2932
}
33+
34+
private ParserRuleContext GetFirst(Type nodeType)
35+
{
36+
var unexploredNodes = new List<ParserRuleContext> {Context};
37+
38+
while (unexploredNodes.Any())
39+
{
40+
if (unexploredNodes[0].GetType() == nodeType)
41+
{
42+
return unexploredNodes[0];
43+
}
44+
45+
unexploredNodes.AddRange(unexploredNodes[0].children.OfType<ParserRuleContext>());
46+
unexploredNodes.RemoveAt(0);
47+
}
48+
49+
return null;
50+
}
3051
}
3152

3253
public class UntypedFunctionUsageQuickFix : CodeInspectionQuickFix
3354
{
3455
public UntypedFunctionUsageQuickFix(ParserRuleContext context, QualifiedSelection selection)
35-
: base(context, selection,
36-
string.Format(InspectionsUI.QuickFixUseTypedFunction_,
37-
context.GetText(), context.GetText().Insert(context.GetText().IndexOf('('), "$")))
56+
: base(context, selection, string.Format(InspectionsUI.QuickFixUseTypedFunction_, context.GetText(), GetNewSignature(context)))
3857
{
3958
}
4059

4160
public override void Fix()
4261
{
4362
var originalInstruction = Context.GetText();
44-
var newInstruction = originalInstruction.Insert(originalInstruction.IndexOf('('), "$");
63+
var newInstruction = GetNewSignature(Context);
4564
var selection = Selection.Selection;
4665

4766
var module = Selection.QualifiedName.Component.CodeModule;
4867
var lines = module.Lines[selection.StartLine, selection.LineCount];
4968

5069
var result = lines.Replace(originalInstruction, newInstruction);
5170
module.ReplaceLine(selection.StartLine, result);
52-
// FIXME trigger reparse
71+
}
72+
73+
private static string GetNewSignature(ParserRuleContext context)
74+
{
75+
return context.children.Aggregate(string.Empty, (current, member) =>
76+
{
77+
var isIdentifierNode = member is VBAParser.IdentifierContext;
78+
return current + member.GetText() + (isIdentifierNode ? "$" : string.Empty);
79+
});
5380
}
5481
}
5582
}

0 commit comments

Comments
 (0)