From e51750a232dce2090f19f34467ee9c3ebaf65de2 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 22 Feb 2013 18:15:58 +0100 Subject: [PATCH] Use SemanticHighlightingVisitor in CSharpSemanticHighlighter. --- .../Project/Src/CSharpSemanticHighlighter.cs | 291 +----------------- .../Analysis/SemanticHighlightingVisitor.cs | 1 - 2 files changed, 9 insertions(+), 283 deletions(-) diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs index 053883c836..1dc17f8100 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs @@ -5,39 +5,25 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Windows.Threading; -using CSharpBinding.Parser; using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Highlighting; -using ICSharpCode.AvalonEdit.Rendering; using ICSharpCode.Core; using ICSharpCode.NRefactory; -using ICSharpCode.NRefactory.CSharp; -using ICSharpCode.NRefactory.CSharp.Resolver; -using ICSharpCode.NRefactory.CSharp.TypeSystem; +using ICSharpCode.NRefactory.CSharp.Analysis; using ICSharpCode.NRefactory.Editor; -using ICSharpCode.NRefactory.Semantics; using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Parser; +using CSharpBinding.Parser; namespace CSharpBinding { /// /// Semantic highlighting for C#. /// - public class CSharpSemanticHighlighter : DepthFirstAstVisitor, IHighlighter + public class CSharpSemanticHighlighter : SemanticHighlightingVisitor, IHighlighter { readonly IDocument document; - readonly HighlightingColor defaultTextColor; - readonly HighlightingColor referenceTypeColor; - readonly HighlightingColor valueTypeColor; - readonly HighlightingColor methodCallColor; - readonly HighlightingColor fieldAccessColor; - readonly HighlightingColor valueKeywordColor; - readonly HighlightingColor parameterModifierColor; - readonly HighlightingColor inactiveCodeColor; List invalidLines; List cachedLines; @@ -48,11 +34,8 @@ namespace CSharpBinding int lineNumber; HighlightedLine line; - CSharpAstResolver resolver; CSharpFullParseInformation parseInfo; - bool isInAccessor; - #region Constructor + Dispose public CSharpSemanticHighlighter(IDocument document) { @@ -61,7 +44,7 @@ namespace CSharpBinding this.document = document; var highlighting = HighlightingManager.Instance.GetDefinition("C#"); - this.defaultTextColor = highlighting.GetNamedColor("DefaultTextAndBackground"); + //this.defaultTextColor = ???; this.referenceTypeColor = highlighting.GetNamedColor("ReferenceTypes"); this.valueTypeColor = highlighting.GetNamedColor("ValueTypes"); this.methodCallColor = highlighting.GetNamedColor("MethodCall"); @@ -281,6 +264,8 @@ namespace CSharpBinding line = new HighlightedLine(document, documentLine); this.lineNumber = lineNumber; + this.regionStart = new TextLocation(lineNumber, 1); + this.regionEnd = new TextLocation(lineNumber, 1 + document.GetLineByNumber(lineNumber).Length); if (Debugger.IsAttached) { parseInfo.SyntaxTree.AcceptVisitor(this); } else { @@ -298,9 +283,9 @@ namespace CSharpBinding return line; } - public HighlightingColor DefaultTextColor { + HighlightingColor IHighlighter.DefaultTextColor { get { - return defaultTextColor; + return null; } } @@ -333,30 +318,7 @@ namespace CSharpBinding #endregion #region Colorize - HighlightingColor GetColor(ResolveResult rr) - { - if (rr is TypeResolveResult) { - if (rr.Type.IsReferenceType == false) - return valueTypeColor; - else - return referenceTypeColor; - } - MemberResolveResult mrr = rr as MemberResolveResult; - if (mrr != null) { - if (mrr.Member is IField) - return fieldAccessColor; - } - return null; - } - - void Colorize(AstNode node, HighlightingColor color) - { - if (node.IsNull || color == null) - return; - Colorize(node.StartLocation, node.EndLocation, color); - } - - void Colorize(TextLocation start, TextLocation end, HighlightingColor color) + protected override void Colorize(TextLocation start, TextLocation end, HighlightingColor color) { if (color == null) return; @@ -377,240 +339,5 @@ namespace CSharpBinding } } #endregion - - #region AST Traversal - protected override void VisitChildren(AstNode node) - { - for (var child = node.FirstChild; child != null; child = child.NextSibling) { - if (child.StartLocation.Line <= lineNumber && child.EndLocation.Line >= lineNumber) - child.AcceptVisitor(this); - } - } - - public override void VisitSimpleType(SimpleType simpleType) - { - Colorize(simpleType.IdentifierToken, GetColor(resolver.Resolve(simpleType))); - simpleType.TypeArguments.AcceptVisitor(this); - } - - public override void VisitMemberType(MemberType memberType) - { - // Ensure we visit/colorize the children in the correct order. - // This is required so that the resulting HighlightedSections are sorted correctly. - memberType.Target.AcceptVisitor(this); - Colorize(memberType.MemberNameToken, GetColor(resolver.Resolve(memberType))); - memberType.TypeArguments.AcceptVisitor(this); - } - - public override void VisitIdentifierExpression(IdentifierExpression identifierExpression) - { - Identifier ident = identifierExpression.GetChildByRole(Roles.Identifier); - if (isInAccessor && identifierExpression.Identifier == "value") { - Colorize(ident, valueKeywordColor); - } else { - ResolveResult rr = resolver.Resolve(identifierExpression); - Colorize(ident, GetColor(rr)); - } - - identifierExpression.TypeArguments.AcceptVisitor(this); - } - - public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression) - { - memberReferenceExpression.Target.AcceptVisitor(this); - - ResolveResult rr = resolver.Resolve(memberReferenceExpression); - Colorize(memberReferenceExpression.MemberNameToken, GetColor(rr)); - - memberReferenceExpression.TypeArguments.AcceptVisitor(this); - } - - public override void VisitInvocationExpression(InvocationExpression invocationExpression) - { - Expression target = invocationExpression.Target; - if (target is IdentifierExpression || target is MemberReferenceExpression || target is PointerReferenceExpression) { - var invocationRR = resolver.Resolve(invocationExpression) as CSharpInvocationResolveResult; - if (invocationRR != null && IsInactiveConditionalMethod(invocationRR.Member)) { - // mark the whole invocation expression as inactive code - Colorize(invocationExpression, inactiveCodeColor); - return; - } - - // apply color to target's target - target.GetChildByRole(Roles.TargetExpression).AcceptVisitor(this); - - // highlight the method call - var identifier = target.GetChildByRole(Roles.Identifier); - if (invocationRR != null && !invocationRR.IsDelegateInvocation) { - Colorize(identifier, methodCallColor); - } else { - ResolveResult targetRR = resolver.Resolve(target); - Colorize(identifier, GetColor(targetRR)); - } - - target.GetChildrenByRole(Roles.TypeArgument).AcceptVisitor(this); - } else { - target.AcceptVisitor(this); - } - // Visit arguments and comments within the arguments: - for (AstNode child = target.NextSibling; child != null; child = child.NextSibling) { - child.AcceptVisitor(this); - } - } - - bool IsInactiveConditionalMethod(IParameterizedMember member) - { - if (member.EntityType != EntityType.Method || member.ReturnType.Kind != TypeKind.Void) - return false; - while (member.IsOverride) - member = (IParameterizedMember)InheritanceHelper.GetBaseMember(member); - return IsInactiveConditional(member.Attributes); - } - - bool IsInactiveConditional(IList attributes) - { - bool hasConditionalAttribute = false; - foreach (var attr in attributes) { - if (attr.AttributeType.Name == "ConditionalAttribute" && attr.AttributeType.Namespace == "System.Diagnostics" && attr.PositionalArguments.Count == 1) { - string symbol = attr.PositionalArguments[0].ConstantValue as string; - if (symbol != null) { - hasConditionalAttribute = true; - var cu = this.resolver.RootNode as SyntaxTree; - if (cu != null) { - if (cu.ConditionalSymbols.Contains(symbol)) - return false; // conditional is active - } - } - } - } - return hasConditionalAttribute; - } - - public override void VisitAccessor(Accessor accessor) - { - isInAccessor = true; - try { - base.VisitAccessor(accessor); - } finally { - isInAccessor = false; - } - } - - public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) - { - for (AstNode child = methodDeclaration.FirstChild; child != null; child = child.NextSibling) { - if (child.StartLocation.Line <= lineNumber && child.EndLocation.Line >= lineNumber) { - if (child.Role == Roles.Identifier) { - // child == methodDeclaration.NameToken - Colorize(child, methodCallColor); - } else { - child.AcceptVisitor(this); - } - } - } - } - - public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration) - { - HandleConstructorOrDestructor(constructorDeclaration); - } - - public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration) - { - HandleConstructorOrDestructor(destructorDeclaration); - } - - void HandleConstructorOrDestructor(AstNode constructorDeclaration) - { - for (AstNode child = constructorDeclaration.FirstChild; child != null; child = child.NextSibling) { - if (child.StartLocation.Line <= lineNumber && child.EndLocation.Line >= lineNumber) { - if (child.Role == Roles.Identifier) { - // child == constructorDeclaration.NameToken - var currentTypeDef = resolver.GetResolverStateBefore(constructorDeclaration).CurrentTypeDefinition; - if (currentTypeDef != null && ((Identifier)child).Name == currentTypeDef.Name) { - if (currentTypeDef.IsReferenceType == true) - Colorize(child, referenceTypeColor); - else if (currentTypeDef.IsReferenceType == false) - Colorize(child, valueTypeColor); - } - } else { - child.AcceptVisitor(this); - } - } - } - } - - public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) - { - // Type declarations often contain #if directives, so we must make sure - // to also visit the comments. - for (AstNode child = typeDeclaration.FirstChild; child != null; child = child.NextSibling) { - if (child.StartLocation.Line <= lineNumber && child.EndLocation.Line >= lineNumber) { - if (child.Role == Roles.Identifier) { - // child == typeDeclaration.NameToken - if (typeDeclaration.ClassType == ClassType.Enum || typeDeclaration.ClassType == ClassType.Struct) - Colorize(typeDeclaration.NameToken, valueTypeColor); - else - Colorize(typeDeclaration.NameToken, referenceTypeColor); - } else { - child.AcceptVisitor(this); - } - } - } - } - - public override void VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration) - { - typeParameterDeclaration.Attributes.AcceptVisitor(this); - - if (typeParameterDeclaration.Variance == VarianceModifier.Contravariant) - Colorize(typeParameterDeclaration.VarianceToken, parameterModifierColor); - - bool isValueType = false; - if (typeParameterDeclaration.Parent != null) { - foreach (var constraint in typeParameterDeclaration.Parent.GetChildrenByRole(Roles.Constraint)) { - if (constraint.TypeParameter.Identifier == typeParameterDeclaration.Name) { - isValueType = constraint.BaseTypes.OfType().Any(p => p.Keyword == "struct"); - } - } - } - Colorize(typeParameterDeclaration.NameToken, isValueType ? valueTypeColor : referenceTypeColor); - } - - public override void VisitConstraint(Constraint constraint) - { - if (constraint.Parent != null && constraint.Parent.GetChildrenByRole(Roles.TypeParameter).Any(tp => tp.Name == constraint.TypeParameter.Identifier)) { - bool isValueType = constraint.BaseTypes.OfType().Any(p => p.Keyword == "struct"); - Colorize(constraint.GetChildByRole(Roles.Identifier), isValueType ? valueTypeColor : referenceTypeColor); - } - - constraint.BaseTypes.AcceptVisitor(this); - } - - public override void VisitVariableInitializer(VariableInitializer variableInitializer) - { - if (variableInitializer.Parent is FieldDeclaration) { - Colorize(variableInitializer.NameToken, fieldAccessColor); - } - variableInitializer.Initializer.AcceptVisitor(this); - } - - public override void VisitComment(Comment comment) - { - if (comment.CommentType == CommentType.InactiveCode) { - Colorize(comment, inactiveCodeColor); - } - } - - public override void VisitAttribute(ICSharpCode.NRefactory.CSharp.Attribute attribute) - { - ITypeDefinition attrDef = resolver.Resolve(attribute.Type).Type.GetDefinition(); - if (attrDef != null && IsInactiveConditional(attrDef.Attributes)) { - Colorize(attribute, inactiveCodeColor); - return; - } - VisitChildren(attribute); - } - #endregion } } diff --git a/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Analysis/SemanticHighlightingVisitor.cs b/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Analysis/SemanticHighlightingVisitor.cs index 5dee09f2f0..a2f0d60f3b 100644 --- a/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Analysis/SemanticHighlightingVisitor.cs +++ b/src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Analysis/SemanticHighlightingVisitor.cs @@ -201,7 +201,6 @@ namespace ICSharpCode.NRefactory.CSharp.Analysis VisitChildrenUntil(memberReferenceExpression, memberNameToken); ResolveResult rr = resolver.Resolve(memberReferenceExpression); Colorize(memberNameToken, rr); - VisitChildren(memberReferenceExpression); VisitChildrenAfter(memberReferenceExpression, memberNameToken); }