@@ -29,11 +29,16 @@ public ProcedureShouldBeFunctionInspectionResult(IInspection inspection, Rubberd
29
29
30
30
public class ChangeProcedureToFunction : CodeInspectionQuickFix
31
31
{
32
+ public override bool CanFixInModule { get { return false ; } }
33
+ public override bool CanFixInProject { get { return false ; } }
34
+
32
35
private readonly RubberduckParserState _state ;
33
36
private readonly QualifiedContext < VBAParser . ArgListContext > _argListQualifiedContext ;
34
37
private readonly QualifiedContext < VBAParser . SubStmtContext > _subStmtQualifiedContext ;
35
38
private readonly QualifiedContext < VBAParser . ArgContext > _argQualifiedContext ;
36
39
40
+ private int _lineOffset ;
41
+
37
42
public ChangeProcedureToFunction ( RubberduckParserState state ,
38
43
QualifiedContext < VBAParser . ArgListContext > argListQualifiedContext ,
39
44
QualifiedContext < VBAParser . SubStmtContext > subStmtQualifiedContext ,
@@ -69,11 +74,11 @@ private void UpdateSignature()
69
74
70
75
var newfunctionWithReturn = newFunctionWithoutReturn
71
76
. Insert ( newFunctionWithoutReturn . LastIndexOf ( Environment . NewLine , StringComparison . Ordinal ) ,
72
- " " + _subStmtQualifiedContext . Context . ambiguousIdentifier ( ) . GetText ( ) +
77
+ Environment . NewLine + " " + _subStmtQualifiedContext . Context . ambiguousIdentifier ( ) . GetText ( ) +
73
78
" = " + _argQualifiedContext . Context . ambiguousIdentifier ( ) . GetText ( ) ) ;
74
79
75
- var rewriter = _state . GetRewriter ( _subStmtQualifiedContext . ModuleName . Component ) ;
76
- rewriter . Replace ( _subStmtQualifiedContext . Context . Start , newfunctionWithReturn ) ;
80
+ _lineOffset = newfunctionWithReturn . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length -
81
+ subStmtText . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length ;
77
82
78
83
var module = _argListQualifiedContext . ModuleName . Component . CodeModule ;
79
84
@@ -98,6 +103,13 @@ d.Context is VBAParser.SubStmtContext &&
98
103
99
104
foreach ( var reference in procedure . References . OrderByDescending ( o => o . Selection . StartLine ) . ThenByDescending ( d => d . Selection . StartColumn ) )
100
105
{
106
+ var startLine = reference . Selection . StartLine ;
107
+
108
+ if ( procedure . ComponentName == reference . QualifiedModuleName . ComponentName && procedure . Selection . EndLine < reference . Selection . StartLine )
109
+ {
110
+ startLine += _lineOffset ;
111
+ }
112
+
101
113
var module = reference . QualifiedModuleName . Component . CodeModule ;
102
114
103
115
var referenceParent = reference . Context . Parent as VBAParser . ICS_B_ProcedureCallContext ;
@@ -108,13 +120,13 @@ d.Context is VBAParser.SubStmtContext &&
108
120
" = " + _subStmtQualifiedContext . Context . ambiguousIdentifier ( ) . GetText ( ) +
109
121
"(" + referenceParent . argsCall ( ) . GetText ( ) + ")" ;
110
122
111
- var oldLines = module . Lines [ reference . Selection . StartLine , reference . Selection . LineCount ] ;
123
+ var oldLines = module . Lines [ startLine , reference . Selection . LineCount ] ;
112
124
113
125
var newText = oldLines . Remove ( reference . Selection . StartColumn - 1 , referenceText . Length )
114
126
. Insert ( reference . Selection . StartColumn - 1 , newCall ) ;
115
127
116
- module . DeleteLines ( reference . Selection . StartLine , reference . Selection . LineCount ) ;
117
- module . InsertLines ( reference . Selection . StartLine , newText ) ;
128
+ module . DeleteLines ( startLine , reference . Selection . LineCount ) ;
129
+ module . InsertLines ( startLine , newText ) ;
118
130
}
119
131
}
120
132
}
0 commit comments