Skip to content

Commit f94cb3d

Browse files
committed
Merge pull request #656 from Hosch250/next
Fix reorder/remove selection bug
2 parents 43d7183 + 17eb19a commit f94cb3d

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersModel.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,15 @@ public Declaration FindTarget(QualifiedSelection selection, DeclarationType[] va
9494

9595
foreach (var declaration in targets)
9696
{
97-
var declarationSelection = new Selection(declaration.Context.Start.Line,
98-
declaration.Context.Start.Column,
99-
declaration.Context.Stop.Line,
100-
declaration.Context.Stop.Column);
97+
var activeSelection = new Selection(declaration.Context.Start.Line,
98+
declaration.Context.Start.Column,
99+
declaration.Context.Stop.Line,
100+
declaration.Context.Stop.Column);
101101

102-
if (currentSelection.Contains(declarationSelection) &&
103-
declarationSelection.Contains(selection.Selection))
102+
if (currentSelection.Contains(activeSelection) && activeSelection.Contains(selection.Selection))
104103
{
105104
target = declaration;
106-
currentSelection = declarationSelection;
105+
currentSelection = activeSelection;
107106
}
108107

109108
foreach (var reference in declaration.References)
@@ -113,30 +112,20 @@ public Declaration FindTarget(QualifiedSelection selection, DeclarationType[] va
113112

114113
// This is to prevent throws when this statement fails:
115114
// (VBAParser.ArgsCallContext)proc.argsCall();
116-
try
117-
{
118-
paramList = (VBAParser.ArgsCallContext) proc.argsCall();
119-
}
120-
catch
121-
{
122-
continue;
123-
}
115+
try { paramList = (VBAParser.ArgsCallContext) proc.argsCall(); }
116+
catch { continue; }
124117

125-
if (paramList == null)
126-
{
127-
continue;
128-
}
118+
if (paramList == null) { continue; }
129119

130-
var referenceSelection = new Selection(paramList.Start.Line,
131-
paramList.Start.Column,
132-
paramList.Stop.Line,
133-
paramList.Stop.Column + paramList.Stop.Text.Length + 1);
120+
activeSelection = new Selection(paramList.Start.Line,
121+
paramList.Start.Column,
122+
paramList.Stop.Line,
123+
paramList.Stop.Column + paramList.Stop.Text.Length + 1);
134124

135-
if (currentSelection.Contains(declarationSelection) &&
136-
referenceSelection.Contains(selection.Selection))
125+
if (currentSelection.Contains(activeSelection) && activeSelection.Contains(selection.Selection))
137126
{
138127
target = reference.Declaration;
139-
currentSelection = referenceSelection;
128+
currentSelection = activeSelection;
140129
}
141130
}
142131
}

RetailCoder.VBE/Refactorings/Rename/RenamePresenter.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void RenameControl()
247247
var module = handler.Project.VBComponents.Item(handler.ComponentName).CodeModule;
248248

249249
var content = module.Lines[handler.Selection.StartLine, 1];
250-
var newContent = GetReplacementLine(content, handler.IdentifierName, newMemberName);
250+
var newContent = GetReplacementLine(content, handler.IdentifierName, newMemberName, handler.Selection);
251251
module.ReplaceLine(handler.Selection.StartLine, newContent);
252252
}
253253

@@ -279,7 +279,7 @@ private void RenameUsages(Declaration target, string interfaceName = null)
279279
var module = member.Project.VBComponents.Item(member.ComponentName).CodeModule;
280280

281281
var content = module.Lines[member.Selection.StartLine, 1];
282-
var newContent = GetReplacementLine(content, member.IdentifierName, newMemberName);
282+
var newContent = GetReplacementLine(content, member.IdentifierName, newMemberName, member.Selection);
283283
module.ReplaceLine(member.Selection.StartLine, newContent);
284284
RenameUsages(member, target.ComponentName);
285285
}
@@ -298,19 +298,25 @@ private void RenameUsages(Declaration target, string interfaceName = null)
298298
var module = grouping.Key.Component.CodeModule;
299299
foreach (var line in grouping.GroupBy(reference => reference.Selection.StartLine))
300300
{
301-
var content = module.Lines[line.Key, 1];
302-
string newContent;
303-
304-
if (interfaceName == null)
305-
{
306-
newContent = GetReplacementLine(content, target.IdentifierName, _view.NewName);
307-
}
308-
else
301+
foreach (var reference in line)
309302
{
310-
newContent = GetReplacementLine(content, target.IdentifierName, interfaceName + "_" + _view.NewName);
311-
}
303+
var content = module.Lines[line.Key, 1];
304+
string newContent;
305+
306+
if (interfaceName == null)
307+
{
308+
newContent = GetReplacementLine(content, target.IdentifierName, _view.NewName,
309+
reference.Selection);
310+
}
311+
else
312+
{
313+
newContent = GetReplacementLine(content, target.IdentifierName,
314+
interfaceName + "_" + _view.NewName,
315+
reference.Selection);
316+
}
312317

313-
module.ReplaceLine(line.Key, newContent);
318+
module.ReplaceLine(line.Key, newContent);
319+
}
314320
}
315321

316322
// renaming interface
@@ -328,17 +334,21 @@ private void RenameUsages(Declaration target, string interfaceName = null)
328334
}
329335

330336
var content = module.Lines[method.Selection.StartLine, 1];
331-
var newContent = GetReplacementLine(content, oldMemberName, newMemberName);
337+
var newContent = GetReplacementLine(content, oldMemberName, newMemberName, member.Selection);
332338
module.ReplaceLine(method.Selection.StartLine, newContent);
333339
}
334340
}
335341
}
336342
}
337343

338-
private string GetReplacementLine(string content, string target, string newName)
344+
private string GetReplacementLine(string content, string target, string newName, Selection selection)
339345
{
340346
// until we figure out how to replace actual tokens,
341347
// this is going to have to be done the ugly way...
348+
349+
// todo: come back after the identifier references are fixed
350+
//var contentWithoutOldName = content.Remove(selection.StartColumn - 1, selection.EndColumn - selection.StartColumn);
351+
//return contentWithoutOldName.Insert(selection.StartColumn - 1, newName);
342352
return Regex.Replace(content, "\\b" + target + "\\b", newName);
343353
}
344354

@@ -420,7 +430,7 @@ private string GetReplacementLine(CodeModule module, Declaration target, string
420430

421431
return rewriter.GetText(new Interval(firstTokenIndex, lastTokenIndex));
422432
}
423-
return GetReplacementLine(content, target.IdentifierName, newName);
433+
return GetReplacementLine(content, target.IdentifierName, newName, target.Selection);
424434
}
425435

426436
private static readonly DeclarationType[] ProcedureDeclarationTypes =

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersModel.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ReorderParametersModel
1818
private readonly Declarations _declarations;
1919
public Declarations Declarations { get { return _declarations; } }
2020

21-
public Declaration TargetDeclaration { get; set; }
21+
public Declaration TargetDeclaration { get; private set; }
2222
public List<Parameter> Parameters { get; set; }
2323

2424
public ReorderParametersModel(VBProjectParseResult parseResult, QualifiedSelection selection)
@@ -86,15 +86,15 @@ private Declaration FindTarget(QualifiedSelection selection, DeclarationType[] v
8686

8787
foreach (var declaration in targets)
8888
{
89-
var declarationSelection = new Selection(declaration.Context.Start.Line,
90-
declaration.Context.Start.Column,
91-
declaration.Context.Stop.Line,
92-
declaration.Context.Stop.Column);
89+
var activeSelection = new Selection(declaration.Context.Start.Line,
90+
declaration.Context.Start.Column,
91+
declaration.Context.Stop.Line,
92+
declaration.Context.Stop.Column);
9393

94-
if (currentSelection.Contains(declarationSelection) && declarationSelection.Contains(selection.Selection))
94+
if (currentSelection.Contains(activeSelection) && activeSelection.Contains(selection.Selection))
9595
{
9696
target = declaration;
97-
currentSelection = declarationSelection;
97+
currentSelection = activeSelection;
9898
}
9999

100100
foreach (var reference in declaration.References)
@@ -109,15 +109,15 @@ private Declaration FindTarget(QualifiedSelection selection, DeclarationType[] v
109109

110110
if (paramList == null) { continue; }
111111

112-
var referenceSelection = new Selection(paramList.Start.Line,
113-
paramList.Start.Column,
114-
paramList.Stop.Line,
115-
paramList.Stop.Column + paramList.Stop.Text.Length + 1);
112+
activeSelection = new Selection(paramList.Start.Line,
113+
paramList.Start.Column,
114+
paramList.Stop.Line,
115+
paramList.Stop.Column + paramList.Stop.Text.Length + 1);
116116

117-
if (currentSelection.Contains(declarationSelection) && referenceSelection.Contains(selection.Selection))
117+
if (currentSelection.Contains(activeSelection) && activeSelection.Contains(selection.Selection))
118118
{
119119
target = reference.Declaration;
120-
currentSelection = referenceSelection;
120+
currentSelection = activeSelection;
121121
}
122122
}
123123
}

0 commit comments

Comments
 (0)