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

Add children modules feature #19255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

geetanshjuneja
Copy link

Links to #17401

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 1, 2025
@geetanshjuneja geetanshjuneja changed the title Added child module feature Added children module feature Mar 1, 2025
@geetanshjuneja
Copy link
Author

@Veykril I have implemented whatever was stated in issue. Looks like I also need need to add childrenModule function in commands.ts and children_module handler in request.rs similar to parentModule.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this plural, that is all children_module occurences should be children_modules

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats up with this file being added

Comment on lines 928 to 987
if let Ok(file_path) = &params.text_document.uri.to_file_path() {
if file_path.file_name().unwrap_or_default() == "Cargo.toml" {
// search workspaces for parent packages or fallback to workspace root
let abs_path_buf = match Utf8PathBuf::from_path_buf(file_path.to_path_buf())
.ok()
.map(AbsPathBuf::try_from)
{
Some(Ok(abs_path_buf)) => abs_path_buf,
_ => return Ok(None),
};

let manifest_path = match ManifestPath::try_from(abs_path_buf).ok() {
Some(manifest_path) => manifest_path,
None => return Ok(None),
};

let links: Vec<LocationLink> = snap
.workspaces
.iter()
.filter_map(|ws| match &ws.kind {
ProjectWorkspaceKind::Cargo { cargo, .. }
| ProjectWorkspaceKind::DetachedFile { cargo: Some((cargo, _, _)), .. } => {
cargo.parent_manifests(&manifest_path)
}
_ => None,
})
.flatten()
.map(|parent_manifest_path| LocationLink {
origin_selection_range: None,
target_uri: to_proto::url_from_abs_path(&parent_manifest_path),
target_range: Range::default(),
target_selection_range: Range::default(),
})
.collect::<_>();
return Ok(Some(links.into()));
}

// check if invoked at the crate root
let file_id = try_default!(from_proto::file_id(&snap, &params.text_document.uri)?);
let crate_id = match snap.analysis.crates_for(file_id)?.first() {
Some(&crate_id) => crate_id,
None => return Ok(None),
};
let cargo_spec = match TargetSpec::for_file(&snap, file_id)? {
Some(TargetSpec::Cargo(it)) => it,
Some(TargetSpec::ProjectJson(_)) | None => return Ok(None),
};

if snap.analysis.crate_root(crate_id)? == file_id {
let cargo_toml_url = to_proto::url_from_abs_path(&cargo_spec.cargo_toml);
let res = vec![LocationLink {
origin_selection_range: None,
target_uri: cargo_toml_url,
target_range: Range::default(),
target_selection_range: Range::default(),
}]
.into();
return Ok(Some(res));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same logic as the parent lookup which isn't what we want here. We'd want to go from Cargo.toml to the respective crates root rust file instead. For simplicity of the PR we can just remove this part for now though

Comment on lines 27 to 36
// If cursor is literally on `mod foo`, go to the grandpa.
if let Some(m) = &module {
if !m
.item_list()
.is_some_and(|it| it.syntax().text_range().contains_inclusive(position.offset))
{
cov_mark::hit!(test_resolve_parent_module_on_module_decl);
module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to show the children of the mod foo in this case, so this here should not be necessary

Comment on lines 40 to 52
// Return all the children module inside the ItemList of the parent module
module
.syntax()
.children()
.filter_map(ast::ItemList::cast)
.flat_map(|it| it.syntax().children())
.filter_map(ast::Module::cast)
.flat_map(|m| {
sema.to_def(&m)
.into_iter()
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
})
.collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Return all the children module inside the ItemList of the parent module
module
.syntax()
.children()
.filter_map(ast::ItemList::cast)
.flat_map(|it| it.syntax().children())
.filter_map(ast::Module::cast)
.flat_map(|m| {
sema.to_def(&m)
.into_iter()
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
})
.collect()
sema
.to_def(&module)
.into_iter()
.flat_map(|module| module.children(db))
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),

Comment on lines 55 to 65
// Return all the children module inside the source file
source_file
.syntax()
.children()
.filter_map(ast::Module::cast)
.flat_map(|m| {
sema.to_def(&m)
.into_iter()
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
})
.collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Return all the children module inside the source file
source_file
.syntax()
.children()
.filter_map(ast::Module::cast)
.flat_map(|m| {
sema.to_def(&m)
.into_iter()
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
})
.collect()
sema
.file_to_module_defs(position.file_id)
.flat_map(|module| module.children(db))
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),

@geetanshjuneja geetanshjuneja changed the title Added children module feature Add children modules feature Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants