-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement .editorconfig model serialization
- Loading branch information
Showing
12 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentExporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Kysect.CommonLib.BaseTypes.Extensions; | ||
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; | ||
|
||
namespace Kysect.Configuin.EditorConfig.DocumentModel; | ||
|
||
public class EditorConfigDocumentSerializer | ||
{ | ||
public string Build(EditorConfigDocument document) | ||
{ | ||
document.ThrowIfNull(); | ||
|
||
return document.ToFullString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
public interface IEditorConfigNode | ||
{ | ||
string ToFullString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigDocument.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
using System.Collections.Immutable; | ||
using System.Text; | ||
|
||
namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; | ||
|
||
public record EditorConfigDocument(ImmutableList<IEditorConfigNode> Children) : IEditorConfigContainerNode | ||
public record EditorConfigDocument(ImmutableList<IEditorConfigNode> Children, ImmutableList<string> TrailingTrivia) : IEditorConfigContainerNode | ||
{ | ||
public EditorConfigDocument(ImmutableList<IEditorConfigNode> children) : this(children, ImmutableList<string>.Empty) | ||
{ | ||
} | ||
|
||
IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode child) | ||
{ | ||
return new EditorConfigDocument(Children: Children.Add(child)); | ||
return AddChild(child); | ||
} | ||
|
||
public EditorConfigDocument AddChild(IEditorConfigNode child) | ||
{ | ||
return new EditorConfigDocument(Children: Children.Add(child)); | ||
return this with { Children = Children.Add(child) }; | ||
} | ||
|
||
public string ToFullString() | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
List<string> lines = new(); | ||
lines.AddRange(Children.Select(c => c.ToFullString())); | ||
lines.AddRange(TrailingTrivia); | ||
|
||
stringBuilder.AppendJoin(Environment.NewLine, lines); | ||
return stringBuilder.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigPropertyNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
using System.Collections.Immutable; | ||
using System.Text; | ||
|
||
namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; | ||
|
||
public record EditorConfigPropertyNode(string Key, string Value, ImmutableList<string> LeadingTrivia, string? TrailingTrivia) : IEditorConfigNode | ||
public record EditorConfigPropertyNode(EditorConfigStringNode Key, EditorConfigStringNode Value, ImmutableList<string> LeadingTrivia, string? TrailingTrivia) : IEditorConfigNode | ||
{ | ||
public EditorConfigPropertyNode(string key, string value) : this(key, value, ImmutableList<string>.Empty, null) | ||
public EditorConfigPropertyNode(EditorConfigStringNode key, EditorConfigStringNode value) : this(key, value, ImmutableList<string>.Empty, null) | ||
{ | ||
} | ||
|
||
public EditorConfigPropertyNode(string key, string value) : this(EditorConfigStringNode.Create(key), EditorConfigStringNode.Create(value)) | ||
{ | ||
} | ||
|
||
public string ToFullString() | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
LeadingTrivia.ForEach(s => stringBuilder.AppendLine(s)); | ||
|
||
string line = $"{Key.ToFullString()}={Value.ToFullString()}"; | ||
if (TrailingTrivia is not null) | ||
line += $"{line} {TrailingTrivia}"; | ||
|
||
stringBuilder.Append(line); | ||
return stringBuilder.ToString(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/Kysect.Configuin.EditorConfig/DocumentModel/Nodes/EditorConfigStringNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Kysect.CommonLib.BaseTypes.Extensions; | ||
|
||
namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes; | ||
|
||
public record EditorConfigStringNode(string Value, string LeadingTrivia, string TrailingTrivia) : IEditorConfigNode | ||
{ | ||
public static EditorConfigStringNode Create(string value) | ||
{ | ||
value.ThrowIfNull(); | ||
|
||
string leadingTriviaLength = value.Substring(0, value.Length - value.TrimStart().Length); | ||
string trailingTriviaLength = value.Substring(value.TrimEnd().Length, value.Length - value.TrimEnd().Length); | ||
|
||
return new EditorConfigStringNode( | ||
value.Trim(), | ||
leadingTriviaLength, | ||
trailingTriviaLength); | ||
} | ||
|
||
public string ToFullString() | ||
{ | ||
return $"{LeadingTrivia}{Value}{TrailingTrivia}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters