rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

"Locate child modules", inverse of the "Locate parent modules" feature

Open Veykril opened this issue 1 year ago • 1 comments

We currently have a feature that allows running a command to open the parent module(s) of a file's corresponding modules. We should have the inverse of this, opening a popup with the selection of all direct children modules of the currently open file's module(s).

See https://github.com/rust-lang/rust-analyzer/blob/4af21ffb026c7ec3a97a484ca27b36f703eb5fb1/crates/ide/src/parent_module.rs for the parent module implementation, you should be able to trace how this is used to know what to edit to add a similar feature.

This will need an lsp-extension like https://github.com/veykril/rust-analyzer/blob/4af21ffb026c7ec3a97a484ca27b36f703eb5fb1/crates/rust-analyzer/src/lsp/ext.rs#L389-L395 and vscode glue like https://github.com/rust-lang/rust-analyzer/blob/4af21ffb026c7ec3a97a484ca27b36f703eb5fb1/editors/code/src/lsp_ext.ts#L188-L192

Veykril avatar Jun 12 '24 06:06 Veykril

Hello @Veykril, I would like to work on it!

randomicon00 avatar Jun 14 '24 01:06 randomicon00

Hi I would like to work on this.

geetanshjuneja avatar Mar 01 '25 07:03 geetanshjuneja

After reading parent_module code I found that find_node_at_offset return the ancestor modules which contains the sourcefile. For the Locate child modules feature we want to get the children which are of type Module. Am I correct here? If yes then I just have to check sourcefile.descendants and filter out modules from this list?

geetanshjuneja avatar Mar 01 '25 07:03 geetanshjuneja

what would be the output in the following cases?

//- /lib.rs $0 mod foo;

is the ans foo here?

//- /lib.rs mod foo { mod bar { mod $0baz {} }
}

is the ans baz here?

//- /lib.rs $0 mod foo { mod bar { mod baz {} }
}

is the ans foo here?

geetanshjuneja avatar Mar 01 '25 09:03 geetanshjuneja

Once you have the hir::Module of the current file you can iterate its children via its children method

Veykril avatar Mar 01 '25 10:03 Veykril

what would be the output in the following cases?

//- /lib.rs $0 mod foo;

is the ans foo here?

Yes sounds reasonable

//- /lib.rs mod foo { mod bar { mod $0baz {} } }

is the ans baz here? Yes sounds reasonable

//- /lib.rs $0 mod foo { mod bar { mod baz {} } }

is the ans foo here?

Yes

Basically just one level of modules, no more

Veykril avatar Mar 01 '25 10:03 Veykril

Implemented in #19255.

lnicola avatar Apr 14 '25 06:04 lnicola