(source_code.find("console"))})
+ );
+ });
+
+ it("can parse code spread across multiple ranges in a document", [&]() {
+ string source_code =
+ "html `Hello, ${name.toUpperCase()}, it's ${now()}.
`";
+
+ ts_parser_set_language(parser, load_real_language("javascript"));
+ TSTree *js_tree = ts_parser_parse_string(parser, nullptr, source_code.c_str(), source_code.size());
+ TSNode root_node = ts_tree_root_node(js_tree);
+ TSNode string_node = ts_node_descendant_for_byte_range(
+ root_node,
+ source_code.find(""),
+ source_code.find("Hell")
+ );
+ TSNode open_quote_node = ts_node_child(string_node, 0);
+ TSNode interpolation_node1 = ts_node_child(string_node, 1);
+ TSNode interpolation_node2 = ts_node_child(string_node, 2);
+ TSNode close_quote_node = ts_node_child(string_node, 3);
+
+ AssertThat(ts_node_type(string_node), Equals("template_string"));
+ AssertThat(ts_node_type(open_quote_node), Equals("`"));
+ AssertThat(ts_node_type(interpolation_node1), Equals("template_substitution"));
+ AssertThat(ts_node_type(interpolation_node2), Equals("template_substitution"));
+ AssertThat(ts_node_type(close_quote_node), Equals("`"));
+ ts_tree_delete(js_tree);
+
+ TSRange included_ranges[] = {
+ {
+ ts_node_end_point(open_quote_node),
+ ts_node_start_point(interpolation_node1),
+ ts_node_end_byte(open_quote_node),
+ ts_node_start_byte(interpolation_node1),
+ },
+ {
+ ts_node_end_point(interpolation_node1),
+ ts_node_start_point(interpolation_node2),
+ ts_node_end_byte(interpolation_node1),
+ ts_node_start_byte(interpolation_node2),
+ },
+ {
+ ts_node_end_point(interpolation_node2),
+ ts_node_start_point(close_quote_node),
+ ts_node_end_byte(interpolation_node2),
+ ts_node_start_byte(close_quote_node),
+ }
+ };
+
+ ts_parser_set_included_ranges(parser, included_ranges, 3);
+ ts_parser_set_language(parser, load_real_language("html"));
+ tree = ts_parser_parse_string(parser, nullptr, source_code.c_str(), source_code.size());
+
+ assert_root_node("(fragment "
+ "(element "
+ "(start_tag (tag_name)) "
+ "(text) "
+ "(element "
+ "(start_tag (tag_name)) "
+ "(end_tag (tag_name))) "
+ "(text) "
+ "(end_tag (tag_name))))");
+
+ root_node = ts_tree_root_node(tree);
+ TSNode hello_text_node = ts_node_child(ts_node_child(root_node, 0), 1);
+
+ AssertThat(ts_node_type(hello_text_node), Equals("text"));
+ AssertThat(
+ ts_node_start_point(hello_text_node),
+ Equals({0, static_cast(source_code.find("Hello"))})
+ );
+ AssertThat(
+ ts_node_end_point(hello_text_node),
+ Equals({0, static_cast(source_code.find(""))})
+ );
+ });
+ });
});
END_TEST
diff --git a/test/runtime/tree_test.cc b/test/runtime/tree_test.cc
index b599f568..d703cd60 100644
--- a/test/runtime/tree_test.cc
+++ b/test/runtime/tree_test.cc
@@ -131,16 +131,22 @@ describe("Tree", [&]() {
return result;
};
+ auto range_for_text = [&](string start_text, string end_text) {
+ return TSRange {
+ point(0, input->content.find(start_text)),
+ point(0, input->content.find(end_text)),
+ static_cast(input->content.find(start_text)),
+ static_cast(input->content.find(end_text)),
+ };
+ };
+
it("reports changes when one token has been updated", [&]() {
// Replace `null` with `nothing`
auto ranges = get_changed_ranges_for_edit([&]() {
return input->replace(input->content.find("ull"), 1, "othing");
});
AssertThat(ranges, Equals(vector({
- TSRange{
- point(0, input->content.find("nothing")),
- point(0, input->content.find("}"))
- },
+ range_for_text("nothing", "}"),
})));
// Replace `nothing` with `null` again
@@ -148,10 +154,7 @@ describe("Tree", [&]() {
return input->undo();
});
AssertThat(ranges, Equals(vector({
- TSRange{
- point(0, input->content.find("null")),
- point(0, input->content.find("}"))
- },
+ range_for_text("null", "}"),
})));
});
@@ -192,10 +195,7 @@ describe("Tree", [&]() {
return input->replace(input->content.find("}"), 0, ", b: false");
});
AssertThat(ranges, Equals(vector({
- TSRange{
- point(0, input->content.find(",")),
- point(0, input->content.find("}"))
- },
+ range_for_text(",", "}"),
})));
// Add a third key-value pair in between the first two
@@ -209,10 +209,7 @@ describe("Tree", [&]() {
"(pair (property_identifier) (false)))))"
);
AssertThat(ranges, Equals(vector({
- TSRange{
- point(0, input->content.find(", c")),
- point(0, input->content.find(", b"))
- },
+ range_for_text(", c", ", b"),
})));
// Delete the middle pair.
@@ -247,10 +244,7 @@ describe("Tree", [&]() {
"(pair (property_identifier) (binary_expression (identifier) (null))))))"
);
AssertThat(ranges, Equals(vector({
- TSRange{
- point(0, input->content.find("b ===")),
- point(0, input->content.find("}"))
- },
+ range_for_text("b ===", "}"),
})));
});
});