Skip to content

Commit

Permalink
Fix auto-indent (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed May 29, 2022
1 parent 5d192a7 commit b849c54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Scriban.Tests/TestFiles/000-basic/004-indent.out.txt
@@ -0,0 +1,12 @@
[
[
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet.
]
[
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet.
]
]
13 changes: 13 additions & 0 deletions src/Scriban.Tests/TestFiles/000-basic/004-indent.txt
@@ -0,0 +1,13 @@
[
{{~ for i in 1..2 ~}}
[
{{~ for j in 1..3 ~}}
{{~ if true ~}}
Lorem ipsum dolor {{"sit"}} {{"amet"}}.
{{~ else ~}}
Ut enim ad minim veniam.
{{~ end ~}}
{{~ end ~}}
]
{{~ end ~}}
]
11 changes: 7 additions & 4 deletions src/Scriban/Parsing/Parser.Statements.cs
Expand Up @@ -292,13 +292,16 @@ private ScriptEscapeStatement ParseEscapeStatement()
{
string indent = null;

var text = rawStatement.Text;
for (int j = text.Length - 1; j >= 0; j--)
// Iterate on the original string to detect \n
var slice = rawStatement.Text;
var text = slice.FullText;
var end = slice.Index + slice.Length - 1;
for (int j = end; j >= 0; j--)
{
var c = text[j];
if (c == '\n')
{
indent = text.Substring(j + 1);
indent = text.Substring(j + 1, end - j);
break;
}

Expand All @@ -312,7 +315,7 @@ private ScriptEscapeStatement ParseEscapeStatement()
// We have a raw statement that has only white spaces
// It could be the first raw statement of the document
// so we continue but we handle it later
indent = text.ToString();
indent = text.Substring(0, end + 1);
}
}
scriptEscapeStatement.Indent = indent;
Expand Down

0 comments on commit b849c54

Please sign in to comment.