From fb7105a5801ab1d0ede830cd53bbc3ccbf0b5e2c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 30 May 2021 16:41:33 +0200 Subject: Add config setting for self-on-the-fly --- crates/ide_completion/src/completions/dot.rs | 2 +- crates/ide_completion/src/config.rs | 1 + crates/ide_completion/src/render.rs | 2 +- crates/ide_completion/src/test_utils.rs | 1 + crates/rust-analyzer/src/config.rs | 4 ++++ crates/rust-analyzer/src/integrated_benchmarks.rs | 2 ++ crates/rust-analyzer/src/to_proto.rs | 1 + docs/dev/architecture.md | 5 +++++ docs/user/generated_config.adoc | 6 ++++++ editors/code/package.json | 5 +++++ 10 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs index 886251639..302c9ccbd 100644 --- a/crates/ide_completion/src/completions/dot.rs +++ b/crates/ide_completion/src/completions/dot.rs @@ -30,7 +30,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { } fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path { + if !ctx.is_trivial_path || !ctx.config.enable_self_on_the_fly { return; } ctx.scope.process_all_names(&mut |name, def| { diff --git a/crates/ide_completion/src/config.rs b/crates/ide_completion/src/config.rs index d70ed6c1c..c300ce887 100644 --- a/crates/ide_completion/src/config.rs +++ b/crates/ide_completion/src/config.rs @@ -10,6 +10,7 @@ use ide_db::helpers::{insert_use::InsertUseConfig, SnippetCap}; pub struct CompletionConfig { pub enable_postfix_completions: bool, pub enable_imports_on_the_fly: bool, + pub enable_self_on_the_fly: bool, pub add_call_parenthesis: bool, pub add_call_argument_snippets: bool, pub snippet_cap: Option, diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 97dd52851..a49a60711 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -139,7 +139,7 @@ impl<'a> Render<'a> { let mut item = CompletionItem::new( CompletionKind::Reference, self.ctx.source_range(), - receiver.map_or_else(|| name.to_string(), |receiver| format!("{}.{}", receiver, name)), + receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name)), ); item.kind(SymbolKind::Field) .detail(ty.display(self.ctx.db()).to_string()) diff --git a/crates/ide_completion/src/test_utils.rs b/crates/ide_completion/src/test_utils.rs index 93c7c872c..b0a4b2026 100644 --- a/crates/ide_completion/src/test_utils.rs +++ b/crates/ide_completion/src/test_utils.rs @@ -19,6 +19,7 @@ use crate::{item::CompletionKind, CompletionConfig, CompletionItem}; pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { enable_postfix_completions: true, enable_imports_on_the_fly: true, + enable_self_on_the_fly: true, add_call_parenthesis: true, add_call_argument_snippets: true, snippet_cap: SnippetCap::new(true), diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index a67b0bb25..ae78fd4f6 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -100,6 +100,9 @@ config_data! { /// Toggles the additional completions that automatically add imports when completed. /// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled. completion_autoimport_enable: bool = "true", + /// Toggles the additional completions that automatically show method calls and field accesses + /// with `self` prefixed to them when inside a method. + completion_autoself_enable: bool = "true", /// Whether to show native rust-analyzer diagnostics. diagnostics_enable: bool = "true", @@ -666,6 +669,7 @@ impl Config { enable_postfix_completions: self.data.completion_postfix_enable, enable_imports_on_the_fly: self.data.completion_autoimport_enable && completion_item_edit_resolve(&self.caps), + enable_self_on_the_fly: self.data.completion_autoself_enable, add_call_parenthesis: self.data.completion_addCallParenthesis, add_call_argument_snippets: self.data.completion_addCallArgumentSnippets, insert_use: self.insert_use_config(), diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs index 781073fe5..ec36a5f5c 100644 --- a/crates/rust-analyzer/src/integrated_benchmarks.rs +++ b/crates/rust-analyzer/src/integrated_benchmarks.rs @@ -132,6 +132,7 @@ fn integrated_completion_benchmark() { let config = CompletionConfig { enable_postfix_completions: true, enable_imports_on_the_fly: true, + enable_self_on_the_fly: true, add_call_parenthesis: true, add_call_argument_snippets: true, snippet_cap: SnippetCap::new(true), @@ -166,6 +167,7 @@ fn integrated_completion_benchmark() { let config = CompletionConfig { enable_postfix_completions: true, enable_imports_on_the_fly: true, + enable_self_on_the_fly: true, add_call_parenthesis: true, add_call_argument_snippets: true, snippet_cap: SnippetCap::new(true), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index f5c8535a2..2b2ef2c60 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -1178,6 +1178,7 @@ mod tests { &ide::CompletionConfig { enable_postfix_completions: true, enable_imports_on_the_fly: true, + enable_self_on_the_fly: true, add_call_parenthesis: true, add_call_argument_snippets: true, snippet_cap: SnippetCap::new(true), diff --git a/docs/dev/architecture.md b/docs/dev/architecture.md index 39edf9e19..2624069a5 100644 --- a/docs/dev/architecture.md +++ b/docs/dev/architecture.md @@ -447,3 +447,8 @@ This is cheap enough to enable in production. Similarly, we save live object counting (`RA_COUNT=1`). It is not cheap enough to enable in prod, and this is a bug which should be fixed. + +### Configurability + +rust-analyzer strives to be as configurable as possible while offering reasonable defaults where no configuration exists yet. +There will always be features that some people find more annoying than helpful, so giving the users the ability to tweak or disable these is a big part of offering a good user experience. diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 4a5782a57..dbd9a3503 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -136,6 +136,12 @@ Whether to show postfix snippets like `dbg`, `if`, `not`, etc. Toggles the additional completions that automatically add imports when completed. Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled. -- +[[rust-analyzer.completion.autoself.enable]]rust-analyzer.completion.autoself.enable (default: `true`):: ++ +-- +Toggles the additional completions that automatically show method calls and field accesses +with `self` prefixed to them when inside a method. +-- [[rust-analyzer.diagnostics.enable]]rust-analyzer.diagnostics.enable (default: `true`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 5b80cc1f9..42a06e137 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -572,6 +572,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.completion.autoself.enable": { + "markdownDescription": "Toggles the additional completions that automatically show method calls and field accesses\nwith `self` prefixed to them when inside a method.", + "default": true, + "type": "boolean" + }, "rust-analyzer.diagnostics.enable": { "markdownDescription": "Whether to show native rust-analyzer diagnostics.", "default": true, -- cgit v1.2.3