Skip to content

Commit 9d0ae71

Browse files
Fix EditorConfig parsing bug and add debugging for namespace declaration support
Co-authored-by: 304NotModified <[email protected]>
1 parent 3b6606e commit 9d0ae71

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

Reqnroll.VisualStudio/Editor/Commands/DefineStepsCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ private void SaveAsStepDefinitionClass(IProjectScope projectScope, string combin
142142
var editorConfigOptions = _editorConfigOptionsProvider.GetEditorConfigOptions(textView);
143143
editorConfigOptions.UpdateFromEditorConfig(csharpConfig);
144144

145+
// Debug: Log the configuration values
146+
System.Diagnostics.Debug.WriteLine($"[DefineStepsCommand] NamespaceDeclarationStyle: '{csharpConfig.NamespaceDeclarationStyle}'");
147+
System.Diagnostics.Debug.WriteLine($"[DefineStepsCommand] UseFileScopedNamespaces: {csharpConfig.UseFileScopedNamespaces}");
148+
145149
// Build template with common structure
146150
var template = new StringBuilder();
147151
template.AppendLine("using System;");

Reqnroll.VisualStudio/Editor/Services/EditorConfig/EditorConfigOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ public EditorConfigOptions(DocumentOptionSet options)
1919

2020
public TResult GetOption<TResult>(string editorConfigKey, TResult defaultValue)
2121
{
22+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] GetOption: key='{editorConfigKey}', defaultValue='{defaultValue}', type={typeof(TResult).Name}");
23+
2224
if (_directEditorConfigValues.TryGetValue(editorConfigKey, out var simplifiedValue))
2325
{
26+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] Found value: '{simplifiedValue}' for key '{editorConfigKey}'");
2427
if (TryConvertFromString(simplifiedValue, defaultValue, out var convertedValue))
2528
{
29+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] Converted value: '{convertedValue}' for key '{editorConfigKey}'");
2630
return convertedValue;
2731
}
2832
}
33+
else
34+
{
35+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] No value found for key '{editorConfigKey}', returning default: '{defaultValue}'");
36+
}
2937
return defaultValue;
3038
}
3139

@@ -60,7 +68,18 @@ private Dictionary<string, string> ExtractEditorConfigValues()
6068
var dacOptionsField = optionsAnalyzerConfigOptions?.GetType()?.GetField("Options", BindingFlags.NonPublic | BindingFlags.Instance);
6169
var optionsCollection = dacOptionsField?.GetValue(optionsAnalyzerConfigOptions) as ImmutableDictionary<string, string>;
6270
if (optionsCollection != null)
71+
{
6372
values = new Dictionary<string, string>(optionsCollection);
73+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] Extracted {values.Count} EditorConfig values");
74+
foreach (var kvp in values)
75+
{
76+
System.Diagnostics.Debug.WriteLine($"[EditorConfigOptions] Key: '{kvp.Key}' = '{kvp.Value}'");
77+
}
78+
}
79+
else
80+
{
81+
System.Diagnostics.Debug.WriteLine("[EditorConfigOptions] No options collection found");
82+
}
6483
}
6584
catch (Exception ex)
6685
{

Reqnroll.VisualStudio/Editor/Services/EditorConfig/EditorConfigOptionsExtensions.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,37 @@ public static void UpdateFromEditorConfig<TConfig>(this IEditorConfigOptions edi
2121
{
2222
if (config == null) throw new ArgumentNullException(nameof(config));
2323

24+
System.Diagnostics.Debug.WriteLine($"[UpdateFromEditorConfig] Updating configuration for type: {typeof(TConfig).Name}");
25+
2426
var propertiesWithEditorConfig = typeof(TConfig)
2527
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
2628
.Select(p => new
2729
{
2830
PropertyInfo = p,
29-
((EditorConfigSettingAttribute) Attribute.GetCustomAttribute(p, typeof(EditorConfigSettingAttribute)))
31+
EditorConfigKey = ((EditorConfigSettingAttribute) Attribute.GetCustomAttribute(p, typeof(EditorConfigSettingAttribute)))
3032
?.EditorConfigSettingName
3133
})
32-
.Where(p => p.EditorConfigSettingName != null);
34+
.Where(p => p.EditorConfigKey != null);
3335

3436
foreach (var property in propertiesWithEditorConfig)
3537
{
3638
var currentValue = property.PropertyInfo.GetValue(config);
39+
System.Diagnostics.Debug.WriteLine($"[UpdateFromEditorConfig] Property: {property.PropertyInfo.Name}, EditorConfigKey: {property.EditorConfigKey}, CurrentValue: {currentValue}");
40+
3741
var updatedValue = editorConfigOptions.GetOption(property.PropertyInfo.PropertyType,
38-
property.EditorConfigSettingName, currentValue);
42+
property.EditorConfigKey, currentValue);
43+
44+
System.Diagnostics.Debug.WriteLine($"[UpdateFromEditorConfig] UpdatedValue: {updatedValue}");
45+
3946
if (!Equals(currentValue, updatedValue))
47+
{
48+
System.Diagnostics.Debug.WriteLine($"[UpdateFromEditorConfig] Setting property {property.PropertyInfo.Name} from '{currentValue}' to '{updatedValue}'");
4049
property.PropertyInfo.SetValue(config, updatedValue);
50+
}
51+
else
52+
{
53+
System.Diagnostics.Debug.WriteLine($"[UpdateFromEditorConfig] Property {property.PropertyInfo.Name} unchanged: '{currentValue}'");
54+
}
4155
}
4256
}
4357
}

Tests/Reqnroll.VisualStudio.Tests/Configuration/CSharpCodeGenerationConfigurationTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Reqnroll.VisualStudio.Configuration;
2+
using Reqnroll.VisualStudio.Editor.Services.EditorConfig;
23
using Xunit;
34

45
namespace Reqnroll.VisualStudio.Tests.Configuration;
@@ -79,4 +80,75 @@ public void UseFileScopedNamespaces_WhenUnknownValue_ReturnsFalse()
7980
// Act & Assert
8081
Assert.False(config.UseFileScopedNamespaces);
8182
}
83+
84+
[Fact]
85+
public void UpdateFromEditorConfig_WhenFileScopedValue_SetsCorrectValue()
86+
{
87+
// Arrange
88+
var config = new CSharpCodeGenerationConfiguration();
89+
var editorConfigOptions = new TestEditorConfigOptions("file_scoped:silent");
90+
91+
// Act
92+
editorConfigOptions.UpdateFromEditorConfig(config);
93+
94+
// Assert
95+
Assert.Equal("file_scoped:silent", config.NamespaceDeclarationStyle);
96+
Assert.True(config.UseFileScopedNamespaces);
97+
}
98+
99+
[Fact]
100+
public void UpdateFromEditorConfig_WhenBlockScopedValue_SetsCorrectValue()
101+
{
102+
// Arrange
103+
var config = new CSharpCodeGenerationConfiguration();
104+
var editorConfigOptions = new TestEditorConfigOptions("block_scoped");
105+
106+
// Act
107+
editorConfigOptions.UpdateFromEditorConfig(config);
108+
109+
// Assert
110+
Assert.Equal("block_scoped", config.NamespaceDeclarationStyle);
111+
Assert.False(config.UseFileScopedNamespaces);
112+
}
113+
114+
[Fact]
115+
public void UpdateFromEditorConfig_WhenNoValue_KeepsDefault()
116+
{
117+
// Arrange
118+
var config = new CSharpCodeGenerationConfiguration();
119+
var editorConfigOptions = new TestEditorConfigOptions(null);
120+
121+
// Act
122+
editorConfigOptions.UpdateFromEditorConfig(config);
123+
124+
// Assert
125+
Assert.Equal("block_scoped", config.NamespaceDeclarationStyle); // Should keep default
126+
Assert.False(config.UseFileScopedNamespaces);
127+
}
128+
}
129+
130+
// Test EditorConfig options provider that simulates reading specific values
131+
public class TestEditorConfigOptions : IEditorConfigOptions
132+
{
133+
private readonly string _namespaceStyle;
134+
135+
public TestEditorConfigOptions(string namespaceStyle)
136+
{
137+
_namespaceStyle = namespaceStyle;
138+
}
139+
140+
public TResult GetOption<TResult>(string editorConfigKey, TResult defaultValue)
141+
{
142+
if (editorConfigKey == "csharp_style_namespace_declarations" && _namespaceStyle != null)
143+
{
144+
return (TResult)(object)_namespaceStyle;
145+
}
146+
147+
return defaultValue;
148+
}
149+
150+
public bool GetBoolOption(string editorConfigKey, bool defaultValue)
151+
{
152+
return defaultValue;
153+
}
82154
}

0 commit comments

Comments
 (0)