rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.13k stars 1.58k forks source link

Structural Search Replace #2267

Closed matklad closed 4 years ago

matklad commented 4 years ago

https://github.com/rust-analyzer/rust-analyzer/pull/2266 added a new function for Source:

impl<T> Source<T> {
    pub fn new(file_id: HirFileId, ast: T) -> Source<T> {
        Source { file_id, ast }
    }
}

The goal of this issue is to replace all Source { file_id: foo, ast: bar } literals with more succinct calls to Source::new(foo, bar).

However, we are not going to do this in a boring manual way. Instead, what we'll do is we provide an IDE feature, such that you enter

Source { file_id: $file_id:expr, ast: $ast:expr }  ==>> Source::new($file_id, $ast)

and IDE uses this template to fix all files.

This is called SSR.

Here's the rough plan:

EDIT: Zulip thread for discussion: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Structural.20Search.20Replace

matklad commented 4 years ago

Future work:

fkohlgrueber commented 4 years ago

I'd like to work on this! I'll see how far I can get and come back here if I've got questions.

bjorn3 commented 4 years ago

There is already a program which kind of does this. However it is nightly only. https://github.com/google/rerast/

Rerast is a search/replace tool for Rust code using rules. [...] Matching is done on syntax, not on text, so formatting doesn't matter. Placeholders are typed and must match the type found in the code for the rule to apply.

matklad commented 4 years ago

@bjorn3 yeah, I am aware that rerast exist. I believe, however, that, in the limit, rust-analyzer is a significantly better foundation for these kinds of tools.

@fkohlgrueber cool! I want not note, however, that at the moment I am pretty swamped with many things, so I might be slow to review the code. Will happily answer any questions though!

mikhail-m1 commented 4 years ago

@fkohlgrueber are you still working on it?

fkohlgrueber commented 4 years ago

@mikhail-m1 unfortunately not. I also didn't make significant progress, so you can take this issue if you want.

mikhail-m1 commented 4 years ago

ok, let's see what I can do

mikhail-m1 commented 4 years ago

i think this one can be closed, we have #3186 for follow up

RReverser commented 4 years ago

I just tried it for the first time, and it seems that it can't match macro invocations. Is it because it operates on the expanded code rather than source one?

matklad commented 4 years ago

replied at https://github.com/rust-analyzer/rust-analyzer/issues/3186#issuecomment-615334347, as that's indeed the current umbrella issue for ssr.