Skip to content

Commit

Permalink
Support custom file extensions with editor config (#1346)
Browse files Browse the repository at this point in the history
closes #1273
  • Loading branch information
belav authored Sep 15, 2024
1 parent e90022e commit 861f281
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
17 changes: 12 additions & 5 deletions Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ internal class EditorConfigSections

public PrinterOptions? ConvertToPrinterOptions(string filePath)
{
if (!(filePath.EndsWith(".cs") || filePath.EndsWith(".csx")))
var sections = this.SectionsIncludingParentFiles.Where(o => o.IsMatch(filePath)).ToList();
var resolvedConfiguration = new ResolvedConfiguration(sections);

var formatter =
resolvedConfiguration.Formatter
?? (filePath.EndsWith(".cs") || filePath.EndsWith(".csx") ? "csharp" : null);

if (formatter == null)
{
return null;
}

var sections = this.SectionsIncludingParentFiles.Where(o => o.IsMatch(filePath)).ToList();
var resolvedConfiguration = new ResolvedConfiguration(sections);

var printerOptions = new PrinterOptions { Formatter = "csharp" };
var printerOptions = new PrinterOptions { Formatter = formatter };

if (resolvedConfiguration.MaxLineLength is { } maxLineLength)
{
Expand Down Expand Up @@ -56,6 +60,7 @@ private class ResolvedConfiguration
public int? TabWidth { get; }
public int? MaxLineLength { get; }
public EndOfLine? EndOfLine { get; }
public string? Formatter { get; }

public ResolvedConfiguration(List<Section> sections)
{
Expand Down Expand Up @@ -100,6 +105,8 @@ public ResolvedConfiguration(List<Section> sections)
{
this.EndOfLine = result;
}

this.Formatter = sections.LastOrDefault(o => o.Formatter is not null)?.Formatter;
}
}
}
2 changes: 2 additions & 0 deletions Src/CSharpier.Cli/EditorConfig/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class Section
public string? TabWidth { get; }
public string? MaxLineLength { get; }
public string? EndOfLine { get; }
public string? Formatter { get; }

public Section(SectionData section, string directory)
{
Expand All @@ -20,6 +21,7 @@ public Section(SectionData section, string directory)
this.TabWidth = section.Keys["tab_width"];
this.MaxLineLength = section.Keys["max_line_length"];
this.EndOfLine = section.Keys["end_of_line"];
this.Formatter = section.Keys["csharpier_formatter"];
}

public bool IsMatch(string fileName)
Expand Down
20 changes: 19 additions & 1 deletion Src/CSharpier.Tests/OptionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public async Task Should_Return_UseTabs_With_Yaml()
}

[Test]
public async Task Should_Return_TabWidth_For_Overrid()
public async Task Should_Return_TabWidth_For_Override()
{
var context = new TestContext();
context.WhenAFileExists(
Expand Down Expand Up @@ -528,6 +528,24 @@ public async Task Should_Support_EditorConfig_Tabs_With_Glob_Braces_Multiples()
result.IndentSize.Should().Be(2);
}

[Test]
public async Task Should_Return_IndentSize_For_Formatter_In_Editorconfig()
{
var context = new TestContext();
context.WhenAFileExists(
"c:/test/.editorconfig",
"""
[*.cst]
indent_size = 2
csharpier_formatter = csharp
"""
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cst");

result.IndentSize.Should().Be(2);
}

[Test]
public async Task Should_Find_EditorConfig_In_Parent_Directory()
{
Expand Down
16 changes: 14 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The long term plan is to improve Csharpier's ability to determine the symbol set

### Configuration Overrides ###
_First available in 0.29.0_

Overrides allows you to specify different configuration options based on glob patterns. This can be used to format non-standard extensions, or to change options based on file path. Top level options will apply to `**/*.{cs,csx}`

```json
Expand Down Expand Up @@ -114,7 +115,7 @@ _First available in 0.26.0_
CSharpier supports configuration via an `.editorconfig` file. A `.csharpierrc*` file in the same directory will take priority.

```ini
[*]
[*.{cs|csx}]
# Non-configurable behaviors
charset = utf-8
insert_final_newline = true
Expand All @@ -123,9 +124,20 @@ dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false
# Configurable behaviors
# end_of_line = lf - there is no 'auto' with a .editorconfig
# end_of_line = lf - there is no 'auto' with an .editorconfig
indent_style = space
indent_size = 4
max_line_length = 100
```

_First available in 0.29.2_

Formatting non-standard file extensions using csharpier can be accomplished with the `csharpier_formatter` option
```ini
[*.cst]
csharpier_formatter = csharp
indent_style = space
indent_size = 2
max_line_length = 80
```

0 comments on commit 861f281

Please sign in to comment.