Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(matching_handlers): Add comparators for class like elements #43

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions matching_handlers/src/java/class_like_declaration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::utils::find_child_of_kind;
use model::{cst_node::NonTerminal, CSTNode};

pub fn compute_matching_score_for_class_like_declaration<'a>(
left: &'a CSTNode,
right: &'a CSTNode,
) -> usize {
match (left, right) {
(
CSTNode::NonTerminal(NonTerminal {
children: children_left,
..
}),
CSTNode::NonTerminal(NonTerminal {
children: children_right,
..
}),
) => {
let identifier_left =
find_child_of_kind(children_left, "identifier").map(|node| node.contents());
let identifier_right =
find_child_of_kind(children_right, "identifier").map(|node| node.contents());

(identifier_left.is_some() && identifier_left == identifier_right).into()
}
(_, _) => 0,
}
}

#[cfg(test)]
mod tests {
#[test]
fn classes_with_the_same_name_match_with_score_one() {
let result = super::compute_matching_score_for_class_like_declaration(
&make_class_like_declaration("ABC"),
&make_class_like_declaration("ABC"),
);
assert_eq!(1, result);
}

#[test]
fn classes_of_different_names_do_not_match() {
let result = super::compute_matching_score_for_class_like_declaration(
&make_class_like_declaration("ABC"),
&make_class_like_declaration("DEF"),
);
assert_eq!(0, result);
}

fn make_class_like_declaration(identifier: &str) -> model::CSTNode {
model::CSTNode::NonTerminal(model::cst_node::NonTerminal {
kind: "class_declaration",
children: vec![model::CSTNode::Terminal(model::cst_node::Terminal {
kind: "identifier",
value: identifier,
..Default::default()
})],
..Default::default()
})
}
}
10 changes: 10 additions & 0 deletions matching_handlers/src/java/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod class_like_declaration;
mod field_declaration;
mod import_declaration;
mod method_declaration;
Expand All @@ -6,6 +7,7 @@ mod utils;
use crate::MatchingHandlers;

use self::{
class_like_declaration::compute_matching_score_for_class_like_declaration,
field_declaration::compute_matching_score_for_field_declaration,
import_declaration::compute_matching_score_for_import_declaration,
method_declaration::compute_matching_score_for_method_declaration,
Expand All @@ -29,5 +31,13 @@ pub fn get_default_java_matching_handlers<'a>() -> MatchingHandlers<'a> {
"import_declaration",
compute_matching_score_for_import_declaration,
);
matching_handlers.register(
"class_declaration",
compute_matching_score_for_class_like_declaration,
);
matching_handlers.register(
"interface_declaration",
compute_matching_score_for_class_like_declaration,
);
matching_handlers
}
Loading