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