mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
Cleanup complete_postfix
This commit is contained in:
parent
d222608a45
commit
b5349af05f
1 changed files with 57 additions and 52 deletions
|
@ -1,5 +1,9 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
|
use hir::{Ty, TypeCtor};
|
||||||
|
use ra_syntax::{ast::AstNode, TextRange, TextUnit};
|
||||||
|
use ra_text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completion::{
|
completion::{
|
||||||
completion_context::CompletionContext,
|
completion_context::CompletionContext,
|
||||||
|
@ -7,9 +11,53 @@ use crate::{
|
||||||
},
|
},
|
||||||
CompletionItem,
|
CompletionItem,
|
||||||
};
|
};
|
||||||
use hir::{Ty, TypeCtor};
|
|
||||||
use ra_syntax::{ast::AstNode, TextRange, TextUnit};
|
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
use ra_text_edit::TextEdit;
|
let dot_receiver = match &ctx.dot_receiver {
|
||||||
|
Some(it) => it,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
let receiver_text = if ctx.dot_receiver_is_ambiguous_float_literal {
|
||||||
|
let text = dot_receiver.syntax().text();
|
||||||
|
let without_dot = ..text.len() - TextUnit::of_char('.');
|
||||||
|
text.slice(without_dot).to_string()
|
||||||
|
} else {
|
||||||
|
dot_receiver.syntax().text().to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver);
|
||||||
|
|
||||||
|
if is_bool_or_unknown(receiver_ty) {
|
||||||
|
postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text))
|
||||||
|
.add_to(acc);
|
||||||
|
postfix_snippet(
|
||||||
|
ctx,
|
||||||
|
"while",
|
||||||
|
"while expr {}",
|
||||||
|
&format!("while {} {{\n$0\n}}", receiver_text),
|
||||||
|
)
|
||||||
|
.add_to(acc);
|
||||||
|
}
|
||||||
|
|
||||||
|
postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
|
||||||
|
|
||||||
|
postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
|
||||||
|
postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
|
||||||
|
|
||||||
|
postfix_snippet(
|
||||||
|
ctx,
|
||||||
|
"match",
|
||||||
|
"match expr {}",
|
||||||
|
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
||||||
|
)
|
||||||
|
.add_to(acc);
|
||||||
|
|
||||||
|
postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
|
||||||
|
|
||||||
|
postfix_snippet(ctx, "box", "Box::new(expr)", &format!("Box::new({})", receiver_text))
|
||||||
|
.add_to(acc);
|
||||||
|
}
|
||||||
|
|
||||||
fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
|
fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
|
||||||
let edit = {
|
let edit = {
|
||||||
|
@ -24,62 +72,19 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_bool_or_unknown(ty: Option<Ty>) -> bool {
|
fn is_bool_or_unknown(ty: Option<Ty>) -> bool {
|
||||||
if let Some(ty) = ty {
|
match &ty {
|
||||||
match ty {
|
Some(Ty::Apply(app)) if app.ctor == TypeCtor::Bool => true,
|
||||||
Ty::Apply(at) => match at.ctor {
|
Some(Ty::Unknown) | None => true,
|
||||||
TypeCtor::Bool => true,
|
Some(_) => false,
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
Ty::Unknown => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
|
||||||
if let Some(dot_receiver) = &ctx.dot_receiver {
|
|
||||||
let receiver_text = if ctx.dot_receiver_is_ambiguous_float_literal {
|
|
||||||
let text = dot_receiver.syntax().text();
|
|
||||||
let without_dot = ..text.len() - TextUnit::of_char('.');
|
|
||||||
text.slice(without_dot).to_string()
|
|
||||||
} else {
|
|
||||||
dot_receiver.syntax().text().to_string()
|
|
||||||
};
|
|
||||||
let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver);
|
|
||||||
if is_bool_or_unknown(receiver_ty) {
|
|
||||||
postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text))
|
|
||||||
.add_to(acc);
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
"while",
|
|
||||||
"while expr {}",
|
|
||||||
&format!("while {} {{\n$0\n}}", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
}
|
|
||||||
postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
|
|
||||||
postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
|
|
||||||
postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
"match",
|
|
||||||
"match expr {}",
|
|
||||||
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
|
|
||||||
postfix_snippet(ctx, "box", "Box::new(expr)", &format!("Box::new({})", receiver_text))
|
|
||||||
.add_to(acc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
|
||||||
use insta::assert_debug_snapshot;
|
use insta::assert_debug_snapshot;
|
||||||
|
|
||||||
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
||||||
|
|
||||||
fn do_postfix_completion(code: &str) -> Vec<CompletionItem> {
|
fn do_postfix_completion(code: &str) -> Vec<CompletionItem> {
|
||||||
do_completion(code, CompletionKind::Postfix)
|
do_completion(code, CompletionKind::Postfix)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue