private static bool TryGetEditorConfigOption <T>( this AnalyzerConfigOptions analyzerConfigOptions, TOption option, bool useDefaultIfMissing, out T?value ) { var hasEditorConfigStorage = false; foreach (var storageLocation in option.StorageLocations) { // This code path will avoid allocating a Dictionary wrapper since we can get direct access to the KeyName. if ( storageLocation is EditorConfigStorageLocation <T> editorConfigStorageLocation && analyzerConfigOptions.TryGetValue( editorConfigStorageLocation.KeyName, out var stringValue ) && editorConfigStorageLocation.TryGetOption(stringValue, typeof(T), out value) ) { return(true); } if (!(storageLocation is IEditorConfigStorageLocation configStorageLocation)) { continue; } // This option has .editorconfig storage defined, even if the current configuration does not provide a // value for it. hasEditorConfigStorage = true; if ( configStorageLocation.TryGetOption( analyzerConfigOptions, option.Type, out var objectValue ) ) { value = (T)objectValue; return(true); } } if (useDefaultIfMissing) { value = (T?)option.DefaultValue; return(hasEditorConfigStorage); } else { value = default; return(false); } }
public static bool TryGetEditorConfigOption <T>( this AnalyzerConfigOptions analyzerConfigOptions, TOption option, [MaybeNullWhen(false)] out T value ) => TryGetEditorConfigOption( analyzerConfigOptions, option, useDefaultIfMissing: false, out value );
public static bool TryGetEditorConfigOptionOrDefault <T>( this AnalyzerConfigOptions analyzerConfigOptions, TOption option, out T value ) => TryGetEditorConfigOption( analyzerConfigOptions, option, useDefaultIfMissing: true, out value ! );
public static bool TryGetEditorConfigOption <T>( this AnalyzerOptions analyzerOptions, TOption option, SyntaxTree syntaxTree, [MaybeNullWhen(false)] out T value ) { var configOptions = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions( syntaxTree ); return(configOptions.TryGetEditorConfigOption(option, out value)); }
private static T GetOptionWithAssertOnFailure <T>( AnalyzerConfigOptions analyzerConfigOptions, TOption option ) { if (!TryGetEditorConfigOptionOrDefault(analyzerConfigOptions, option, out T value)) { // There are couple of reasons this assert might fire: // 1. Attempting to access an option which does not have an IEditorConfigStorageLocation. // 2. Attempting to access an option which is not exposed from any option provider, i.e. IOptionProvider.Options. Debug.Fail("Failed to find a .editorconfig key for the option."); value = (T)option.DefaultValue !; } return(value); }
public static T GetEditorConfigOption <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, T defaultValue) => TryGetEditorConfigOption(analyzerConfigOptions, option, new Optional <T?>(defaultValue), out var value) ? value ! : throw ExceptionUtilities.Unreachable;
public static bool TryGetEditorConfigOption <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, [MaybeNullWhen(false)] out T value) => TryGetEditorConfigOption(analyzerConfigOptions, option, defaultValue: default, out value);
public static bool TryGetEditorConfigOptionOrDefault <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, out T value) => TryGetEditorConfigOption(analyzerConfigOptions, option, (T?)option.DefaultValue, out value !);
public static T GetEditorConfigOptionValue <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, T defaultValue) => TryGetEditorConfigOption(analyzerConfigOptions, option, new Optional <CodeStyleOption2 <T>?>(new CodeStyleOption2 <T>(defaultValue, NotificationOption2.None)), out var style) ? style !.Value : throw ExceptionUtilities.Unreachable;