|
| 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 | +#nullable disable |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Composition; |
| 10 | +using System.Threading; |
| 11 | +using Microsoft.CodeAnalysis; |
| 12 | +using Microsoft.CodeAnalysis.CSharp; |
| 13 | +using Microsoft.CodeAnalysis.CSharp.Symbols; |
| 14 | +using Microsoft.CodeAnalysis.Host.Mef; |
| 15 | +using Microsoft.CodeAnalysis.PooledObjects; |
| 16 | +using Microsoft.CodeAnalysis.Shared.Extensions; |
| 17 | +using Microsoft.VisualStudio.LanguageServices.Implementation.Progression; |
| 18 | + |
| 19 | +namespace Microsoft.VisualStudio.LanguageServices.CSharp.Progression; |
| 20 | + |
| 21 | +[ExportLanguageService(typeof(IProgressionLanguageService), LanguageNames.CSharp), Shared] |
| 22 | +internal sealed partial class CSharpProgressionLanguageService : IProgressionLanguageService |
| 23 | +{ |
| 24 | + private static readonly SymbolDisplayFormat s_descriptionFormat = new( |
| 25 | + globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.OmittedAsContaining, |
| 26 | + typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, |
| 27 | + genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, |
| 28 | + memberOptions: SymbolDisplayMemberOptions.IncludeParameters | |
| 29 | + SymbolDisplayMemberOptions.IncludeContainingType, |
| 30 | + parameterOptions: SymbolDisplayParameterOptions.IncludeType | |
| 31 | + SymbolDisplayParameterOptions.IncludeParamsRefOut | |
| 32 | + SymbolDisplayParameterOptions.IncludeOptionalBrackets, |
| 33 | + miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes); |
| 34 | + |
| 35 | + private static readonly SymbolDisplayFormat s_labelFormat = new( |
| 36 | + genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, |
| 37 | + memberOptions: SymbolDisplayMemberOptions.IncludeParameters | |
| 38 | + SymbolDisplayMemberOptions.IncludeExplicitInterface, |
| 39 | + parameterOptions: SymbolDisplayParameterOptions.IncludeType | |
| 40 | + SymbolDisplayParameterOptions.IncludeParamsRefOut | |
| 41 | + SymbolDisplayParameterOptions.IncludeOptionalBrackets, |
| 42 | + delegateStyle: SymbolDisplayDelegateStyle.NameAndParameters, |
| 43 | + miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes); |
| 44 | + |
| 45 | + [ImportingConstructor] |
| 46 | + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] |
| 47 | + public CSharpProgressionLanguageService() |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + public string GetDescriptionForSymbol(ISymbol symbol, bool includeContainingSymbol) |
| 52 | + => GetSymbolText(symbol, includeContainingSymbol, s_descriptionFormat); |
| 53 | + |
| 54 | + public string GetLabelForSymbol(ISymbol symbol, bool includeContainingSymbol) |
| 55 | + => GetSymbolText(symbol, includeContainingSymbol, s_labelFormat); |
| 56 | + |
| 57 | + private static string GetSymbolText(ISymbol symbol, bool includeContainingSymbol, SymbolDisplayFormat displayFormat) |
| 58 | + { |
| 59 | + var label = symbol.ToDisplayString(displayFormat); |
| 60 | + |
| 61 | + var typeToShow = GetType(symbol); |
| 62 | + |
| 63 | + if (typeToShow != null) |
| 64 | + { |
| 65 | + label += " : " + typeToShow.ToDisplayString(s_labelFormat); |
| 66 | + } |
| 67 | + |
| 68 | + if (includeContainingSymbol && symbol.ContainingSymbol != null) |
| 69 | + { |
| 70 | + label += " (" + symbol.ContainingSymbol.ToDisplayString(s_labelFormat) + ")"; |
| 71 | + } |
| 72 | + |
| 73 | + return label; |
| 74 | + } |
| 75 | + |
| 76 | + private static ITypeSymbol GetType(ISymbol symbol) |
| 77 | + { |
| 78 | + switch (symbol) |
| 79 | + { |
| 80 | + case IEventSymbol f: return f.Type; |
| 81 | + case IFieldSymbol f: return f.ContainingType.TypeKind == TypeKind.Enum ? null : f.Type; |
| 82 | + case IMethodSymbol m: return IncludeReturnType(m) ? m.ReturnType : null; |
| 83 | + case IPropertySymbol p: return p.Type; |
| 84 | + case INamedTypeSymbol n: return n.IsDelegateType() ? n.DelegateInvokeMethod.ReturnType : null; |
| 85 | + default: return null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static bool IncludeReturnType(IMethodSymbol f) |
| 90 | + => f.MethodKind is MethodKind.Ordinary or MethodKind.ExplicitInterfaceImplementation; |
| 91 | +} |
0 commit comments