Skip to content

Commit

Permalink
Always end written files with a newline
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
JimmyCushnie committed Oct 2, 2024
1 parent 5b29246 commit 4404d9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions JECS/Parsing Logic/DataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ internal static string GetLineTextIncludingChildLines(Line line)
/// <summary>
/// Turns a data structure into raw JECS.
/// </summary>
internal static string JecsFromDataStructure(IEnumerable<Line> lines)
internal static string JecsFromDataStructure(IReadOnlyList<Line> lines)
{
var jecsBuilder = new StringBuilder();
RecursivelyBuildLines(lines, jecsBuilder);
return FinishJecsBuilder(jecsBuilder);

// All written files should end with a newline. This is convention when generating plaintext files for a number of good reasons.
// If the last line that already exists is empty, then we already have a newline at the end of the file.
// In this case, we need to remove the extra newline that was added in RecursivelyBuildLines.
// Without this check, files gain an extra newline every time they're saved, ad infinitum!
if (lines[lines.Count - 1].RawText == string.Empty)
jecsBuilder.Remove(jecsBuilder.Length - Utilities.NewLine.Length, Utilities.NewLine.Length);

return jecsBuilder.ToString();


void RecursivelyBuildLines(IEnumerable<Line> _lines, StringBuilder builder)
Expand All @@ -32,9 +40,6 @@ void RecursivelyBuildLines(IEnumerable<Line> _lines, StringBuilder builder)
RecursivelyBuildLines(node.ChildLines, builder);
}
}

string FinishJecsBuilder(StringBuilder builder)
=> builder.ToString().TrimEnd('\n', '\r', ' ');
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion JECS/Parsing Logic/Nodes/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Line(string rawText)
this.RawText = rawText;
}

public string RawText { get; set; }
public string RawText { get; protected set; }
public int IndentationLevel
{
get => RawText.GetIndentationLevel();
Expand Down

0 comments on commit 4404d9e

Please sign in to comment.