Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
 public static bool TryGetEditorConfigOption <T>(
     this AnalyzerConfigOptions analyzerConfigOptions,
     TOption option,
     [MaybeNullWhen(false)] out T value
     ) =>
 TryGetEditorConfigOption(
     analyzerConfigOptions,
     option,
     useDefaultIfMissing: false,
     out value
     );
Exemplo n.º 3
0
 public static bool TryGetEditorConfigOptionOrDefault <T>(
     this AnalyzerConfigOptions analyzerConfigOptions,
     TOption option,
     out T value
     ) =>
 TryGetEditorConfigOption(
     analyzerConfigOptions,
     option,
     useDefaultIfMissing: true,
     out value !
     );
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 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;
Exemplo n.º 7
0
 public static bool TryGetEditorConfigOption <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, [MaybeNullWhen(false)] out T value)
 => TryGetEditorConfigOption(analyzerConfigOptions, option, defaultValue: default, out value);
Exemplo n.º 8
0
 public static bool TryGetEditorConfigOptionOrDefault <T>(this AnalyzerConfigOptions analyzerConfigOptions, TOption option, out T value)
 => TryGetEditorConfigOption(analyzerConfigOptions, option, (T?)option.DefaultValue, out value !);
Exemplo n.º 9
0
 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;