Skip to content

Commit 877c454

Browse files
committed
Merge pull request #915 from Hosch250/ProcuedureShouldBeFunction
Procuedure should be function
2 parents b7ca91a + 755fa54 commit 877c454

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspectionResult.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ public ProcedureShouldBeFunctionInspectionResult(IInspection inspection, Rubberd
2929

3030
public class ChangeProcedureToFunction : CodeInspectionQuickFix
3131
{
32+
public override bool CanFixInModule { get { return false; } }
33+
public override bool CanFixInProject { get { return false; } }
34+
3235
private readonly RubberduckParserState _state;
3336
private readonly QualifiedContext<VBAParser.ArgListContext> _argListQualifiedContext;
3437
private readonly QualifiedContext<VBAParser.SubStmtContext> _subStmtQualifiedContext;
3538
private readonly QualifiedContext<VBAParser.ArgContext> _argQualifiedContext;
3639

40+
private int _lineOffset;
41+
3742
public ChangeProcedureToFunction(RubberduckParserState state,
3843
QualifiedContext<VBAParser.ArgListContext> argListQualifiedContext,
3944
QualifiedContext<VBAParser.SubStmtContext> subStmtQualifiedContext,
@@ -69,11 +74,11 @@ private void UpdateSignature()
6974

7075
var newfunctionWithReturn = newFunctionWithoutReturn
7176
.Insert(newFunctionWithoutReturn.LastIndexOf(Environment.NewLine, StringComparison.Ordinal),
72-
" " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
77+
Environment.NewLine + " " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
7378
" = " + _argQualifiedContext.Context.ambiguousIdentifier().GetText());
7479

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

7883
var module = _argListQualifiedContext.ModuleName.Component.CodeModule;
7984

@@ -98,6 +103,13 @@ d.Context is VBAParser.SubStmtContext &&
98103

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

103115
var referenceParent = reference.Context.Parent as VBAParser.ICS_B_ProcedureCallContext;
@@ -108,13 +120,13 @@ d.Context is VBAParser.SubStmtContext &&
108120
" = " + _subStmtQualifiedContext.Context.ambiguousIdentifier().GetText() +
109121
"(" + referenceParent.argsCall().GetText() + ")";
110122

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

113125
var newText = oldLines.Remove(reference.Selection.StartColumn - 1, referenceText.Length)
114126
.Insert(reference.Selection.StartColumn - 1, newCall);
115127

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);
118130
}
119131
}
120132
}

0 commit comments

Comments
 (0)