2012: #1435 postfix completion for integer literals r=matklad a=pasa

Fix for #1435 Incorrect postfix completion for integer literals

Co-authored-by: Sergey Parilin <parilinsa@gmail.com>
This commit is contained in:
bors[bot] 2019-10-17 11:21:00 +00:00 committed by GitHub
commit 762fce5bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 2 deletions

View File

@ -8,7 +8,7 @@ use crate::{
CompletionItem,
};
use hir::{Ty, TypeCtor};
use ra_syntax::{ast::AstNode, TextRange};
use ra_syntax::{ast::AstNode, TextRange, TextUnit};
use ra_text_edit::TextEditBuilder;
fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder {
@ -42,7 +42,13 @@ fn is_bool_or_unknown(ty: Option<Ty>) -> bool {
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
if let Some(dot_receiver) = &ctx.dot_receiver {
let receiver_text = dot_receiver.syntax().text().to_string();
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))
@ -206,6 +212,63 @@ mod tests {
insert: "&mut bar",
detail: "&mut expr",
},
]"###
);
}
#[test]
fn postfix_completion_works_for_ambiguous_float_literal() {
assert_debug_snapshot!(
do_postfix_completion(
r#"
fn main() {
42.<|>
}
"#,
),
@r###"[
CompletionItem {
label: "box",
source_range: [52; 52),
delete: [49; 52),
insert: "Box::new(42)",
detail: "Box::new(expr)",
},
CompletionItem {
label: "dbg",
source_range: [52; 52),
delete: [49; 52),
insert: "dbg!(42)",
detail: "dbg!(expr)",
},
CompletionItem {
label: "match",
source_range: [52; 52),
delete: [49; 52),
insert: "match 42 {\n ${1:_} => {$0\\},\n}",
detail: "match expr {}",
},
CompletionItem {
label: "not",
source_range: [52; 52),
delete: [49; 52),
insert: "!42",
detail: "!expr",
},
CompletionItem {
label: "ref",
source_range: [52; 52),
delete: [49; 52),
insert: "&42",
detail: "&expr",
},
CompletionItem {
label: "refm",
source_range: [52; 52),
delete: [49; 52),
insert: "&mut 42",
detail: "&mut expr",
},
]"###
);
}

View File

@ -38,6 +38,7 @@ pub(crate) struct CompletionContext<'a> {
pub(super) is_new_item: bool,
/// The receiver if this is a field or method access, i.e. writing something.<|>
pub(super) dot_receiver: Option<ast::Expr>,
pub(super) dot_receiver_is_ambiguous_float_literal: bool,
/// If this is a call (method or function) in particular, i.e. the () are already there.
pub(super) is_call: bool,
pub(super) is_path_type: bool,
@ -80,6 +81,7 @@ impl<'a> CompletionContext<'a> {
is_call: false,
is_path_type: false,
has_type_args: false,
dot_receiver_is_ambiguous_float_literal: false,
};
ctx.fill(&original_parse, position.offset);
Some(ctx)
@ -235,6 +237,16 @@ impl<'a> CompletionContext<'a> {
.expr()
.map(|e| e.syntax().text_range())
.and_then(|r| find_node_with_range(original_file.syntax(), r));
self.dot_receiver_is_ambiguous_float_literal = if let Some(ast::Expr::Literal(l)) =
&self.dot_receiver
{
match l.kind() {
ast::LiteralKind::FloatNumber { suffix: _ } => l.token().text().ends_with('.'),
_ => false,
}
} else {
false
}
}
if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) {
// As above