1
+ using System ;
2
+ using System . Linq ;
3
+ using Rubberduck . Parsing . Symbols ;
4
+ using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
5
+
6
+ namespace Rubberduck . UI . Command . MenuItems . CommandBars
7
+ {
8
+ public interface IContextFormatter
9
+ {
10
+ /// <summary>
11
+ /// Determines the formatting of the contextual selection caption.
12
+ /// </summary>
13
+ string Format ( ICodePane activeCodePane , Declaration declaration ) ;
14
+ }
15
+
16
+ public class ContextFormatter : IContextFormatter
17
+ {
18
+ public string Format ( ICodePane activeCodePane , Declaration declaration )
19
+ {
20
+ if ( activeCodePane == null )
21
+ {
22
+ return string . Empty ;
23
+ }
24
+
25
+ var qualifiedSelection = activeCodePane . GetQualifiedSelection ( ) ;
26
+ if ( declaration == null || ! qualifiedSelection . HasValue )
27
+ {
28
+ return string . Empty ;
29
+ }
30
+
31
+ var selection = qualifiedSelection . Value ;
32
+ var codePaneSelectionText = selection . Selection . ToString ( ) ;
33
+ var contextSelectionText = Format ( declaration ) ;
34
+
35
+ return string . Format ( "{0} | {1}" , codePaneSelectionText , contextSelectionText ) ;
36
+ }
37
+
38
+ private string Format ( Declaration declaration )
39
+ {
40
+ var formattedDeclaration = string . Empty ;
41
+ var moduleName = declaration . QualifiedName . QualifiedModuleName ;
42
+ var typeName = declaration . HasTypeHint
43
+ ? Declaration . TypeHintToTypeName [ declaration . TypeHint ]
44
+ : declaration . AsTypeName ;
45
+ var declarationType = RubberduckUI . ResourceManager . GetString ( "DeclarationType_" + declaration . DeclarationType , Settings . Settings . Culture ) ;
46
+
47
+ typeName = "(" + declarationType + ( string . IsNullOrEmpty ( typeName ) ? string . Empty : ":" + typeName ) + ")" ;
48
+
49
+ if ( declaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
50
+ {
51
+ formattedDeclaration = moduleName . ToString ( ) ;
52
+ }
53
+
54
+ if ( declaration . DeclarationType . HasFlag ( DeclarationType . Member ) )
55
+ {
56
+ formattedDeclaration = declaration . QualifiedName . ToString ( ) ;
57
+ if ( declaration . DeclarationType == DeclarationType . Function
58
+ || declaration . DeclarationType == DeclarationType . PropertyGet )
59
+ {
60
+ formattedDeclaration += typeName ;
61
+ }
62
+ }
63
+
64
+ if ( declaration . DeclarationType == DeclarationType . Enumeration
65
+ || declaration . DeclarationType == DeclarationType . UserDefinedType )
66
+ {
67
+ formattedDeclaration = declaration . IsBuiltIn
68
+ // built-in enums & UDT's don't have a module
69
+ ? System . IO . Path . GetFileName ( moduleName . ProjectPath ) + ";" + moduleName . ProjectName + "." + declaration . IdentifierName
70
+ : moduleName . ToString ( ) ;
71
+ }
72
+
73
+ if ( declaration . DeclarationType == DeclarationType . EnumerationMember
74
+ || declaration . DeclarationType == DeclarationType . UserDefinedTypeMember )
75
+ {
76
+ formattedDeclaration = string . Format ( "{0}.{1}.{2}" ,
77
+ declaration . IsBuiltIn
78
+ ? System . IO . Path . GetFileName ( moduleName . ProjectPath ) + ";" + moduleName . ProjectName
79
+ : moduleName . ToString ( ) ,
80
+ declaration . ParentDeclaration . IdentifierName ,
81
+ declaration . IdentifierName ) ;
82
+ }
83
+
84
+ var subscripts = declaration . IsArray ? "()" : string . Empty ;
85
+ if ( declaration . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Member ) )
86
+ {
87
+ // locals, parameters
88
+ formattedDeclaration = string . Format ( "{0}:{1}{2} {3}" , declaration . ParentDeclaration . QualifiedName , declaration . IdentifierName , subscripts , typeName ) ;
89
+ }
90
+
91
+ if ( declaration . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
92
+ {
93
+ // fields
94
+ var withEvents = declaration . IsWithEvents ? "(WithEvents) " : string . Empty ;
95
+ formattedDeclaration = string . Format ( "{0}{1}.{2} {3}" , withEvents , moduleName , declaration . IdentifierName , typeName ) ;
96
+ }
97
+
98
+ return string . Format ( "{0} | {1} {2}" , formattedDeclaration . Trim ( ) , declaration . References . Count ( ) , RubberduckUI . ContextReferences ) ;
99
+ }
100
+ }
101
+ }
0 commit comments