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

@ -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"),
})));
});
});