|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Collections.Immutable; |
| 8 | +using System.Composition; |
| 9 | +using System.Linq; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
| 11 | +using Microsoft.CodeAnalysis.CSharp.Extensions; |
| 12 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | +using Microsoft.CodeAnalysis.Host.Mef; |
| 14 | +using Microsoft.CodeAnalysis.InheritanceMargin; |
| 15 | +using Roslyn.Utilities; |
| 16 | + |
| 17 | +namespace Microsoft.CodeAnalysis.Editor.CSharp.InheritanceMargin |
| 18 | +{ |
| 19 | + [ExportLanguageService(typeof(IInheritanceMarginService), LanguageNames.CSharp), Shared] |
| 20 | + internal class CSharpInheritanceMarginService : AbstractInheritanceMarginService |
| 21 | + { |
| 22 | + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 23 | + [ImportingConstructor] |
| 24 | + public CSharpInheritanceMarginService() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + protected override ImmutableArray<SyntaxNode> GetMembers(IEnumerable<SyntaxNode> nodesToSearch) |
| 29 | + { |
| 30 | + var typeDeclarationNodes = nodesToSearch.OfType<TypeDeclarationSyntax>(); |
| 31 | + |
| 32 | + using var _ = PooledObjects.ArrayBuilder<SyntaxNode>.GetInstance(out var builder); |
| 33 | + foreach (var typeDeclarationNode in typeDeclarationNodes) |
| 34 | + { |
| 35 | + // 1. Add the type declaration node.(e.g. class, struct etc..) |
| 36 | + // Use its identifier's position as the line number, since we want the margin to be placed with the identifier |
| 37 | + builder.Add(typeDeclarationNode); |
| 38 | + |
| 39 | + // 2. Add type members inside this type declaration. |
| 40 | + foreach (var member in typeDeclarationNode.Members) |
| 41 | + { |
| 42 | + if (member.IsKind( |
| 43 | + SyntaxKind.MethodDeclaration, |
| 44 | + SyntaxKind.PropertyDeclaration, |
| 45 | + SyntaxKind.EventDeclaration, |
| 46 | + SyntaxKind.IndexerDeclaration)) |
| 47 | + { |
| 48 | + builder.Add(member); |
| 49 | + } |
| 50 | + |
| 51 | + // For multiple events that declared in the same EventFieldDeclaration, |
| 52 | + // add all VariableDeclarators |
| 53 | + if (member is EventFieldDeclarationSyntax eventFieldDeclarationNode) |
| 54 | + { |
| 55 | + builder.AddRange(eventFieldDeclarationNode.Declaration.Variables); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return builder.ToImmutableArray(); |
| 61 | + } |
| 62 | + |
| 63 | + protected override SyntaxToken GetDeclarationToken(SyntaxNode declarationNode) |
| 64 | + => declarationNode switch |
| 65 | + { |
| 66 | + MethodDeclarationSyntax methodDeclarationNode => methodDeclarationNode.Identifier, |
| 67 | + PropertyDeclarationSyntax propertyDeclarationNode => propertyDeclarationNode.Identifier, |
| 68 | + EventDeclarationSyntax eventDeclarationNode => eventDeclarationNode.Identifier, |
| 69 | + VariableDeclaratorSyntax variableDeclaratorNode => variableDeclaratorNode.Identifier, |
| 70 | + TypeDeclarationSyntax baseTypeDeclarationNode => baseTypeDeclarationNode.Identifier, |
| 71 | + IndexerDeclarationSyntax indexerDeclarationNode => indexerDeclarationNode.ThisKeyword, |
| 72 | + // Shouldn't reach here since the input declaration nodes are coming from GetMembers() method above |
| 73 | + _ => throw ExceptionUtilities.UnexpectedValue(declarationNode), |
| 74 | + }; |
| 75 | + } |
| 76 | +} |
0 commit comments