summaryrefslogtreecommitdiff
path: root/src/source.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/source.rs')
-rw-r--r--src/source.rs128
1 files changed, 9 insertions, 119 deletions
diff --git a/src/source.rs b/src/source.rs
index aaf009e0..421412ee 100644
--- a/src/source.rs
+++ b/src/source.rs
@@ -10,9 +10,9 @@ use serde::{Deserialize, Serialize};
use crate::diag::TypResult;
use crate::loading::{FileHash, Loader};
-use crate::parse::{is_newline, parse, Scanner};
+use crate::parse::{is_newline, parse, Reparser, Scanner};
use crate::syntax::ast::Markup;
-use crate::syntax::{self, Category, GreenNode, RedNode, Reparser, Span};
+use crate::syntax::{self, Category, GreenNode, RedNode, Span};
use crate::util::PathExt;
#[cfg(feature = "codespan-reporting")]
@@ -286,7 +286,7 @@ impl SourceFile {
// Update the root node.
let span = Span::new(self.id, replace.start, replace.end);
let reparser = Reparser::new(&self.src, span, with.len());
- if let Ok(range) = reparser.incremental(Rc::make_mut(&mut self.root)) {
+ if let Ok(range) = reparser.reparse(Rc::make_mut(&mut self.root)) {
range
} else {
self.root = parse(&self.src);
@@ -302,6 +302,12 @@ impl SourceFile {
let red = RedNode::from_root(self.root.clone(), self.id);
syntax::highlight(red.as_ref(), range, &mut f)
}
+
+ /// Obtain a reference to the source's root green node.
+ #[cfg(test)]
+ pub(crate) fn root(&self) -> Rc<GreenNode> {
+ self.root.clone()
+ }
}
/// The indices at which lines start (right behind newlines).
@@ -480,120 +486,4 @@ mod tests {
// Test removing everything.
test(TEST, 0 .. 21, "", "");
}
-
- #[test]
- fn test_incremental_parse() {
- #[track_caller]
- fn test(prev: &str, range: Range<usize>, with: &str, incr: Range<usize>) {
- let mut source = SourceFile::detached(prev);
- let range = source.edit(range, with);
- assert_eq!(range, incr);
-
- let incr_tree = source.root.clone();
- assert_eq!(parse(source.src()), incr_tree);
- }
-
- // Test simple replacements.
- test("hello world", 6 .. 11, "wankers", 5 .. 13);
- test("a d e", 1 .. 3, " b c d", 0 .. 8);
- test("a #f() e", 1 .. 6, " b c d", 0 .. 8);
- test("{(0, 1, 2)}", 5 .. 6, "11pt", 5 .. 9);
- test("= A heading", 3 .. 3, "n evocative", 2 .. 15);
- test("your thing", 5 .. 5, "a", 4 .. 11);
- test("a your thing a", 6 .. 7, "a", 2 .. 12);
- test("{call(); abc}", 7 .. 7, "[]", 0 .. 15);
- test("#call() abc", 7 .. 7, "[]", 0 .. 13);
- // test(
- // "hi\n- item\n- item 2\n - item 3",
- // 10 .. 10,
- // " ",
- // 9 .. 33,
- // );
- test(
- "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
- 16 .. 20,
- "none",
- 16 .. 20,
- );
- test(
- "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
- 33 .. 42,
- "[_gronk_]",
- 33 .. 42,
- );
- test(
- "#grid(columns: (auto, 1fr, 40%), [*plonk*], rect(width: 100%, height: 1pt, fill: conifer), [thing])",
- 34 .. 41,
- "_bar_",
- 34 .. 39,
- );
- test("{let i=1; for x in range(5) {i}}", 6 .. 6, " ", 1 .. 9);
- test("{let i=1; for x in range(5) {i}}", 13 .. 14, " ", 13 .. 15);
- test("hello {x}", 6 .. 9, "#f()", 5 .. 10);
- test(
- "this is -- in my opinion -- spectacular",
- 8 .. 10,
- "---",
- 7 .. 12,
- );
- test(
- "understanding `code` is complicated",
- 15 .. 15,
- "C ",
- 14 .. 22,
- );
- test("{ let x = g() }", 10 .. 12, "f(54", 0 .. 17);
- test(
- "a #let rect with (fill: eastern)\nb",
- 16 .. 31,
- " (stroke: conifer",
- 2 .. 34,
- );
-
- // Test the whitespace invariants.
- test("hello \\ world", 7 .. 8, "a ", 6 .. 14);
- test("hello \\ world", 7 .. 8, " a", 6 .. 14);
- test("x = y", 1 .. 1, " + y", 0 .. 6);
- test("x = y", 1 .. 1, " + y\n", 0 .. 10);
- test("abc\n= a heading\njoke", 3 .. 4, "\nmore\n\n", 0 .. 21);
- test("abc\n= a heading\njoke", 3 .. 4, "\nnot ", 0 .. 19);
- test("hey #myfriend", 4 .. 4, "\\", 0 .. 14);
- test("hey #myfriend", 4 .. 4, "\\", 3 .. 6);
-
- // Test type invariants.
- test("a #for x in array {x}", 18 .. 21, "[#x]", 2 .. 22);
- test("a #let x = 1 {5}", 3 .. 6, "if", 0 .. 15);
- test("a {let x = 1 {5}} b", 3 .. 6, "if", 2 .. 16);
- test("#let x = 1 {5}", 4 .. 4, " if", 0 .. 17);
- test("{let x = 1 {5}}", 4 .. 4, " if", 0 .. 18);
- test("a // b c #f()", 3 .. 4, "", 0 .. 12);
- test("{\nf()\n//g(a)\n}", 6 .. 8, "", 0 .. 12);
- test("a{\nf()\n//g(a)\n}b", 7 .. 9, "", 1 .. 13);
- test("a #while x {\n g(x) \n} b", 11 .. 11, "//", 0 .. 26);
- test("{(1, 2)}", 1 .. 1, "while ", 0 .. 14);
- test("a b c", 1 .. 1, "{[}", 0 .. 5);
-
- // Test unclosed things.
- test(r#"{"hi"}"#, 4 .. 5, "c", 0 .. 6);
- test(r"this \u{abcd}", 8 .. 9, "", 5 .. 12);
- test(r"this \u{abcd} that", 12 .. 13, "", 0 .. 17);
- test(r"{{let x = z}; a = 1} b", 6 .. 6, "//", 0 .. 24);
- test("a b c", 1 .. 1, " /* letters */", 0 .. 16);
- test("a b c", 1 .. 1, " /* letters", 0 .. 16);
- test(
- "{if i==1 {a} else [b]; b()}",
- 12 .. 12,
- " /* letters */",
- 1 .. 35,
- );
- test(
- "{if i==1 {a} else [b]; b()}",
- 12 .. 12,
- " /* letters",
- 0 .. 38,
- );
-
- test(r#"a ```typst hello``` b"#, 16 .. 17, "", 0 .. 20);
- test(r#"a ```typst hello```"#, 16 .. 17, "", 2 .. 18);
- }
}