Skip to content

Commit

Permalink
Tidy up whitespace in choices by leaving it to runtime to clean
Browse files Browse the repository at this point in the history
  • Loading branch information
joethephish committed Apr 24, 2018
1 parent f30c0ba commit 2ba454c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 62 deletions.
25 changes: 4 additions & 21 deletions compiler/InkParser/InkParser_Choices.cs
Expand Up @@ -61,14 +61,6 @@ protected Choice Choice()
innerContent = new ContentList (innerTextAndLogic);
}

// Trim
if( innerContent )
TrimChoiceContent (ref innerContent);
else if( optionOnlyContent )
TrimChoiceContent (ref optionOnlyContent);
else
TrimChoiceContent (ref startContent);

Whitespace ();

// Finally, now we know we're at the end of the main choice body, parse
Expand All @@ -95,8 +87,6 @@ protected Choice Choice()
innerContent.AddContent(tags);
}

innerContent.AddContent (new Text ("\n"));

// Normal diverts on the end of a choice - simply add to the normal content
if (diverts != null) {
foreach (var divObj in diverts) {
Expand All @@ -112,7 +102,10 @@ protected Choice Choice()
}
}


// Terminate main content with a newline since this is the end of the line
// Note that this will be redundant if the diverts above definitely take
// the flow away permanently.
innerContent.AddContent (new Text ("\n"));

var choice = new Choice (startContent, optionOnlyContent, innerContent);
choice.name = optionalName;
Expand All @@ -125,16 +118,6 @@ protected Choice Choice()
return choice;

}

void TrimChoiceContent(ref ContentList content)
{
if (content != null) {
content.TrimTrailingWhitespace ();
if (content.content.Count == 0) {
content = null;
}
}
}

protected Expression ChoiceCondition()
{
Expand Down
5 changes: 0 additions & 5 deletions compiler/ParsedHierarchy/Choice.cs
Expand Up @@ -223,11 +223,6 @@ public override Runtime.Object GenerateRuntimeObject ()
_innerContentContainer.AddContentsOfContainer (innerChoiceOnlyContent);
}

// Fully parsed choice will be a full line, so it needs to be terminated
if (startContent || innerContent) {
_innerContentContainer.AddContent(new Runtime.StringValue("\n"));
}

if (this.story.countAllVisits) {
_innerContentContainer.visitsShouldBeCounted = true;
_innerContentContainer.turnIndexShouldBeCounted = true;
Expand Down
2 changes: 1 addition & 1 deletion ink-engine-runtime/Story.cs
Expand Up @@ -823,7 +823,7 @@ Choice ProcessChoice(ChoicePoint choicePoint)
choice.threadAtGeneration = state.callStack.currentThread.Copy ();

// Set final text for the choice
choice.text = startText + choiceOnlyText;
choice.text = (startText + choiceOnlyText).Trim(' ', '\t');

return choice;
}
Expand Down
30 changes: 29 additions & 1 deletion ink-engine-runtime/StoryState.cs
Expand Up @@ -162,7 +162,7 @@ internal string currentText
}
}

_currentText = sb.ToString ();
_currentText = CleanOutputWhitespace (sb.ToString ());

_outputStreamTextDirty = false;
}
Expand All @@ -172,6 +172,34 @@ internal string currentText
}
string _currentText;

string CleanOutputWhitespace (string str)
{
var sb = new StringBuilder (str.Length);

int currentWhitespaceStart = -1;

for (int i = 0; i < str.Length; i++) {
var c = str [i];

bool isInlineWhitespace = c == ' ' || c == '\t';

if (isInlineWhitespace && currentWhitespaceStart == -1)
currentWhitespaceStart = i;

if (!isInlineWhitespace) {
if (c != '\n' && currentWhitespaceStart > 0) {
sb.Append (str.Substring (currentWhitespaceStart, i - currentWhitespaceStart));
}
currentWhitespaceStart = -1;
}

if (!isInlineWhitespace)
sb.Append (c);
}

return sb.ToString ();
}

internal List<string> currentTags
{
get
Expand Down
91 changes: 57 additions & 34 deletions tests/Tests.cs
Expand Up @@ -172,21 +172,21 @@ public void TestBlanksInInlineSequences()

Assert.AreEqual(
@"1. a
2.
2.
3. b
4. b
---
1.
1.
2. a
3. a
---
1. a
2.
3.
2.
3.
---
1.
2.
3.
1.
2.
3.
".Replace("\r", ""), story.ContinueMaximally().Replace("\r", ""));
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public void TestChoiceDivertsToDone()
Assert.AreEqual(1, story.currentChoices.Count);
story.ChooseChoiceIndex(0);

Assert.AreEqual("choice\n", story.Continue());
Assert.AreEqual("choice", story.Continue());
Assert.IsFalse(story.hasError);
}

Expand Down Expand Up @@ -818,7 +818,7 @@ ~ message(""hello world"")

Assert.AreEqual("15\n", story.Continue());

Assert.AreEqual("knock knock knock \n", story.Continue());
Assert.AreEqual("knock knock knock\n", story.Continue());

Assert.AreEqual("MESSAGE: hello world", message);
}
Expand Down Expand Up @@ -1034,7 +1034,7 @@ public void TestImplicitInlineGlueB ()
}
");

Assert.AreEqual ("A \nX\n", story.ContinueMaximally ());
Assert.AreEqual ("A\nX\n", story.ContinueMaximally ());
}

[Test ()]
Expand Down Expand Up @@ -1370,8 +1370,7 @@ public void TestNonTextInChoiceInnerContent()
story.Continue();

story.ChooseChoiceIndex(0);
Assert.AreEqual("option text. Conditional bit.\n", story.Continue());
Assert.AreEqual("Next.\n", story.Continue());
Assert.AreEqual("option text. Conditional bit. Next.\n", story.Continue());
}

[Test()]
Expand Down Expand Up @@ -1829,10 +1828,10 @@ public void TestStringsInChoices()
story.ContinueMaximally();

Assert.AreEqual(1, story.currentChoices.Count);
Assert.AreEqual(@" test1 ""test2 test3""", story.currentChoices[0].text);
Assert.AreEqual(@"test1 ""test2 test3""", story.currentChoices[0].text);

story.ChooseChoiceIndex(0);
Assert.AreEqual(" test1 test4\n", story.Continue());
Assert.AreEqual("test1 test4\n", story.Continue());
}

[Test()]
Expand Down Expand Up @@ -2413,10 +2412,10 @@ public void TestTempUsageInOptions ()
story.Continue ();

Assert.AreEqual (1, story.currentChoices.Count);
Assert.AreEqual (" 1", story.currentChoices[0].text);
Assert.AreEqual ("1", story.currentChoices[0].text);
story.ChooseChoiceIndex (0);

Assert.AreEqual (" 1\nEnd of choice\nthis another\n", story.ContinueMaximally ());
Assert.AreEqual ("1\nEnd of choice\nthis another\n", story.ContinueMaximally ());

Assert.AreEqual (0, story.currentChoices.Count);
}
Expand Down Expand Up @@ -3048,7 +3047,7 @@ public void TestTagOnChoice ()
var txt = story.Continue ();
var tags = story.currentTags;

Assert.AreEqual (" Hello\n", txt); // argh need to fix space?
Assert.AreEqual ("Hello", txt);
Assert.AreEqual (1, tags.Count);
Assert.AreEqual ("hey", tags[0]);
}
Expand Down Expand Up @@ -3145,23 +3144,6 @@ EXTERNAL gameInc(x)
}



[Test ()]
public void TestStartingLineWithEscapedWhitespace ()
{
var storyStr =
@"
hello{1:
\ world
}
";

var story = CompileString (storyStr);

Assert.AreEqual ("hello\n world\n", story.ContinueMaximally ());
}


[Test ()]
public void TestNewlinesWithStringEval ()
{
Expand Down Expand Up @@ -3343,6 +3325,47 @@ public void TestTopFlowTerminatorShouldntKillThreadChoices ()
}


[Test ()]
public void TestNewlineConsistency ()
{
var storyStr =
@"
hello -> world
== world
world
-> END";

var story = CompileString (storyStr);
Assert.AreEqual ("hello world\n", story.ContinueMaximally ());

storyStr =
@"
* hello -> world
== world
world
-> END";
story = CompileString (storyStr);

story.Continue ();
story.ChooseChoiceIndex (0);
Assert.AreEqual ("hello world\n", story.ContinueMaximally ());


storyStr =
@"
* hello
-> world
== world
world
-> END";
story = CompileString (storyStr);

story.Continue ();
story.ChooseChoiceIndex (0);
Assert.AreEqual ("hello\nworld\n", story.ContinueMaximally ());
}


// Helper compile function
protected Story CompileString(string str, bool countAllVisits = false, bool testingErrors = false)
{
Expand Down

0 comments on commit 2ba454c

Please sign in to comment.