Skip to content

Commit c84f141

Browse files
committed
3218 issue modifications for rename of Labels AND Enums
1 parent 727fb6f commit c84f141

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed

Rubberduck.Parsing/Grammar/PartialExtensions/VBAParserPartialExtensions.cs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,149 @@ public void AddAttributes(Attributes attributes)
351351
}
352352
#endregion
353353
}
354+
355+
public partial class EnumerationStmtContext : IIdentifierContext, IAnnotatedContext
356+
{
357+
#region IIdentifierContext
358+
public Interval IdentifierTokens
359+
{
360+
get
361+
{
362+
Interval tokenInterval;
363+
Identifier.GetName(this, out tokenInterval);
364+
return tokenInterval;
365+
}
366+
}
367+
#endregion
368+
369+
#region IAnnotatedContext
370+
public Attributes Attributes { get; } = new Attributes();
371+
public int AttributeTokenIndex => Start.TokenIndex - 1;
372+
373+
private readonly List<AnnotationContext> _annotations = new List<AnnotationContext>();
374+
public IEnumerable<AnnotationContext> Annotations => _annotations;
375+
376+
public void Annotate(AnnotationContext annotation)
377+
{
378+
_annotations.Add(annotation);
379+
}
380+
381+
public void AddAttributes(Attributes attributes)
382+
{
383+
foreach (var attribute in attributes)
384+
{
385+
Attributes.Add(new AttributeNode(attribute.Name, attribute.Values));
386+
}
387+
}
388+
#endregion
389+
}
390+
391+
public partial class EnumerationStmt_ConstantContext : IIdentifierContext, IAnnotatedContext
392+
{
393+
#region IIdentifierContext
394+
public Interval IdentifierTokens
395+
{
396+
get
397+
{
398+
Interval tokenInterval;
399+
Identifier.GetName(this, out tokenInterval);
400+
return tokenInterval;
401+
}
402+
}
403+
#endregion
404+
405+
#region IAnnotatedContext
406+
public Attributes Attributes { get; } = new Attributes();
407+
public int AttributeTokenIndex => Start.TokenIndex - 1;
408+
409+
private readonly List<AnnotationContext> _annotations = new List<AnnotationContext>();
410+
public IEnumerable<AnnotationContext> Annotations => _annotations;
411+
412+
public void Annotate(AnnotationContext annotation)
413+
{
414+
_annotations.Add(annotation);
415+
}
416+
417+
public void AddAttributes(Attributes attributes)
418+
{
419+
foreach (var attribute in attributes)
420+
{
421+
Attributes.Add(new AttributeNode(attribute.Name, attribute.Values));
422+
}
423+
}
424+
#endregion
425+
}
426+
427+
public partial class IdentifierStatementLabelContext : IIdentifierContext, IAnnotatedContext
428+
{
429+
#region IIdentifierContext
430+
public Interval IdentifierTokens
431+
{
432+
get
433+
{
434+
Interval tokenInterval;
435+
Identifier.GetName(this, out tokenInterval);
436+
return tokenInterval;
437+
}
438+
}
439+
#endregion
440+
441+
#region IAnnotatedContext
442+
public Attributes Attributes { get; } = new Attributes();
443+
public int AttributeTokenIndex => Start.TokenIndex - 1;
444+
445+
private readonly List<AnnotationContext> _annotations = new List<AnnotationContext>();
446+
public IEnumerable<AnnotationContext> Annotations => _annotations;
447+
448+
public void Annotate(AnnotationContext annotation)
449+
{
450+
_annotations.Add(annotation);
451+
}
452+
453+
public void AddAttributes(Attributes attributes)
454+
{
455+
foreach (var attribute in attributes)
456+
{
457+
Attributes.Add(new AttributeNode(attribute.Name, attribute.Values));
458+
}
459+
}
460+
#endregion
461+
}
462+
463+
public partial class SimpleNameExprContext : IIdentifierContext, IAnnotatedContext
464+
{
465+
#region IIdentifierContext
466+
public Interval IdentifierTokens
467+
{
468+
get
469+
{
470+
Interval tokenInterval;
471+
Identifier.GetName(this, out tokenInterval);
472+
return tokenInterval;
473+
}
474+
}
475+
#endregion
476+
477+
#region IAnnotatedContext
478+
public Attributes Attributes { get; } = new Attributes();
479+
public int AttributeTokenIndex => Start.TokenIndex - 1;
480+
481+
private readonly List<AnnotationContext> _annotations = new List<AnnotationContext>();
482+
public IEnumerable<AnnotationContext> Annotations => _annotations;
483+
484+
public void Annotate(AnnotationContext annotation)
485+
{
486+
_annotations.Add(annotation);
487+
}
488+
489+
public void AddAttributes(Attributes attributes)
490+
{
491+
foreach (var attribute in attributes)
492+
{
493+
Attributes.Add(new AttributeNode(attribute.Name, attribute.Values));
494+
}
495+
}
496+
#endregion
497+
}
354498
}
355499
}

Rubberduck.Parsing/Symbols/Identifier.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,54 @@ public static string GetName(VBAParser.UnrestrictedIdentifierContext context)
108108
}
109109
}
110110

111+
public static string GetName(VBAParser.EnumerationStmtContext context, out Interval tokenInterval)
112+
{
113+
var nameContext = context.identifier();
114+
tokenInterval = Interval.Of(nameContext.Start.TokenIndex, nameContext.Stop.TokenIndex);
115+
return GetName(context);
116+
}
117+
118+
public static string GetName(VBAParser.EnumerationStmtContext context)
119+
{
120+
return GetName(context.identifier());
121+
}
122+
123+
public static string GetName(VBAParser.EnumerationStmt_ConstantContext context, out Interval tokenInterval)
124+
{
125+
var nameContext = context.identifier();
126+
tokenInterval = Interval.Of(nameContext.Start.TokenIndex, nameContext.Stop.TokenIndex);
127+
return GetName(context);
128+
}
129+
130+
public static string GetName(VBAParser.EnumerationStmt_ConstantContext context)
131+
{
132+
return GetName(context.identifier());
133+
}
134+
135+
public static string GetName(VBAParser.IdentifierStatementLabelContext context, out Interval tokenInterval)
136+
{
137+
var nameContext = context.unrestrictedIdentifier();
138+
tokenInterval = Interval.Of(nameContext.Start.TokenIndex, nameContext.Stop.TokenIndex);
139+
return GetName(context);
140+
}
141+
142+
public static string GetName(VBAParser.IdentifierStatementLabelContext context)
143+
{
144+
return GetName(context.unrestrictedIdentifier());
145+
}
146+
147+
public static string GetName(VBAParser.SimpleNameExprContext context, out Interval tokenInterval)
148+
{
149+
var nameContext = context.identifier();
150+
tokenInterval = Interval.Of(nameContext.Start.TokenIndex, nameContext.Stop.TokenIndex);
151+
return GetName(context);
152+
}
153+
154+
public static string GetName(VBAParser.SimpleNameExprContext context)
155+
{
156+
return GetName(context.identifier());
157+
}
158+
111159
public static string GetName(VBAParser.IdentifierContext context, out Interval tokenInterval)
112160
{
113161
tokenInterval = Interval.Of(context.Start.TokenIndex, context.Stop.TokenIndex);

RubberduckTests/Refactoring/RenameTests.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,122 @@ public void RenameRefactoring_RenameProject()
16721672
Assert.AreEqual(newName, vbe.Object.VBProjects[0].Name);
16731673
}
16741674

1675+
#endregion
1676+
#region Rename Enumeration Tests
1677+
1678+
[TestMethod]
1679+
[TestCategory("Refactorings")]
1680+
[TestCategory("Rename")]
1681+
public void RenameRefactoring_RenameEnumeration()
1682+
{
1683+
var tdo = new RenameTestsDataObject(selection: "FruitType", newName: "Fruits");
1684+
var inputOutput = new RenameTestModuleDefinition("Module1", ComponentType.StandardModule)
1685+
{
1686+
Input =
1687+
@"Option Explicit
1688+
1689+
Public Enum Frui|tType
1690+
Apple = 1
1691+
Orange = 2
1692+
Plum = 3
1693+
End Enum
1694+
1695+
Sub DoSomething()
1696+
MsgBox CStr(FruitType.Apple)
1697+
End Sub",
1698+
Expected =
1699+
@"Option Explicit
1700+
1701+
Public Enum Fruits
1702+
Apple = 1
1703+
Orange = 2
1704+
Plum = 3
1705+
End Enum
1706+
1707+
Sub DoSomething()
1708+
MsgBox CStr(Fruits.Apple)
1709+
End Sub"
1710+
};
1711+
PerformExpectedVersusActualRenameTests(tdo, inputOutput);
1712+
1713+
tdo.MsgBox.Verify(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>()), Times.Never);
1714+
}
1715+
1716+
[TestMethod]
1717+
[TestCategory("Refactorings")]
1718+
[TestCategory("Rename")]
1719+
public void RenameRefactoring_RenameEnumerationMember()
1720+
{
1721+
var tdo = new RenameTestsDataObject(selection: "Apple", newName: "CranApple");
1722+
var inputOutput = new RenameTestModuleDefinition("Module1", ComponentType.StandardModule)
1723+
{
1724+
Input =
1725+
@"Option Explicit
1726+
1727+
Public Enum FruitType
1728+
App|le = 1
1729+
Orange = 2
1730+
Plum = 3
1731+
End Enum
1732+
1733+
Sub DoSomething()
1734+
MsgBox CStr(Apple)
1735+
End Sub",
1736+
Expected =
1737+
@"Option Explicit
1738+
1739+
Public Enum FruitType
1740+
CranApple = 1
1741+
Orange = 2
1742+
Plum = 3
1743+
End Enum
1744+
1745+
Sub DoSomething()
1746+
MsgBox CStr(CranApple)
1747+
End Sub"
1748+
};
1749+
PerformExpectedVersusActualRenameTests(tdo, inputOutput);
1750+
1751+
tdo.MsgBox.Verify(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>()), Times.Never);
1752+
}
1753+
1754+
#endregion
1755+
#region Rename Label Tests
1756+
[TestMethod]
1757+
[TestCategory("Refactorings")]
1758+
[TestCategory("Rename")]
1759+
public void RenameRefactoring_RenameLabel()
1760+
{
1761+
var tdo = new RenameTestsDataObject(selection: "EH", newName: "ErrorHandler");
1762+
var inputOutput1 = new RenameTestModuleDefinition("Module1", ComponentType.StandardModule)
1763+
{
1764+
Input =
1765+
@"Option Explicit
1766+
1767+
Sub DoSomething()
1768+
On Error goto EH
1769+
Dim check As Double
1770+
check = 1/0
1771+
Exit Sub
1772+
E|H:
1773+
MsgBox ""We had an error""
1774+
End Sub",
1775+
Expected =
1776+
@"Option Explicit
1777+
1778+
Sub DoSomething()
1779+
On Error goto ErrorHandler
1780+
Dim check As Double
1781+
check = 1/0
1782+
Exit Sub
1783+
ErrorHandler:
1784+
MsgBox ""We had an error""
1785+
End Sub"
1786+
};
1787+
PerformExpectedVersusActualRenameTests(tdo, inputOutput1);
1788+
1789+
tdo.MsgBox.Verify(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>()), Times.Never);
1790+
}
16751791
#endregion
16761792
#region Other Tests
16771793

0 commit comments

Comments
 (0)