Skip to content

Commit ad204f7

Browse files
committed
Mostly remove ImoportLocator infra
1 parent cf812c1 commit ad204f7

File tree

3 files changed

+21
-178
lines changed

3 files changed

+21
-178
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use ra_syntax::{
66

77
use crate::{
88
assist_ctx::{ActionBuilder, Assist, AssistCtx},
9-
auto_import_text_edit, AssistId, ImportsLocator,
9+
auto_import_text_edit, AssistId,
1010
};
11+
use ra_ide_db::imports_locator::ImportsLocatorIde;
1112

1213
// Assist: auto_import
1314
//
@@ -26,10 +27,7 @@ use crate::{
2627
// let map = HashMap<|>::new();
2728
// }
2829
// ```
29-
pub(crate) fn auto_import<F: ImportsLocator>(
30-
ctx: AssistCtx,
31-
imports_locator: &mut F,
32-
) -> Option<Assist> {
30+
pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
3331
let path_to_import: ast::Path = ctx.find_node_at_offset()?;
3432
let path_to_import_syntax = path_to_import.syntax();
3533
if path_to_import_syntax.ancestors().find_map(ast::UseItem::cast).is_some() {
@@ -52,6 +50,8 @@ pub(crate) fn auto_import<F: ImportsLocator>(
5250
return None;
5351
}
5452

53+
let mut imports_locator = ImportsLocatorIde::new(ctx.db);
54+
5555
let proposed_imports = imports_locator
5656
.find_imports(&name_to_import)
5757
.into_iter()
@@ -81,16 +81,12 @@ fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode)
8181
#[cfg(test)]
8282
mod tests {
8383
use super::*;
84-
use crate::helpers::{
85-
check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable,
86-
TestImportsLocator,
87-
};
84+
use crate::helpers::{check_assist, check_assist_not_applicable};
8885

8986
#[test]
9087
fn applicable_when_found_an_import() {
91-
check_assist_with_imports_locator(
88+
check_assist(
9289
auto_import,
93-
TestImportsLocator::new,
9490
r"
9591
<|>PubStruct
9692
@@ -112,9 +108,8 @@ mod tests {
112108

113109
#[test]
114110
fn auto_imports_are_merged() {
115-
check_assist_with_imports_locator(
111+
check_assist(
116112
auto_import,
117-
TestImportsLocator::new,
118113
r"
119114
use PubMod::PubStruct1;
120115
@@ -148,9 +143,8 @@ mod tests {
148143

149144
#[test]
150145
fn applicable_when_found_multiple_imports() {
151-
check_assist_with_imports_locator(
146+
check_assist(
152147
auto_import,
153-
TestImportsLocator::new,
154148
r"
155149
PubSt<|>ruct
156150
@@ -184,9 +178,8 @@ mod tests {
184178

185179
#[test]
186180
fn not_applicable_for_already_imported_types() {
187-
check_assist_with_imports_locator_not_applicable(
181+
check_assist_not_applicable(
188182
auto_import,
189-
TestImportsLocator::new,
190183
r"
191184
use PubMod::PubStruct;
192185
@@ -201,9 +194,8 @@ mod tests {
201194

202195
#[test]
203196
fn not_applicable_for_types_with_private_paths() {
204-
check_assist_with_imports_locator_not_applicable(
197+
check_assist_not_applicable(
205198
auto_import,
206-
TestImportsLocator::new,
207199
r"
208200
PrivateStruct<|>
209201
@@ -216,19 +208,17 @@ mod tests {
216208

217209
#[test]
218210
fn not_applicable_when_no_imports_found() {
219-
check_assist_with_imports_locator_not_applicable(
211+
check_assist_not_applicable(
220212
auto_import,
221-
TestImportsLocator::new,
222213
"
223214
PubStruct<|>",
224215
);
225216
}
226217

227218
#[test]
228219
fn not_applicable_in_import_statements() {
229-
check_assist_with_imports_locator_not_applicable(
220+
check_assist_not_applicable(
230221
auto_import,
231-
TestImportsLocator::new,
232222
r"
233223
use PubStruct<|>;
234224
@@ -240,9 +230,8 @@ mod tests {
240230

241231
#[test]
242232
fn function_import() {
243-
check_assist_with_imports_locator(
233+
check_assist(
244234
auto_import,
245-
TestImportsLocator::new,
246235
r"
247236
test_function<|>
248237

crates/ra_assists/src/lib.rs

Lines changed: 6 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ mod doc_tests;
1212
pub mod ast_transform;
1313

1414
use either::Either;
15-
use hir::ModuleDef;
1615
use ra_db::FileRange;
17-
use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase};
16+
use ra_ide_db::RootDatabase;
1817
use ra_syntax::{TextRange, TextUnit};
1918
use ra_text_edit::TextEdit;
2019

@@ -73,50 +72,6 @@ pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe
7372
})
7473
}
7574

76-
/// A functionality for locating imports for the given name.
77-
///
78-
/// Currently has to be a trait with the real implementation provided by the ra_ide_api crate,
79-
/// due to the search functionality located there.
80-
/// Later, this trait should be removed completely and the search functionality moved to a separate crate,
81-
/// accessible from the ra_assists crate.
82-
pub trait ImportsLocator {
83-
/// Finds all imports for the given name and the module that contains this name.
84-
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>;
85-
}
86-
87-
impl ImportsLocator for ImportsLocatorIde<'_> {
88-
fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> {
89-
self.find_imports(name_to_import)
90-
}
91-
}
92-
93-
/// Return all the assists applicable at the given position
94-
/// and additional assists that need the imports locator functionality to work.
95-
///
96-
/// Assists are returned in the "resolved" state, that is with edit fully
97-
/// computed.
98-
pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
99-
let mut imports_locator = ImportsLocatorIde::new(db);
100-
AssistCtx::with_ctx(db, range, true, |ctx| {
101-
let mut assists = assists::all()
102-
.iter()
103-
.map(|f| f(ctx.clone()))
104-
.chain(
105-
assists::all_with_imports_locator()
106-
.iter()
107-
.map(|f| f(ctx.clone(), &mut imports_locator)),
108-
)
109-
.filter_map(std::convert::identity)
110-
.map(|a| match a {
111-
Assist::Resolved { assist } => assist,
112-
Assist::Unresolved { .. } => unreachable!(),
113-
})
114-
.collect();
115-
sort_assists(&mut assists);
116-
assists
117-
})
118-
}
119-
12075
/// Return all the assists applicable at the given position.
12176
///
12277
/// Assists are returned in the "resolved" state, that is with edit fully
@@ -147,7 +102,7 @@ fn sort_assists(assists: &mut Vec<ResolvedAssist>) {
147102
}
148103

149104
mod assists {
150-
use crate::{Assist, AssistCtx, ImportsLocator};
105+
use crate::{Assist, AssistCtx};
151106

152107
mod add_derive;
153108
mod add_explicit_type;
@@ -206,72 +161,19 @@ mod assists {
206161
raw_string::make_usual_string,
207162
raw_string::remove_hash,
208163
early_return::convert_to_guarded_return,
164+
auto_import::auto_import,
209165
]
210166
}
211-
212-
pub(crate) fn all_with_imports_locator<'a, F: ImportsLocator>(
213-
) -> &'a [fn(AssistCtx, &mut F) -> Option<Assist>] {
214-
&[auto_import::auto_import]
215-
}
216167
}
217168

218169
#[cfg(test)]
219170
mod helpers {
220-
use hir::db::DefDatabase;
221-
use ra_db::{fixture::WithFixture, FileId, FileRange};
171+
use ra_db::{fixture::WithFixture, FileRange};
172+
use ra_ide_db::RootDatabase;
222173
use ra_syntax::TextRange;
223174
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
224175

225-
use crate::{Assist, AssistCtx, ImportsLocator};
226-
use ra_ide_db::RootDatabase;
227-
use std::sync::Arc;
228-
229-
// FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed.
230-
pub(crate) struct TestImportsLocator {
231-
db: Arc<RootDatabase>,
232-
test_file_id: FileId,
233-
}
234-
235-
impl TestImportsLocator {
236-
pub(crate) fn new(db: Arc<RootDatabase>, test_file_id: FileId) -> Self {
237-
TestImportsLocator { db, test_file_id }
238-
}
239-
}
240-
241-
impl ImportsLocator for TestImportsLocator {
242-
fn find_imports(&mut self, name_to_import: &str) -> Vec<hir::ModuleDef> {
243-
let crate_def_map = self.db.crate_def_map(self.db.test_crate());
244-
let mut findings = Vec::new();
245-
246-
let mut module_ids_to_process =
247-
crate_def_map.modules_for_file(self.test_file_id).collect::<Vec<_>>();
248-
249-
while !module_ids_to_process.is_empty() {
250-
let mut more_ids_to_process = Vec::new();
251-
for local_module_id in module_ids_to_process.drain(..) {
252-
for (name, namespace_data) in
253-
crate_def_map[local_module_id].scope.entries_without_primitives()
254-
{
255-
let found_a_match = &name.to_string() == name_to_import;
256-
vec![namespace_data.types, namespace_data.values]
257-
.into_iter()
258-
.filter_map(std::convert::identity)
259-
.for_each(|(module_def_id, _)| {
260-
if found_a_match {
261-
findings.push(module_def_id.into());
262-
}
263-
if let hir::ModuleDefId::ModuleId(module_id) = module_def_id {
264-
more_ids_to_process.push(module_id.local_id);
265-
}
266-
});
267-
}
268-
}
269-
module_ids_to_process = more_ids_to_process;
270-
}
271-
272-
findings
273-
}
274-
}
176+
use crate::{Assist, AssistCtx};
275177

276178
pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
277179
let (before_cursor_pos, before) = extract_offset(before);
@@ -297,38 +199,6 @@ mod helpers {
297199
assert_eq_text!(after, &actual);
298200
}
299201

300-
pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>(
301-
assist: fn(AssistCtx, &mut F) -> Option<Assist>,
302-
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
303-
before: &str,
304-
after: &str,
305-
) {
306-
let (before_cursor_pos, before) = extract_offset(before);
307-
let (db, file_id) = RootDatabase::with_single_file(&before);
308-
let db = Arc::new(db);
309-
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
310-
let frange =
311-
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
312-
let assist =
313-
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator))
314-
.expect("code action is not applicable");
315-
let action = match assist {
316-
Assist::Unresolved { .. } => unreachable!(),
317-
Assist::Resolved { assist } => assist.get_first_action(),
318-
};
319-
320-
let actual = action.edit.apply(&before);
321-
let actual_cursor_pos = match action.cursor_position {
322-
None => action
323-
.edit
324-
.apply_to_offset(before_cursor_pos)
325-
.expect("cursor position is affected by the edit"),
326-
Some(off) => off,
327-
};
328-
let actual = add_cursor(&actual, actual_cursor_pos);
329-
assert_eq_text!(after, &actual);
330-
}
331-
332202
pub(crate) fn check_assist_range(
333203
assist: fn(AssistCtx) -> Option<Assist>,
334204
before: &str,
@@ -402,22 +272,6 @@ mod helpers {
402272
assert!(assist.is_none());
403273
}
404274

405-
pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>(
406-
assist: fn(AssistCtx, &mut F) -> Option<Assist>,
407-
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
408-
before: &str,
409-
) {
410-
let (before_cursor_pos, before) = extract_offset(before);
411-
let (db, file_id) = RootDatabase::with_single_file(&before);
412-
let db = Arc::new(db);
413-
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
414-
let frange =
415-
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
416-
let assist =
417-
AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator));
418-
assert!(assist.is_none());
419-
}
420-
421275
pub(crate) fn check_assist_range_not_applicable(
422276
assist: fn(AssistCtx) -> Option<Assist>,
423277
before: &str,

crates/ra_ide/src/assists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Assist {
1717
}
1818

1919
pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> {
20-
ra_assists::assists_with_imports_locator(db, frange)
20+
ra_assists::assists(db, frange)
2121
.into_iter()
2222
.map(|assist| {
2323
let file_id = frange.file_id;

0 commit comments

Comments
 (0)