feat: cross-parent module move in willRenameFiles#22737
Conversation
Dragging a `.rs` file or a module folder into a *different* directory now fixes the module tree and rewrites references, instead of leaving the code broken. Previously `willRenameFiles` only handled same-directory renames (rust-lang#7009); a cross-parent move is the open request in rust-lang#8872. New `ide` entry point `Analysis::will_move_module`, backed by `ide_db::rename::move_mod`: * mod-tree surgery — drop the old `mod X;` (with its trailing newline) and re-declare it, keeping visibility, at the end of the new parent's item list; * reference rewrite — for each usage, splice the leading `ast::Path` ending at the module segment with the module's new absolute `crate::…` path, leaving any trailing `::Item` untouched (HIR still describes the pre-move tree, so the old/new absolute segment paths are computed textually); * `super::` re-anchoring inside the moved file — a leading `super` run is rewritten to the `crate::…` path it denotes today, since after the move the same text would resolve against a different parent (`self::` is left alone); * grouped `use` imports — a member importing through the moved module is pulled out into its own `use` statement; when a group empties it is replaced wholesale, otherwise the moved members are deleted as overlap-free runs; * cross-crate references keep the extern crate name (`foo::a::m` → `foo::b::m`), honouring `package = "…"` renames; * `mod.rs` / directory modules — the same surgery generalizes; the editor moves the directory physically, so only text edits are emitted. Wiring: `handle_will_rename_files` gained a cross-parent arm that maps the destination directory to the new parent's crate-relative module segments and routes both dropped files and dragged folders (via their `mod.rs`) to `will_move_module`. File-system edits stay cleared — the editor owns the move. Boundaries left to the compiler (documented in code): macro-expanded references (edits would have to land in the macro input), a `super::super…` chain inside a *child* file of a moved directory module climbing above it, and `pub(super)`/`pub(in super::x)` visibility. Tests: `will_move_module_*` unit tests in `ide` cover each case exhaustively via `expect_test`; two `rust-analyzer` slow-tests pin the LSP wiring for a file drag and a folder drag. This change was written end-to-end with the assistance of an AI coding agent (Claude); a human directed and reviewed the work. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
A human reviewing the AI-generated code is not enough; said human must have the ability to fully understand the code, to the point that they could have written it themselves and only use the AI as a way to cut development time. Given that this is only your second contribution to rust-analyzer, and the first one was nowhere close to the size of this one, I heavily doubt this is the case. Also, I suspect you also used AI to author the pull request description, and that is plain forbidden by our AI policy. |
|
i need feature - i asked claude to implement this feature. you are free to play your ai-denial games. i did pr because hundreds of people did the same but as scripts. |
|
You are, of course, free to merge and personally maintain any feature or code you want in your rust-analyzer fork, AI-generated or not. What you cannot do is asking us to merge it into the main repo. If you want your code to be merged, you need to obey our rules. |
|
With that said: I don't want to review hundreds of unsupervised AI-generated code lines. If you confirm that you think you completely understand this patch, I'll take your word for it and review this. Otherwise, I will close this PR. Note that this PR has a lot of comments, which is typical for AI-generated code and usually not a good sign. That is unrelated to the fact you used AI to generate the PR description. Given that you probably didn't know that is forbidden that is acceptable this time, but if you'll continue doing that we'll reach out to moderation. |
|
Well. Fair enough. I'm not sure this is solid right now. I need some time |
Summary
Implements the request in #8872: moving a module to a new parent — dragging a
.rsfile or a module folder into a different directory — now fixes the moduletree and rewrites references, instead of leaving the code broken.
workspace/willRenameFilespreviously handled only same-directory renames (#7009).This adds the cross-parent case.
What it does
A new
ideentry pointAnalysis::will_move_module, backed byide_db::rename::move_mod, produces a singleSourceChange:mod X;(with its trailing newline) andre-declare it, preserving visibility, at the end of the new parent's item list;
ast::Pathending atthe module segment with the module's new absolute
crate::…path, leaving anytrailing
::Itemuntouched (HIR still describes the pre-move tree, so theold/new absolute paths are computed textually);
super::re-anchoring inside the moved file — a leadingsuperrun isrewritten to the
crate::…path it denotes today, since after the move the sametext would resolve against a different parent (
self::is left alone);useimports — a member importing through the moved module is pulledout into its own
use; an emptied group is replaced wholesale, otherwise movedmembers are deleted as overlap-free runs;
foo::a::m→foo::b::m),honouring
package = "…"renames;mod.rs/ directory modules — the same surgery generalizes; the editor movesthe directory physically, so only text edits are emitted.
handle_will_rename_filesgained a cross-parent arm that maps the destinationdirectory to the new parent's crate-relative module segments and routes both dropped
files and dragged folders (resolved via their
mod.rs) towill_move_module.File-system edits stay cleared — the editor owns the physical move.
Boundaries (documented in code, left to the compiler)
super::super…chain inside a child file of a moved directory module thatclimbs above the module (only the moved module's own file is re-anchored);
pub(super)/pub(in super::x)visibility.Tests
will_move_module_*unit tests inidecover each case exhaustively viaexpect_test;rust-analyzerslow-tests pin the LSP wiring:test_will_rename_files_cross_parent(file drag) andtest_will_rename_files_cross_parent_folder(folder drag).Disclosure
This change was written end-to-end by an AI coding agent (Anthropic's Claude) —
it is entirely AI-generated ("vibe-coded"). A human directed the work, reviewed the
diff, and ran the tests, but did not hand-write the implementation. Flagging this
explicitly so reviewers can weigh it accordingly; happy to iterate on anything.
Closes #8872.