1
+ using System ;
1
2
using Rubberduck . Parsing ;
2
3
using Rubberduck . Parsing . Symbols ;
3
4
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
4
5
using Rubberduck . Resources ;
6
+ using Rubberduck . VBEditor ;
5
7
6
8
namespace Rubberduck . UI . Command . MenuItems . CommandBars
7
9
{
@@ -46,96 +48,97 @@ public string Format(Declaration declaration, bool multipleControls)
46
48
47
49
private string FormatDeclaration ( Declaration declaration , bool multipleControls = false )
48
50
{
49
- var formattedDeclaration = string . Empty ;
50
51
var moduleName = declaration . QualifiedName . QualifiedModuleName ;
51
- var typeName = declaration . HasTypeHint
52
- ? SymbolList . TypeHintToTypeName [ declaration . TypeHint ]
53
- : declaration . AsTypeName ;
54
52
var declarationType = RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ;
55
53
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
+ }
65
58
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 )
74
66
{
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
+ }
77
80
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
-
88
81
if ( declaration . DeclarationType . HasFlag ( DeclarationType . Member ) )
89
82
{
90
- formattedDeclaration = declaration . QualifiedName . ToString ( ) ;
83
+ var formattedDeclaration = declaration . QualifiedName . ToString ( ) ;
91
84
if ( declaration . DeclarationType == DeclarationType . Function
92
85
|| declaration . DeclarationType == DeclarationType . PropertyGet )
93
86
{
94
87
formattedDeclaration += typeName ;
95
88
}
89
+
90
+ return formattedDeclaration ;
96
91
}
97
92
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 ) )
108
94
{
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 } )";
116
96
}
117
- else if ( declaration . DeclarationType == DeclarationType . ComAlias )
97
+
98
+ switch ( declaration . DeclarationType )
118
99
{
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 } )";
122
117
}
123
118
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 )
126
125
{
127
- // locals, parameters
128
- formattedDeclaration = string . Format ( "{0}:{1}{2} {3}" , declaration . ParentDeclaration . QualifiedName , declaration . IdentifierName , subscripts , typeName ) ;
126
+ return RubberduckUI . ContextMultipleControlsSelection ;
129
127
}
130
128
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 )
132
134
{
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 ) } )";
136
141
}
137
-
138
- return formattedDeclaration . Trim ( ) ;
139
142
}
140
143
}
141
144
}
0 commit comments