From f3c7499be58941827ac8f500083b32ca74e0e8c4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 21 Mar 2021 11:05:04 +0100 Subject: Add `'` to trigger_characters, allowing more direct lifetime completions --- crates/ide_completion/src/completions/lifetime.rs | 31 +++++++++++++++++++++++ crates/ide_completion/src/context.rs | 6 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'crates/ide_completion') diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs index 628c1fb9b..5eeddf7a4 100644 --- a/crates/ide_completion/src/completions/lifetime.rs +++ b/crates/ide_completion/src/completions/lifetime.rs @@ -68,6 +68,16 @@ fn func<'lifetime>(foo: &'li$0) {} "#, r#" fn func<'lifetime>(foo: &'lifetime) {} +"#, + ); + cov_mark::check!(completes_if_lifetime_without_idents); + check_edit( + "'lifetime", + r#" +fn func<'lifetime>(foo: &'$0) {} +"#, + r#" +fn func<'lifetime>(foo: &'lifetime) {} "#, ); } @@ -191,6 +201,27 @@ fn foo<'footime, 'lifetime: 'a$0>() {} ); } + #[test] + fn check_label_edit() { + check_edit( + "'label", + r#" +fn foo() { + 'label: loop { + break '$0 + } +} +"#, + r#" +fn foo() { + 'label: loop { + break 'label + } +} +"#, + ); + } + #[test] fn complete_label_in_loop() { check( diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 67e2d6f6c..32f81aec1 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -255,6 +255,10 @@ impl<'a> CompletionContext<'a> { if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() { cov_mark::hit!(completes_if_prefix_is_keyword); self.original_token.text_range() + } else if kind == CHAR { + // assume we are completing a lifetime but the user has only typed the ' + cov_mark::hit!(completes_if_lifetime_without_idents); + TextRange::at(self.original_token.text_range().start(), TextSize::from(1)) } else { TextRange::empty(self.position.offset) } @@ -471,7 +475,7 @@ impl<'a> CompletionContext<'a> { self.lifetime_syntax = find_node_at_offset(original_file, lifetime.syntax().text_range().start()); if let Some(parent) = lifetime.syntax().parent() { - if parent.kind() == syntax::SyntaxKind::ERROR { + if parent.kind() == ERROR { return; } -- cgit v1.2.3