Skip to content

add apply ssr assist #7874

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

Merged
merged 1 commit into from
Mar 10, 2021
Merged

add apply ssr assist #7874

merged 1 commit into from
Mar 10, 2021

Conversation

JoshMcguigan
Copy link
Contributor

@JoshMcguigan JoshMcguigan commented Mar 5, 2021

This PR adds an Apply SSR assist which was briefly mentioned in #3186. It allows writing an ssr rule as a comment, and then applying that SSR via an assist. This workflow is much nicer than the default available via coc-rust-analyzer when iterating to find the proper replacement.

As currently implemented, this requires the ssr rule is written as a single line in the comment, and it doesn't require any kind of prefix. Anything which properly parses as a ssr rule will enable the assist. The benefit of requiring the ssr rule be on a single line is it allows for a workflow where the user has several rules written one after the other, possibly to be triggered in order, without having to try to parse multiple lines of text and determine where one rule ends and the next begins. The benefit of not requiring a prefix is less typing 😆 - plus, I think the chance of something accidentally parsing as an ssr rule is minimal.

I think a reasonable extension of this would be to allow either any ssr rule that fits on a single line, or any comment block which in its entirety makes up a single ssr rule (parsing a comment block containing multiple ssr rules and running them all would break the use case that currently works where a user writes multiple ssr rules then runs them each one by one in arbitrary order).

I've marked this as a draft because for some reason I am strugging to make the unit tests pass. It does work when I compile rust-analyzer and test it in my editor though, so I'm not sure what is going on.

@JoshMcguigan
Copy link
Contributor Author

JoshMcguigan commented Mar 5, 2021

This is the error I am getting from the unit test. I confirmed it is properly parsing the ssr rule, but for some reason it seems to not return any edits. As noted above, this assist does actually work if I build rust-analyzer and run it with my editor, so the unit test errors are quite puzzling.

$ cargo test -p ide_assists handlers::apply_ssr
    Finished test [unoptimized] target(s) in 0.07s
     Running target/debug/deps/ide_assists-cfba0b9b7eebf6de

running 1 test
test handlers::apply_ssr::tests::in_file ... FAILED

failures:

---- handlers::apply_ssr::tests::in_file stdout ----
thread 'handlers::apply_ssr::tests::in_file' panicked at 'assertion failed: !source_change.source_
file_edits.is_empty()', crates/ide_assists/src/tests.rs:47:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    handlers::apply_ssr::tests::in_file

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 867 filtered out

error: test failed, to rerun pass '-p ide_assists --lib'

@JoshMcguigan
Copy link
Contributor Author

JoshMcguigan commented Mar 5, 2021

    let mut local_roots = FxHashSet::default();
    local_roots.insert(ide_db::base_db::fixture::WORKSPACE);
    db.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);

It seems the test setup in ide_assists::tests::check was not setting any local root. I added these three lines and my test is now passing!

I'm leaving this as a draft for now as there are some additional tests I'd like to add. But I'm certainly open to feedback on this PR.

@JoshMcguigan JoshMcguigan force-pushed the assist-apply-ssr branch 2 times, most recently from b617256 to a35809b Compare March 5, 2021 14:01
@lnicola

This comment has been minimized.

@kjeremy
Copy link
Contributor

kjeremy commented Mar 9, 2021 via email

@JoshMcguigan JoshMcguigan force-pushed the assist-apply-ssr branch 8 times, most recently from 9befa07 to 8989352 Compare March 10, 2021 13:54
@matklad
Copy link
Member

matklad commented Mar 10, 2021

LGTM now!

bors d+

@bors
Copy link
Contributor

bors bot commented Mar 10, 2021

✌️ JoshMcguigan can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@JoshMcguigan JoshMcguigan marked this pull request as ready for review March 10, 2021 14:26
@JoshMcguigan
Copy link
Contributor Author

I started working on adding this into the doc generation infra for assists, but that looks to be a larger change, since when we generate user facing docs we also generate doc tests - and those doc tests assume the assist to be tested is in the ide_assists crate.

I'm going to punt on the doc infrastructure changes for now, as it isn't directly related to the creation of this assist and I'm not exactly sure how we'd want to handle it. But @matklad let me know if you have thoughts on how this should be done and I can submit a follow up PR if you'd like.

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 10, 2021

@bors bors bot merged commit 9dc1340 into rust-lang:master Mar 10, 2021
@matklad
Copy link
Member

matklad commented Mar 10, 2021

@matklad
Copy link
Member

matklad commented Mar 10, 2021

bors bot added a commit that referenced this pull request Mar 10, 2021
7961: add user docs for ssr assist r=JoshMcguigan a=JoshMcguigan

@matklad 

This is a small follow up on #7874, adding user docs for the SSR assist functionality. Since most other assists aren't handled this way I wasn't sure exactly how we wanted to document this, so feel free to suggest alternatives.

Co-authored-by: Josh Mcguigan <[email protected]>
@lnicola
Copy link
Member

lnicola commented Mar 15, 2021

ssr

@matklad
Copy link
Member

matklad commented Mar 15, 2021

Almost forgot, cc @davidlattimore , you'll like this ^ :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants