using System.Collections.Immutable; using Microsoft.CodeAnalysis; namespace ExtractMethod.Tooling; /// /// Compact symbol display for the suggestion report. The defaults are wrong /// for a report a human reads next to their own source: /// - ToDisplayString() fully qualifies every namespace /// (System.Collections.Generic.List<Widget>), /// - ToMinimalDisplayString() keeps containing types AND prefixes /// members with their own type (int Demo._seed). /// The report wants the shortest form that stays readable in context: /// types as bare names (Widget, List<Widget>), members as /// ContainingType.Member with parameter types. /// internal static class Displays { /// /// Type display for suggestion lines: shortest unambiguous-enough form /// (no namespaces, no containing types, keyword spellings for special /// types, nullable annotations kept). Built explicitly because /// MinimallyQualifiedFormat keeps containing types for nested types /// (Demo.Widget) and has no With-style override here. /// internal static readonly SymbolDisplayFormat TypeFormat = new( globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance, miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes | SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers | SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier); /// Compact member display for extract-first entries: /// Demo._seed, Demo.Scale, Widget.Bigger(Widget), /// List<Widget>.this[int]. internal static string Member(ISymbol symbol) => symbol switch { IMethodSymbol method => $"{Type(method.ContainingType)}.{method.Name}({Parameters(method.Parameters)})", IPropertySymbol { IsIndexer: true } indexer => $"{Type(indexer.ContainingType)}.this[{Parameters(indexer.Parameters)}]", IFieldSymbol or IPropertySymbol or IEventSymbol => $"{Type(symbol.ContainingType)}.{symbol.Name}", _ => symbol.ToDisplayString(TypeFormat), }; private static string Parameters(ImmutableArray parameters) => string.Join(", ", parameters.Select(p => Type(p.Type))); private static string Type(ITypeSymbol? type) => type?.ToDisplayString(TypeFormat) ?? "?"; }