Add lexer API for detecting boundaries of included ranges

Co-Authored-By: Ashi Krishnan <queerviolet@github.com>
This commit is contained in:
Max Brunsfeld 2018-07-17 13:58:26 -07:00
parent d54412266e
commit 87c992a7f0
9 changed files with 88 additions and 44 deletions

View file

@ -918,6 +918,29 @@ describe("Parser", [&]() {
assert_root_node("(program (ERROR (identifier)))");
});
it("allows external scanners to detect the boundaries of included ranges", [&]() {
string source_code = "a <%= b() %> c <% d() %>";
TSRange included_ranges[] = {
range_for_substring(source_code, "b()"),
range_for_substring(source_code, "d()"),
};
ts_parser_set_included_ranges(parser, included_ranges, 2);
ts_parser_set_language(parser, load_real_language("javascript"));
tree = ts_parser_parse_string(parser, nullptr, source_code.c_str(), source_code.size());
assert_root_node("(program "
"(expression_statement (call_expression (identifier) (arguments))) "
"(expression_statement (call_expression (identifier) (arguments))))");
TSNode statement_node1 = ts_node_child(ts_tree_root_node(tree), 0);
TSNode statement_node2 = ts_node_child(ts_tree_root_node(tree), 1);
AssertThat(ts_node_end_point(statement_node1), Equals(extent_for_string("a <%= b()")));
AssertThat(ts_node_end_point(statement_node2), Equals(extent_for_string("a <%= b() %> c <% d()")));
});
});
});

View file

@ -131,22 +131,13 @@ 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<uint32_t>(input->content.find(start_text)),
static_cast<uint32_t>(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>({
range_for_text("nothing", "}"),
range_for_substring(input->content, "nothing"),
})));
// Replace `nothing` with `null` again
@ -154,7 +145,7 @@ describe("Tree", [&]() {
return input->undo();
});
AssertThat(ranges, Equals(vector<TSRange>({
range_for_text("null", "}"),
range_for_substring(input->content, "null"),
})));
});
@ -195,7 +186,7 @@ describe("Tree", [&]() {
return input->replace(input->content.find("}"), 0, ", b: false");
});
AssertThat(ranges, Equals(vector<TSRange>({
range_for_text(",", "}"),
range_for_substring(input->content, ", b: false"),
})));
// Add a third key-value pair in between the first two
@ -209,7 +200,7 @@ describe("Tree", [&]() {
"(pair (property_identifier) (false)))))"
);
AssertThat(ranges, Equals(vector<TSRange>({
range_for_text(", c", ", b"),
range_for_substring(input->content, ", c: 1, b: false"),
})));
// Delete the middle pair.
@ -244,7 +235,7 @@ describe("Tree", [&]() {
"(pair (property_identifier) (binary_expression (identifier) (null))))))"
);
AssertThat(ranges, Equals(vector<TSRange>({
range_for_text("b ===", "}"),
range_for_substring(input->content, "b === null"),
})));
});
});