// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.NamingStyles;
namespace Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles
{
///
/// Contains all information related to Naming Style Preferences.
/// 1. Symbol Specifications
/// 2. Name Style
/// 3. Naming Rule (points to Symbol Specification IDs)
///
internal class NamingStylePreferences : IEquatable
{
private readonly static int s_serializationVersion = 3;
public readonly ImmutableArray SymbolSpecifications;
public readonly ImmutableArray NamingStyles;
public readonly ImmutableArray NamingRules;
private readonly Lazy _lazyRules;
internal NamingStylePreferences(
ImmutableArray symbolSpecifications,
ImmutableArray namingStyles,
ImmutableArray namingRules)
{
SymbolSpecifications = symbolSpecifications;
NamingStyles = namingStyles;
NamingRules = namingRules;
_lazyRules = new Lazy(CreateRules, isThreadSafe: true);
}
public static NamingStylePreferences Default => FromXElement(XElement.Parse(DefaultNamingPreferencesString));
public static string DefaultNamingPreferencesString => _defaultNamingPreferencesString;
internal NamingStyle GetNamingStyle(Guid namingStyleID)
=> NamingStyles.Single(s => s.ID == namingStyleID);
internal SymbolSpecification GetSymbolSpecification(Guid symbolSpecificationID)
=> SymbolSpecifications.Single(s => s.ID == symbolSpecificationID);
public NamingStyleRules Rules => _lazyRules.Value;
public NamingStyleRules CreateRules()
=> new NamingStyleRules(NamingRules.Select(r => r.GetRule(this)).ToImmutableArray());
internal XElement CreateXElement()
{
return new XElement("NamingPreferencesInfo",
new XAttribute("SerializationVersion", s_serializationVersion),
new XElement(nameof(SymbolSpecifications), SymbolSpecifications.Select(s => s.CreateXElement())),
new XElement(nameof(NamingStyles), NamingStyles.Select(n => n.CreateXElement())),
new XElement(nameof(NamingRules), NamingRules.Select(n => n.CreateXElement())));
}
internal static NamingStylePreferences FromXElement(XElement element)
{
var serializationVersion = int.Parse(element.Attribute("SerializationVersion").Value);
if (serializationVersion != s_serializationVersion)
{
element = XElement.Parse(DefaultNamingPreferencesString);
}
return new NamingStylePreferences(
element.Element(nameof(SymbolSpecifications)).Elements(nameof(SymbolSpecification))
.Select(SymbolSpecification.FromXElement).ToImmutableArray(),
element.Element(nameof(NamingStyles)).Elements(nameof(NamingStyle))
.Select(NamingStyle.FromXElement).ToImmutableArray(),
element.Element(nameof(NamingRules)).Elements(nameof(SerializableNamingRule))
.Select(SerializableNamingRule.FromXElement).ToImmutableArray());
}
public override bool Equals(object obj)
=> Equals(obj as NamingStylePreferences);
public bool Equals(NamingStylePreferences other)
{
return !ReferenceEquals(other, null) &&
CreateXElement().ToString() == other.CreateXElement().ToString();
}
public static bool operator ==(NamingStylePreferences left, NamingStylePreferences right)
=> left?.Equals(right) == true;
public static bool operator !=(NamingStylePreferences left, NamingStylePreferences right)
=> !(left == right);
public override int GetHashCode()
=> CreateXElement().ToString().GetHashCode();
private static readonly string _defaultNamingPreferencesString = $@"
Class
Public
Internal
Private
Protected
ProtectedOrInternal
Interface
Public
Internal
Private
Protected
ProtectedOrInternal
Struct
Public
Internal
Private
Protected
ProtectedOrInternal
Enum
Public
Internal
Private
Protected
ProtectedOrInternal
Delegate
Public
Internal
Private
Protected
ProtectedOrInternal
Event
Public
Internal
Private
Protected
ProtectedOrInternal
Method
Public
Method
Private
Method
Public
Internal
Private
Protected
ProtectedOrInternal
IsAbstract
Method
Public
Internal
Private
Protected
ProtectedOrInternal
IsStatic
Method
Public
Internal
Private
Protected
ProtectedOrInternal
IsAsync
Property
Public
Internal
Private
Protected
ProtectedOrInternal
Field
Public
Protected
Field
Public
Internal
Private
Protected
ProtectedOrInternal
IsStatic
Field
Internal
Private
Field
Internal
Private
IsStatic
Class
Struct
Interface
Enum
Public
Internal
Private
Protected
ProtectedOrInternal
Property
Method
Event
Public
Internal
Private
Protected
ProtectedOrInternal
";
}
}