Skip to content

Commit 1ae20d2

Browse files
bors[bot]Veykril
andauthored
Merge #8132
8132: Add `'` to trigger_characters, allowing more direct lifetime completions r=Veykril a=Veykril Fixes having to type a character after `'` to complete lifetimes and labels Co-authored-by: Lukas Wirth <[email protected]>
2 parents d51cf13 + f3c7499 commit 1ae20d2

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ fn func<'lifetime>(foo: &'li$0) {}
6868
"#,
6969
r#"
7070
fn func<'lifetime>(foo: &'lifetime) {}
71+
"#,
72+
);
73+
cov_mark::check!(completes_if_lifetime_without_idents);
74+
check_edit(
75+
"'lifetime",
76+
r#"
77+
fn func<'lifetime>(foo: &'$0) {}
78+
"#,
79+
r#"
80+
fn func<'lifetime>(foo: &'lifetime) {}
7181
"#,
7282
);
7383
}
@@ -191,6 +201,27 @@ fn foo<'footime, 'lifetime: 'a$0>() {}
191201
);
192202
}
193203

204+
#[test]
205+
fn check_label_edit() {
206+
check_edit(
207+
"'label",
208+
r#"
209+
fn foo() {
210+
'label: loop {
211+
break '$0
212+
}
213+
}
214+
"#,
215+
r#"
216+
fn foo() {
217+
'label: loop {
218+
break 'label
219+
}
220+
}
221+
"#,
222+
);
223+
}
224+
194225
#[test]
195226
fn complete_label_in_loop() {
196227
check(

crates/ide_completion/src/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ impl<'a> CompletionContext<'a> {
255255
if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() {
256256
cov_mark::hit!(completes_if_prefix_is_keyword);
257257
self.original_token.text_range()
258+
} else if kind == CHAR {
259+
// assume we are completing a lifetime but the user has only typed the '
260+
cov_mark::hit!(completes_if_lifetime_without_idents);
261+
TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
258262
} else {
259263
TextRange::empty(self.position.offset)
260264
}
@@ -471,7 +475,7 @@ impl<'a> CompletionContext<'a> {
471475
self.lifetime_syntax =
472476
find_node_at_offset(original_file, lifetime.syntax().text_range().start());
473477
if let Some(parent) = lifetime.syntax().parent() {
474-
if parent.kind() == syntax::SyntaxKind::ERROR {
478+
if parent.kind() == ERROR {
475479
return;
476480
}
477481

crates/rust-analyzer/src/caps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
3333
hover_provider: Some(HoverProviderCapability::Simple(true)),
3434
completion_provider: Some(CompletionOptions {
3535
resolve_provider: completions_resolve_provider(client_caps),
36-
trigger_characters: Some(vec![":".to_string(), ".".to_string()]),
36+
trigger_characters: Some(vec![":".to_string(), ".".to_string(), "'".to_string()]),
3737
all_commit_characters: None,
3838
completion_item: None,
3939
work_done_progress_options: WorkDoneProgressOptions { work_done_progress: None },

0 commit comments

Comments
 (0)