Skip to content

Commit

Permalink
Fix newline handling on Windows (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygg01 committed Sep 10, 2023
1 parent 7b1a947 commit 8255ea7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
22 changes: 21 additions & 1 deletion Linguini.Syntax/Ast/Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,37 @@ public interface IPatternElement

public class TextElementPlaceholder : IPatternElementPlaceholder
{
/// <summary>
/// Start of text slice
/// </summary>
public int Start { get; }

/// <summary>
/// End of text slice
/// </summary>
public int End { get; }
/// <summary>
/// Indent of Text element
/// </summary>
public int Indent { get; }

/// <summary>
/// Text element position used in pattern processing
/// </summary>
public TextElementPosition Role { get; }

/// <summary>
/// Boolean flag to add an EOL to text slice. It's used on Windows to make sure newlines are processed correctly
/// </summary>
public bool MissingEol { get; }

public TextElementPlaceholder(int start, int end, int indent, TextElementPosition role)
public TextElementPlaceholder(int start, int end, int indent, TextElementPosition role, bool missingEol)
{
Start = start;
End = end;
Indent = indent;
Role = role;
MissingEol = missingEol;
}
}
}
29 changes: 21 additions & 8 deletions Linguini.Syntax/Parser/LinguiniParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ private bool TryGetPattern(out Pattern? pattern, out ParseError? error)
sliceStart,
text.End,
indent,
textElementRole
textElementRole,
text.TerminationReason == TextElementTermination.CRLF
));
}
}
Expand Down Expand Up @@ -567,7 +568,18 @@ private bool TryGetPattern(out Pattern? pattern, out ParseError? error)
}
}

var value = _reader.ReadSlice(start, end);
ReadOnlyMemory<char> value;
if (textLiteral.MissingEol)
{
var str = _reader.ReadSlice(start, end) + "\n";
value = str.AsMemory();
}
else
{
value = _reader.ReadSlice(start, end);
}
// var value = _reader.ReadSlice(start, end);

if (lastNonBlank == i)
{
#if NET5_0_OR_GREATER
Expand Down Expand Up @@ -630,17 +642,18 @@ private bool TryGetTextSlice([NotNullWhen(true)] out TextSlice? textElement, out
else if ('\r' == c
&& '\n' == _reader.PeekChar(1))
{
_reader.Position += 2;
_reader.Row += 1;

// This takes one less element because it converts CRLF endings
// to LF endings
// If this is one `/r/n` (CRLF) line ending we take position before CRLF
// and set flag in TextElementPlaceholder that value is missing a EOL mark (which is LF)
textElement = new TextSlice(
startPos,
_reader.Position - 1,
_reader.Position,
textElementType,
TextElementTermination.CRLF
);

_reader.Position += 2;
_reader.Row += 1;

error = null;
return true;
}
Expand Down

0 comments on commit 8255ea7

Please sign in to comment.