From ea594c4c44ead8bffcc02c222869334c42ce3b51 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 30 May 2022 14:17:58 +0200 Subject: [PATCH] Use char for trigger character --- crates/ide-completion/src/lib.rs | 4 ++-- crates/ide-completion/src/tests.rs | 6 +++--- crates/ide-completion/src/tests/fn_param.rs | 6 +++--- crates/ide-completion/src/tests/visibility.rs | 6 +++--- crates/ide/src/lib.rs | 2 +- crates/rust-analyzer/src/handlers.rs | 7 ++++--- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs index 2bbdc48e63..9659efad61 100644 --- a/crates/ide-completion/src/lib.rs +++ b/crates/ide-completion/src/lib.rs @@ -143,7 +143,7 @@ pub fn completions( db: &RootDatabase, config: &CompletionConfig, position: FilePosition, - trigger_character: Option<&str>, + trigger_character: Option, ) -> Option { let ctx = &CompletionContext::new(db, position, config)?; let mut acc = Completions::default(); @@ -151,7 +151,7 @@ pub fn completions( { let acc = &mut acc; // prevent `(` from triggering unwanted completion noise - if trigger_character != Some("(") { + if trigger_character != Some('(') { completions::attribute::complete_attribute(acc, ctx); completions::attribute::complete_derive(acc, ctx); completions::attribute::complete_known_attribute_input(acc, ctx); diff --git a/crates/ide-completion/src/tests.rs b/crates/ide-completion/src/tests.rs index 7625018058..d30ff77bab 100644 --- a/crates/ide-completion/src/tests.rs +++ b/crates/ide-completion/src/tests.rs @@ -88,7 +88,7 @@ pub(crate) fn completion_list_no_kw(ra_fixture: &str) -> String { pub(crate) fn completion_list_with_trigger_character( ra_fixture: &str, - trigger_character: Option<&str>, + trigger_character: Option, ) -> String { completion_list_with_config(TEST_CONFIG, ra_fixture, true, trigger_character) } @@ -97,7 +97,7 @@ fn completion_list_with_config( config: CompletionConfig, ra_fixture: &str, include_keywords: bool, - trigger_character: Option<&str>, + trigger_character: Option, ) -> String { // filter out all but one builtintype completion for smaller test outputs let items = get_all_items(config, ra_fixture, trigger_character); @@ -225,7 +225,7 @@ pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxE pub(crate) fn get_all_items( config: CompletionConfig, code: &str, - trigger_character: Option<&str>, + trigger_character: Option, ) -> Vec { let (db, position) = position(code); let res = crate::completions(&db, &config, position, trigger_character) diff --git a/crates/ide-completion/src/tests/fn_param.rs b/crates/ide-completion/src/tests/fn_param.rs index a08f3f6e88..f5a5b5bae6 100644 --- a/crates/ide-completion/src/tests/fn_param.rs +++ b/crates/ide-completion/src/tests/fn_param.rs @@ -7,8 +7,8 @@ fn check(ra_fixture: &str, expect: Expect) { expect.assert_eq(&actual); } -fn check_with_trigger_character(ra_fixture: &str, trigger_character: Option<&str>, expect: Expect) { - let actual = completion_list_with_trigger_character(ra_fixture, trigger_character); +fn check_with_trigger_character(ra_fixture: &str, trigger_character: char, expect: Expect) { + let actual = completion_list_with_trigger_character(ra_fixture, Some(trigger_character)); expect.assert_eq(&actual) } @@ -124,7 +124,7 @@ fn trigger_by_l_paren() { r#" fn foo($0) "#, - Some("("), + '(', expect![[]], ) } diff --git a/crates/ide-completion/src/tests/visibility.rs b/crates/ide-completion/src/tests/visibility.rs index 64bbb444bf..151dd6a7e8 100644 --- a/crates/ide-completion/src/tests/visibility.rs +++ b/crates/ide-completion/src/tests/visibility.rs @@ -8,8 +8,8 @@ fn check(ra_fixture: &str, expect: Expect) { expect.assert_eq(&actual) } -fn check_with_trigger_character(ra_fixture: &str, trigger_character: Option<&str>, expect: Expect) { - let actual = completion_list_with_trigger_character(ra_fixture, trigger_character); +fn check_with_trigger_character(ra_fixture: &str, trigger_character: char, expect: Expect) { + let actual = completion_list_with_trigger_character(ra_fixture, Some(trigger_character)); expect.assert_eq(&actual) } @@ -20,7 +20,7 @@ fn empty_pub() { r#" pub($0) "#, - Some("("), + '(', expect![[r#" kw crate kw in diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 29bd86430d..aab5ceda36 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -548,7 +548,7 @@ impl Analysis { &self, config: &CompletionConfig, position: FilePosition, - trigger_character: Option<&str>, + trigger_character: Option, ) -> Cancellable>> { self.with_db(|db| { ide_completion::completions(db, config, position, trigger_character).map(Into::into) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index a8e3e93b8f..ff61081aa8 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -796,9 +796,10 @@ pub(crate) fn handle_completion( let _p = profile::span("handle_completion"); let text_document_position = params.text_document_position.clone(); let position = from_proto::file_position(&snap, params.text_document_position)?; - let completion_trigger_character = params.context.and_then(|ctx| ctx.trigger_character); + let completion_trigger_character = + params.context.and_then(|ctx| ctx.trigger_character).and_then(|s| s.chars().next()); - if Some(":") == completion_trigger_character.as_deref() { + if Some(':') == completion_trigger_character { let source_file = snap.analysis.parse(position.file_id)?; let left_token = source_file.syntax().token_at_offset(position.offset).left_biased(); let completion_triggered_after_single_colon = match left_token { @@ -814,7 +815,7 @@ pub(crate) fn handle_completion( let items = match snap.analysis.completions( completion_config, position, - completion_trigger_character.as_deref(), + completion_trigger_character, )? { None => return Ok(None), Some(items) => items,