Skip to content

Commit

Permalink
Support custom file extensions with editor config (belav#1346)
Browse files Browse the repository at this point in the history
closes belav#1273
  • Loading branch information
pisolofin committed Sep 24, 2024
1 parent f13c02d commit 482e02c
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 @@ -77,6 +81,7 @@ private class ResolvedConfiguration
public int? TabWidth { get; }
public int? MaxLineLength { get; }
public EndOfLine? EndOfLine { get; }
public string? Formatter { get; }

public BraceNewLine? NewLineBeforeOpenBrace { get; }
public bool? NewLineBeforeElse { get; }
Expand Down Expand Up @@ -130,6 +135,8 @@ public ResolvedConfiguration(List<Section> sections)
this.EndOfLine = result;
}

this.Formatter = sections.LastOrDefault(o => o.Formatter is not null)?.Formatter;

var newLineBeforeOpenBrace = sections
.LastOrDefault(o => o.NewLineBeforeOpenBrace != null)
?.NewLineBeforeOpenBrace;
Expand Down
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 string? NewLineBeforeOpenBrace { get; }
public string? NewLineBeforeElse { get; }
public string? NewLineBeforeCatch { get; }
Expand All @@ -27,6 +28,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"];
this.NewLineBeforeOpenBrace = section.Keys["csharp_new_line_before_open_brace"];
this.NewLineBeforeElse = section.Keys["csharp_new_line_before_else"];
this.NewLineBeforeCatch = section.Keys["csharp_new_line_before_catch"];
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 @@ -511,7 +511,7 @@ public async Task Should_Return_UsePrettierStyleTrailingCommas_With_Yaml(bool va
}

[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 @@ -836,6 +836,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 @@ -251,6 +251,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 @@ -284,7 +285,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 @@ -293,9 +294,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 482e02c

Please sign in to comment.