Skip to content

Commit 8e7df2c

Browse files
committed
Fix Obsolete Type Hint bugs.
1 parent 1a17365 commit 8e7df2c

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

RetailCoder.VBE/Inspections/ObsoleteTypeHintInspectionResult.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text.RegularExpressions;
45
using Antlr4.Runtime;
56
using Microsoft.Vbe.Interop;
@@ -78,13 +79,30 @@ public override void Fix()
7879

7980
private void FixTypeHintUsage(string hint, CodeModule module, Selection selection, bool isDeclaration = false)
8081
{
81-
var line = module.get_Lines(selection.StartLine, 1);
82+
var line = module.Lines[selection.StartLine, 1];
8283

8384
var asTypeClause = ' ' + Tokens.As + ' ' + TypeHints[hint];
84-
var pattern = "\\b" + _declaration.IdentifierName + "\\" + hint;
85-
var fix = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : String.Empty));
8685

87-
module.ReplaceLine(selection.StartLine, fix);
86+
string fix;
87+
88+
if (isDeclaration && (Context is VBAParser.FunctionStmtContext || Context is VBAParser.PropertyGetStmtContext))
89+
{
90+
var typeHint = (ParserRuleContext)Context.children.First(c => c is VBAParser.TypeHintContext);
91+
var argList = (ParserRuleContext) Context.children.First(c => c is VBAParser.ArgListContext);
92+
var endLine = argList.Stop.Line;
93+
var endColumn = argList.Stop.Column;
94+
95+
var oldLine = module.Lines[endLine, selection.LineCount];
96+
fix = oldLine.Insert(endColumn + 1, asTypeClause).Remove(typeHint.Start.Column, 1); // adjust for VBA 0-based indexing
97+
98+
module.ReplaceLine(endLine, fix);
99+
}
100+
else
101+
{
102+
var pattern = "\\b" + _declaration.IdentifierName + "\\" + hint;
103+
fix = Regex.Replace(line, pattern, _declaration.IdentifierName + (isDeclaration ? asTypeClause : string.Empty));
104+
module.ReplaceLine(selection.StartLine, fix);
105+
}
88106
}
89107
}
90108
}

RubberduckTests/Inspections/ObsoleteTypeHintInspectionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ public void ObsoleteCallStatement_QuickFixWorks_Field_SingleTypeHint()
435435
public void ObsoleteCallStatement_QuickFixWorks_Field_DecimalTypeHint()
436436
{
437437
const string inputCode =
438-
@"Public Foo&";
438+
@"Public Foo@";
439439

440440
const string expectedCode =
441-
@"Public Foo As Decimal ' This doesn't compile, should it be float?";
441+
@"Public Foo As Decimal";
442442

443443
//Arrange
444444
var builder = new MockVbeBuilder();

0 commit comments

Comments
 (0)