Ensure that LHS and RHS syntax nodes have different IDs

ida_star
Wilfred Hughes 2021-09-04 11:04:30 +07:00
parent 86a330c44e
commit 433b20ff8a
3 changed files with 24 additions and 24 deletions

@ -609,7 +609,6 @@ mod tests {
"]",
pos_helper(2),
)];
init_info(&lhs);
let rhs = vec![Syntax::new_list(
&arena,
@ -619,7 +618,7 @@ mod tests {
"]",
pos_helper(2),
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -653,7 +652,6 @@ mod tests {
"]",
pos_helper(2),
)];
init_info(&lhs);
let rhs = vec![Syntax::new_list(
&arena,
@ -666,7 +664,7 @@ mod tests {
"]",
pos_helper(3),
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -704,7 +702,6 @@ mod tests {
"]",
pos_helper(4),
)];
init_info(&lhs);
let rhs = vec![Syntax::new_list(
&arena,
@ -717,7 +714,7 @@ mod tests {
"}",
pos_helper(4),
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -752,10 +749,9 @@ mod tests {
Syntax::new_atom(&arena, col_helper(2, 0), "bar"),
Syntax::new_atom(&arena, col_helper(2, 1), "foo"),
];
init_info(&lhs);
let rhs = vec![Syntax::new_atom(&arena, col_helper(1, 0), "foo")];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -790,9 +786,9 @@ mod tests {
"]",
pos_helper(2),
)];
init_info(&lhs);
let rhs = vec![];
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -827,9 +823,9 @@ mod tests {
),
Syntax::new_atom(&arena, col_helper(2, 2), ";"),
];
init_info(&lhs);
let rhs = vec![];
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -884,7 +880,6 @@ mod tests {
"]",
pos_helper(100),
)];
init_info(&lhs);
let rhs = vec![Syntax::new_list(
&arena,
@ -917,7 +912,7 @@ mod tests {
"]",
pos_helper(100),
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -950,14 +945,13 @@ mod tests {
pos_helper(1),
"the quick brown fox",
)];
init_info(&lhs);
let rhs = vec![Syntax::new_comment(
&arena,
pos_helper(1),
"the quick brown cat",
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -985,10 +979,9 @@ mod tests {
pos_helper(1),
"the quick brown fox",
)];
init_info(&lhs);
let rhs = vec![Syntax::new_comment(&arena, pos_helper(1), "foo bar")];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),
@ -1015,14 +1008,13 @@ mod tests {
Syntax::new_comment(&arena, pos_helper(1), "the quick brown fox"),
Syntax::new_comment(&arena, pos_helper(2), "the quick brown thing"),
];
init_info(&lhs);
let rhs = vec![Syntax::new_comment(
&arena,
pos_helper(1),
"the quick brown fox.",
)];
init_info(&rhs);
init_info(&lhs, &rhs);
let start = Vertex {
lhs_syntax: lhs.get(0).copied(),

@ -20,7 +20,7 @@ use typed_arena::Arena;
use crate::dijkstra::mark_syntax;
use crate::files::{is_probably_binary, read_or_die};
use crate::lines::{join_overlapping, visible_groups, MaxLine};
use crate::syntax::{change_positions, init_info, matching_lines};
use crate::syntax::{change_positions, init_info, init_info_single, matching_lines};
use crate::tree_sitter_parser as tsp;
extern crate pretty_env_logger;
@ -183,7 +183,7 @@ fn main() {
let bytes = read_or_die(&path);
let src = String::from_utf8_lossy(&bytes).to_string();
let ast = tsp::parse(&arena, &src, &ts_lang);
init_info(&ast);
init_info_single(&ast, 0);
println!("{:#?}", ast);
}
None => {
@ -249,8 +249,7 @@ fn main() {
println!("{}", style::header(&display_path, &lang_name));
init_info(&lhs);
init_info(&rhs);
init_info(&lhs, &rhs);
mark_syntax(lhs.get(0).copied(), rhs.get(0).copied());
let lhs_positions = change_positions(&lhs_src, &rhs_src, &lhs);

@ -444,11 +444,20 @@ impl<'a> Syntax<'a> {
}
}
pub fn init_info<'a>(roots: &[&'a Syntax<'a>]) {
set_unique_id(roots, 0);
pub fn init_info<'a>(lhs_roots: &[&'a Syntax<'a>], rhs_roots: &[&'a Syntax<'a>]) {
let next_id = init_info_single(lhs_roots, 0);
init_info_single(rhs_roots, next_id);
}
/// Initialise all the fields in SyntaxInfo.
///
/// Return the next unique ID available, so we can ensure LHS and RHS
/// have different IDs.
pub fn init_info_single<'a>(roots: &[&'a Syntax<'a>], first_id: u64) -> u64 {
set_next(roots, None);
set_prev(roots, None);
set_num_ancestors(roots, 0);
set_unique_id(roots, first_id)
}
fn set_unique_id<'a>(nodes: &[&'a Syntax<'a>], prev_id: u64) -> u64 {