1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Text . RegularExpressions ;
4
5
using Antlr4 . Runtime ;
5
6
using Microsoft . Vbe . Interop ;
@@ -78,13 +79,30 @@ public override void Fix()
78
79
79
80
private void FixTypeHintUsage ( string hint , CodeModule module , Selection selection , bool isDeclaration = false )
80
81
{
81
- var line = module . get_Lines ( selection . StartLine , 1 ) ;
82
+ var line = module . Lines [ selection . StartLine , 1 ] ;
82
83
83
84
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 ) ) ;
86
85
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
+ }
88
106
}
89
107
}
90
108
}
0 commit comments