-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Add children modules feature #19255
Conversation
@Veykril I have implemented whatever was stated in issue. Looks like I also need need to add |
There was a problem hiding this 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
There was a problem hiding this comment.
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
if let Ok(file_path) = ¶ms.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, ¶ms.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)); | ||
} | ||
} |
There was a problem hiding this comment.
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
crates/ide/src/children_module.rs
Outdated
// 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); | ||
} | ||
} |
There was a problem hiding this comment.
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
crates/ide/src/children_module.rs
Outdated
// 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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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(), |
crates/ide/src/children_module.rs
Outdated
// 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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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(), |
Links to #17401