Skip to content

Commit 2ba454c

Browse files
committed
Tidy up whitespace in choices by leaving it to runtime to clean
1 parent f30c0ba commit 2ba454c

5 files changed

Lines changed: 91 additions & 62 deletions

File tree

compiler/InkParser/InkParser_Choices.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ protected Choice Choice()
6161
innerContent = new ContentList (innerTextAndLogic);
6262
}
6363

64-
// Trim
65-
if( innerContent )
66-
TrimChoiceContent (ref innerContent);
67-
else if( optionOnlyContent )
68-
TrimChoiceContent (ref optionOnlyContent);
69-
else
70-
TrimChoiceContent (ref startContent);
71-
7264
Whitespace ();
7365

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

98-
innerContent.AddContent (new Text ("\n"));
99-
10090
// Normal diverts on the end of a choice - simply add to the normal content
10191
if (diverts != null) {
10292
foreach (var divObj in diverts) {
@@ -112,7 +102,10 @@ protected Choice Choice()
112102
}
113103
}
114104

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

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

127120
}
128-
129-
void TrimChoiceContent(ref ContentList content)
130-
{
131-
if (content != null) {
132-
content.TrimTrailingWhitespace ();
133-
if (content.content.Count == 0) {
134-
content = null;
135-
}
136-
}
137-
}
138121

139122
protected Expression ChoiceCondition()
140123
{

compiler/ParsedHierarchy/Choice.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ public override Runtime.Object GenerateRuntimeObject ()
223223
_innerContentContainer.AddContentsOfContainer (innerChoiceOnlyContent);
224224
}
225225

226-
// Fully parsed choice will be a full line, so it needs to be terminated
227-
if (startContent || innerContent) {
228-
_innerContentContainer.AddContent(new Runtime.StringValue("\n"));
229-
}
230-
231226
if (this.story.countAllVisits) {
232227
_innerContentContainer.visitsShouldBeCounted = true;
233228
_innerContentContainer.turnIndexShouldBeCounted = true;

ink-engine-runtime/Story.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ Choice ProcessChoice(ChoicePoint choicePoint)
823823
choice.threadAtGeneration = state.callStack.currentThread.Copy ();
824824

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

828828
return choice;
829829
}

ink-engine-runtime/StoryState.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ internal string currentText
162162
}
163163
}
164164

165-
_currentText = sb.ToString ();
165+
_currentText = CleanOutputWhitespace (sb.ToString ());
166166

167167
_outputStreamTextDirty = false;
168168
}
@@ -172,6 +172,34 @@ internal string currentText
172172
}
173173
string _currentText;
174174

175+
string CleanOutputWhitespace (string str)
176+
{
177+
var sb = new StringBuilder (str.Length);
178+
179+
int currentWhitespaceStart = -1;
180+
181+
for (int i = 0; i < str.Length; i++) {
182+
var c = str [i];
183+
184+
bool isInlineWhitespace = c == ' ' || c == '\t';
185+
186+
if (isInlineWhitespace && currentWhitespaceStart == -1)
187+
currentWhitespaceStart = i;
188+
189+
if (!isInlineWhitespace) {
190+
if (c != '\n' && currentWhitespaceStart > 0) {
191+
sb.Append (str.Substring (currentWhitespaceStart, i - currentWhitespaceStart));
192+
}
193+
currentWhitespaceStart = -1;
194+
}
195+
196+
if (!isInlineWhitespace)
197+
sb.Append (c);
198+
}
199+
200+
return sb.ToString ();
201+
}
202+
175203
internal List<string> currentTags
176204
{
177205
get

tests/Tests.cs

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,21 @@ public void TestBlanksInInlineSequences()
172172

173173
Assert.AreEqual(
174174
@"1. a
175-
2.
175+
2.
176176
3. b
177177
4. b
178178
---
179-
1.
179+
1.
180180
2. a
181181
3. a
182182
---
183183
1. a
184-
2.
185-
3.
184+
2.
185+
3.
186186
---
187-
1.
188-
2.
189-
3.
187+
1.
188+
2.
189+
3.
190190
".Replace("\r", ""), story.ContinueMaximally().Replace("\r", ""));
191191
}
192192

@@ -240,7 +240,7 @@ public void TestChoiceDivertsToDone()
240240
Assert.AreEqual(1, story.currentChoices.Count);
241241
story.ChooseChoiceIndex(0);
242242

243-
Assert.AreEqual("choice\n", story.Continue());
243+
Assert.AreEqual("choice", story.Continue());
244244
Assert.IsFalse(story.hasError);
245245
}
246246

@@ -818,7 +818,7 @@ EXTERNAL times(i,str)
818818

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

821-
Assert.AreEqual("knock knock knock \n", story.Continue());
821+
Assert.AreEqual("knock knock knock\n", story.Continue());
822822

823823
Assert.AreEqual("MESSAGE: hello world", message);
824824
}
@@ -1034,7 +1034,7 @@ public void TestImplicitInlineGlueB ()
10341034
}
10351035
");
10361036

1037-
Assert.AreEqual ("A \nX\n", story.ContinueMaximally ());
1037+
Assert.AreEqual ("A\nX\n", story.ContinueMaximally ());
10381038
}
10391039

10401040
[Test ()]
@@ -1370,8 +1370,7 @@ public void TestNonTextInChoiceInnerContent()
13701370
story.Continue();
13711371

13721372
story.ChooseChoiceIndex(0);
1373-
Assert.AreEqual("option text. Conditional bit.\n", story.Continue());
1374-
Assert.AreEqual("Next.\n", story.Continue());
1373+
Assert.AreEqual("option text. Conditional bit. Next.\n", story.Continue());
13751374
}
13761375

13771376
[Test()]
@@ -1829,10 +1828,10 @@ public void TestStringsInChoices()
18291828
story.ContinueMaximally();
18301829

18311830
Assert.AreEqual(1, story.currentChoices.Count);
1832-
Assert.AreEqual(@" test1 ""test2 test3""", story.currentChoices[0].text);
1831+
Assert.AreEqual(@"test1 ""test2 test3""", story.currentChoices[0].text);
18331832

18341833
story.ChooseChoiceIndex(0);
1835-
Assert.AreEqual(" test1 test4\n", story.Continue());
1834+
Assert.AreEqual("test1 test4\n", story.Continue());
18361835
}
18371836

18381837
[Test()]
@@ -2413,10 +2412,10 @@ public void TestTempUsageInOptions ()
24132412
story.Continue ();
24142413

24152414
Assert.AreEqual (1, story.currentChoices.Count);
2416-
Assert.AreEqual (" 1", story.currentChoices[0].text);
2415+
Assert.AreEqual ("1", story.currentChoices[0].text);
24172416
story.ChooseChoiceIndex (0);
24182417

2419-
Assert.AreEqual (" 1\nEnd of choice\nthis another\n", story.ContinueMaximally ());
2418+
Assert.AreEqual ("1\nEnd of choice\nthis another\n", story.ContinueMaximally ());
24202419

24212420
Assert.AreEqual (0, story.currentChoices.Count);
24222421
}
@@ -3048,7 +3047,7 @@ public void TestTagOnChoice ()
30483047
var txt = story.Continue ();
30493048
var tags = story.currentTags;
30503049

3051-
Assert.AreEqual (" Hello\n", txt); // argh need to fix space?
3050+
Assert.AreEqual ("Hello", txt);
30523051
Assert.AreEqual (1, tags.Count);
30533052
Assert.AreEqual ("hey", tags[0]);
30543053
}
@@ -3145,23 +3144,6 @@ In top external
31453144
}
31463145

31473146

3148-
3149-
[Test ()]
3150-
public void TestStartingLineWithEscapedWhitespace ()
3151-
{
3152-
var storyStr =
3153-
@"
3154-
hello{1:
3155-
\ world
3156-
}
3157-
";
3158-
3159-
var story = CompileString (storyStr);
3160-
3161-
Assert.AreEqual ("hello\n world\n", story.ContinueMaximally ());
3162-
}
3163-
3164-
31653147
[Test ()]
31663148
public void TestNewlinesWithStringEval ()
31673149
{
@@ -3343,6 +3325,47 @@ public void TestTopFlowTerminatorShouldntKillThreadChoices ()
33433325
}
33443326

33453327

3328+
[Test ()]
3329+
public void TestNewlineConsistency ()
3330+
{
3331+
var storyStr =
3332+
@"
3333+
hello -> world
3334+
== world
3335+
world
3336+
-> END";
3337+
3338+
var story = CompileString (storyStr);
3339+
Assert.AreEqual ("hello world\n", story.ContinueMaximally ());
3340+
3341+
storyStr =
3342+
@"
3343+
* hello -> world
3344+
== world
3345+
world
3346+
-> END";
3347+
story = CompileString (storyStr);
3348+
3349+
story.Continue ();
3350+
story.ChooseChoiceIndex (0);
3351+
Assert.AreEqual ("hello world\n", story.ContinueMaximally ());
3352+
3353+
3354+
storyStr =
3355+
@"
3356+
* hello
3357+
-> world
3358+
== world
3359+
world
3360+
-> END";
3361+
story = CompileString (storyStr);
3362+
3363+
story.Continue ();
3364+
story.ChooseChoiceIndex (0);
3365+
Assert.AreEqual ("hello\nworld\n", story.ContinueMaximally ());
3366+
}
3367+
3368+
33463369
// Helper compile function
33473370
protected Story CompileString(string str, bool countAllVisits = false, bool testingErrors = false)
33483371
{

0 commit comments

Comments
 (0)