-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathBaseMapper.cs
More file actions
178 lines (159 loc) · 6.38 KB
/
Copy pathBaseMapper.cs
File metadata and controls
178 lines (159 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using CodeNav.OutOfProc.Constants;
using CodeNav.OutOfProc.ViewModels;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace CodeNav.OutOfProc.Languages.CSharp.Mappers;
public static class BaseMapper
{
/// <summary>
/// Map commonly shared code item properties based on the syntaxt token that is been mapped
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">Syntax node of the code member</param>
/// <param name="semanticModel">Semantic model used during compilation</param>
/// <param name="codeDocumentViewModel">Code document view model used in the CodeNav tool window</param>
/// <param name="identifier">Syntax token of the code member identifier</param>
/// <param name="nameSyntax">Syntax token of the code member name</param>
/// <param name="name">Name of the code member</param>
/// <param name="modifiers">Accessibility modifiers of the code member</param>
/// <returns>Code item class or othe code class derived from code item</returns>
public static T MapBase<T>(
SyntaxNode source,
SemanticModel semanticModel,
CodeDocumentViewModel codeDocumentViewModel,
SyntaxToken? identifier = null,
NameSyntax? nameSyntax = null,
string name = "",
SyntaxTokenList? modifiers = null) where T : CodeItem
{
var codeItem = Activator.CreateInstance<T>();
var codeItemName = MapName(identifier, nameSyntax, name);
codeItem.Name = codeItemName;
codeItem.FullName = MapFullName(source, codeItemName, semanticModel);
codeItem.FilePath = string.IsNullOrEmpty(source.SyntaxTree.FilePath)
? null
: new Uri(source.SyntaxTree.FilePath);
codeItem.Id = codeItem.FullName;
codeItem.Tooltip = codeItemName;
codeItem.Access = MapAccess(modifiers, source);
codeItem.CodeDocumentViewModel = codeDocumentViewModel;
codeItem.Span = source.Span;
codeItem.IdentifierSpan = identifier?.Span;
codeItem.OutlineSpan = MapOutlineSpan(codeItem.Span, codeItem.IdentifierSpan, nameSyntax?.Span);
return codeItem;
}
/// <summary>
/// Map the span that is used for expanding/collapsing outline regions
/// </summary>
/// <param name="span">Normal span of the syntax node</param>
/// <param name="identifierSpan">Identifier span of the syntax node</param>
/// <param name="name">Name of the syntax node</param>
/// <returns>TextSpan usable for outlining</returns>
private static TextSpan MapOutlineSpan(TextSpan span, TextSpan? identifierSpan, TextSpan? nameSpan)
{
var outlineSpanStart = 0;
if (nameSpan != null)
{
outlineSpanStart = nameSpan.Value.End;
}
if (identifierSpan != null)
{
outlineSpanStart = identifierSpan.Value.End;
}
return new TextSpan(outlineSpanStart, span.End - outlineSpanStart);
}
/// <summary>
/// Map the full name of a code item
/// </summary>
/// <remarks>Used to create a unique id for the code item</remarks>
/// <param name="source">Syntax node of the code item</param>
/// <param name="name">Display name of the code item</param>
/// <param name="semanticModel">Semantic model used during compilation</param>
/// <returns>String full name</returns>
private static string MapFullName(SyntaxNode source, string name, SemanticModel semanticModel)
{
try
{
var symbol = semanticModel.GetDeclaredSymbol(source);
return symbol?.ToString() ?? name;
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// Map the display name of a code item
/// </summary>
/// <param name="identifier">Identifier syntax token of the code item</param>
/// <param name="nameSyntax">Name syntax token of the code item</param>
/// <returns>String display name</returns>
private static string MapName(SyntaxToken? identifier, NameSyntax? nameSyntax, string name = "")
{
if (identifier != null &&
!identifier.Value.IsKind(SyntaxKind.None))
{
return identifier.Value.Text;
}
if (nameSyntax != null)
{
return nameSyntax.ToString();
}
return name;
}
private static CodeItemAccessEnum MapAccess(SyntaxTokenList? modifiers, SyntaxNode source)
{
if (modifiers == null)
{
return MapDefaultAccess(source);
}
if (modifiers.Value.Any(m => m.RawKind == (int)SyntaxKind.SealedKeyword))
{
return CodeItemAccessEnum.Sealed;
}
if (modifiers.Value.Any(m => m.RawKind == (int)SyntaxKind.PublicKeyword))
{
return CodeItemAccessEnum.Public;
}
if (modifiers.Value.Any(m => m.RawKind == (int)SyntaxKind.PrivateKeyword))
{
return CodeItemAccessEnum.Private;
}
if (modifiers.Value.Any(m => m.RawKind == (int)SyntaxKind.ProtectedKeyword))
{
return CodeItemAccessEnum.Protected;
}
if (modifiers.Value.Any(m => m.RawKind == (int)SyntaxKind.InternalKeyword))
{
return CodeItemAccessEnum.Internal;
}
return MapDefaultAccess(source);
}
/// <summary>
/// When no access modifier is given map to the default access modifier
/// https://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private static CodeItemAccessEnum MapDefaultAccess(SyntaxNode source)
{
if (source.Parent.IsKind(SyntaxKind.CompilationUnit))
{
return source.Kind() switch
{
SyntaxKind.EnumDeclaration => CodeItemAccessEnum.Public,
SyntaxKind.NamespaceDeclaration => CodeItemAccessEnum.Public,
_ => CodeItemAccessEnum.Internal,
};
}
return source.Kind() switch
{
SyntaxKind.NamespaceDeclaration => CodeItemAccessEnum.Public,
SyntaxKind.EnumDeclaration => CodeItemAccessEnum.Public,
SyntaxKind.InterfaceDeclaration => CodeItemAccessEnum.Public,
_ => CodeItemAccessEnum.Private,
};
}
}