public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var diagnostic = context.Diagnostics.First(); var serializedNamingStyle = diagnostic.Properties[nameof(NamingStyle)]; var style = NamingStyle.FromXElement(XElement.Parse(serializedNamingStyle)); var document = context.Document; var span = context.Span; var root = await document .GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false); var node = root.FindNode(span); if (document.GetLanguageService <ISyntaxFactsService>().IsIdentifierName(node)) { // The location we get from the analyzer only contains the identifier token and when we get its containing node, // it is usually the right one (such as a variable declarator, designation or a foreach statement) // because there is no other node in between. But there is one case in a VB catch clause where the token // is wrapped in an identifier name. So if what we found is an identifier, take the parent node instead. // Note that this is the correct thing to do because GetDeclaredSymbol never works on identifier names. node = node.Parent; } var model = await document .GetSemanticModelAsync(context.CancellationToken) .ConfigureAwait(false); var symbol = model.GetDeclaredSymbol(node, context.CancellationToken); // TODO: We should always be able to find the symbol that generated this diagnostic, // but this cannot always be done by simply asking for the declared symbol on the node // from the symbol's declaration location. // See https://github.com/dotnet/roslyn/issues/16588 if (symbol == null) { return; } var fixedNames = style.MakeCompliant(symbol.Name); foreach (var fixedName in fixedNames) { context.RegisterCodeFix( new FixNameCodeAction( #if !CODE_STYLE document.Project.Solution, symbol, fixedName, #endif string.Format(CodeFixesResources.Fix_Name_Violation_colon_0, fixedName), c => FixAsync(document, symbol, fixedName, c), equivalenceKey: nameof(NamingStyleCodeFixProvider) ), diagnostic ); } }
public NamingRule(string name, SymbolSpecification symbolSpecification, NamingStyle namingStyle, ReportDiagnostic enforcementLevel) { Name = name; SymbolSpecification = symbolSpecification; NamingStyle = namingStyle; EnforcementLevel = enforcementLevel; }
private static NamingStylePreferences ConstantsAreUpperCaseOption() { var symbolSpecification = new SymbolSpecification( null, "Name", ImmutableArray.Create( new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Field), new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Local)), ImmutableArray <Accessibility> .Empty, ImmutableArray.Create(new SymbolSpecification.ModifierKind(SymbolSpecification.ModifierKindEnum.IsConst))); var namingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.AllUpper, name: "Name", prefix: "", suffix: "", wordSeparator: ""); var namingRule = new SerializableNamingRule() { SymbolSpecificationID = symbolSpecification.ID, NamingStyleID = namingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var info = new NamingStylePreferences( ImmutableArray.Create(symbolSpecification), ImmutableArray.Create(namingStyle), ImmutableArray.Create(namingRule)); return(info); }
private NamingStylePreferences CreateCustomStaticFieldNamingStylePreference() { var symbolSpecification = new SymbolSpecification( null, "Name", ImmutableArray.Create(new SymbolKindOrTypeKind(SymbolKind.Field)), ImmutableArray <Accessibility> .Empty, ImmutableArray.Create(new ModifierKind(DeclarationModifiers.Static))); var namingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.PascalCase, name: "CustomStaticFieldTest", prefix: "staticfieldtest", suffix: "", wordSeparator: ""); var namingRule = new SerializableNamingRule() { SymbolSpecificationID = symbolSpecification.ID, NamingStyleID = namingStyle.ID, EnforcementLevel = ReportDiagnostic.Error }; var info = new NamingStylePreferences( ImmutableArray.Create(symbolSpecification), ImmutableArray.Create(namingStyle), ImmutableArray.Create(namingRule)); return(info); }
private static bool TryGetNamingStyleData( string namingRuleName, IReadOnlyDictionary <string, object> allRawConventions, out NamingStyle namingStyle) { namingStyle = default; if (!TryGetNamingStyleTitle(namingRuleName, allRawConventions, out string namingStyleTitle)) { return(false); } var requiredPrefix = GetNamingRequiredPrefix(namingStyleTitle, allRawConventions); var requiredSuffix = GetNamingRequiredSuffix(namingStyleTitle, allRawConventions); var wordSeparator = GetNamingWordSeparator(namingStyleTitle, allRawConventions); if (!TryGetNamingCapitalization(namingStyleTitle, allRawConventions, out var capitalization)) { return(false); } namingStyle = new NamingStyle( Guid.NewGuid(), name: namingStyleTitle, prefix: requiredPrefix, suffix: requiredSuffix, wordSeparator: wordSeparator, capitalizationScheme: capitalization); return(true); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var diagnostic = context.Diagnostics.First(); var serializedNamingStyle = diagnostic.Properties[nameof(NamingStyle)]; var style = NamingStyle.FromXElement(XElement.Parse(serializedNamingStyle)); var document = context.Document; var span = context.Span; var root = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var node = root.FindNode(span); var model = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); var symbol = model.GetDeclaredSymbol(node, context.CancellationToken); var fixedNames = style.MakeCompliant(symbol.Name); foreach (var fixedName in fixedNames) { var solution = context.Document.Project.Solution; context.RegisterCodeFix( new FixNameCodeAction( string.Format(FeaturesResources.FixNamingViolation, fixedName), async c => await Renamer.RenameSymbolAsync( solution, symbol, fixedName, document.Options, c).ConfigureAwait(false), nameof(AbstractNamingStyleCodeFixProvider)), diagnostic); } }
public NamingConvention(LayerConfig owner, XmlNode node) : this(owner) { this.style = ParseStyle(Utils.Xml.GetAttrValue(node, "style")); this.maxLength = Utils.Xml.GetAttrValue(node, "maxLength", this.maxLength); namingConventionParams = new NamingConventionParams(this, owner.GenieLampConfig.Lamp.QueryNode(node, "./{0}:Param")); }
private string MethodNamesArePascalCaseOptionString() { var symbolSpecification = new SymbolSpecification( Guid.NewGuid(), "Name", SpecializedCollections.SingletonEnumerable(new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Method)).ToList(), SpecializedCollections.EmptyList <SymbolSpecification.AccessibilityKind>(), SpecializedCollections.EmptyList <SymbolSpecification.ModifierKind>()); var namingStyle = new NamingStyle(); namingStyle.CapitalizationScheme = Capitalization.PascalCase; namingStyle.Name = "Name"; namingStyle.Prefix = ""; namingStyle.Suffix = ""; namingStyle.WordSeparator = ""; var namingRule = new SerializableNamingRule(); namingRule.SymbolSpecificationID = symbolSpecification.ID; namingRule.NamingStyleID = namingStyle.ID; namingRule.EnforcementLevel = DiagnosticSeverity.Error; var info = new SerializableNamingStylePreferencesInfo(); info.SymbolSpecifications.Add(symbolSpecification); info.NamingStyles.Add(namingStyle); info.NamingRules.Add(namingRule); return(info.CreateXElement().ToString()); }
private NamingStylePreferences MethodNamesArePascalCaseOption() { var symbolSpecification = new SymbolSpecification( "Name", SpecializedCollections.SingletonEnumerable(new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Method)).ToList(), SpecializedCollections.EmptyList <Accessibility>(), SpecializedCollections.EmptyList <SymbolSpecification.ModifierKind>()); var namingStyle = new NamingStyle() { CapitalizationScheme = Capitalization.PascalCase, Name = "Name", Prefix = "", Suffix = "", WordSeparator = "" }; var namingRule = new SerializableNamingRule() { SymbolSpecificationID = symbolSpecification.ID, NamingStyleID = namingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var info = new NamingStylePreferences(); info.SymbolSpecifications.Add(symbolSpecification); info.NamingStyles.Add(namingStyle); info.NamingRules.Add(namingRule); return(info); }
private NamingStylePreferences MethodNamesArePascalCaseOption() { var symbolSpecification = new SymbolSpecification( null, "Name", ImmutableArray.Create(new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Method)), ImmutableArray <Accessibility> .Empty, ImmutableArray <SymbolSpecification.ModifierKind> .Empty); var namingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.PascalCase, name: "Name", prefix: "", suffix: "", wordSeparator: ""); var namingRule = new SerializableNamingRule() { SymbolSpecificationID = symbolSpecification.ID, NamingStyleID = namingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var info = new NamingStylePreferences( ImmutableArray.Create(symbolSpecification), ImmutableArray.Create(namingStyle), ImmutableArray.Create(namingRule)); return(info); }
private static NamingStylePreferences AsyncFunctionNamesEndWithAsyncOption() { var symbolSpecification = new SymbolSpecification( null, "Name", ImmutableArray.Create( new SymbolSpecification.SymbolKindOrTypeKind(MethodKind.Ordinary), new SymbolSpecification.SymbolKindOrTypeKind(MethodKind.LocalFunction)), ImmutableArray <Accessibility> .Empty, ImmutableArray.Create(new SymbolSpecification.ModifierKind(SymbolSpecification.ModifierKindEnum.IsAsync))); var namingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.PascalCase, name: "Name", prefix: "", suffix: "Async", wordSeparator: ""); var namingRule = new SerializableNamingRule() { SymbolSpecificationID = symbolSpecification.ID, NamingStyleID = namingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var info = new NamingStylePreferences( ImmutableArray.Create(symbolSpecification), ImmutableArray.Create(namingStyle), ImmutableArray.Create(namingRule)); return(info); }
public NameSuffixLinkedTypesProvider(ISolution solution, ISettingsStore settingsStore, ISettingsOptimization settingsOptimization) { var contextBoundSettingsStore = settingsStore.BindToContextTransient(ContextRange.Smart(solution.ToDataContext())); var settings = contextBoundSettingsStore.GetKey <TestLinkerSettings>(settingsOptimization); _namingStyle = settings.NamingStyle; _namingSuffixes = settings.NamingSuffixes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => Pair.Of(x, x.Length)) .ToList(); }
public void AddItem() { var style = new NamingStyle(); var viewModel = new NamingStyleViewModel(style, canBeDeleted: true, notificationService: _notificationService); var dialog = new NamingStyleDialog(viewModel); if (dialog.ShowDialog().Value == true) { Items.Add(viewModel); } }
private static NamingStylePreferences LocalsAreCamelCaseConstantsAreUpperCaseOption() { var localsSymbolSpecification = new SymbolSpecification( null, "Locals", ImmutableArray.Create(new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Local)), ImmutableArray <Accessibility> .Empty, ImmutableArray <SymbolSpecification.ModifierKind> .Empty); var constLocalsSymbolSpecification = new SymbolSpecification( null, "Const Locals", ImmutableArray.Create(new SymbolSpecification.SymbolKindOrTypeKind(SymbolKind.Local)), ImmutableArray <Accessibility> .Empty, ImmutableArray.Create(new SymbolSpecification.ModifierKind(SymbolSpecification.ModifierKindEnum.IsConst))); var camelCaseNamingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.CamelCase, name: "Camel Case", prefix: "", suffix: "", wordSeparator: ""); var allUpperNamingStyle = new NamingStyle( Guid.NewGuid(), capitalizationScheme: Capitalization.AllUpper, name: "All Upper", prefix: "", suffix: "", wordSeparator: ""); var localsCamelCaseNamingRule = new SerializableNamingRule() { SymbolSpecificationID = localsSymbolSpecification.ID, NamingStyleID = camelCaseNamingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var constLocalsUpperCaseNamingRule = new SerializableNamingRule() { SymbolSpecificationID = constLocalsSymbolSpecification.ID, NamingStyleID = allUpperNamingStyle.ID, EnforcementLevel = DiagnosticSeverity.Error }; var info = new NamingStylePreferences( ImmutableArray.Create(localsSymbolSpecification, constLocalsSymbolSpecification), ImmutableArray.Create(camelCaseNamingStyle, allUpperNamingStyle), ImmutableArray.Create(constLocalsUpperCaseNamingRule, localsCamelCaseNamingRule)); return(info); }
private static NamingStylePreferences NamesEndWithSuffixPreferences() { var specificationStyles = new[] { SpecificationStyle(new SymbolKindOrTypeKind(SymbolKind.Field), "Field"), SpecificationStyle(new SymbolKindOrTypeKind(SymbolKind.Property), "Property"), SpecificationStyle(new SymbolKindOrTypeKind(MethodKind.Ordinary), "Method"), SpecificationStyle(new SymbolKindOrTypeKind(SymbolKind.Local), "Local"), SpecificationStyle(new SymbolKindOrTypeKind(MethodKind.LocalFunction), "LocalFunction"), }; return(new NamingStylePreferences( specificationStyles.Select(t => t.specification).ToImmutableArray(), specificationStyles.Select(t => t.style).ToImmutableArray(), specificationStyles.Select(t => CreateRule(t.specification, t.style)).ToImmutableArray())); // Local functions (SymbolSpecification specification, NamingStyle style) SpecificationStyle(SymbolKindOrTypeKind kind, string suffix) { var symbolSpecification = new SymbolSpecification( id: null, symbolSpecName: suffix, ImmutableArray.Create(kind), ImmutableArray <Accessibility> .Empty, ImmutableArray <ModifierKind> .Empty); var namingStyle = new NamingStyle( Guid.NewGuid(), name: suffix, capitalizationScheme: Capitalization.PascalCase, prefix: "", suffix: suffix, wordSeparator: ""); return(symbolSpecification, namingStyle); } SerializableNamingRule CreateRule(SymbolSpecification specification, NamingStyle style) { return(new SerializableNamingRule() { SymbolSpecificationID = specification.ID, NamingStyleID = style.ID, EnforcementLevel = ReportDiagnostic.Error }); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var diagnostic = context.Diagnostics.First(); var serializedNamingStyle = diagnostic.Properties[nameof(NamingStyle)]; var style = NamingStyle.FromXElement(XElement.Parse(serializedNamingStyle)); var document = context.Document; var span = context.Span; var root = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var node = root.FindNode(span); var model = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); var symbol = model.GetDeclaredSymbol(node, context.CancellationToken); // TODO: We should always be able to find the symbol that generated this diagnostic, // but this cannot always be done by simply asking for the declared symbol on the node // from the symbol's declaration location. // See https://github.com/dotnet/roslyn/issues/16588 if (symbol == null) { return; } var fixedNames = style.MakeCompliant(symbol.Name); foreach (var fixedName in fixedNames) { var solution = context.Document.Project.Solution; context.RegisterCodeFix( new FixNameCodeAction( solution, symbol, fixedName, string.Format(FeaturesResources.Fix_Name_Violation_colon_0, fixedName), c => FixAsync(document, symbol, fixedName, c), equivalenceKey: nameof(NamingStyleCodeFixProvider)), diagnostic); } }
/// <summary> 转换到对应的命名样式 </summary> public static string ToNamingStyle(this string name, NamingStyle style) { if (!name.IsValidName()) { Console.WriteLine("Invalid name: {0}", name); return(null); } char[] nameCharArr = name.ToCharArray(); int nameCharCount = nameCharArr.Length; switch (style) { case NamingStyle.lowerCamel: for (int j = 0; j < nameCharCount; j++) { if (nameCharArr[j] < 'A' || nameCharArr[j] > 'Z') { break; } nameCharArr[j] = (char)(nameCharArr[j] + 32); } break; case NamingStyle.UpperCamel: if (nameCharArr[0] >= 'a' && nameCharArr[0] <= 'z') { nameCharArr[0] = (char)(nameCharArr[0] - 32); } break; case NamingStyle.all_lower: // TODO break; case NamingStyle.ALL_UPPER: // TODO break; default: throw new ArgumentOutOfRangeException(nameof(style), style, null); } return(new string(nameCharArr)); }
public NamingStyleViewModel(NamingStyle style, bool canBeDeleted, INotificationService notificationService) { _notificationService = notificationService; _style = style; ID = style.ID; RequiredPrefix = style.Prefix; RequiredSuffix = style.Suffix; WordSeparator = style.WordSeparator; ItemName = style.Name; CanBeDeleted = canBeDeleted; CapitalizationSchemes = new List <CapitalizationDisplay> { new CapitalizationDisplay(Capitalization.PascalCase, ServicesVSResources.Pascal_Case_Name), new CapitalizationDisplay(Capitalization.CamelCase, ServicesVSResources.camel_Case_Name), new CapitalizationDisplay(Capitalization.FirstUpper, ServicesVSResources.First_word_upper), new CapitalizationDisplay(Capitalization.AllUpper, ServicesVSResources.ALL_UPPER), new CapitalizationDisplay(Capitalization.AllLower, ServicesVSResources.all_lower) }; CapitalizationSchemeIndex = CapitalizationSchemes.IndexOf(CapitalizationSchemes.Single(s => s.Capitalization == style.CapitalizationScheme)); }
public NamingStyleViewModel(NamingStyle style, INotificationService notificationService) { _notificationService = notificationService; _style = style; this.ID = style.ID; this.RequiredPrefix = style.Prefix; this.RequiredSuffix = style.Suffix; this.WordSeparator = style.WordSeparator; this.FirstWordGroupCapitalization = (int)style.CapitalizationScheme; this.NamingConventionName = style.Name; CapitalizationSchemes = new List <CapitalizationDisplay> { new CapitalizationDisplay(Capitalization.PascalCase, ServicesVSResources.CapitalizationStyleExample_PascalCase), new CapitalizationDisplay(Capitalization.CamelCase, ServicesVSResources.CapitalizationStyleExample_CamelCase), new CapitalizationDisplay(Capitalization.FirstUpper, ServicesVSResources.CapitalizationStyleExample_FirstWordUpper), new CapitalizationDisplay(Capitalization.AllUpper, ServicesVSResources.CapitalizationStyleExample_AllUpper), new CapitalizationDisplay(Capitalization.AllLower, ServicesVSResources.CapitalizationStyleExample_AllLower) }; CapitalizationSchemeIndex = CapitalizationSchemes.IndexOf(CapitalizationSchemes.Single(s => s.Capitalization == style.CapitalizationScheme)); }
private static bool TryGetSerializableNamingRule( string namingRuleTitle, SymbolSpecification symbolSpec, NamingStyle namingStyle, IReadOnlyDictionary <string, object> conventionsDictionary, out SerializableNamingRule serializableNamingRule) { if (!TryGetRuleSeverity(namingRuleTitle, conventionsDictionary, out var severity)) { serializableNamingRule = null; return(false); } serializableNamingRule = new SerializableNamingRule() { EnforcementLevel = severity, NamingStyleID = namingStyle.ID, SymbolSpecificationID = symbolSpec.ID }; return(true); }
private static string RemovePrefixesAndSuffixes(ISymbol symbol, ImmutableArray <NamingRule> rules, string baseName) { var newBaseName = baseName; foreach (var rule in rules) { if (rule.SymbolSpecification.AppliesTo(symbol)) { // remove specified prefix var prefix = rule.NamingStyle.Prefix; newBaseName = newBaseName.StartsWith(prefix) ? newBaseName.Substring(prefix.Length) : newBaseName; // remove specified suffix var suffix = rule.NamingStyle.Suffix; newBaseName = newBaseName.EndsWith(suffix) ? newBaseName.Substring(0, newBaseName.Length - suffix.Length) : newBaseName; break; } } // remove any common prefixes newBaseName = NamingStyle.StripCommonPrefixes(newBaseName, out var _); // If no changes were made to the basename passed in, we're done if (newBaseName == baseName) { return(baseName); } // otherwise, see if any other prefixes exist return(RemovePrefixesAndSuffixes(symbol, rules, newBaseName)); }
static bool AreSameNamingStyle(NamingStyle left, NamingStyle right) => left.Prefix == right.Prefix && left.Suffix == right.Suffix && left.WordSeparator == right.WordSeparator && left.CapitalizationScheme == right.CapitalizationScheme;
public INamingConvention GetNamingConvention(NamingStyle style) { return(new NamingConvention(style)); }
internal NamingConvention(string styleStr) { style = ParseStyle(styleStr); }
internal NamingConvention(NamingStyle style) { this.style = style; }
public string RandomName(NamingStyle style) { //Not many styles here so just return from The One List (to rule them all) return(nameList[random.Next(0, nameList.Count - 1)]); }