diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs index e95617a..0351483 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs +++ b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs @@ -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; } @@ -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; } @@ -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]); + } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs index f47ac41..986a9ff 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs +++ b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs @@ -19,11 +19,13 @@ public EditorConfigDocumentParsingContext() _currentTrivia = new List(); } - 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(); } diff --git a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs b/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs index 540fa8d..e0fa134 100644 --- a/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs +++ b/Sources/Kysect.Configuin.Tests/EditorConfig/EditorConfigDocumentParserTests.cs @@ -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() { @@ -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() {