Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -86,11 +92,17 @@ private static T GetEnumOptionValue<T>(JsonNode? jsonNode, T defaultValue) where
return defaultValue;
}

if (Enum.TryParse<T>(jsonNode.GetValue<string>(), out var value))
if (Enum.TryParse<T>(jsonNode.GetValue<string>(), ignoreCase: true, out var value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetValue

Out of curiosity, this code uses GetValue, but GetBooleanOptionsValue above it does a ToString. Is there a reason GetBooleanOptionsValue doesn't use GetValue?

Copy link
Member Author

@davidwengier davidwengier Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember if it was paranoia or a user report, or maybe even me stuffing up my own settings file, but users directly modify the settings json in VS Code so the difference between "value": true and "value": "true" doesn't seem like it should be their problem, IMO.

{
return value;
}

return defaultValue;
}

public static class TestAccessor
{
public static ClientAdvancedSettings UpdateSettingsFromJson(ClientAdvancedSettings settigns, JsonArray jsonArray)
=> CohostConfigurationChangedService.UpdateSettingsFromJson(settigns, jsonArray);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}