[education/rkward] rkward: Fix failure to detect end of chunk, if chunk ends with a comment
Thomas Friedrichsmeier
null at kde.org
Tue May 27 21:33:14 BST 2025
Git commit 05f26d20465f4a441d2e922d3433fbc9dcccf110 by Thomas Friedrichsmeier.
Committed on 27/05/2025 at 20:32.
Pushed by tfry into branch 'master'.
Fix failure to detect end of chunk, if chunk ends with a comment
M +1 -1 rkward/autotests/data/script1.Rmd
M +10 -2 rkward/autotests/rkparsedscript_test.cpp
M +2 -1 rkward/misc/rkparsedscript.cpp
https://invent.kde.org/education/rkward/-/commit/05f26d20465f4a441d2e922d3433fbc9dcccf110
diff --git a/rkward/autotests/data/script1.Rmd b/rkward/autotests/data/script1.Rmd
index 6f8a981de..4adeeebf0 100644
--- a/rkward/autotests/data/script1.Rmd
+++ b/rkward/autotests/data/script1.Rmd
@@ -12,7 +12,7 @@ Usually, there would be lot of markdown (and this may contain many relevant mark
```{r echo=TRUE}
symb01 <-symb02()
symb03 <- symb04
-symb05(symb06); symb07
+symb05(symb06); symb07 # comment at end of region shall not confuse us
```
Usually, code chunks shall contain {r}, but we're relaxed about that.
diff --git a/rkward/autotests/rkparsedscript_test.cpp b/rkward/autotests/rkparsedscript_test.cpp
index 62f491e7b..1a51825d0 100644
--- a/rkward/autotests/rkparsedscript_test.cpp
+++ b/rkward/autotests/rkparsedscript_test.cpp
@@ -85,7 +85,7 @@ class RKParsedScriptTest : public QObject {
ctx = ctx0;
while (ctx.valid())
ctx = ps.nextStatement(ctx);
- ctx = ps.firstContextInStatement(ctx0); // NOTE: This one may stay at the same position
+/* ctx = ps.firstContextInStatement(ctx0); // NOTE: This one may stay at the same position
ctx = ps.firstContextInChunk(ctx0);
ctx = ctx0;
while (ctx.valid())
@@ -129,7 +129,7 @@ class RKParsedScriptTest : public QObject {
ctx = ctx0;
while (ctx.valid())
- ctx = ps.parentRegion(ctx);
+ ctx = ps.parentRegion(ctx); */
}
}
private Q_SLOTS:
@@ -313,6 +313,14 @@ class RKParsedScriptTest : public QObject {
ctx = moveAndCheck(ps.nextStatement(ctx), u"symb03"_s);
ctx = moveAndCheck(ps.nextStatement(ctx), u"symb05"_s);
ctx = moveAndCheck(ps.nextStatement(ctx), u"symb07"_s);
+ int posa = ps.getContext(ps.firstContextInStatement(ctx)).start;
+ int posb = ps.lastPositionInStatement(ctx);
+ QVERIFY(posb > posa);
+ // NOTE: Note sure wether or not we want to include the trailing comment,
+ // as part of the statement, so this test will only check the start
+ QVERIFY(script.mid(posa, posb-posa+1).startsWith(u"symb07"_s));
+ // We do not want to include the delimiting newline, however
+ QVERIFY(!script.mid(posa, posb-posa+1).contains(u'\n'));
QVERIFY(!ps.nextStatement(ctx).valid());
ctx = ps.contextAtPos(script.indexOf(u"symb14"));
diff --git a/rkward/misc/rkparsedscript.cpp b/rkward/misc/rkparsedscript.cpp
index 9e0c4428c..aa31cba52 100644
--- a/rkward/misc/rkparsedscript.cpp
+++ b/rkward/misc/rkparsedscript.cpp
@@ -65,7 +65,8 @@ int RKParsedScript::addNextMarkdownChunk(int start, const QString &content) {
context_list.emplace_back(Comment, start, chunkstart - 1);
addContext(Top, chunkstart - 1, content.left(chunkend)); // in case markdown region has incomplete syntax
// limit parsing to the actual markdown region
- context_list.emplace_back(Top, chunkend, chunkend); // HACK: Used as a dummy separtor, here...
+ if (context_list.back().type != Delimiter) context_list.emplace_back(Delimiter, chunkend, chunkend);
+ context_list.emplace_back(Top, chunkend, chunkend); // HACK: Used as a dummy separator, here...
return chunkend + chunk_barrier.length();
}
More information about the rkward-tracker
mailing list