Closed matklad closed 4 years ago
Future work:
I'd like to work on this! I'll see how far I can get and come back here if I've got questions.
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.
@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!
@fkohlgrueber are you still working on it?
@mikhail-m1 unfortunately not. I also didn't make significant progress, so you can take this issue if you want.
ok, let's see what I can do
i think this one can be closed, we have #3186 for follow up
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?
replied at https://github.com/rust-analyzer/rust-analyzer/issues/3186#issuecomment-615334347, as that's indeed the current umbrella issue for ssr.
https://github.com/rust-analyzer/rust-analyzer/pull/2266 added a
new
function forSource
:The goal of this issue is to replace all
Source { file_id: foo, ast: bar }
literals with more succinct calls toSource::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
and IDE uses this template to fix all files.
This is called SSR.
Here's the rough plan:
Analysis::structural_search_replace(&self, template: &str) -> Cancelable<Result<SourceChange, SomeError>>
method: this will be the main entry point.ssr
module tora_ide_api
crate==>>
into pattern and template (separator is bikesheddable)$ident:fragment
with__search_pattern_ident
SsrPattern
andSsrTemplate
SsrPattern
. Note that this should do only searching, replacement should be separate. Again, this should return a structured result likeSsrMatches
.SsrMatches
, anSsrTemplate
, and produces a `TextEditSourceEdit
ra_cli
EDIT: Zulip thread for discussion: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Structural.20Search.20Replace