mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Simplify
This commit is contained in:
parent
4b7675fcc3
commit
7faa35cbbd
3 changed files with 45 additions and 156 deletions
|
@ -53,14 +53,14 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver);
|
||||||
|
|
||||||
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
|
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
|
||||||
if let Some(try_enum) = &try_enum {
|
if let Some(try_enum) = &try_enum {
|
||||||
match try_enum {
|
match try_enum {
|
||||||
TryEnum::Result => {
|
TryEnum::Result => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"ifl",
|
"ifl",
|
||||||
"if let Ok {}",
|
"if let Ok {}",
|
||||||
&format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
|
&format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
|
||||||
|
@ -68,9 +68,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
|
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"while",
|
"while",
|
||||||
"while let Ok {}",
|
"while let Ok {}",
|
||||||
&format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
|
&format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
|
||||||
|
@ -79,9 +76,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
TryEnum::Option => {
|
TryEnum::Option => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"ifl",
|
"ifl",
|
||||||
"if let Some {}",
|
"if let Some {}",
|
||||||
&format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
|
&format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
|
||||||
|
@ -89,9 +83,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
|
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"while",
|
"while",
|
||||||
"while let Some {}",
|
"while let Some {}",
|
||||||
&format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
|
&format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
|
||||||
|
@ -100,32 +91,18 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
|
} else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
|
||||||
postfix_snippet(
|
postfix_snippet("if", "if expr {}", &format!("if {} {{\n $0\n}}", receiver_text))
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"if",
|
|
||||||
"if expr {}",
|
|
||||||
&format!("if {} {{\n $0\n}}", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"while",
|
"while",
|
||||||
"while expr {}",
|
"while expr {}",
|
||||||
&format!("while {} {{\n $0\n}}", receiver_text),
|
&format!("while {} {{\n $0\n}}", receiver_text),
|
||||||
)
|
)
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
postfix_snippet(ctx, cap, dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
|
postfix_snippet("not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
|
||||||
.add_to(acc);
|
|
||||||
} else if let Some(trait_) = FamousDefs(&ctx.sema, ctx.krate).core_iter_IntoIterator() {
|
} else if let Some(trait_) = FamousDefs(&ctx.sema, ctx.krate).core_iter_IntoIterator() {
|
||||||
if receiver_ty.impls_trait(ctx.db, trait_, &[]) {
|
if receiver_ty.impls_trait(ctx.db, trait_, &[]) {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"for",
|
"for",
|
||||||
"for ele in expr {}",
|
"for ele in expr {}",
|
||||||
&format!("for ele in {} {{\n $0\n}}", receiver_text),
|
&format!("for ele in {} {{\n $0\n}}", receiver_text),
|
||||||
|
@ -134,30 +111,19 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postfix_snippet(ctx, cap, dot_receiver, "ref", "&expr", &format!("&{}", receiver_text))
|
postfix_snippet("ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
|
||||||
.add_to(acc);
|
postfix_snippet("refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
dot_receiver,
|
|
||||||
"refm",
|
|
||||||
"&mut expr",
|
|
||||||
&format!("&mut {}", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
// The rest of the postfix completions create an expression that moves an argument,
|
// The rest of the postfix completions create an expression that moves an argument,
|
||||||
// so it's better to consider references now to avoid breaking the compilation
|
// so it's better to consider references now to avoid breaking the compilation
|
||||||
let dot_receiver = include_references(dot_receiver);
|
let dot_receiver = include_references(dot_receiver);
|
||||||
let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal);
|
let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal);
|
||||||
|
let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver);
|
||||||
|
|
||||||
match try_enum {
|
match try_enum {
|
||||||
Some(try_enum) => match try_enum {
|
Some(try_enum) => match try_enum {
|
||||||
TryEnum::Result => {
|
TryEnum::Result => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"match",
|
"match",
|
||||||
"match expr {}",
|
"match expr {}",
|
||||||
&format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
|
&format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
|
||||||
|
@ -166,9 +132,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
TryEnum::Option => {
|
TryEnum::Option => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"match",
|
"match",
|
||||||
"match expr {}",
|
"match expr {}",
|
||||||
&format!(
|
&format!(
|
||||||
|
@ -181,9 +144,6 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"match",
|
"match",
|
||||||
"match expr {}",
|
"match expr {}",
|
||||||
&format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text),
|
&format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text),
|
||||||
|
@ -192,88 +152,18 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postfix_snippet(
|
postfix_snippet("box", "Box::new(expr)", &format!("Box::new({})", receiver_text)).add_to(acc);
|
||||||
ctx,
|
postfix_snippet("ok", "Ok(expr)", &format!("Ok({})", receiver_text)).add_to(acc);
|
||||||
cap,
|
postfix_snippet("err", "Err(expr)", &format!("Err({})", receiver_text)).add_to(acc);
|
||||||
&dot_receiver,
|
postfix_snippet("some", "Some(expr)", &format!("Some({})", receiver_text)).add_to(acc);
|
||||||
"box",
|
postfix_snippet("dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
|
||||||
"Box::new(expr)",
|
postfix_snippet("dbgr", "dbg!(&expr)", &format!("dbg!(&{})", receiver_text)).add_to(acc);
|
||||||
&format!("Box::new({})", receiver_text),
|
postfix_snippet("call", "function(expr)", &format!("${{1}}({})", receiver_text)).add_to(acc);
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(ctx, cap, &dot_receiver, "ok", "Ok(expr)", &format!("Ok({})", receiver_text))
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"err",
|
|
||||||
"Err(expr)",
|
|
||||||
&format!("Err({})", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"some",
|
|
||||||
"Some(expr)",
|
|
||||||
&format!("Some({})", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"dbg",
|
|
||||||
"dbg!(expr)",
|
|
||||||
&format!("dbg!({})", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"dbgr",
|
|
||||||
"dbg!(&expr)",
|
|
||||||
&format!("dbg!(&{})", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"call",
|
|
||||||
"function(expr)",
|
|
||||||
&format!("${{1}}({})", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
|
|
||||||
if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) {
|
if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) {
|
||||||
if matches!(parent.kind(), STMT_LIST | EXPR_STMT) {
|
if matches!(parent.kind(), STMT_LIST | EXPR_STMT) {
|
||||||
postfix_snippet(
|
postfix_snippet("let", "let", &format!("let $0 = {};", receiver_text)).add_to(acc);
|
||||||
ctx,
|
postfix_snippet("letm", "let mut", &format!("let mut $0 = {};", receiver_text))
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"let",
|
|
||||||
"let",
|
|
||||||
&format!("let $0 = {};", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
|
||||||
postfix_snippet(
|
|
||||||
ctx,
|
|
||||||
cap,
|
|
||||||
&dot_receiver,
|
|
||||||
"letm",
|
|
||||||
"let mut",
|
|
||||||
&format!("let mut $0 = {};", receiver_text),
|
|
||||||
)
|
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -305,20 +195,17 @@ fn include_references(initial_element: &ast::Expr) -> ast::Expr {
|
||||||
resulting_element
|
resulting_element
|
||||||
}
|
}
|
||||||
|
|
||||||
fn postfix_snippet(
|
fn build_postfix_snippet_builder<'a>(
|
||||||
ctx: &CompletionContext,
|
ctx: &'a CompletionContext,
|
||||||
cap: SnippetCap,
|
cap: SnippetCap,
|
||||||
receiver: &ast::Expr,
|
receiver: &'a ast::Expr,
|
||||||
label: &str,
|
) -> impl Fn(&str, &str, &str) -> Builder + 'a {
|
||||||
detail: &str,
|
|
||||||
snippet: &str,
|
|
||||||
) -> Builder {
|
|
||||||
let edit = {
|
|
||||||
let receiver_syntax = receiver.syntax();
|
let receiver_syntax = receiver.syntax();
|
||||||
let receiver_range = ctx.sema.original_range(receiver_syntax).range;
|
let receiver_range = ctx.sema.original_range(receiver_syntax).range;
|
||||||
let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
|
let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
|
||||||
TextEdit::replace(delete_range, snippet.to_string())
|
|
||||||
};
|
move |label, detail, snippet| {
|
||||||
|
let edit = TextEdit::replace(delete_range, snippet.to_string());
|
||||||
let mut item = CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label);
|
let mut item = CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label);
|
||||||
item.detail(detail).kind(CompletionItemKind::Snippet).snippet_edit(cap, edit);
|
item.detail(detail).kind(CompletionItemKind::Snippet).snippet_edit(cap, edit);
|
||||||
if ctx.original_token.text() == label {
|
if ctx.original_token.text() == label {
|
||||||
|
@ -328,6 +215,7 @@ fn postfix_snippet(
|
||||||
}
|
}
|
||||||
|
|
||||||
item
|
item
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -19,7 +19,9 @@
|
||||||
use ide_db::helpers::SnippetCap;
|
use ide_db::helpers::SnippetCap;
|
||||||
use syntax::ast::{self, AstToken};
|
use syntax::ast::{self, AstToken};
|
||||||
|
|
||||||
use crate::{completions::postfix::postfix_snippet, context::CompletionContext, Completions};
|
use crate::{
|
||||||
|
completions::postfix::build_postfix_snippet_builder, context::CompletionContext, Completions,
|
||||||
|
};
|
||||||
|
|
||||||
/// Mapping ("postfix completion item" => "macro to use")
|
/// Mapping ("postfix completion item" => "macro to use")
|
||||||
static KINDS: &[(&str, &str)] = &[
|
static KINDS: &[(&str, &str)] = &[
|
||||||
|
@ -47,13 +49,14 @@ pub(crate) fn add_format_like_completions(
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let postfix_snippet = build_postfix_snippet_builder(ctx, cap, dot_receiver);
|
||||||
let mut parser = FormatStrParser::new(input);
|
let mut parser = FormatStrParser::new(input);
|
||||||
|
|
||||||
if parser.parse().is_ok() {
|
if parser.parse().is_ok() {
|
||||||
for (label, macro_name) in KINDS {
|
for (label, macro_name) in KINDS {
|
||||||
let snippet = parser.to_suggestion(macro_name);
|
let snippet = parser.to_suggestion(macro_name);
|
||||||
|
|
||||||
postfix_snippet(ctx, cap, dot_receiver, label, macro_name, &snippet).add_to(acc);
|
postfix_snippet(label, macro_name, &snippet).add_to(acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,16 +45,14 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
|
||||||
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
|
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
|
||||||
let parent = our_path.parent()?;
|
let parent = our_path.parent()?;
|
||||||
let paths = {
|
let paths = {
|
||||||
let temp;
|
|
||||||
let parent = if module_name == "mod" {
|
let parent = if module_name == "mod" {
|
||||||
// for mod.rs we need to actually look up one higher
|
// for mod.rs we need to actually look up one higher
|
||||||
// and take the parent as our to be module name
|
// and take the parent as our to be module name
|
||||||
let (name, _) = parent.name_and_extension()?;
|
let (name, _) = parent.name_and_extension()?;
|
||||||
module_name = name;
|
module_name = name;
|
||||||
temp = parent.parent()?;
|
parent.parent()?
|
||||||
&temp
|
|
||||||
} else {
|
} else {
|
||||||
&parent
|
parent
|
||||||
};
|
};
|
||||||
let mut paths =
|
let mut paths =
|
||||||
vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
|
vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue