Skip to content

Commit 3b0d094

Browse files
committed
Added ForbiddenAttribute to mark keywords, reserved identifier names, and other forbidden names; adjusted name validator accordingly.
1 parent 46452a1 commit 3b0d094

File tree

11 files changed

+425
-21
lines changed

11 files changed

+425
-21
lines changed

Rubberduck.Core/Refactorings/ExtractMethod/ExtractedParameter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Rubberduck.Parsing.Grammar;
22
using Rubberduck.Resources;
3+
using Tokens = Rubberduck.Resources.Tokens;
34

45
namespace Rubberduck.Refactorings.ExtractMethod
56
{

Rubberduck.Core/UI/Command/MenuItems/CommandBars/IContextFormatter.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ public string Format(ICodePane activeCodePane, Declaration declaration)
4242

4343
public string Format(Declaration declaration, bool multipleControls)
4444
{
45-
return declaration == null ? string.Empty : FormatDeclaration(declaration, multipleControls);
45+
if (declaration == null)
46+
{
47+
return string.Empty;
48+
}
49+
50+
// designer, there is no code pane selection
51+
return FormatDeclaration(declaration, multipleControls);
4652
}
4753

4854
private string FormatDeclaration(Declaration declaration, bool multipleControls = false)
@@ -72,18 +78,18 @@ private static string FormattedDeclaration(
7278
if (declaration.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
7379
{
7480
// fields
75-
var withEvents = declaration.IsWithEvents ? "(WithEvents) " : string.Empty;
81+
var withEvents = declaration.IsWithEvents ? $"({Tokens.WithEvents}) " : string.Empty;
7682
return $"{withEvents}{moduleName}.{declaration.IdentifierName} {typeName}";
7783
}
7884
}
7985

8086
if (declaration.DeclarationType.HasFlag(DeclarationType.Member))
8187
{
82-
var formattedDeclaration = declaration.QualifiedName.ToString();
88+
var formattedDeclaration = $"{declaration.QualifiedName}";
8389
if (declaration.DeclarationType == DeclarationType.Function
8490
|| declaration.DeclarationType == DeclarationType.PropertyGet)
8591
{
86-
formattedDeclaration += typeName;
92+
formattedDeclaration += $" {typeName}";
8793
}
8894

8995
return formattedDeclaration;
@@ -103,16 +109,16 @@ private static string FormattedDeclaration(
103109
case DeclarationType.Enumeration:
104110
case DeclarationType.UserDefinedType:
105111
return !declaration.IsUserDefined
106-
// built-in enums & UDT's don't have a module
107-
? $"{Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.IdentifierName}"
112+
// built-in enums & UDTs don't have a module
113+
? $"{Path.GetFileName(moduleName.ProjectPath)};{declaration.IdentifierName}"
108114
: moduleName.ToString();
109115
case DeclarationType.EnumerationMember:
110116
case DeclarationType.UserDefinedTypeMember:
111117
return declaration.IsUserDefined
112118
? $"{moduleName}.{declaration.ParentDeclaration.IdentifierName}.{declaration.IdentifierName} {typeName}"
113-
: $"{Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.ParentDeclaration.IdentifierName}.{declaration.IdentifierName} {typeName}";
119+
: $"{Path.GetFileName(moduleName.ProjectPath)};{declaration.ParentDeclaration.IdentifierName}.{declaration.IdentifierName} {typeName}";
114120
case DeclarationType.ComAlias:
115-
return $"{Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.IdentifierName} (alias:{declaration.AsTypeName})";
121+
return $"{Path.GetFileName(moduleName.ProjectPath)};{declaration.IdentifierName} (alias:{declaration.AsTypeName})";
116122
}
117123

118124
return string.Empty;
@@ -125,22 +131,20 @@ private static string TypeName(Declaration declaration, bool multipleControls, s
125131
return RubberduckUI.ContextMultipleControlsSelection;
126132
}
127133

128-
var friendlyTypeName = "IDispatch".Equals(declaration.AsTypeName, System.StringComparison.InvariantCultureIgnoreCase)
129-
? "Object"
134+
var typeName = Tokens.IDispatch.Equals(declaration.AsTypeName, System.StringComparison.InvariantCultureIgnoreCase)
135+
? Tokens.Object
130136
: declaration.AsTypeName ?? string.Empty;
131137

132-
var typeName = declaration.IsArray
133-
? $"{friendlyTypeName}()"
134-
: friendlyTypeName;
138+
var friendlyTypeName = declaration.IsArray ? $"{typeName}()" : typeName;
135139

136140
switch (declaration)
137141
{
138142
case ValuedDeclaration valued:
139-
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)}{(string.IsNullOrEmpty(valued.Expression) ? string.Empty : $" = {valued.Expression}")})";
143+
return $"({declarationType}{(string.IsNullOrEmpty(friendlyTypeName) ? string.Empty : ":" + friendlyTypeName)}{(string.IsNullOrEmpty(valued.Expression) ? string.Empty : $" = {valued.Expression}")})";
140144
case ParameterDeclaration parameter:
141-
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)}{(string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : $" = {parameter.DefaultValue}")})";
145+
return $"({declarationType}{(string.IsNullOrEmpty(friendlyTypeName) ? string.Empty : ":" + friendlyTypeName)}{(string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : $" = {parameter.DefaultValue}")})";
142146
default:
143-
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)})";
147+
return $"({declarationType}{(string.IsNullOrEmpty(friendlyTypeName) ? string.Empty : ":" + friendlyTypeName)})";
144148
}
145149
}
146150
}

Rubberduck.Core/UI/FindSymbol/FindSymbolControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<ColumnDefinition Width="32" />
2626
</Grid.ColumnDefinitions>
2727

28-
<ComboBox x:Name="searchComboBox"
28+
<ComboBox x:Name="SearchComboBox"
2929
IsEditable="True"
3030
ItemsSource="{Binding MatchResults}"
3131
SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

Rubberduck.Core/UI/FindSymbol/FindSymbolControl.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private void UIElement_OnPreviewKeyDown(object sender, KeyEventArgs e)
4646

4747
private void FindSymbolControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
4848
{
49-
searchComboBox.Focus();
49+
SearchComboBox.Focus();
5050
}
5151
}
5252
}

Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
using System.ComponentModel;
44
using System.Drawing;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Windows.Forms;
78
using Rubberduck.Parsing.Grammar;
89
using Rubberduck.Parsing.Symbols;
910
using Rubberduck.Refactorings.ExtractMethod;
1011
using Rubberduck.Resources;
12+
using Tokens = Rubberduck.Resources.Tokens;
1113

1214
namespace Rubberduck.UI.Refactorings
1315
{
@@ -193,7 +195,9 @@ public string MethodName
193195

194196
private void ValidateName()
195197
{
196-
var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
198+
var tokenValues = typeof(Tokens).GetFields()
199+
.Where(item => !item.GetCustomAttributes<NotReservedAttribute>().Any())
200+
.Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
197201

198202
OkButton.Enabled = MethodName != OldMethodName
199203
&& char.IsLetter(MethodName.FirstOrDefault())

Rubberduck.Refactorings/Common/CodeBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using Tokens = Rubberduck.Resources.Tokens;
910

1011
namespace Rubberduck.Refactorings
1112
{

Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using System.Linq;
8+
using System.Reflection;
9+
using Tokens = Rubberduck.Resources.Tokens;
810

911
namespace Rubberduck.Refactorings.Common
1012
{
1113
public static class VBAIdentifierValidator
1214
{
13-
private static IEnumerable<string> ReservedIdentifiers =
14-
typeof(Tokens).GetFields().Select(item => item.GetValue(null).ToString()).ToArray();
15+
// NOTE: ForbiddenAttribute marks the tokens that don't compile as identifier names. Includes "bad but legal" names.
16+
// TODO: Compare with the unfiltered tokens if a client needs to tell "bad but legal" from "bad and illegal" names.
17+
private static readonly IEnumerable<string> ReservedIdentifiers =
18+
typeof(Tokens).GetFields()
19+
.Where(item => item.GetType().GetCustomAttributes<ForbiddenAttribute>().Any())
20+
.Select(item => item.GetValue(null).ToString()).ToArray();
1521

1622
/// <summary>
1723
/// Predicate function determining if an identifier string's content will trigger a result for the UseMeaningfulNames inspection.

Rubberduck.Refactorings/ExtractInterface/ExtractInterfaceRefactoringAction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Rubberduck.VBEditor.ComManagement;
1616
using Rubberduck.VBEditor.SafeComWrappers;
1717
using Rubberduck.VBEditor.Utility;
18+
using Tokens = Rubberduck.Resources.Tokens;
1819

1920
namespace Rubberduck.Refactorings.ExtractInterface
2021
{

Rubberduck.Refactorings/ImplementInterface/AddInterfaceImplementations/AddInterfaceImplementationsRefactoringAction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Rubberduck.Parsing.Rewriter;
66
using Rubberduck.Parsing.Symbols;
77
using Rubberduck.Resources;
8+
using Tokens = Rubberduck.Resources.Tokens;
89

910
namespace Rubberduck.Refactorings.AddInterfaceImplementations
1011
{

0 commit comments

Comments
 (0)