Skip to content

Commit

Permalink
Add support of comment after content in .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 17, 2024
1 parent b6f2712 commit 179529f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ public EditorConfigDocument Parse(string[] lines)
bool isCategory = trimmedLine.StartsWith("[");
if (isCategory)
{
var categoryName = line.Substring(1, line.Length - 2);
context.AddCategory(categoryName);
(string? lineWithoutComment, string? comment) = ExtractComment(trimmedLine);
lineWithoutComment = lineWithoutComment.Trim();
if (!lineWithoutComment.EndsWith("]"))
throw new ArgumentException($"Line is not valid category definition: {lineWithoutComment}");

string categoryName = lineWithoutComment.Substring(1, lineWithoutComment.Length - 2);

EditorConfigCategoryNode categoryNode = comment is not null
? new EditorConfigCategoryNode(categoryName) { TrailingTrivia = comment }
: new EditorConfigCategoryNode(categoryName);

context.AddCategory(categoryNode);
continue;
}

Expand All @@ -49,11 +59,17 @@ public EditorConfigDocument Parse(string[] lines)
bool isProperty = trimmedLine.Contains('=');
if (isProperty)
{
string[] parts = line.Split('=');
(string? lineWithoutComment, string? comment) = ExtractComment(trimmedLine);
lineWithoutComment = lineWithoutComment.Trim();

string[] parts = lineWithoutComment.Split('=');
if (parts.Length != 2)
throw new ArgumentException($"Line {line} contains unexpected count of '='");

var propertyNode = new EditorConfigPropertyNode(EditorConfigStringNode.Create(parts[0]), EditorConfigStringNode.Create(parts[1]));
if (comment is not null)
propertyNode = propertyNode with { TrailingTrivia = comment };

context.AddProperty(propertyNode);
continue;
}
Expand All @@ -63,4 +79,13 @@ public EditorConfigDocument Parse(string[] lines)

return context.Build();
}

private (string lineWithoutComment, string? comment) ExtractComment(string originalString)
{
if (!originalString.Contains('#'))
return (originalString, null);

string[] parts = originalString.Split('#');
return (parts[0], parts[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public EditorConfigDocumentParsingContext()
_currentTrivia = new List<string>();
}

public void AddCategory(string categoryName)
public void AddCategory(EditorConfigCategoryNode categoryNode)
{
categoryNode.ThrowIfNull();

DumpSection();
DumpCategory();
_currentCategory = new EditorConfigCategoryNode(categoryName) { LeadingTrivia = _currentTrivia.ToImmutableList() };
_currentCategory = categoryNode with { LeadingTrivia = _currentTrivia.ToImmutableList() };
_currentTrivia = new List<string>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public void Parse_Category_ReturnDocumentWithCategoryNode()
ParseAndCompare(content, expected);
}

[Fact]
public void Parse_CategoryWithComment_ReturnDocumentWithCategoryNode()
{
const string content = """
[*.cs] # comment
""";
EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigCategoryNode("*.cs") { TrailingTrivia = " comment" }]);

ParseAndCompare(content, expected);
}

[Fact]
public void Parse_Group_ReturnDocumentWithGroupNode()
{
Expand All @@ -63,6 +74,17 @@ public void Parse_Property_ReturnDocumentWithPropertyNode()
ParseAndCompare(content, expected);
}

[Fact]
public void Parse_PropertyWithComment_ReturnDocumentWithPropertyNode()
{
const string content = """
key=value # comment
""";
EditorConfigDocument expected = new EditorConfigDocument([new EditorConfigPropertyNode("key", "value") { TrailingTrivia = " comment" }]);

ParseAndCompare(content, expected);
}

[Fact]
public void Parse_CategoryWithProperty_ReturnCorrectDocument()
{
Expand Down

0 comments on commit 179529f

Please sign in to comment.