static void TestParseEditorConfigCodeStyleOption(string args, bool isEnabled, DiagnosticSeverity severity)
        {
            var notificationOption = NotificationOption.None;

            switch (severity)
            {
            case DiagnosticSeverity.Hidden:
                notificationOption = NotificationOption.None;
                break;

            case DiagnosticSeverity.Info:
                notificationOption = NotificationOption.Suggestion;
                break;

            case DiagnosticSeverity.Warning:
                notificationOption = NotificationOption.Warning;
                break;

            case DiagnosticSeverity.Error:
                notificationOption = NotificationOption.Error;
                break;
            }

            var codeStyleOption = new CodeStyleOption <bool>(value: isEnabled, notification: notificationOption);

            CodeStyleHelpers.TryParseBoolEditorConfigCodeStyleOption(args, out var result);
            Assert.True(result.Value == isEnabled,
                        $"Expected {nameof(isEnabled)} to be {isEnabled}, was {result.Value}");
            Assert.True(result.Notification.Value == severity,
                        $"Expected {nameof(severity)} to be {severity}, was {result.Notification.Value}");
        }
        public static CodeStyleOption <ExpressionBodyPreference> ParseExpressionBodyPreference(
            string optionString, CodeStyleOption <ExpressionBodyPreference> @default)
        {
            // optionString must be similar to true:error or when_on_single_line:suggestion.
            if (CodeStyleHelpers.TryGetCodeStyleValueAndOptionalNotification(optionString,
                                                                             out var value, out var notificationOpt))
            {
                // A notification value must be provided.
                if (notificationOpt != null)
                {
                    if (bool.TryParse(value, out var boolValue))
                    {
                        return(boolValue
                            ? new CodeStyleOption <ExpressionBodyPreference>(ExpressionBodyPreference.WhenPossible, notificationOpt)
                            : new CodeStyleOption <ExpressionBodyPreference>(ExpressionBodyPreference.Never, notificationOpt));
                    }

                    if (value == "when_on_single_line")
                    {
                        return(new CodeStyleOption <ExpressionBodyPreference>(ExpressionBodyPreference.WhenOnSingleLine, notificationOpt));
                    }
                }
            }

            return(@default);
        }
Example #3
0
        public static CodeStyleOption <AddImportPlacement> ParseUsingDirectivesPlacement(
            string optionString, CodeStyleOption <AddImportPlacement> @default)
        {
            // optionString must be similar to outside_namespace:error or inside_namespace:suggestion.
            if (CodeStyleHelpers.TryGetCodeStyleValueAndOptionalNotification(
                    optionString, out var value, out var notificationOpt))
            {
                // A notification value must be provided.
                if (notificationOpt != null)
                {
                    switch (value)
                    {
                    case "inside_namespace":
                        return(new CodeStyleOption <AddImportPlacement>(AddImportPlacement.InsideNamespace, notificationOpt));

                    case "outside_namespace":
                        return(new CodeStyleOption <AddImportPlacement>(AddImportPlacement.OutsideNamespace, notificationOpt));

                    default:
                        throw new NotSupportedException();
                    }
                }
            }

            return(@default);
        }
Example #4
0
        private static CodeStyleOption <PreferBracesPreference> ParsePreferBracesPreference(
            string optionString,
            CodeStyleOption <PreferBracesPreference> defaultValue)
        {
            if (CodeStyleHelpers.TryGetCodeStyleValueAndOptionalNotification(
                    optionString,
                    out var value,
                    out var notificationOption))
            {
                if (notificationOption != null)
                {
                    if (bool.TryParse(value, out var boolValue))
                    {
                        return(boolValue
                            ? new CodeStyleOption <PreferBracesPreference>(PreferBracesPreference.Always, notificationOption)
                            : new CodeStyleOption <PreferBracesPreference>(PreferBracesPreference.None, notificationOption));
                    }
                }

                if (value == "when_multiline")
                {
                    return(new CodeStyleOption <PreferBracesPreference>(PreferBracesPreference.WhenMultiline, notificationOption));
                }
            }

            return(defaultValue);
        }
Example #5
0
        public void TestParseEditorConfigCodeStyleOption(string args, bool isEnabled, ReportDiagnostic severity)
        {
            var notificationOption = NotificationOption.Silent;

            switch (severity)
            {
            case ReportDiagnostic.Hidden:
                notificationOption = NotificationOption.Silent;
                break;

            case ReportDiagnostic.Info:
                notificationOption = NotificationOption.Suggestion;
                break;

            case ReportDiagnostic.Warn:
                notificationOption = NotificationOption.Warning;
                break;

            case ReportDiagnostic.Error:
                notificationOption = NotificationOption.Error;
                break;
            }

            CodeStyleHelpers.TryParseBoolEditorConfigCodeStyleOption(args, out var result);
            Assert.True(result.Value == isEnabled,
                        $"Expected {nameof(isEnabled)} to be {isEnabled}, was {result.Value}");
            Assert.True(result.Notification.Severity == severity,
                        $"Expected {nameof(severity)} to be {severity}, was {result.Notification.Severity}");
        }
Example #6
0
 public void TestParseEditorConfigCodeStyleOption(string args, bool isEnabled, ReportDiagnostic severity)
 {
     CodeStyleHelpers.TryParseBoolEditorConfigCodeStyleOption(args, out var result);
     Assert.True(result.Value == isEnabled,
                 $"Expected {nameof(isEnabled)} to be {isEnabled}, was {result.Value}");
     Assert.True(result.Notification.Severity == severity,
                 $"Expected {nameof(severity)} to be {severity}, was {result.Notification.Severity}");
 }
Example #7
0
        private static string GetExpressionBodyPreferenceEditorConfigString(CodeStyleOption2 <ExpressionBodyPreference> value, CodeStyleOption2 <ExpressionBodyPreference> defaultValue)
        {
            var notificationString = CodeStyleHelpers.GetEditorConfigStringNotificationPart(value, defaultValue);

            return(value.Value switch
            {
                ExpressionBodyPreference.Never => $"false{notificationString}",
                ExpressionBodyPreference.WhenPossible => $"true{notificationString}",
                ExpressionBodyPreference.WhenOnSingleLine => $"when_on_single_line{notificationString}",
                _ => throw new NotSupportedException(),
            });
        public static Optional <OperatorPlacementWhenWrappingPreference> Parse(string optionString)
        {
            if (CodeStyleHelpers.TryGetCodeStyleValue(optionString, out var value))
            {
                switch (value)
                {
                case end_of_line: return(OperatorPlacementWhenWrappingPreference.EndOfLine);

                case beginning_of_line: return(OperatorPlacementWhenWrappingPreference.BeginningOfLine);
                }
            }

            // Default to beginning_of_line if we get something we don't understand.
            return(OperatorPlacementWhenWrappingPreference.BeginningOfLine);
        }
Example #9
0
 private static Optional <CodeStyleOption <bool> > ParseBoolCodeStyleOption(string str)
 => CodeStyleHelpers.TryParseEditorConfigCodeStyleOption(str, out var result) ? result : new Optional <CodeStyleOption <bool> >();
Example #10
0
 private static Option2 <T> CreateOption <T>(OptionGroup group, string name, T defaultValue, params OptionStorageLocation2[] storageLocations)
 => CodeStyleHelpers.CreateOption(group, nameof(CSharpCodeStyleOptions), name, defaultValue, s_allOptionsBuilder, storageLocations);
Example #11
0
 private static Option2 <T> CreateOption <T>(
     OptionGroup group, string name, T defaultValue, OptionStorageLocation2 storageLocation1, OptionStorageLocation2 storageLocation2)
 => CodeStyleHelpers.CreateOption(
     group, nameof(CSharpCodeStyleOptions), name, defaultValue,
     s_allOptionsBuilder, storageLocation1, storageLocation2, LanguageNames.CSharp);