Skip to content

Commit 02bda4f

Browse files
committed
Fix formatting in RD command bar broken by prior commits
This includes a complete refactoring of ContextFormatter and changes the behaviour to always show parentheses in in the type if the declaration is an array. Moreover, the parentheses following the identifier name are no longer added for array variables. (They are obviously arrays based on the new parentheses in the type name.)
1 parent 6aa489b commit 02bda4f

File tree

2 files changed

+70
-67
lines changed

2 files changed

+70
-67
lines changed
Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System;
12
using Rubberduck.Parsing;
23
using Rubberduck.Parsing.Symbols;
34
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
45
using Rubberduck.Resources;
6+
using Rubberduck.VBEditor;
57

68
namespace Rubberduck.UI.Command.MenuItems.CommandBars
79
{
@@ -46,96 +48,97 @@ public string Format(Declaration declaration, bool multipleControls)
4648

4749
private string FormatDeclaration(Declaration declaration, bool multipleControls = false)
4850
{
49-
var formattedDeclaration = string.Empty;
5051
var moduleName = declaration.QualifiedName.QualifiedModuleName;
51-
var typeName = declaration.HasTypeHint
52-
? SymbolList.TypeHintToTypeName[declaration.TypeHint]
53-
: declaration.AsTypeName;
5452
var declarationType = RubberduckUI.ResourceManager.GetString("DeclarationType_" + declaration.DeclarationType, Settings.Settings.Culture);
5553

56-
if (multipleControls)
57-
{
58-
typeName = RubberduckUI.ContextMultipleControlsSelection;
59-
}
60-
else if (declaration is ValuedDeclaration)
61-
{
62-
var valued = (ValuedDeclaration)declaration;
63-
typeName = "(" + declarationType + (string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName) +
64-
(string.IsNullOrEmpty(valued.Expression) ? string.Empty : $" = {valued.Expression}") + ")";
54+
var typeName = TypeName(declaration, multipleControls, declarationType);
55+
var formattedDeclaration = FormattedDeclaration(declaration, typeName, moduleName, declarationType);
56+
return formattedDeclaration.Trim();
57+
}
6558

66-
}
67-
else if (declaration is ParameterDeclaration)
68-
{
69-
var parameter = (ParameterDeclaration)declaration;
70-
typeName = "(" + declarationType + (string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName) +
71-
(string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : $" = {parameter.DefaultValue}") + ")";
72-
}
73-
else
59+
private static string FormattedDeclaration(
60+
Declaration declaration,
61+
string typeName,
62+
QualifiedModuleName moduleName,
63+
string declarationType)
64+
{
65+
if (declaration.ParentDeclaration != null)
7466
{
75-
typeName = "(" + declarationType + (string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName) + ")";
76-
}
67+
if (declaration.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Member))
68+
{
69+
// locals, parameters
70+
return $"{declaration.ParentDeclaration.QualifiedName}:{declaration.IdentifierName} {typeName}";
71+
}
72+
73+
if (declaration.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
74+
{
75+
// fields
76+
var withEvents = declaration.IsWithEvents ? "(WithEvents) " : string.Empty;
77+
return $"{withEvents}{moduleName}.{declaration.IdentifierName} {typeName}";
78+
}
79+
}
7780

78-
if (declaration.DeclarationType.HasFlag(DeclarationType.Project) || declaration.DeclarationType == DeclarationType.BracketedExpression)
79-
{
80-
var filename = System.IO.Path.GetFileName(declaration.QualifiedName.QualifiedModuleName.ProjectPath);
81-
formattedDeclaration = string.Format("{0}{1}{2} ({3})", filename, string.IsNullOrEmpty(filename) ? string.Empty : ";", declaration.IdentifierName, declarationType);
82-
}
83-
else if (declaration.DeclarationType.HasFlag(DeclarationType.Module))
84-
{
85-
formattedDeclaration = moduleName + " (" + declarationType + ")";
86-
}
87-
8881
if (declaration.DeclarationType.HasFlag(DeclarationType.Member))
8982
{
90-
formattedDeclaration = declaration.QualifiedName.ToString();
83+
var formattedDeclaration = declaration.QualifiedName.ToString();
9184
if (declaration.DeclarationType == DeclarationType.Function
9285
|| declaration.DeclarationType == DeclarationType.PropertyGet)
9386
{
9487
formattedDeclaration += typeName;
9588
}
89+
90+
return formattedDeclaration;
9691
}
9792

98-
if (declaration.DeclarationType == DeclarationType.Enumeration
99-
|| declaration.DeclarationType == DeclarationType.UserDefinedType)
100-
{
101-
formattedDeclaration = !declaration.IsUserDefined
102-
// built-in enums & UDT's don't have a module
103-
? System.IO.Path.GetFileName(moduleName.ProjectPath) + ";" + moduleName.ProjectName + "." + declaration.IdentifierName
104-
: moduleName.ToString();
105-
}
106-
else if (declaration.DeclarationType == DeclarationType.EnumerationMember
107-
|| declaration.DeclarationType == DeclarationType.UserDefinedTypeMember)
93+
if (declaration.DeclarationType.HasFlag(DeclarationType.Module))
10894
{
109-
formattedDeclaration = string.Format("{0}.{1}.{2} {3}",
110-
!declaration.IsUserDefined
111-
? System.IO.Path.GetFileName(moduleName.ProjectPath) + ";" + moduleName.ProjectName
112-
: moduleName.ToString(),
113-
declaration.ParentDeclaration.IdentifierName,
114-
declaration.IdentifierName,
115-
typeName);
95+
return $"{moduleName} ({declarationType})";
11696
}
117-
else if (declaration.DeclarationType == DeclarationType.ComAlias)
97+
98+
switch (declaration.DeclarationType)
11899
{
119-
formattedDeclaration = string.Format("{0};{1}.{2} (alias:{3})",
120-
System.IO.Path.GetFileName(moduleName.ProjectPath), moduleName.ProjectName,
121-
declaration.IdentifierName, declaration.AsTypeName);
100+
case DeclarationType.Project:
101+
case DeclarationType.BracketedExpression:
102+
var filename = System.IO.Path.GetFileName(declaration.QualifiedName.QualifiedModuleName.ProjectPath);
103+
return $"{filename}{(string.IsNullOrEmpty(filename) ? string.Empty : ";")}{declaration.IdentifierName} ({declarationType})";
104+
case DeclarationType.Enumeration:
105+
case DeclarationType.UserDefinedType:
106+
return !declaration.IsUserDefined
107+
// built-in enums & UDT's don't have a module
108+
? $"{System.IO.Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.IdentifierName}"
109+
: moduleName.ToString();
110+
case DeclarationType.EnumerationMember:
111+
case DeclarationType.UserDefinedTypeMember:
112+
return declaration.IsUserDefined
113+
? $"{moduleName}.{declaration.ParentDeclaration.IdentifierName}.{declaration.IdentifierName} {typeName}"
114+
: $"{System.IO.Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.ParentDeclaration.IdentifierName}.{declaration.IdentifierName} {typeName}";
115+
case DeclarationType.ComAlias:
116+
return $"{System.IO.Path.GetFileName(moduleName.ProjectPath)};{moduleName.ProjectName}.{declaration.IdentifierName} (alias:{declaration.AsTypeName})";
122117
}
123118

124-
var subscripts = declaration.IsArray ? "()" : string.Empty;
125-
if (declaration.ParentDeclaration != null && declaration.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Member))
119+
return string.Empty;
120+
}
121+
122+
private static string TypeName(Declaration declaration, bool multipleControls, string declarationType)
123+
{
124+
if (multipleControls)
126125
{
127-
// locals, parameters
128-
formattedDeclaration = string.Format("{0}:{1}{2} {3}", declaration.ParentDeclaration.QualifiedName, declaration.IdentifierName, subscripts, typeName);
126+
return RubberduckUI.ContextMultipleControlsSelection;
129127
}
130128

131-
if (declaration.ParentDeclaration != null && declaration.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
129+
var typeName = declaration.IsArray
130+
? $"{declaration.AsTypeName}()"
131+
: declaration.AsTypeName;
132+
133+
switch (declaration)
132134
{
133-
// fields
134-
var withEvents = declaration.IsWithEvents ? "(WithEvents) " : string.Empty;
135-
formattedDeclaration = string.Format("{0}{1}.{2} {3}", withEvents, moduleName, declaration.IdentifierName, typeName);
135+
case ValuedDeclaration valued:
136+
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)}{(string.IsNullOrEmpty(valued.Expression) ? string.Empty : $" = {valued.Expression}")})";
137+
case ParameterDeclaration parameter:
138+
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)}{(string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : $" = {parameter.DefaultValue}")})";
139+
default:
140+
return $"({declarationType}{(string.IsNullOrEmpty(typeName) ? string.Empty : ":" + typeName)})";
136141
}
137-
138-
return formattedDeclaration.Trim();
139142
}
140143
}
141144
}

RubberduckTests/CodeExplorer/CodeExplorerProjectViewModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public void TrackedDeclarations_ExcludesNonNodeTypes(DeclarationType excluded)
147147
[Test]
148148
[Category("Code Explorer")]
149149
[TestCase(DeclarationType.Variable)]
150-
[TestCase(DeclarationType.Control)]
151-
[TestCase(DeclarationType.Constant, Ignore = "Pending test setup that will actually create one.")]
150+
[TestCase(DeclarationType.Control, Ignore = "Pending test setup that will actually create one.")]
151+
[TestCase(DeclarationType.Constant)]
152152
public void TrackedDeclarations_ExcludesMemberEnclosedTypes(DeclarationType excluded)
153153
{
154154
var declarations = CodeExplorerTestSetup.TestProjectOneDeclarations;

0 commit comments

Comments
 (0)