private async Task <Document> ApplyRuleNameMultipleZeroAsync(Document document, INamedTypeSymbol enumType, CancellationToken cancellationToken) { // Diagnostic: Remove all members that have the value zero from '{0}' except for one member that is named 'None'. // Fix: Remove all members that have the value zero except for one member that is named 'None'. SymbolEditor editor = SymbolEditor.Create(document); bool needsNewZeroValuedNoneField = true; ISet <IFieldSymbol> set = EnumsShouldHaveZeroValueAnalyzer.GetZeroValuedFields(enumType).ToSet(); bool makeNextFieldExplicit = false; foreach (IFieldSymbol field in enumType.GetMembers().Where(m => m.Kind == SymbolKind.Field)) { bool isZeroValued = set.Contains(field); bool isZeroValuedNamedNone = isZeroValued && EnumsShouldHaveZeroValueAnalyzer.IsMemberNamedNone(field); if (!isZeroValued || isZeroValuedNamedNone) { if (makeNextFieldExplicit) { await editor.EditOneDeclarationAsync(field, (e, d) => e.ReplaceNode(d, GetExplicitlyAssignedField(field, d, e.Generator)), cancellationToken).ConfigureAwait(false); makeNextFieldExplicit = false; } if (isZeroValuedNamedNone) { needsNewZeroValuedNoneField = false; } } else { await editor.EditOneDeclarationAsync(field, (e, d) => e.RemoveNode(d), cancellationToken).ConfigureAwait(false); // removes the field declaration makeNextFieldExplicit = true; } } if (needsNewZeroValuedNoneField) { await editor.EditOneDeclarationAsync(enumType, (e, d) => e.InsertMembers(d, 0, new[] { e.Generator.EnumMember("None") }), cancellationToken).ConfigureAwait(false); } return(editor.GetChangedDocuments().First()); }
private async Task <Document> ApplyRuleNameNoZeroValueAsync(Document document, INamedTypeSymbol enumType, CancellationToken cancellationToken) { SymbolEditor editor = SymbolEditor.Create(document); // remove any non-zero member named 'None' foreach (IFieldSymbol field in enumType.GetMembers().Where(m => m.Kind == SymbolKind.Field)) { if (EnumsShouldHaveZeroValueAnalyzer.IsMemberNamedNone(field)) { await editor.EditOneDeclarationAsync(field, (e, d) => e.RemoveNode(d), cancellationToken).ConfigureAwait(false); } } // insert zero-valued member 'None' to top await editor.EditOneDeclarationAsync(enumType, (e, d) => e.InsertMembers(d, 0, new[] { e.Generator.EnumMember("None") }), cancellationToken).ConfigureAwait(false); return(editor.GetChangedDocuments().First()); }