From 2347c03dcd717fbc0648c1e4e3d64a886217de5d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 10 Mar 2020 18:39:17 +0100 Subject: Introduce CompletionOptions --- crates/ra_ide/src/completion.rs | 25 ++++++++++++++++++++-- crates/ra_ide/src/completion/complete_postfix.rs | 2 +- crates/ra_ide/src/completion/completion_context.rs | 5 ++++- crates/ra_ide/src/completion/completion_item.rs | 10 ++++++--- crates/ra_ide/src/completion/presentation.rs | 11 ++-------- crates/ra_ide/src/lib.rs | 13 +++++++++-- 6 files changed, 48 insertions(+), 18 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index c378c2c62..a27e0fc15 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -33,6 +33,23 @@ pub use crate::completion::completion_item::{ CompletionItem, CompletionItemKind, InsertTextFormat, }; +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CompletionOptions { + pub enable_postfix_completions: bool, + pub add_call_parenthesis: bool, + pub add_call_argument_snippets: bool, +} + +impl Default for CompletionOptions { + fn default() -> Self { + CompletionOptions { + enable_postfix_completions: true, + add_call_parenthesis: true, + add_call_argument_snippets: true, + } + } +} + /// Main entry point for completion. We run completion as a two-phase process. /// /// First, we look at the position and collect a so-called `CompletionContext. @@ -55,8 +72,12 @@ pub use crate::completion::completion_item::{ /// `foo` *should* be present among the completion variants. Filtering by /// identifier prefix/fuzzy match should be done higher in the stack, together /// with ordering of completions (currently this is done by the client). -pub(crate) fn completions(db: &RootDatabase, position: FilePosition) -> Option { - let ctx = CompletionContext::new(db, position)?; +pub(crate) fn completions( + db: &RootDatabase, + position: FilePosition, + opts: &CompletionOptions, +) -> Option { + let ctx = CompletionContext::new(db, position, opts)?; let mut acc = Completions::default(); diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index 65ecea125..6d000548d 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -12,7 +12,7 @@ use crate::{ }; pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.db.feature_flags.get("completion.enable-postfix") { + if !ctx.options.enable_postfix_completions { return; } diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 40535c09e..3646fb8dc 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -11,7 +11,7 @@ use ra_syntax::{ }; use ra_text_edit::AtomTextEdit; -use crate::FilePosition; +use crate::{completion::CompletionOptions, FilePosition}; /// `CompletionContext` is created early during completion to figure out, where /// exactly is the cursor, syntax-wise. @@ -19,6 +19,7 @@ use crate::FilePosition; pub(crate) struct CompletionContext<'a> { pub(super) sema: Semantics<'a, RootDatabase>, pub(super) db: &'a RootDatabase, + pub(super) options: &'a CompletionOptions, pub(super) offset: TextUnit, /// The token before the cursor, in the original file. pub(super) original_token: SyntaxToken, @@ -57,6 +58,7 @@ impl<'a> CompletionContext<'a> { pub(super) fn new( db: &'a RootDatabase, position: FilePosition, + options: &'a CompletionOptions, ) -> Option> { let sema = Semantics::new(db); @@ -80,6 +82,7 @@ impl<'a> CompletionContext<'a> { let mut ctx = CompletionContext { sema, db, + options, original_token, token, offset: position.offset, diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index 19bbb2517..1d14e9636 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs @@ -321,14 +321,18 @@ impl Into> for Completions { #[cfg(test)] pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { - use crate::completion::completions; - use crate::mock_analysis::{analysis_and_position, single_file_with_position}; + use crate::{ + completion::{completions, CompletionOptions}, + mock_analysis::{analysis_and_position, single_file_with_position}, + }; + let (analysis, position) = if code.contains("//-") { analysis_and_position(code) } else { single_file_with_position(code) }; - let completions = completions(&analysis.db, position).unwrap(); + let options = CompletionOptions::default(); + let completions = completions(&analysis.db, position, &options).unwrap(); let completion_items: Vec = completions.into(); let mut kind_completions: Vec = completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index aada4d025..25aff329e 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -212,21 +212,14 @@ impl Completions { .detail(function_signature.to_string()); // If not an import, add parenthesis automatically. - if ctx.use_item_syntax.is_none() - && !ctx.is_call - && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") - { + if ctx.use_item_syntax.is_none() && !ctx.is_call && ctx.options.add_call_parenthesis { tested_by!(inserts_parens_for_function_calls); let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 { (format!("{}()$0", name), format!("{}()", name)) } else { builder = builder.trigger_call_info(); - let snippet = if ctx - .db - .feature_flags - .get("completion.insertion.add-argument-snippets") - { + let snippet = if ctx.options.add_call_argument_snippets { let to_skip = if has_self_param { 1 } else { 0 }; let function_params_snippet = join( function_signature.parameter_names.iter().skip(to_skip).enumerate().map( diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index c60e86aea..0d91ea749 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -62,7 +62,7 @@ use crate::display::ToNav; pub use crate::{ assists::{Assist, AssistId}, call_hierarchy::CallItem, - completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, + completion::{CompletionItem, CompletionItemKind, CompletionOptions, InsertTextFormat}, diagnostics::Severity, display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, expand_macro::ExpandedMacro, @@ -451,7 +451,16 @@ impl Analysis { /// Computes completions at the given position. pub fn completions(&self, position: FilePosition) -> Cancelable>> { - self.with_db(|db| completion::completions(db, position).map(Into::into)) + let opts = CompletionOptions { + enable_postfix_completions: self.feature_flags().get("completion.enable-postfix"), + add_call_parenthesis: self + .feature_flags() + .get("completion.insertion.add-call-parenthesis"), + add_call_argument_snippets: self + .feature_flags() + .get("completion.insertion.add-argument-snippets"), + }; + self.with_db(|db| completion::completions(db, position, &opts).map(Into::into)) } /// Computes assists (aka code actions aka intentions) for the given -- cgit v1.2.3 From bf582e77d6e5603149b355a5650cd4d15318f776 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 10 Mar 2020 18:47:09 +0100 Subject: Pull completion options up to the rust-analyzer --- crates/ra_ide/src/completion/presentation.rs | 5 +---- crates/ra_ide/src/lib.rs | 17 ++++++----------- 2 files changed, 7 insertions(+), 15 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 25aff329e..3dc56e4a3 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -104,10 +104,7 @@ impl Completions { }; // Add `<>` for generic types - if ctx.is_path_type - && !ctx.has_type_args - && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") - { + if ctx.is_path_type && !ctx.has_type_args && ctx.options.add_call_parenthesis { let has_non_default_type_params = match resolution { ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(ctx.db), ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(ctx.db), diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 0d91ea749..d888bb745 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -450,17 +450,12 @@ impl Analysis { } /// Computes completions at the given position. - pub fn completions(&self, position: FilePosition) -> Cancelable>> { - let opts = CompletionOptions { - enable_postfix_completions: self.feature_flags().get("completion.enable-postfix"), - add_call_parenthesis: self - .feature_flags() - .get("completion.insertion.add-call-parenthesis"), - add_call_argument_snippets: self - .feature_flags() - .get("completion.insertion.add-argument-snippets"), - }; - self.with_db(|db| completion::completions(db, position, &opts).map(Into::into)) + pub fn completions( + &self, + position: FilePosition, + options: &CompletionOptions, + ) -> Cancelable>> { + self.with_db(|db| completion::completions(db, position, options).map(Into::into)) } /// Computes assists (aka code actions aka intentions) for the given -- cgit v1.2.3 From 14094e44770559c13a1e8bdfcfb989d3bedd00d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 10 Mar 2020 18:56:15 +0100 Subject: Move FeatureFlags --- crates/ra_ide/src/lib.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index d888bb745..9f45003d3 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -84,7 +84,6 @@ pub use ra_db::{ }; pub use ra_ide_db::{ change::{AnalysisChange, LibraryData}, - feature_flags::FeatureFlags, line_index::{LineCol, LineIndex}, line_index_utils::translate_offset_with_edit, search::SearchScope, @@ -131,13 +130,13 @@ pub struct AnalysisHost { impl Default for AnalysisHost { fn default() -> AnalysisHost { - AnalysisHost::new(None, FeatureFlags::default()) + AnalysisHost::new(None) } } impl AnalysisHost { - pub fn new(lru_capcity: Option, feature_flags: FeatureFlags) -> AnalysisHost { - AnalysisHost { db: RootDatabase::new(lru_capcity, feature_flags) } + pub fn new(lru_capacity: Option) -> AnalysisHost { + AnalysisHost { db: RootDatabase::new(lru_capacity) } } /// Returns a snapshot of the current state, which you can query for /// semantic information. @@ -145,10 +144,6 @@ impl AnalysisHost { Analysis { db: self.db.snapshot() } } - pub fn feature_flags(&self) -> &FeatureFlags { - &self.db.feature_flags - } - /// Applies changes to the current state of the world. If there are /// outstanding snapshots, they will be canceled. pub fn apply_change(&mut self, change: AnalysisChange) { @@ -224,11 +219,6 @@ impl Analysis { (host.analysis(), file_id) } - /// Features for Analysis. - pub fn feature_flags(&self) -> &FeatureFlags { - &self.db.feature_flags - } - /// Debug info about the current state of the analysis. pub fn status(&self) -> Cancelable { self.with_db(|db| status::status(&*db)) -- cgit v1.2.3