mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-01 15:51:31 +00:00
Stylistic changes. Updated features.md with the new assists.
This commit is contained in:
parent
a4ba3841b4
commit
c5f8f3b1f4
2 changed files with 37 additions and 9 deletions
|
|
@ -18,9 +18,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
|
||||||
// Must be a binding
|
// Must be a binding
|
||||||
let pat = match pat.kind() {
|
let pat = match pat.kind() {
|
||||||
PatKind::BindPat(bind_pat) => bind_pat,
|
PatKind::BindPat(bind_pat) => bind_pat,
|
||||||
_ => {
|
_ => return None,
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let pat_range = pat.syntax().range();
|
let pat_range = pat.syntax().range();
|
||||||
// The binding must have a name
|
// The binding must have a name
|
||||||
|
|
@ -31,20 +29,20 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
// Infer type
|
// Infer type
|
||||||
let func = function_from_child_node(ctx.db, ctx.frange.file_id, pat.syntax())?;
|
let db = ctx.db;
|
||||||
let inference_res = func.infer(ctx.db);
|
let func = function_from_child_node(db, ctx.frange.file_id, pat.syntax())?;
|
||||||
let source_map = func.body_source_map(ctx.db);
|
let inference_res = func.infer(db);
|
||||||
|
let source_map = func.body_source_map(db);
|
||||||
let expr_id = source_map.node_expr(expr.into())?;
|
let expr_id = source_map.node_expr(expr.into())?;
|
||||||
let ty = inference_res[expr_id].clone();
|
let ty = inference_res[expr_id].clone();
|
||||||
// Assist not applicable if the type is unknown
|
// Assist not applicable if the type is unknown
|
||||||
if is_unknown(&ty) {
|
if is_unknown(&ty) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let ty_str = ty.display(ctx.db).to_string();
|
|
||||||
|
|
||||||
ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| {
|
ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| {
|
||||||
edit.target(pat_range);
|
edit.target(pat_range);
|
||||||
edit.insert(name_range.end(), format!(": {}", ty_str));
|
edit.insert(name_range.end(), format!(": {}", ty.display(db)));
|
||||||
});
|
});
|
||||||
ctx.build()
|
ctx.build()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -333,10 +333,40 @@ impl VariantData {
|
||||||
```rust
|
```rust
|
||||||
// before:
|
// before:
|
||||||
use algo:<|>:visitor::{Visitor, visit};
|
use algo:<|>:visitor::{Visitor, visit};
|
||||||
//after:
|
// after:
|
||||||
use algo::{<|>visitor::{Visitor, visit}};
|
use algo::{<|>visitor::{Visitor, visit}};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- Flip binary expression
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// before:
|
||||||
|
fn foo() {
|
||||||
|
if 1 <<|> 2 {
|
||||||
|
println!("Who would have thought?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// after:
|
||||||
|
fn foo() {
|
||||||
|
if 2 ><|> 1 {
|
||||||
|
println!("Who would have thought?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Add explicit type
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// before:
|
||||||
|
fn foo() {
|
||||||
|
let t<|> = (&2, Some(1));
|
||||||
|
}
|
||||||
|
// after:
|
||||||
|
fn foo() {
|
||||||
|
let t<|>: (&i32, Option<i32>) = (&2, Some(1));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Magic Completions
|
### Magic Completions
|
||||||
|
|
||||||
In addition to usual reference completion, rust-analyzer provides some ✨magic✨
|
In addition to usual reference completion, rust-analyzer provides some ✨magic✨
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue