Skip to content

Commit

Permalink
Fixes #13 exposed properties should read most recent Section (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Jul 20, 2022
1 parent 16e7b8e commit fbbc37c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
46 changes: 23 additions & 23 deletions src/EditorConfig.Core/FileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace EditorConfig.Core
{

public class FileConfiguration
public class FileConfiguration
{
private List<ConfigSection> Sections { get; }

Expand All @@ -17,41 +17,41 @@ public class FileConfiguration
public IndentStyle? IndentStyle { get; }

/// <summary>
/// a whole number defining the number of columns used for each indentation level and the width of soft tabs (when supported).
/// a whole number defining the number of columns used for each indentation level and the width of soft tabs (when supported).
/// When set to tab, the value of tab_width (if specified) will be used.
/// </summary>
public IndentSize IndentSize { get; }

/// <summary>
/// a whole number defining the number of columns used to represent a tab character.
/// a whole number defining the number of columns used to represent a tab character.
/// This defaults to the value of indent_size and doesn't usually need to be specified.
/// </summary>
public int? TabWidth { get; }

/// <summary>
/// set to lf, cr, or crlf to control how line breaks are represented.
/// </summary>
public EndOfLine? EndOfLine { get; }
public EndOfLine? EndOfLine { get; }

/// <summary>
/// set to latin1, utf-8, utf-8-bom, utf-16be or utf-16le to control the character set. Use of utf-8-bom is discouraged.
/// </summary>
public Charset? Charset { get; }
public Charset? Charset { get; }

/// <summary>
/// set to true to remove any whitespace characters preceding newline characters and false to ensure it doesn't.
/// </summary>
public bool? TrimTrailingWhitespace { get; }
public bool? TrimTrailingWhitespace { get; }

/// <summary>
/// set to true ensure file ends with a newline when saving and false to ensure it doesn't.
/// </summary>
public bool? InsertFinalNewline { get; }
public bool? InsertFinalNewline { get; }

/// <summary>
/// Forces hard line wrapping after the amount of characters specified
/// </summary>
public int? MaxLineLength { get; }
public int? MaxLineLength { get; }

/// <summary>
/// All the editorconfig properties relevant for this file
Expand All @@ -62,7 +62,7 @@ public class FileConfiguration
/// The filename we asked the configuration for
/// </summary>
public string FileName { get; }

/// <summary>
/// A reference to the version number of the parser
/// </summary>
Expand All @@ -85,14 +85,14 @@ internal FileConfiguration(Version version, string fileName, List<ConfigSection>
foreach (var kv in allProperties)
properties[kv.Key] = kv.Value;

IndentStyle = Sections.FirstOrDefault(s => s.IndentStyle.HasValue)?.IndentStyle;
IndentStyle = Sections.LastOrDefault(s => s.IndentStyle.HasValue)?.IndentStyle;
IndentSize = Sections.LastOrDefault(s => s.IndentSize != null)?.IndentSize;
TabWidth = Sections.FirstOrDefault(s => s.TabWidth.HasValue)?.TabWidth;
EndOfLine = Sections.FirstOrDefault(s => s.EndOfLine.HasValue)?.EndOfLine;
Charset = Sections.FirstOrDefault(s => s.Charset.HasValue)?.Charset;
TrimTrailingWhitespace = Sections.FirstOrDefault(s => s.TrimTrailingWhitespace.HasValue)?.TrimTrailingWhitespace;
InsertFinalNewline = Sections.FirstOrDefault(s => s.InsertFinalNewline.HasValue)?.InsertFinalNewline;
MaxLineLength = Sections.FirstOrDefault(s => s.MaxLineLength.HasValue)?.MaxLineLength;
TabWidth = Sections.LastOrDefault(s => s.TabWidth.HasValue)?.TabWidth;
EndOfLine = Sections.LastOrDefault(s => s.EndOfLine.HasValue)?.EndOfLine;
Charset = Sections.LastOrDefault(s => s.Charset.HasValue)?.Charset;
TrimTrailingWhitespace = Sections.LastOrDefault(s => s.TrimTrailingWhitespace.HasValue)?.TrimTrailingWhitespace;
InsertFinalNewline = Sections.LastOrDefault(s => s.InsertFinalNewline.HasValue)?.InsertFinalNewline;
MaxLineLength = Sections.LastOrDefault(s => s.MaxLineLength.HasValue)?.MaxLineLength;

//default tab_width to indent_size when indent size is a number
if (IndentSize != null && IndentSize.NumberOfColumns.HasValue)
Expand All @@ -118,7 +118,7 @@ internal FileConfiguration(Version version, string fileName, List<ConfigSection>
TabWidth = IndentSize.NumberOfColumns.Value;
properties["tab_width"] = TabWidth.Value.ToString();
}

// unset carries over see:
// ctest . -R "unset_indent_size"
else if (IndentSize.IsUnset)
Expand All @@ -134,7 +134,7 @@ internal FileConfiguration(Version version, string fileName, List<ConfigSection>
IndentSize = IndentSize.Columns(TabWidth.Value);
properties["indent_size"] = TabWidth.Value.ToString();
}

Properties = new ReadOnlyDictionary<string, string>(properties);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/EditorConfig.Tests/CTestReproduce/ReproduceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void UnsetIndentSize()
file.Properties["tab_width"].Should().Be("unset");
}

//unset_indent_size_ML
[Test]
public void GithubReport18()
{
Expand All @@ -48,5 +47,14 @@ public void GithubReport18()
file.Properties["csharp_style_namespace_declarations"].Should().Be("file_scoped:warning");
}

[Test]
public void GithubReport13()
{
var file = GetConfig(MethodBase.GetCurrentMethod(), @"my.exe", "github_report_13.editorconfig");

file.TrimTrailingWhitespace.Should().BeFalse();
file.Properties["trim_trailing_whitespace"].Should().Be("false");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
trim_trailing_whitespace = true

[*.exe]
trim_trailing_whitespace = false

0 comments on commit fbbc37c

Please sign in to comment.