3878: A more precise panic macro r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-07 14:41:07 +00:00 committed by GitHub
commit 33c364b545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 7 deletions

View File

@ -1,12 +1,13 @@
//! Complete fields in record literals and patterns.
use crate::completion::{CompletionContext, Completions};
use ra_syntax::{ast, ast::NameOwner, SmolStr};
use crate::completion::{CompletionContext, Completions};
pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
let (ty, variant, already_present_fields) =
match (ctx.record_lit_pat.as_ref(), ctx.record_lit_syntax.as_ref()) {
(None, None) => return None,
(Some(_), Some(_)) => panic!("A record cannot be both a literal and a pattern"),
(Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"),
(Some(record_pat), _) => (
ctx.sema.type_of_pat(&record_pat.clone().into())?,
ctx.sema.resolve_record_pattern(record_pat)?,
@ -59,9 +60,10 @@ fn pattern_ascribed_fields(record_pat: &ast::RecordPat) -> Vec<SmolStr> {
#[cfg(test)]
mod tests {
mod record_lit_tests {
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
fn complete(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
}
@ -204,9 +206,10 @@ mod tests {
}
mod record_pat_tests {
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
fn complete(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
}

View File

@ -50,6 +50,8 @@ pub(crate) struct CompletionContext<'a> {
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,
/// If this is a macro call, i.e. the () are already there.
pub(super) is_macro_call: bool,
pub(super) is_path_type: bool,
pub(super) has_type_args: bool,
}
@ -102,6 +104,7 @@ impl<'a> CompletionContext<'a> {
is_new_item: false,
dot_receiver: None,
is_call: false,
is_macro_call: false,
is_path_type: false,
has_type_args: false,
dot_receiver_is_ambiguous_float_literal: false,
@ -269,6 +272,7 @@ impl<'a> CompletionContext<'a> {
.and_then(ast::PathExpr::cast)
.and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
.is_some();
self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
self.has_type_args = segment.type_arg_list().is_some();

View File

@ -174,7 +174,8 @@ impl Completions {
.set_deprecated(is_deprecated(macro_, ctx.db))
.detail(detail);
builder = if ctx.use_item_syntax.is_some() {
builder = if ctx.use_item_syntax.is_some() || ctx.is_macro_call {
tested_by!(dont_insert_macro_call_parens_unncessary);
builder.insert_text(name)
} else {
let macro_braces_to_insert =
@ -960,7 +961,8 @@ mod tests {
}
#[test]
fn dont_insert_macro_call_braces_in_use() {
fn dont_insert_macro_call_parens_unncessary() {
covers!(dont_insert_macro_call_parens_unncessary);
assert_debug_snapshot!(
do_reference_completion(
r"
@ -986,6 +988,41 @@ mod tests {
},
]
"###
)
);
assert_debug_snapshot!(
do_reference_completion(
r"
//- /main.rs
macro_rules frobnicate {
() => ()
}
fn main() {
frob<|>!();
}
"
),
@r###"
[
CompletionItem {
label: "frobnicate!",
source_range: [56; 60),
delete: [56; 60),
insert: "frobnicate",
kind: Macro,
detail: "macro_rules! frobnicate",
},
CompletionItem {
label: "main()",
source_range: [56; 60),
delete: [56; 60),
insert: "main()$0",
kind: Function,
lookup: "main",
detail: "fn main()",
},
]
"###
);
}
}

View File

@ -7,4 +7,5 @@ test_utils::marks!(
dont_complete_current_use
test_resolve_parent_module_on_module_decl
search_filters_by_range
dont_insert_macro_call_parens_unncessary
);