Skip to content

Commit 468a258

Browse files
committed
Column offset works. (Note - requires reparse between changes if the line offset is greater than 0).
1 parent b9761a6 commit 468a258

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspectionResult.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ChangeProcedureToFunction : CodeInspectionQuickFix
3333
private readonly QualifiedContext<VBAParser.ArgListContext> _argListQualifiedContext;
3434
private readonly QualifiedContext<VBAParser.SubStmtContext> _subStmtQualifiedContext;
3535
private readonly QualifiedContext<VBAParser.ArgContext> _argQualifiedContext;
36+
private int _lineOffset;
3637

3738
public ChangeProcedureToFunction(RubberduckParserState state,
3839
QualifiedContext<VBAParser.ArgListContext> argListQualifiedContext,
@@ -69,11 +70,11 @@ private void UpdateSignature()
6970

7071
var newfunctionWithReturn = newFunctionWithoutReturn
7172
.Insert(newFunctionWithoutReturn.LastIndexOf(Environment.NewLine, StringComparison.Ordinal),
72-
" " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
73+
Environment.NewLine + " " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
7374
" = " + _argQualifiedContext.Context.ambiguousIdentifier().GetText());
7475

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;
7778

7879
var module = _argListQualifiedContext.ModuleName.Component.CodeModule;
7980

@@ -98,6 +99,13 @@ d.Context is VBAParser.SubStmtContext &&
9899

99100
foreach (var reference in procedure.References.OrderByDescending(o => o.Selection.StartLine).ThenByDescending(d => d.Selection.StartColumn))
100101
{
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+
101109
var module = reference.QualifiedModuleName.Component.CodeModule;
102110

103111
var referenceParent = reference.Context.Parent as VBAParser.ICS_B_ProcedureCallContext;
@@ -108,13 +116,13 @@ d.Context is VBAParser.SubStmtContext &&
108116
" = " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
109117
"(" + referenceParent.argsCall().GetText() + ")";
110118

111-
var oldLines = module.Lines[reference.Selection.StartLine, reference.Selection.LineCount];
119+
var oldLines = module.Lines[startLine, reference.Selection.LineCount];
112120

113121
var newText = oldLines.Remove(reference.Selection.StartColumn - 1, referenceText.Length)
114122
.Insert(reference.Selection.StartColumn - 1, newCall);
115123

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);
118126
}
119127
}
120128
}

0 commit comments

Comments
 (0)