1
- using Antlr4 . Runtime ;
1
+ using System ;
2
+ using Antlr4 . Runtime ;
2
3
using Rubberduck . Parsing ;
3
4
using Rubberduck . Parsing . Grammar ;
4
5
using Rubberduck . VBEditor ;
5
6
using System . Collections . Generic ;
6
7
using System . Linq ;
7
8
using System . Text . RegularExpressions ;
9
+ using Microsoft . Vbe . Interop ;
10
+ using Rubberduck . Parsing . Symbols ;
8
11
9
12
namespace Rubberduck . Inspections
10
13
{
@@ -25,25 +28,42 @@ public ConvertToProcedureQuickFix(ParserRuleContext context, QualifiedSelection
25
28
26
29
public override void Fix ( )
27
30
{
28
- var context = ( VBAParser . FunctionStmtContext ) Context ;
29
- var visibility = context . visibility ( ) == null ? string . Empty : context . visibility ( ) . GetText ( ) + ' ' ;
30
- var name = ' ' + context . ambiguousIdentifier ( ) . GetText ( ) ;
31
- var args = context . argList ( ) . GetText ( ) ;
32
- var asType = context . asTypeClause ( ) == null ? string . Empty : ' ' + context . asTypeClause ( ) . GetText ( ) ;
31
+ dynamic functionContext = Context as VBAParser . FunctionStmtContext ;
32
+ dynamic propertyGetContext = Context as VBAParser . PropertyGetStmtContext ;
33
33
34
- var oldSignature = visibility + Tokens . Function + name + args + asType ;
35
- var newSignature = visibility + Tokens . Sub + name + args ;
34
+ var context = functionContext ?? propertyGetContext ;
35
+ if ( context == null )
36
+ {
37
+ throw new InvalidOperationException ( string . Format ( "Context type '{0}' is not valid for {1}." , Context . GetType ( ) , GetType ( ) ) ) ;
38
+ }
36
39
37
- var procedure = Context . GetText ( ) ;
40
+ string token = functionContext != null
41
+ ? Tokens . Function
42
+ : Tokens . Property + ' ' + Tokens . Get ;
43
+ string endToken = token == Tokens . Function
44
+ ? token
45
+ : Tokens . Property ;
46
+
47
+ string visibility = context . visibility ( ) == null ? string . Empty : context . visibility ( ) . GetText ( ) + ' ' ;
48
+ string name = ' ' + context . ambiguousIdentifier ( ) . GetText ( ) ;
49
+ bool hasTypeHint = context . typeHint ( ) != null ;
50
+
51
+ string args = context . argList ( ) . GetText ( ) ;
52
+ string asType = context . asTypeClause ( ) == null ? string . Empty : ' ' + context . asTypeClause ( ) . GetText ( ) ;
53
+
54
+ string oldSignature = visibility + token + name + ( hasTypeHint ? context . typeHint ( ) . GetText ( ) : string . Empty ) + args + asType ;
55
+ string newSignature = visibility + Tokens . Sub + name + args ;
56
+
57
+ string procedure = Context . GetText ( ) ;
38
58
string noReturnStatements = procedure ;
39
59
_returnStatements . ToList ( ) . ForEach ( returnStatement =>
40
60
noReturnStatements = Regex . Replace ( noReturnStatements , @"[ \t\f]*" + returnStatement + @"[ \t\f]*\r?\n?" , "" ) ) ;
41
- var result = noReturnStatements . Replace ( oldSignature , newSignature )
42
- . Replace ( Tokens . End + ' ' + Tokens . Function , Tokens . End + ' ' + Tokens . Sub )
43
- . Replace ( Tokens . Exit + ' ' + Tokens . Function , Tokens . Exit + ' ' + Tokens . Sub ) ;
61
+ string result = noReturnStatements . Replace ( oldSignature , newSignature )
62
+ . Replace ( Tokens . End + ' ' + endToken , Tokens . End + ' ' + Tokens . Sub )
63
+ . Replace ( Tokens . Exit + ' ' + endToken , Tokens . Exit + ' ' + Tokens . Sub ) ;
44
64
45
- var module = Selection . QualifiedName . Component . CodeModule ;
46
- var selection = Context . GetSelection ( ) ;
65
+ CodeModule module = Selection . QualifiedName . Component . CodeModule ;
66
+ Selection selection = Context . GetSelection ( ) ;
47
67
48
68
module . DeleteLines ( selection . StartLine , selection . LineCount ) ;
49
69
module . InsertLines ( selection . StartLine , result ) ;
0 commit comments