1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Diagnostics; |
---|
22 | using System.Linq; |
---|
23 | using ICSharpCode.NRefactory.CSharp.Resolver; |
---|
24 | using ICSharpCode.NRefactory.Semantics; |
---|
25 | using ICSharpCode.NRefactory.TypeSystem; |
---|
26 | using System.Threading; |
---|
27 | using ICSharpCode.NRefactory.CSharp.Completion; |
---|
28 | using System.Collections.ObjectModel; |
---|
29 | |
---|
30 | namespace ICSharpCode.NRefactory.CSharp.Analysis |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// C# Semantic highlighter. |
---|
34 | /// </summary> |
---|
35 | public abstract class SemanticHighlightingVisitor<TColor> : DepthFirstAstVisitor |
---|
36 | { |
---|
37 | protected CancellationToken cancellationToken = default (CancellationToken); |
---|
38 | |
---|
39 | protected TColor defaultTextColor; |
---|
40 | protected TColor referenceTypeColor; |
---|
41 | protected TColor valueTypeColor; |
---|
42 | protected TColor interfaceTypeColor; |
---|
43 | protected TColor enumerationTypeColor; |
---|
44 | protected TColor typeParameterTypeColor; |
---|
45 | protected TColor delegateTypeColor; |
---|
46 | |
---|
47 | protected TColor methodCallColor; |
---|
48 | protected TColor methodDeclarationColor; |
---|
49 | |
---|
50 | protected TColor eventDeclarationColor; |
---|
51 | protected TColor eventAccessColor; |
---|
52 | |
---|
53 | protected TColor propertyDeclarationColor; |
---|
54 | protected TColor propertyAccessColor; |
---|
55 | |
---|
56 | protected TColor fieldDeclarationColor; |
---|
57 | protected TColor fieldAccessColor; |
---|
58 | |
---|
59 | protected TColor variableDeclarationColor; |
---|
60 | protected TColor variableAccessColor; |
---|
61 | |
---|
62 | protected TColor parameterDeclarationColor; |
---|
63 | protected TColor parameterAccessColor; |
---|
64 | |
---|
65 | protected TColor valueKeywordColor; |
---|
66 | protected TColor externAliasKeywordColor; |
---|
67 | protected TColor varKeywordTypeColor; |
---|
68 | |
---|
69 | /// <summary> |
---|
70 | /// Used for 'in' modifiers on type parameters. |
---|
71 | /// </summary> |
---|
72 | /// <remarks> |
---|
73 | /// 'in' may have a different color when used with 'foreach'. |
---|
74 | /// 'out' is not colored by semantic highlighting, as syntax highlighting can already detect it as a parameter modifier. |
---|
75 | /// </remarks> |
---|
76 | protected TColor parameterModifierColor; |
---|
77 | |
---|
78 | /// <summary> |
---|
79 | /// Used for inactive code (excluded by preprocessor or ConditionalAttribute) |
---|
80 | /// </summary> |
---|
81 | protected TColor inactiveCodeColor; |
---|
82 | |
---|
83 | protected TColor stringFormatItemColor; |
---|
84 | |
---|
85 | |
---|
86 | protected TColor syntaxErrorColor; |
---|
87 | |
---|
88 | protected TextLocation regionStart; |
---|
89 | protected TextLocation regionEnd; |
---|
90 | |
---|
91 | protected CSharpAstResolver resolver; |
---|
92 | protected bool isInAccessorContainingValueParameter; |
---|
93 | |
---|
94 | protected abstract void Colorize(TextLocation start, TextLocation end, TColor color); |
---|
95 | |
---|
96 | #region Colorize helper methods |
---|
97 | protected void Colorize(Identifier identifier, ResolveResult rr) |
---|
98 | { |
---|
99 | if (identifier.IsNull) |
---|
100 | return; |
---|
101 | if (rr.IsError) { |
---|
102 | Colorize(identifier, syntaxErrorColor); |
---|
103 | return; |
---|
104 | } |
---|
105 | if (rr is TypeResolveResult) { |
---|
106 | if (blockDepth > 0 && identifier.Name == "var" && rr.Type.Kind != TypeKind.Null && rr.Type.Name != "var" ) { |
---|
107 | Colorize(identifier, varKeywordTypeColor); |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | TColor color; |
---|
112 | if (TryGetTypeHighlighting (rr.Type.Kind, out color)) { |
---|
113 | Colorize(identifier, color); |
---|
114 | } |
---|
115 | return; |
---|
116 | } |
---|
117 | var mrr = rr as MemberResolveResult; |
---|
118 | if (mrr != null) { |
---|
119 | TColor color; |
---|
120 | if (TryGetMemberColor (mrr.Member, out color)) { |
---|
121 | Colorize(identifier, color); |
---|
122 | return; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | if (rr is MethodGroupResolveResult) { |
---|
127 | Colorize (identifier, methodCallColor); |
---|
128 | return; |
---|
129 | } |
---|
130 | |
---|
131 | var localResult = rr as LocalResolveResult; |
---|
132 | if (localResult != null) { |
---|
133 | if (localResult.Variable is IParameter) { |
---|
134 | Colorize (identifier, parameterAccessColor); |
---|
135 | } else { |
---|
136 | Colorize (identifier, variableAccessColor); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | VisitIdentifier(identifier); // un-colorize contextual keywords |
---|
142 | } |
---|
143 | |
---|
144 | protected void Colorize(AstNode node, TColor color) |
---|
145 | { |
---|
146 | if (node.IsNull) |
---|
147 | return; |
---|
148 | Colorize(node.StartLocation, node.EndLocation, color); |
---|
149 | } |
---|
150 | #endregion |
---|
151 | |
---|
152 | protected override void VisitChildren(AstNode node) |
---|
153 | { |
---|
154 | for (var child = node.FirstChild; child != null; child = child.NextSibling) { |
---|
155 | if (child.StartLocation < regionEnd && child.EndLocation > regionStart) |
---|
156 | child.AcceptVisitor(this); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /// <summary> |
---|
161 | /// Visit all children of <c>node</c> until (but excluding) <c>end</c>. |
---|
162 | /// If <c>end</c> is a null node, nothing will be visited. |
---|
163 | /// </summary> |
---|
164 | protected void VisitChildrenUntil(AstNode node, AstNode end) |
---|
165 | { |
---|
166 | if (end.IsNull) |
---|
167 | return; |
---|
168 | Debug.Assert(node == end.Parent); |
---|
169 | for (var child = node.FirstChild; child != end; child = child.NextSibling) { |
---|
170 | cancellationToken.ThrowIfCancellationRequested(); |
---|
171 | if (child.StartLocation < regionEnd && child.EndLocation > regionStart) |
---|
172 | child.AcceptVisitor(this); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /// <summary> |
---|
177 | /// Visit all children of <c>node</c> after (excluding) <c>start</c>. |
---|
178 | /// If <c>start</c> is a null node, all children will be visited. |
---|
179 | /// </summary> |
---|
180 | protected void VisitChildrenAfter(AstNode node, AstNode start) |
---|
181 | { |
---|
182 | Debug.Assert(start.IsNull || start.Parent == node); |
---|
183 | for (var child = (start.IsNull ? node.FirstChild : start.NextSibling); child != null; child = child.NextSibling) { |
---|
184 | cancellationToken.ThrowIfCancellationRequested(); |
---|
185 | if (child.StartLocation < regionEnd && child.EndLocation > regionStart) |
---|
186 | child.AcceptVisitor(this); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | public override void VisitIdentifier(Identifier identifier) |
---|
191 | { |
---|
192 | switch (identifier.Name) { |
---|
193 | case "add": |
---|
194 | case "async": |
---|
195 | case "await": |
---|
196 | case "get": |
---|
197 | case "partial": |
---|
198 | case "remove": |
---|
199 | case "set": |
---|
200 | case "where": |
---|
201 | case "yield": |
---|
202 | case "from": |
---|
203 | case "select": |
---|
204 | case "group": |
---|
205 | case "into": |
---|
206 | case "orderby": |
---|
207 | case "join": |
---|
208 | case "let": |
---|
209 | case "on": |
---|
210 | case "equals": |
---|
211 | case "by": |
---|
212 | case "ascending": |
---|
213 | case "descending": |
---|
214 | case "dynamic": |
---|
215 | case "var": |
---|
216 | // Reset color of contextual keyword to default if it's used as an identifier. |
---|
217 | // Note that this method does not get called when 'var' or 'dynamic' is used as a type, |
---|
218 | // because types get highlighted with valueTypeColor/referenceTypeColor instead. |
---|
219 | Colorize(identifier, defaultTextColor); |
---|
220 | break; |
---|
221 | case "global": |
---|
222 | // Reset color of 'global' keyword to default unless its used as part of 'global::'. |
---|
223 | MemberType parentMemberType = identifier.Parent as MemberType; |
---|
224 | if (parentMemberType == null || !parentMemberType.IsDoubleColon) |
---|
225 | Colorize(identifier, defaultTextColor); |
---|
226 | break; |
---|
227 | } |
---|
228 | // "value" is handled in VisitIdentifierExpression() |
---|
229 | // "alias" is handled in VisitExternAliasDeclaration() |
---|
230 | } |
---|
231 | |
---|
232 | public override void VisitSimpleType(SimpleType simpleType) |
---|
233 | { |
---|
234 | var identifierToken = simpleType.IdentifierToken; |
---|
235 | VisitChildrenUntil(simpleType, identifierToken); |
---|
236 | Colorize(identifierToken, resolver.Resolve(simpleType, cancellationToken)); |
---|
237 | VisitChildrenAfter(simpleType, identifierToken); |
---|
238 | } |
---|
239 | |
---|
240 | public override void VisitMemberType(MemberType memberType) |
---|
241 | { |
---|
242 | var memberNameToken = memberType.MemberNameToken; |
---|
243 | VisitChildrenUntil(memberType, memberNameToken); |
---|
244 | Colorize(memberNameToken, resolver.Resolve(memberType, cancellationToken)); |
---|
245 | VisitChildrenAfter(memberType, memberNameToken); |
---|
246 | } |
---|
247 | |
---|
248 | public override void VisitIdentifierExpression(IdentifierExpression identifierExpression) |
---|
249 | { |
---|
250 | var identifier = identifierExpression.IdentifierToken; |
---|
251 | VisitChildrenUntil(identifierExpression, identifier); |
---|
252 | if (isInAccessorContainingValueParameter && identifierExpression.Identifier == "value") { |
---|
253 | Colorize(identifier, valueKeywordColor); |
---|
254 | } else { |
---|
255 | Colorize(identifier, resolver.Resolve(identifierExpression, cancellationToken)); |
---|
256 | } |
---|
257 | VisitChildrenAfter(identifierExpression, identifier); |
---|
258 | } |
---|
259 | |
---|
260 | public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression) |
---|
261 | { |
---|
262 | var memberNameToken = memberReferenceExpression.MemberNameToken; |
---|
263 | VisitChildrenUntil(memberReferenceExpression, memberNameToken); |
---|
264 | ResolveResult rr = resolver.Resolve(memberReferenceExpression, cancellationToken); |
---|
265 | Colorize(memberNameToken, rr); |
---|
266 | VisitChildrenAfter(memberReferenceExpression, memberNameToken); |
---|
267 | } |
---|
268 | |
---|
269 | void HighlightStringFormatItems(PrimitiveExpression expr) |
---|
270 | { |
---|
271 | if (!(expr.Value is string)) |
---|
272 | return; |
---|
273 | int line = expr.StartLocation.Line; |
---|
274 | int col = expr.StartLocation.Column; |
---|
275 | TextLocation start = TextLocation.Empty; |
---|
276 | for (int i = 0; i < expr.LiteralValue.Length; i++) { |
---|
277 | char ch = expr.LiteralValue [i]; |
---|
278 | |
---|
279 | if (NewLine.GetDelimiterType(ch, i + 1 < expr.LiteralValue.Length ? expr.LiteralValue [i + 1] : '\0') != UnicodeNewline.Unknown) { |
---|
280 | line++; |
---|
281 | col = 1; |
---|
282 | continue; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | if (ch == '{' && start.IsEmpty) { |
---|
287 | char next = i + 1 < expr.LiteralValue.Length ? expr.LiteralValue [i + 1] : '\0'; |
---|
288 | if (next == '{') { |
---|
289 | i++; |
---|
290 | col += 2; |
---|
291 | continue; |
---|
292 | } |
---|
293 | start = new TextLocation(line, col); |
---|
294 | } |
---|
295 | if (ch == '}' &&!start.IsEmpty) { |
---|
296 | Colorize(start, new TextLocation(line, col + 1), stringFormatItemColor); |
---|
297 | start = TextLocation.Empty; |
---|
298 | } |
---|
299 | col++; |
---|
300 | } |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | public override void VisitInvocationExpression(InvocationExpression invocationExpression) |
---|
305 | { |
---|
306 | Expression target = invocationExpression.Target; |
---|
307 | if (target is IdentifierExpression || target is MemberReferenceExpression || target is PointerReferenceExpression) { |
---|
308 | var invocationRR = resolver.Resolve(invocationExpression, cancellationToken) as CSharpInvocationResolveResult; |
---|
309 | if (invocationRR != null) { |
---|
310 | if (invocationExpression.Parent is ExpressionStatement && (IsInactiveConditionalMethod(invocationRR.Member) || IsEmptyPartialMethod(invocationRR.Member))) { |
---|
311 | // mark the whole invocation statement as inactive code |
---|
312 | Colorize(invocationExpression.Parent, inactiveCodeColor); |
---|
313 | return; |
---|
314 | } |
---|
315 | |
---|
316 | Expression fmtArgumets; |
---|
317 | IList<Expression> args; |
---|
318 | if (invocationRR.Arguments.Count > 1 && FormatStringHelper.TryGetFormattingParameters(invocationRR, invocationExpression, out fmtArgumets, out args, null)) { |
---|
319 | var expr = invocationExpression.Arguments.First() as PrimitiveExpression; |
---|
320 | if (expr != null) |
---|
321 | HighlightStringFormatItems(expr); |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | VisitChildrenUntil(invocationExpression, target); |
---|
326 | |
---|
327 | // highlight the method call |
---|
328 | var identifier = target.GetChildByRole(Roles.Identifier); |
---|
329 | VisitChildrenUntil(target, identifier); |
---|
330 | if (invocationRR != null && !invocationRR.IsDelegateInvocation) { |
---|
331 | Colorize(identifier, methodCallColor); |
---|
332 | } else { |
---|
333 | ResolveResult targetRR = resolver.Resolve(target, cancellationToken); |
---|
334 | Colorize(identifier, targetRR); |
---|
335 | } |
---|
336 | VisitChildrenAfter(target, identifier); |
---|
337 | VisitChildrenAfter(invocationExpression, target); |
---|
338 | } else { |
---|
339 | VisitChildren(invocationExpression); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | #region IsInactiveConditional helper methods |
---|
344 | bool IsInactiveConditionalMethod(IParameterizedMember member) |
---|
345 | { |
---|
346 | if (member.SymbolKind != SymbolKind.Method || member.ReturnType.Kind != TypeKind.Void) |
---|
347 | return false; |
---|
348 | foreach (var baseMember in InheritanceHelper.GetBaseMembers(member, false)) { |
---|
349 | if (IsInactiveConditional (baseMember.Attributes)) |
---|
350 | return true; |
---|
351 | } |
---|
352 | return IsInactiveConditional(member.Attributes); |
---|
353 | } |
---|
354 | |
---|
355 | static bool IsEmptyPartialMethod(IParameterizedMember member) |
---|
356 | { |
---|
357 | if (member.SymbolKind != SymbolKind.Method || member.ReturnType.Kind != TypeKind.Void) |
---|
358 | return false; |
---|
359 | var method = (IMethod)member; |
---|
360 | return method.IsPartial && !method.HasBody; |
---|
361 | } |
---|
362 | |
---|
363 | bool IsInactiveConditional(IList<IAttribute> attributes) |
---|
364 | { |
---|
365 | bool hasConditionalAttribute = false; |
---|
366 | foreach (var attr in attributes) { |
---|
367 | if (attr.AttributeType.Name == "ConditionalAttribute" && attr.AttributeType.Namespace == "System.Diagnostics" && attr.PositionalArguments.Count == 1) { |
---|
368 | string symbol = attr.PositionalArguments[0].ConstantValue as string; |
---|
369 | if (symbol != null) { |
---|
370 | hasConditionalAttribute = true; |
---|
371 | var cu = this.resolver.RootNode as SyntaxTree; |
---|
372 | if (cu != null) { |
---|
373 | if (cu.ConditionalSymbols.Contains(symbol)) |
---|
374 | return false; // conditional is active |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | return hasConditionalAttribute; |
---|
381 | } |
---|
382 | #endregion |
---|
383 | |
---|
384 | public override void VisitExternAliasDeclaration (ExternAliasDeclaration externAliasDeclaration) |
---|
385 | { |
---|
386 | var aliasToken = externAliasDeclaration.AliasToken; |
---|
387 | VisitChildrenUntil(externAliasDeclaration, aliasToken); |
---|
388 | Colorize (aliasToken, externAliasKeywordColor); |
---|
389 | VisitChildrenAfter(externAliasDeclaration, aliasToken); |
---|
390 | } |
---|
391 | |
---|
392 | public override void VisitAccessor(Accessor accessor) |
---|
393 | { |
---|
394 | isInAccessorContainingValueParameter = accessor.Role != PropertyDeclaration.GetterRole; |
---|
395 | try { |
---|
396 | VisitChildren(accessor); |
---|
397 | } finally { |
---|
398 | isInAccessorContainingValueParameter = false; |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | bool CheckInterfaceImplementation (EntityDeclaration entityDeclaration) |
---|
403 | { |
---|
404 | var result = resolver.Resolve (entityDeclaration, cancellationToken) as MemberResolveResult; |
---|
405 | if (result == null) |
---|
406 | return false; |
---|
407 | if (result.Member.ImplementedInterfaceMembers.Count == 0) { |
---|
408 | Colorize (entityDeclaration.NameToken, syntaxErrorColor); |
---|
409 | return false; |
---|
410 | } |
---|
411 | return true; |
---|
412 | } |
---|
413 | |
---|
414 | public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
---|
415 | { |
---|
416 | var nameToken = methodDeclaration.NameToken; |
---|
417 | VisitChildrenUntil(methodDeclaration, nameToken); |
---|
418 | if (!methodDeclaration.PrivateImplementationType.IsNull) { |
---|
419 | if (!CheckInterfaceImplementation (methodDeclaration)) { |
---|
420 | VisitChildrenAfter(methodDeclaration, nameToken); |
---|
421 | return; |
---|
422 | } |
---|
423 | } |
---|
424 | Colorize(nameToken, methodDeclarationColor); |
---|
425 | VisitChildrenAfter(methodDeclaration, nameToken); |
---|
426 | } |
---|
427 | |
---|
428 | public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration) |
---|
429 | { |
---|
430 | var nameToken = parameterDeclaration.NameToken; |
---|
431 | VisitChildrenUntil(parameterDeclaration, nameToken); |
---|
432 | Colorize(nameToken, parameterDeclarationColor); |
---|
433 | VisitChildrenAfter(parameterDeclaration, nameToken); |
---|
434 | } |
---|
435 | |
---|
436 | public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
---|
437 | { |
---|
438 | var nameToken = eventDeclaration.NameToken; |
---|
439 | VisitChildrenUntil(eventDeclaration, nameToken); |
---|
440 | Colorize(nameToken, eventDeclarationColor); |
---|
441 | VisitChildrenAfter(eventDeclaration, nameToken); |
---|
442 | } |
---|
443 | |
---|
444 | public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
---|
445 | { |
---|
446 | var nameToken = eventDeclaration.NameToken; |
---|
447 | VisitChildrenUntil(eventDeclaration, nameToken); |
---|
448 | if (!eventDeclaration.PrivateImplementationType.IsNull) { |
---|
449 | if (!CheckInterfaceImplementation (eventDeclaration)) { |
---|
450 | VisitChildrenAfter(eventDeclaration, nameToken); |
---|
451 | return; |
---|
452 | } |
---|
453 | } |
---|
454 | Colorize(nameToken, eventDeclarationColor); |
---|
455 | VisitChildrenAfter(eventDeclaration, nameToken); |
---|
456 | } |
---|
457 | |
---|
458 | public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) |
---|
459 | { |
---|
460 | var nameToken = propertyDeclaration.NameToken; |
---|
461 | VisitChildrenUntil(propertyDeclaration, nameToken); |
---|
462 | if (!propertyDeclaration.PrivateImplementationType.IsNull) { |
---|
463 | if (!CheckInterfaceImplementation (propertyDeclaration)) { |
---|
464 | VisitChildrenAfter(propertyDeclaration, nameToken); |
---|
465 | return; |
---|
466 | } |
---|
467 | } |
---|
468 | Colorize(nameToken, propertyDeclarationColor); |
---|
469 | VisitChildrenAfter(propertyDeclaration, nameToken); |
---|
470 | } |
---|
471 | |
---|
472 | public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration) |
---|
473 | { |
---|
474 | base.VisitIndexerDeclaration(indexerDeclaration); |
---|
475 | if (!indexerDeclaration.PrivateImplementationType.IsNull) { |
---|
476 | CheckInterfaceImplementation (indexerDeclaration); |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) |
---|
481 | { |
---|
482 | fieldDeclaration.ReturnType.AcceptVisitor (this); |
---|
483 | foreach (var init in fieldDeclaration.Variables) { |
---|
484 | Colorize (init.NameToken, fieldDeclarationColor); |
---|
485 | init.Initializer.AcceptVisitor (this); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration) |
---|
490 | { |
---|
491 | fixedFieldDeclaration.ReturnType.AcceptVisitor (this); |
---|
492 | foreach (var init in fixedFieldDeclaration.Variables) { |
---|
493 | Colorize (init.NameToken, fieldDeclarationColor); |
---|
494 | init.CountExpression.AcceptVisitor (this); |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration) |
---|
499 | { |
---|
500 | HandleConstructorOrDestructor(constructorDeclaration); |
---|
501 | } |
---|
502 | |
---|
503 | public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration) |
---|
504 | { |
---|
505 | HandleConstructorOrDestructor(destructorDeclaration); |
---|
506 | } |
---|
507 | |
---|
508 | void HandleConstructorOrDestructor(AstNode constructorDeclaration) |
---|
509 | { |
---|
510 | Identifier nameToken = constructorDeclaration.GetChildByRole(Roles.Identifier); |
---|
511 | VisitChildrenUntil(constructorDeclaration, nameToken); |
---|
512 | var currentTypeDef = resolver.GetResolverStateBefore(constructorDeclaration).CurrentTypeDefinition; |
---|
513 | if (currentTypeDef != null && nameToken.Name == currentTypeDef.Name) { |
---|
514 | TColor color; |
---|
515 | if (TryGetTypeHighlighting (currentTypeDef.Kind, out color)) |
---|
516 | Colorize(nameToken, color); |
---|
517 | } |
---|
518 | VisitChildrenAfter(constructorDeclaration, nameToken); |
---|
519 | } |
---|
520 | |
---|
521 | bool TryGetMemberColor(IMember member, out TColor color) |
---|
522 | { |
---|
523 | switch (member.SymbolKind) { |
---|
524 | case SymbolKind.Field: |
---|
525 | color = fieldAccessColor; |
---|
526 | return true; |
---|
527 | case SymbolKind.Property: |
---|
528 | color = propertyAccessColor; |
---|
529 | return true; |
---|
530 | case SymbolKind.Event: |
---|
531 | color = eventAccessColor; |
---|
532 | return true; |
---|
533 | case SymbolKind.Method: |
---|
534 | color = methodCallColor; |
---|
535 | return true; |
---|
536 | case SymbolKind.Constructor: |
---|
537 | case SymbolKind.Destructor: |
---|
538 | return TryGetTypeHighlighting (member.DeclaringType.Kind, out color); |
---|
539 | default: |
---|
540 | color = default (TColor); |
---|
541 | return false; |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | TColor GetTypeHighlighting (ClassType classType) |
---|
546 | { |
---|
547 | switch (classType) { |
---|
548 | case ClassType.Class: |
---|
549 | return referenceTypeColor; |
---|
550 | case ClassType.Struct: |
---|
551 | return valueTypeColor; |
---|
552 | case ClassType.Interface: |
---|
553 | return interfaceTypeColor; |
---|
554 | case ClassType.Enum: |
---|
555 | return enumerationTypeColor; |
---|
556 | default: |
---|
557 | throw new InvalidOperationException ("Unknown class type :" + classType); |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | bool TryGetTypeHighlighting (TypeKind kind, out TColor color) |
---|
562 | { |
---|
563 | switch (kind) { |
---|
564 | case TypeKind.Class: |
---|
565 | color = referenceTypeColor; |
---|
566 | return true; |
---|
567 | case TypeKind.Struct: |
---|
568 | color = valueTypeColor; |
---|
569 | return true; |
---|
570 | case TypeKind.Interface: |
---|
571 | color = interfaceTypeColor; |
---|
572 | return true; |
---|
573 | case TypeKind.Enum: |
---|
574 | color = enumerationTypeColor; |
---|
575 | return true; |
---|
576 | case TypeKind.TypeParameter: |
---|
577 | color = typeParameterTypeColor; |
---|
578 | return true; |
---|
579 | case TypeKind.Delegate: |
---|
580 | color = delegateTypeColor; |
---|
581 | return true; |
---|
582 | case TypeKind.Unknown: |
---|
583 | case TypeKind.Null: |
---|
584 | color = syntaxErrorColor; |
---|
585 | return true; |
---|
586 | default: |
---|
587 | color = default (TColor); |
---|
588 | return false; |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) |
---|
593 | { |
---|
594 | var nameToken = typeDeclaration.NameToken; |
---|
595 | VisitChildrenUntil(typeDeclaration, nameToken); |
---|
596 | Colorize(nameToken, GetTypeHighlighting (typeDeclaration.ClassType)); |
---|
597 | VisitChildrenAfter(typeDeclaration, nameToken); |
---|
598 | } |
---|
599 | |
---|
600 | public override void VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration) |
---|
601 | { |
---|
602 | if (typeParameterDeclaration.Variance == VarianceModifier.Contravariant) |
---|
603 | Colorize(typeParameterDeclaration.VarianceToken, parameterModifierColor); |
---|
604 | |
---|
605 | // bool isValueType = false; |
---|
606 | // if (typeParameterDeclaration.Parent != null) { |
---|
607 | // foreach (var constraint in typeParameterDeclaration.Parent.GetChildrenByRole(Roles.Constraint)) { |
---|
608 | // if (constraint.TypeParameter.Identifier == typeParameterDeclaration.Name) { |
---|
609 | // isValueType = constraint.BaseTypes.OfType<PrimitiveType>().Any(p => p.Keyword == "struct"); |
---|
610 | // } |
---|
611 | // } |
---|
612 | // } |
---|
613 | var nameToken = typeParameterDeclaration.NameToken; |
---|
614 | VisitChildrenUntil(typeParameterDeclaration, nameToken); |
---|
615 | Colorize(nameToken, typeParameterTypeColor); /*isValueType ? valueTypeColor : referenceTypeColor*/ |
---|
616 | VisitChildrenAfter(typeParameterDeclaration, nameToken); |
---|
617 | } |
---|
618 | |
---|
619 | public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration) |
---|
620 | { |
---|
621 | var nameToken = delegateDeclaration.NameToken; |
---|
622 | VisitChildrenUntil(delegateDeclaration, nameToken); |
---|
623 | Colorize(nameToken, delegateTypeColor); |
---|
624 | VisitChildrenAfter(delegateDeclaration, nameToken); |
---|
625 | } |
---|
626 | |
---|
627 | public override void VisitVariableInitializer(VariableInitializer variableInitializer) |
---|
628 | { |
---|
629 | var nameToken = variableInitializer.NameToken; |
---|
630 | VisitChildrenUntil(variableInitializer, nameToken); |
---|
631 | if (variableInitializer.Parent is FieldDeclaration) { |
---|
632 | Colorize(nameToken, fieldDeclarationColor); |
---|
633 | } else if (variableInitializer.Parent is EventDeclaration) { |
---|
634 | Colorize(nameToken, eventDeclarationColor); |
---|
635 | } else { |
---|
636 | Colorize(nameToken, variableDeclarationColor); |
---|
637 | } |
---|
638 | VisitChildrenAfter(variableInitializer, nameToken); |
---|
639 | } |
---|
640 | |
---|
641 | public override void VisitComment(Comment comment) |
---|
642 | { |
---|
643 | if (comment.CommentType == CommentType.InactiveCode) { |
---|
644 | Colorize(comment, inactiveCodeColor); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | public override void VisitPreProcessorDirective(PreProcessorDirective preProcessorDirective) |
---|
649 | { |
---|
650 | } |
---|
651 | |
---|
652 | public override void VisitAttribute(ICSharpCode.NRefactory.CSharp.Attribute attribute) |
---|
653 | { |
---|
654 | ITypeDefinition attrDef = resolver.Resolve(attribute.Type, cancellationToken).Type.GetDefinition(); |
---|
655 | if (attrDef != null && IsInactiveConditional(attrDef.Attributes)) { |
---|
656 | Colorize(attribute, inactiveCodeColor); |
---|
657 | } else { |
---|
658 | VisitChildren(attribute); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | public override void VisitArrayInitializerExpression (ArrayInitializerExpression arrayInitializerExpression) |
---|
663 | { |
---|
664 | foreach (var a in arrayInitializerExpression.Elements) { |
---|
665 | var namedElement = a as NamedExpression; |
---|
666 | if (namedElement != null) { |
---|
667 | var result = resolver.Resolve (namedElement, cancellationToken); |
---|
668 | if (result.IsError) |
---|
669 | Colorize (namedElement.NameToken, syntaxErrorColor); |
---|
670 | namedElement.Expression.AcceptVisitor (this); |
---|
671 | } else { |
---|
672 | a.AcceptVisitor (this); |
---|
673 | } |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | int blockDepth; |
---|
678 | public override void VisitBlockStatement(BlockStatement blockStatement) |
---|
679 | { |
---|
680 | blockDepth++; |
---|
681 | base.VisitBlockStatement(blockStatement); |
---|
682 | blockDepth--; |
---|
683 | } |
---|
684 | } |
---|
685 | } |
---|