Skip to content

Commit b931814

Browse files
committed
Various position fixes, and additional test cases
1 parent 5acc942 commit b931814

File tree

8 files changed

+149
-20
lines changed

8 files changed

+149
-20
lines changed

run.n

224 Bytes
Binary file not shown.

src/loreline/Interpreter.hx

+24-8
Original file line numberDiff line numberDiff line change
@@ -1735,13 +1735,6 @@ typedef InterpreterOptions = {
17351735

17361736
function initializeTopLevelFunction(func:NFunctionDecl) {
17371737

1738-
var list:Array<Dynamic> = [
1739-
"carrot",
1740-
1234,
1741-
null,
1742-
"potato",
1743-
];
1744-
17451738
#if hscript
17461739
if (func.name != null) {
17471740
final codeToHscript = new CodeToHscript();
@@ -2703,18 +2696,41 @@ typedef InterpreterOptions = {
27032696
final tags:Array<TextTag> = [];
27042697
var offset = 0;
27052698

2699+
final numParts = str.parts.length;
2700+
27062701
var keepWhitespace = (str.quotes != Unquoted);
27072702
var keepIndents = (str.quotes != Unquoted);
27082703
var keepComments = (str.quotes != Unquoted);
27092704

2710-
for (i in 0...str.parts.length) {
2705+
var trailingTextPartIndex = -1;
2706+
if (!keepWhitespace) {
2707+
var n = numParts - 1;
2708+
while (n >= 0) {
2709+
final part = str.parts[n];
2710+
switch part.partType {
2711+
case Raw(_):
2712+
// This is the trailing text part
2713+
trailingTextPartIndex = n;
2714+
break;
2715+
case Expr(_):
2716+
break; // No trailing text part
2717+
case Tag(_, _):
2718+
n--;
2719+
}
2720+
}
2721+
}
2722+
2723+
for (i in 0...numParts) {
27112724
final part = str.parts[i];
27122725

27132726
switch (part.partType) {
27142727
case Raw(text):
27152728
if (!keepWhitespace) {
27162729
text = text.ltrim();
27172730
}
2731+
if (i == trailingTextPartIndex) {
2732+
text = text.rtrim();
2733+
}
27182734
if (!keepComments) {
27192735
text = stripStringComments(text);
27202736
}

src/loreline/Parser.hx

+12-9
Original file line numberDiff line numberDiff line change
@@ -1621,12 +1621,12 @@ class ParserContext {
16211621
* @return StringPart representing the tag
16221622
*/
16231623
function parseStringTag(closing:Bool, start:Int, length:Int, content:String, quotes:Quotes, attachments:Array<LStringAttachment>):NStringPart {
1624-
var pos = makeStringPartPosition(currentPos(), content, start);
1625-
pos.length = length;
1626-
1624+
var strPos = currentPos();
16271625
if (quotes != Unquoted) {
1628-
pos = pos.withOffset(content, 1, pos.length);
1626+
strPos = strPos.withOffset(content, 1, strPos.length - 2);
16291627
}
1628+
var pos = makeStringPartPosition(strPos, content, start);
1629+
pos.length = length;
16301630

16311631
// Calculate tag content boundaries
16321632
final offsetStart = (closing ? 2 : 1);
@@ -1654,7 +1654,7 @@ class ParserContext {
16541654

16551655
// Return simple tag if no interpolations
16561656
if (!hasAttachmentsInRange) {
1657-
final partPos = makeStringPartPosition(pos, content, innerStart);
1657+
final partPos = makeStringPartPosition(strPos, content, innerStart);
16581658
partPos.length = innerLength;
16591659
final literalId = nextNodeId(NODE);
16601660
final partId = nextNodeId(NODE);
@@ -1683,22 +1683,25 @@ class ParserContext {
16831683
if (aStart >= innerStart && aEnd <= innerEnd) {
16841684
// Add raw text before interpolation
16851685
if (aStart > currentPos) {
1686-
final partPos = makeStringPartPosition(pos, content.uSubstr(start), currentPos - start + offsetStart);
1687-
partPos.length = aStart - currentPos;
1686+
final partPos = makeStringPartPosition(strPos, content, innerStart);
1687+
partPos.length = aStart - innerStart;
16881688
parts.push(new NStringPart(nextNodeId(NODE), partPos, Raw(
16891689
content.uSubstr(currentPos, aStart - currentPos)
16901690
)));
16911691
}
16921692

16931693
// Process interpolation
1694-
parts.push(parseStringInterpolation(
1694+
final interpPart = parseStringInterpolation(
16951695
braces,
16961696
true,
16971697
tokens,
16981698
aStart,
16991699
aLength,
17001700
content
1701-
));
1701+
);
1702+
interpPart.pos = makeStringPartPosition(strPos, content, aStart);
1703+
interpPart.pos.length = aLength;
1704+
parts.push(interpPart);
17021705

17031706
currentPos = aEnd;
17041707
}

src/loreline/lsp/Server.hx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ class Server {
14881488
case Expr(expr):
14891489
return makeNodeHover(lorelinePos, lens, uri, content, expr);
14901490
case Tag(closing, expr):
1491-
return makeHover(hoverTitle('Tag', "&lt;" + printLoreline(expr) + "&gt;"), hoverDescriptionForNode(expr), content, stringPart);
1491+
return makeHover(hoverTitle('Tag', "&lt;" + (closing ? "/" : "") + printLoreline(expr) + "&gt;"), hoverDescriptionForNode(expr), content, stringPart);
14921492
}
14931493

14941494
final literal = lens.getFirstParentOfType(node, NStringLiteral);
@@ -1502,7 +1502,7 @@ class Server {
15021502
case Expr(expr):
15031503
return makeNodeHover(lorelinePos, lens, uri, content, expr);
15041504
case Tag(closing, expr):
1505-
return makeHover(hoverTitle('Tag', "&lt;" + printLoreline(expr) + "&gt;"), hoverDescriptionForNode(expr), content, parentStringPart);
1505+
return makeHover(hoverTitle('Tag', "&lt;" + (closing ? "/" : "") + printLoreline(expr) + "&gt;"), hoverDescriptionForNode(expr), content, parentStringPart);
15061506
}
15071507
}
15081508

src/loreline/test/TestRunner.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TestRunner {
8181
}
8282
for (t in 0...tags.length) {
8383
final tag = tags[t];
84-
if (tag.offset > len) {
84+
if (tag.offset >= len) {
8585
textBuf.addChar("<".code);
8686
textBuf.addChar("<".code);
8787
if (tag.closing) {

test/Characters-Advanced.lor

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
character barista
3+
name: Alex
4+
friendliness: 3
5+
expertise: high
6+
7+
character customer
8+
name: Jamie
9+
firstVisit: true
10+
favoriteDrink: "Americano"
11+
12+
// Testing character property interpolation
13+
This coffee shop is run by $barista.name who has $barista.friendliness friendliness points.
14+
15+
// Testing character property in conditionals
16+
if barista.expertise == "high"
17+
barista: I've been making coffee for 10 years now.
18+
else
19+
barista: I'm still learning the craft.
20+
21+
// Testing character name resolution with name property
22+
$barista greets you with a smile.
23+
24+
$customer looks around nervously.
25+
26+
// Testing character property update
27+
if customer.firstVisit
28+
barista: Welcome to our shop! First time here?
29+
customer.firstVisit = false
30+
else
31+
barista: Welcome back! Would you like your usual $customer.favoriteDrink?
32+
33+
/*
34+
<test>
35+
- expected: |
36+
~ This coffee shop is run by Alex who has 3 friendliness points.
37+
38+
Alex: I've been making coffee for 10 years now.
39+
40+
~ Alex greets you with a smile.
41+
42+
~ Jamie looks around nervously.
43+
44+
Alex: Welcome to our shop! First time here?
45+
</test>
46+
*/

test/Functions-Basic.lor

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2+
// Create a function with simple arithmetics
23
function add(a, b)
34
return a + b
45

56
state
67
apples: 7
78
oranges: 3
89

10+
// Call the function with state values as arguments and display the result in text
911
We have $apples apples and $oranges oranges, which makes a total of $add(apples, oranges) fruits!
1012

1113
/*

test/Tags-Formatting.lor

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
character barista
3+
name: Alex
4+
5+
character customer
6+
name: Jamie
7+
8+
// Testing basic tags
9+
barista: <happy>Great to see you again!</happy>
10+
11+
// Testing nested tags
12+
barista: <excited><loud>WELCOME BACK!</loud> It's been a while.</excited>
13+
14+
// Testing space between text and tags (space should be ignored)
15+
barista: <slightly confused> Didn't you order something different last time?
16+
17+
// Testing space between text and tags in double quotes (space should be preserved)
18+
barista: "<relaxed> Well, nevermind!"
19+
20+
// Testing tags in narrative text
21+
The espresso machine <whirs>makes a pleasant humming sound</whirs> as the <steam>hot vapor rises</steam>.
22+
23+
// Testing tags with interpolation
24+
barista: <knows $customer.name>Oh hey, $customer.name! Your usual?
25+
26+
/* Testing tags in multiline dialogue: there should be
27+
no space between them and the actual text content */
28+
barista:
29+
<professional>
30+
Let me explain our seasonal offerings.
31+
We have three new blends this month.
32+
Each one has unique characteristics.
33+
</professional>
34+
35+
/* Another multiline dialogue where white spaces between tags should be preserved */
36+
barista: "<enthusiastic>
37+
I personally recommend our Ethiopian roast.
38+
It has bright notes of citrus and chocolate.
39+
Perfect for an afternoon pick-me-up!
40+
</enthusiastic>"
41+
42+
/*
43+
<test>
44+
- expected: |
45+
Alex: <<happy>>Great to see you again!<</happy>>
46+
47+
Alex: <<excited>><<loud>>WELCOME BACK!<</loud>> It's been a while.<</excited>>
48+
49+
Alex: <<slightly confused>>Didn't you order something different last time?
50+
51+
Alex: <<relaxed>> Well, nevermind!
52+
53+
~ The espresso machine <<whirs>>makes a pleasant humming sound<</whirs>> as the <<steam>>hot vapor rises<</steam>>.
54+
55+
Alex: <<knows Jamie>>Oh hey, Jamie! Your usual?
56+
57+
Alex:
58+
<<professional>>Let me explain our seasonal offerings.
59+
We have three new blends this month.
60+
Each one has unique characteristics.<</professional>>
61+
</test>
62+
*/

0 commit comments

Comments
 (0)