Skip to content

Commit e5df8c4

Browse files
bors[bot]matklad
andauthored
Merge #3551
3551: Move FeatureFlags r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents daf3609 + 14094e4 commit e5df8c4

File tree

14 files changed

+88
-68
lines changed

14 files changed

+88
-68
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: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ impl Completions {
104104
};
105105

106106
// Add `<>` for generic types
107-
if ctx.is_path_type
108-
&& !ctx.has_type_args
109-
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
110-
{
107+
if ctx.is_path_type && !ctx.has_type_args && ctx.options.add_call_parenthesis {
111108
let has_non_default_type_params = match resolution {
112109
ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(ctx.db),
113110
ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(ctx.db),
@@ -212,21 +209,14 @@ impl Completions {
212209
.detail(function_signature.to_string());
213210

214211
// 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-
{
212+
if ctx.use_item_syntax.is_none() && !ctx.is_call && ctx.options.add_call_parenthesis {
219213
tested_by!(inserts_parens_for_function_calls);
220214

221215
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
222216
(format!("{}()$0", name), format!("{}()", name))
223217
} else {
224218
builder = builder.trigger_call_info();
225-
let snippet = if ctx
226-
.db
227-
.feature_flags
228-
.get("completion.insertion.add-argument-snippets")
229-
{
219+
let snippet = if ctx.options.add_call_argument_snippets {
230220
let to_skip = if has_self_param { 1 } else { 0 };
231221
let function_params_snippet = join(
232222
function_signature.parameter_names.iter().skip(to_skip).enumerate().map(

crates/ra_ide/src/lib.rs

Lines changed: 10 additions & 16 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,
@@ -84,7 +84,6 @@ pub use ra_db::{
8484
};
8585
pub use ra_ide_db::{
8686
change::{AnalysisChange, LibraryData},
87-
feature_flags::FeatureFlags,
8887
line_index::{LineCol, LineIndex},
8988
line_index_utils::translate_offset_with_edit,
9089
search::SearchScope,
@@ -131,24 +130,20 @@ pub struct AnalysisHost {
131130

132131
impl Default for AnalysisHost {
133132
fn default() -> AnalysisHost {
134-
AnalysisHost::new(None, FeatureFlags::default())
133+
AnalysisHost::new(None)
135134
}
136135
}
137136

138137
impl AnalysisHost {
139-
pub fn new(lru_capcity: Option<usize>, feature_flags: FeatureFlags) -> AnalysisHost {
140-
AnalysisHost { db: RootDatabase::new(lru_capcity, feature_flags) }
138+
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
139+
AnalysisHost { db: RootDatabase::new(lru_capacity) }
141140
}
142141
/// Returns a snapshot of the current state, which you can query for
143142
/// semantic information.
144143
pub fn analysis(&self) -> Analysis {
145144
Analysis { db: self.db.snapshot() }
146145
}
147146

148-
pub fn feature_flags(&self) -> &FeatureFlags {
149-
&self.db.feature_flags
150-
}
151-
152147
/// Applies changes to the current state of the world. If there are
153148
/// outstanding snapshots, they will be canceled.
154149
pub fn apply_change(&mut self, change: AnalysisChange) {
@@ -224,11 +219,6 @@ impl Analysis {
224219
(host.analysis(), file_id)
225220
}
226221

227-
/// Features for Analysis.
228-
pub fn feature_flags(&self) -> &FeatureFlags {
229-
&self.db.feature_flags
230-
}
231-
232222
/// Debug info about the current state of the analysis.
233223
pub fn status(&self) -> Cancelable<String> {
234224
self.with_db(|db| status::status(&*db))
@@ -450,8 +440,12 @@ impl Analysis {
450440
}
451441

452442
/// Computes completions at the given position.
453-
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
454-
self.with_db(|db| completion::completions(db, position).map(Into::into))
443+
pub fn completions(
444+
&self,
445+
position: FilePosition,
446+
options: &CompletionOptions,
447+
) -> Cancelable<Option<Vec<CompletionItem>>> {
448+
self.with_db(|db| completion::completions(db, position, options).map(Into::into))
455449
}
456450

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

crates/ra_ide_db/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
pub mod marks;
66
pub mod line_index;
77
pub mod line_index_utils;
8-
pub mod feature_flags;
98
pub mod symbol_index;
109
pub mod change;
1110
pub mod defs;
@@ -22,7 +21,7 @@ use ra_db::{
2221
};
2322
use rustc_hash::FxHashMap;
2423

25-
use crate::{feature_flags::FeatureFlags, line_index::LineIndex, symbol_index::SymbolsDatabase};
24+
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
2625

2726
#[salsa::database(
2827
ra_db::SourceDatabaseStorage,
@@ -37,7 +36,6 @@ use crate::{feature_flags::FeatureFlags, line_index::LineIndex, symbol_index::Sy
3736
#[derive(Debug)]
3837
pub struct RootDatabase {
3938
runtime: salsa::Runtime<RootDatabase>,
40-
pub feature_flags: Arc<FeatureFlags>,
4139
pub(crate) debug_data: Arc<DebugData>,
4240
pub last_gc: crate::wasm_shims::Instant,
4341
pub last_gc_check: crate::wasm_shims::Instant,
@@ -82,17 +80,16 @@ impl salsa::Database for RootDatabase {
8280

8381
impl Default for RootDatabase {
8482
fn default() -> RootDatabase {
85-
RootDatabase::new(None, FeatureFlags::default())
83+
RootDatabase::new(None)
8684
}
8785
}
8886

8987
impl RootDatabase {
90-
pub fn new(lru_capacity: Option<usize>, feature_flags: FeatureFlags) -> RootDatabase {
88+
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
9189
let mut db = RootDatabase {
9290
runtime: salsa::Runtime::default(),
9391
last_gc: crate::wasm_shims::Instant::now(),
9492
last_gc_check: crate::wasm_shims::Instant::now(),
95-
feature_flags: Arc::new(feature_flags),
9693
debug_data: Default::default(),
9794
};
9895
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
@@ -112,7 +109,6 @@ impl salsa::ParallelDatabase for RootDatabase {
112109
runtime: self.runtime.snapshot(self),
113110
last_gc: self.last_gc,
114111
last_gc_check: self.last_gc_check,
115-
feature_flags: Arc::clone(&self.feature_flags),
116112
debug_data: Arc::clone(&self.debug_data),
117113
})
118114
}

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ra_db::{
1212
salsa::{Database, Durability},
1313
FileId, SourceDatabaseExt,
1414
};
15-
use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol};
15+
use ra_ide::{Analysis, AnalysisChange, AnalysisHost, CompletionOptions, FilePosition, LineCol};
1616

1717
use crate::cli::{load_cargo::load_cargo, Verbosity};
1818

@@ -94,17 +94,19 @@ pub fn analysis_bench(verbosity: Verbosity, path: &Path, what: BenchWhat) -> Res
9494
.analysis()
9595
.file_line_index(file_id)?
9696
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
97-
let file_postion = FilePosition { file_id, offset };
97+
let file_position = FilePosition { file_id, offset };
9898

9999
if is_completion {
100-
let res =
101-
do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
100+
let options = CompletionOptions::default();
101+
let res = do_work(&mut host, file_id, |analysis| {
102+
analysis.completions(file_position, &options)
103+
});
102104
if verbosity.is_verbose() {
103105
println!("\n{:#?}", res);
104106
}
105107
} else {
106108
let res =
107-
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion));
109+
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_position));
108110
if verbosity.is_verbose() {
109111
println!("\n{:#?}", res);
110112
}

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::Path;
66
use anyhow::Result;
77
use crossbeam_channel::{unbounded, Receiver};
88
use ra_db::{CrateGraph, FileId, SourceRootId};
9-
use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags};
9+
use ra_ide::{AnalysisChange, AnalysisHost};
1010
use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace};
1111
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
1212
use rustc_hash::{FxHashMap, FxHashSet};
@@ -82,7 +82,7 @@ pub(crate) fn load(
8282
receiver: Receiver<VfsTask>,
8383
) -> AnalysisHost {
8484
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
85-
let mut host = AnalysisHost::new(lru_cap, FeatureFlags::default());
85+
let mut host = AnalysisHost::new(lru_cap);
8686
let mut analysis_change = AnalysisChange::new();
8787
analysis_change.set_crate_graph(crate_graph);
8888

crates/ra_ide_db/src/feature_flags.rs renamed to crates/rust-analyzer/src/feature_flags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
33
use rustc_hash::FxHashMap;
44

5+
// FIXME: looks like a much better design is to pass options to each call,
6+
// rather than to have a global ambient feature flags -- that way, the clients
7+
// can issue two successive calls with different options.
8+
59
/// Feature flags hold fine-grained toggles for all *user-visible* features of
610
/// rust-analyzer.
711
///

0 commit comments

Comments
 (0)