refactor: Store a reference to LangProfile in each AstNode #386

Merged
wetneb merged 14 commits from wetneb/mergiraf:refactor_lang_profile into main 2025-05-16 07:20:59 +02:00
Owner

This is the first step towards #5. Keeping track of which language a node comes from will make it possible to store nodes coming from different languages in the same tree, while still being able to look up their commutative parent and signature definitions from the correct language profile.

@ada4a I've tried to make sufficiently granular commits to ease the review, I hope it works for you! I'm curious how much reviewing effort that represents on your side :)

This is the first step towards #5. Keeping track of which language a node comes from will make it possible to store nodes coming from different languages in the same tree, while still being able to look up their commutative parent and signature definitions from the correct language profile. @ada4a I've tried to make sufficiently granular commits to ease the review, I hope it works for you! I'm curious how much reviewing effort that represents on your side :)
ada4a requested changes 2025-05-15 08:56:16 +02:00
Dismissed
ada4a left a comment
Owner

Very nice! Having commits nicely split like this was indeed very helpful.

I didn't have too many things to add this time -- maybe more will come to mind when I take another look at this later

Very nice! Having commits nicely split like this was indeed very helpful. I didn't have _too_ many things to add this time -- maybe more will come to mind when I take another look at this later
src/ast.rs Outdated
@ -705,0 +712,4 @@
.children
.iter()
.copied()
.filter_map(AstNode::signature)
Owner

nice:)

nice:)
src/matching.rs Outdated
@ -37,6 +37,7 @@ impl<'tree> Matching<'tree> {
/// Is it possible to add this pair while keeping the matching consistent?
pub fn can_be_matched(&self, from: &AstNode<'tree>, to: &AstNode<'tree>) -> bool {
from.grammar_name == to.grammar_name
&& from.lang_profile.name == to.lang_profile.name // TODO rely on a better identifier, as there could be name collisions? Just pointer equality?
Owner

I'd say add a test to supported_langs.rs that there are no name collisions, and then continue using this comparison, yes.

Also it might be a good idea to have a helper for this, something like LangProfile::eq (see Iterator::eq for a precedent), which would initially just compare names, but which we'd be free to refactor/improve later, instead of needing to modify all the code everywhere

I'd say add a test to `supported_langs.rs` that there are no name collisions, and then continue using this comparison, yes. Also it might be a good idea to have a helper for this, something like `LangProfile::eq` (see [`Iterator::eq`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.eq) for a precedent), which would initially just compare `name`s, but which we'd be free to refactor/improve later, instead of needing to modify all the code everywhere
Owner

Hm, not sure if this should be a PartialEq impl (I was thinking of a free-standing method), but since we've decided names are unique identifiers, this is probably okay?

Hm, not sure if this should be a `PartialEq` impl (I was thinking of a free-standing method), but since we've decided names are unique identifiers, this is probably okay?
@ -829,0 +837,4 @@
let definition = match self {
MergedTree::ExactTree { node, .. }
| MergedTree::MixedTree { node, .. }
| MergedTree::LineBasedMerge { node, .. } => node
Owner

The following 3 lines (the ability to highlight multiple lines for a comment is something I always miss from GitLab), are basically AstNode::signature, but we can't call that because we have a Leader instead. Maybe it'd make sense to surface that method to Leader, like it was done with AstNode::commutative_parent_definition?

Though one might argue that it's less important in this case, since that newly created method would only really be used here, whereas Leader::commutative_parent_definition is used in at least 3 places

The following 3 lines (the ability to highlight multiple lines for a comment is something I always miss from GitLab), are basically `AstNode::signature`, but we can't call that because we have a `Leader` instead. Maybe it'd make sense to surface that method to `Leader`, like it was done with `AstNode::commutative_parent_definition`? Though one might argue that it's less important in this case, since that newly created method would only really be used here, whereas `Leader::commutative_parent_definition` is used in at least 3 places
Owner

OTOH adding Leader::commutative_parent_definition would remove the only use of Leader::lang_profile, so we could be able to remove that at least? But maybe having access to a leader's language profile is helpful in some other occasions

OTOH adding `Leader::commutative_parent_definition` would remove the only use of `Leader::lang_profile`, so we could be able to remove that at least? But maybe having access to a leader's language profile is helpful in some other occasions
Add semicolon that my rustfmt doesn't add by itself, weird
All checks were successful
/ test (pull_request) Successful in 57s
b4af023f28
Owner

I think the reason the semicolon wasn't added is that assert! technically returns (), so it could be a last expression of a block (or a function, in this case), defining the return type of the latter

I think the reason the semicolon wasn't added is that `assert!` technically returns `()`, so it could be a last expression of a block (or a function, in this case), defining the return type of the latter
Owner

Do you mind if I leave this open for a bit longer? I'd like to take another look at it this evening

Do you mind if I leave this open for a bit longer? I'd like to take another look at it this evening
Author
Owner

Sure, take your time!

Sure, take your time!
ada4a approved these changes 2025-05-15 23:21:05 +02:00
wetneb merged commit 690a5137dc into main 2025-05-16 07:20:59 +02:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: mergiraf/mergiraf#386
No description provided.