[Feature Request] Add `Update Expect` CodeLens for `expect-test`
First, let's create a new project and add the expect-test dependency to demonstrate:
cargo new demo
cd demo
cargo add expect-test
add the following code to src/main.rs:
fn main() {
println!("Hello, world!");
}
#[test]
fn test() {
expect_test::expect![].assert_eq("hi");
}
For a test block, CodeLens will be shown:
#[test]
▶ Run Test | Debug
fn test() {
expect_test::expect![].assert_eq("hi");
}
But to update an expect_test block(not update all by UPDATE_EXPECT=1 cargo test), we currently need to:
- Move the cursor into the test block.
- Use
Ctrl+Shift+P. - Select rust-analyzer: Run.
- Choose test test + expect.
Proposed Feature
It would be more convenient if we could directly update the expect_test block with a single click by adding an Update Expect button.
#[test]
▶ Run Test | Debug | Update Expect
fn test() {
expect_test::expect![].assert_eq("hi");
}
After clicking Update Expect, the content will be updated to:
#[test]
▶ Run Test | Debug | Update Expect
fn test() {
expect_test::expect!["hi"].assert_eq("hi");
}
This feature would greatly enhance the user experience when working with expect-test in Rust projects. Thank you for considering this request.
I have read code related to generating Runnables, and found that we will generate all Runnables for the entire file at once. So if we want to identify expect!s, we need to check all the bodies of tests, which may take a lot of time.
@Veykril Do you have any suggestions?
Ye there would be no way around that which seems unfortunate. Might be enough to iterate through all blocks (as in block def maps) of a body and check the macro calls from that only though?
iterate through all blocks (as in block def maps) of a body
Since we check all definitions at once to fetch Runnables, we need to query the def_map of all tests first, which may take a long time, and slow down codelens. Unless we rewrite the current logic for fetching runnables and change the related API to only request runnables from certain regions.
This feature may require some refactoring. For now, I would prioritize not implementing it, and maybe revisit it in the future. 🫤
Yes its a nice to have I think (even nicer if we could generalize this for insta and other snapshot test libs) but it's probably not a great idea for now given the perf implications
I’ve implemented a demo.
I used FindUsage which should be efficient (each check requires a text search within the range and a goto-definition request).
@Veykril
I have implemented most of the features (including support for expect-test, insta, and snapbox) and am now considering removing the previous implementation of the expect! macro detection. However, I found this part a little puzzling: why is the RUSTC_TOOLCHAIN being set only when expect_test is true?
https://github.com/rust-lang/rust-analyzer/blob/80c97cab43b0a41c87d073361e3073787b3e9173/crates/rust-analyzer/src/handlers/request.rs#L955-L968
Oh that should be set for non expect runnables as well I think
On Tue, Dec 24, 2024, 7:32 PM roife @.***> wrote:
@Veykril https://github.com/Veykril
I have implemented most of the features (including support for expect-test, insta, and snapbox) and am now considering removing the original code for detecting the expect! macro. However, I found this part a little puzzling: why is the RUSTC_TOOLCHAIN being set here?
https://github.com/rust-lang/rust-analyzer/blob/80c97cab43b0a41c87d073361e3073787b3e9173/crates/rust-analyzer/src/handlers/request.rs#L955-L968
— Reply to this email directly, view it on GitHub https://github.com/rust-lang/rust-analyzer/issues/17812#issuecomment-2561339478, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4VNS2UAAOHGDPF5PHHC2D2HGSEBAVCNFSM6AAAAABMCMVE72VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKNRRGMZTSNBXHA . You are receiving this because you were mentioned.Message ID: @.***>