Skip to content

Commit 2347c03

Browse files
committed
Introduce CompletionOptions
1 parent 6b9d66b commit 2347c03

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

crates/ra_ide/src/completion.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ pub use crate::completion::completion_item::{
3333
CompletionItem, CompletionItemKind, InsertTextFormat,
3434
};
3535

36+
#[derive(Clone, Debug, PartialEq, Eq)]
37+
pub struct CompletionOptions {
38+
pub enable_postfix_completions: bool,
39+
pub add_call_parenthesis: bool,
40+
pub add_call_argument_snippets: bool,
41+
}
42+
43+
impl Default for CompletionOptions {
44+
fn default() -> Self {
45+
CompletionOptions {
46+
enable_postfix_completions: true,
47+
add_call_parenthesis: true,
48+
add_call_argument_snippets: true,
49+
}
50+
}
51+
}
52+
3653
/// Main entry point for completion. We run completion as a two-phase process.
3754
///
3855
/// First, we look at the position and collect a so-called `CompletionContext.
@@ -55,8 +72,12 @@ pub use crate::completion::completion_item::{
5572
/// `foo` *should* be present among the completion variants. Filtering by
5673
/// identifier prefix/fuzzy match should be done higher in the stack, together
5774
/// with ordering of completions (currently this is done by the client).
58-
pub(crate) fn completions(db: &RootDatabase, position: FilePosition) -> Option<Completions> {
59-
let ctx = CompletionContext::new(db, position)?;
75+
pub(crate) fn completions(
76+
db: &RootDatabase,
77+
position: FilePosition,
78+
opts: &CompletionOptions,
79+
) -> Option<Completions> {
80+
let ctx = CompletionContext::new(db, position, opts)?;
6081

6182
let mut acc = Completions::default();
6283

crates/ra_ide/src/completion/complete_postfix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
};
1313

1414
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
15-
if !ctx.db.feature_flags.get("completion.enable-postfix") {
15+
if !ctx.options.enable_postfix_completions {
1616
return;
1717
}
1818

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use ra_syntax::{
1111
};
1212
use ra_text_edit::AtomTextEdit;
1313

14-
use crate::FilePosition;
14+
use crate::{completion::CompletionOptions, FilePosition};
1515

1616
/// `CompletionContext` is created early during completion to figure out, where
1717
/// exactly is the cursor, syntax-wise.
1818
#[derive(Debug)]
1919
pub(crate) struct CompletionContext<'a> {
2020
pub(super) sema: Semantics<'a, RootDatabase>,
2121
pub(super) db: &'a RootDatabase,
22+
pub(super) options: &'a CompletionOptions,
2223
pub(super) offset: TextUnit,
2324
/// The token before the cursor, in the original file.
2425
pub(super) original_token: SyntaxToken,
@@ -57,6 +58,7 @@ impl<'a> CompletionContext<'a> {
5758
pub(super) fn new(
5859
db: &'a RootDatabase,
5960
position: FilePosition,
61+
options: &'a CompletionOptions,
6062
) -> Option<CompletionContext<'a>> {
6163
let sema = Semantics::new(db);
6264

@@ -80,6 +82,7 @@ impl<'a> CompletionContext<'a> {
8082
let mut ctx = CompletionContext {
8183
sema,
8284
db,
85+
options,
8386
original_token,
8487
token,
8588
offset: position.offset,

crates/ra_ide/src/completion/completion_item.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,18 @@ impl Into<Vec<CompletionItem>> for Completions {
321321

322322
#[cfg(test)]
323323
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
324-
use crate::completion::completions;
325-
use crate::mock_analysis::{analysis_and_position, single_file_with_position};
324+
use crate::{
325+
completion::{completions, CompletionOptions},
326+
mock_analysis::{analysis_and_position, single_file_with_position},
327+
};
328+
326329
let (analysis, position) = if code.contains("//-") {
327330
analysis_and_position(code)
328331
} else {
329332
single_file_with_position(code)
330333
};
331-
let completions = completions(&analysis.db, position).unwrap();
334+
let options = CompletionOptions::default();
335+
let completions = completions(&analysis.db, position, &options).unwrap();
332336
let completion_items: Vec<CompletionItem> = completions.into();
333337
let mut kind_completions: Vec<CompletionItem> =
334338
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();

crates/ra_ide/src/completion/presentation.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,14 @@ impl Completions {
212212
.detail(function_signature.to_string());
213213

214214
// If not an import, add parenthesis automatically.
215-
if ctx.use_item_syntax.is_none()
216-
&& !ctx.is_call
217-
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
218-
{
215+
if ctx.use_item_syntax.is_none() && !ctx.is_call && ctx.options.add_call_parenthesis {
219216
tested_by!(inserts_parens_for_function_calls);
220217

221218
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
222219
(format!("{}()$0", name), format!("{}()", name))
223220
} else {
224221
builder = builder.trigger_call_info();
225-
let snippet = if ctx
226-
.db
227-
.feature_flags
228-
.get("completion.insertion.add-argument-snippets")
229-
{
222+
let snippet = if ctx.options.add_call_argument_snippets {
230223
let to_skip = if has_self_param { 1 } else { 0 };
231224
let function_params_snippet = join(
232225
function_signature.parameter_names.iter().skip(to_skip).enumerate().map(

crates/ra_ide/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::display::ToNav;
6262
pub use crate::{
6363
assists::{Assist, AssistId},
6464
call_hierarchy::CallItem,
65-
completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
65+
completion::{CompletionItem, CompletionItemKind, CompletionOptions, InsertTextFormat},
6666
diagnostics::Severity,
6767
display::{file_structure, FunctionSignature, NavigationTarget, StructureNode},
6868
expand_macro::ExpandedMacro,
@@ -451,7 +451,16 @@ impl Analysis {
451451

452452
/// Computes completions at the given position.
453453
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
454-
self.with_db(|db| completion::completions(db, position).map(Into::into))
454+
let opts = CompletionOptions {
455+
enable_postfix_completions: self.feature_flags().get("completion.enable-postfix"),
456+
add_call_parenthesis: self
457+
.feature_flags()
458+
.get("completion.insertion.add-call-parenthesis"),
459+
add_call_argument_snippets: self
460+
.feature_flags()
461+
.get("completion.insertion.add-argument-snippets"),
462+
};
463+
self.with_db(|db| completion::completions(db, position, &opts).map(Into::into))
455464
}
456465

457466
/// Computes assists (aka code actions aka intentions) for the given

0 commit comments

Comments
 (0)