diff --git a/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/CohostConfigurationChangedService.cs b/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/CohostConfigurationChangedService.cs index 86e674e85da..c3321011963 100644 --- a/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/CohostConfigurationChangedService.cs +++ b/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/CohostConfigurationChangedService.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.ExternalAccess.Razor; using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost; using Microsoft.CodeAnalysis.Razor.Logging; +using Microsoft.CodeAnalysis.Razor.Settings; using Microsoft.CodeAnalysis.Razor.Workspaces.Settings; using Microsoft.VisualStudio.Razor.LanguageClient.Cohost; @@ -59,16 +60,21 @@ private async Task RefreshOptionsAsync(RazorCohostRequestContext requestContext, cancellationToken).ConfigureAwait(false); var current = _clientSettingsManager.GetClientSettings().AdvancedSettings; - var settings = current with - { - CodeBlockBraceOnNextLine = GetBooleanOptionValue(options[0], current.CodeBlockBraceOnNextLine), - AttributeIndentStyle = GetEnumOptionValue(options[1], current.AttributeIndentStyle), - CommitElementsWithSpace = GetBooleanOptionValue(options[2], current.CommitElementsWithSpace), - }; + var settings = UpdateSettingsFromJson(current, options); _clientSettingsManager.Update(settings); } + private static ClientAdvancedSettings UpdateSettingsFromJson(ClientAdvancedSettings settings, JsonArray jsonArray) + { + return settings with + { + CodeBlockBraceOnNextLine = GetBooleanOptionValue(jsonArray[0], settings.CodeBlockBraceOnNextLine), + AttributeIndentStyle = GetEnumOptionValue(jsonArray[1], settings.AttributeIndentStyle), + CommitElementsWithSpace = GetBooleanOptionValue(jsonArray[2], settings.CommitElementsWithSpace), + }; + } + private static bool GetBooleanOptionValue(JsonNode? jsonNode, bool defaultValue) { if (jsonNode is null) @@ -86,11 +92,17 @@ private static T GetEnumOptionValue(JsonNode? jsonNode, T defaultValue) where return defaultValue; } - if (Enum.TryParse(jsonNode.GetValue(), out var value)) + if (Enum.TryParse(jsonNode.GetValue(), ignoreCase: true, out var value)) { return value; } return defaultValue; } + + public static class TestAccessor + { + public static ClientAdvancedSettings UpdateSettingsFromJson(ClientAdvancedSettings settigns, JsonArray jsonArray) + => CohostConfigurationChangedService.UpdateSettingsFromJson(settigns, jsonArray); + } } diff --git a/src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/CohostConfigurationChangedServiceTest.cs b/src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/CohostConfigurationChangedServiceTest.cs new file mode 100644 index 00000000000..a8fcd759007 --- /dev/null +++ b/src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/CohostConfigurationChangedServiceTest.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Razor; +using Microsoft.CodeAnalysis.Razor.Settings; +using Microsoft.VisualStudio.Razor.LanguageClient.Cohost; +using Microsoft.VisualStudioCode.RazorExtension.Endpoints; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.VisualStudioCode.RazorExtension.Test; + +public class CohostConfigurationChangedServiceTest(ITestOutputHelper testOutput) : CohostEndpointTestBase(testOutput) +{ + [Fact] + public void ReadSettings() + { + var settings = ClientSettingsManager.GetClientSettings().AdvancedSettings; + + // If the defaults for these settings change, the json parsing could break and we might not know + Assert.False(settings.CodeBlockBraceOnNextLine); + Assert.Equal(AttributeIndentStyle.AlignWithFirst, settings.AttributeIndentStyle); + Assert.True(settings.CommitElementsWithSpace); + + var json = (JsonArray)JsonNode.Parse(""" + ["true", "indentByOne", "false"] + """).AssumeNotNull(); + + var updatedSettings = CohostConfigurationChangedService.TestAccessor.UpdateSettingsFromJson(settings, json); + + Assert.True(updatedSettings.CodeBlockBraceOnNextLine); + Assert.Equal(AttributeIndentStyle.IndentByOne, updatedSettings.AttributeIndentStyle); + Assert.False(updatedSettings.CommitElementsWithSpace); + } +}