aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/lib.rs35
-rw-r--r--crates/ra_ide_db/src/defs.rs2
2 files changed, 24 insertions, 13 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 8285e93a4..1343043dd 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -168,16 +168,27 @@ mod assists {
168 168
169#[cfg(test)] 169#[cfg(test)]
170mod helpers { 170mod helpers {
171 use ra_db::{fixture::WithFixture, FileRange}; 171 use std::sync::Arc;
172 use ra_ide_db::RootDatabase; 172
173 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
174 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
173 use ra_syntax::TextRange; 175 use ra_syntax::TextRange;
174 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; 176 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
175 177
176 use crate::{Assist, AssistCtx}; 178 use crate::{Assist, AssistCtx};
177 179
180 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
181 let (mut db, file_id) = RootDatabase::with_single_file(text);
182 // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`,
183 // but it looks like this might need specialization? :(
184 let local_roots = vec![db.file_source_root(file_id)];
185 db.set_local_roots(Arc::new(local_roots));
186 (db, file_id)
187 }
188
178 pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) { 189 pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
179 let (before_cursor_pos, before) = extract_offset(before); 190 let (before_cursor_pos, before) = extract_offset(before);
180 let (db, file_id) = RootDatabase::with_single_file(&before); 191 let (db, file_id) = with_single_file(&before);
181 let frange = 192 let frange =
182 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 193 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
183 let assist = 194 let assist =
@@ -205,7 +216,7 @@ mod helpers {
205 after: &str, 216 after: &str,
206 ) { 217 ) {
207 let (range, before) = extract_range(before); 218 let (range, before) = extract_range(before);
208 let (db, file_id) = RootDatabase::with_single_file(&before); 219 let (db, file_id) = with_single_file(&before);
209 let frange = FileRange { file_id, range }; 220 let frange = FileRange { file_id, range };
210 let assist = 221 let assist =
211 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 222 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -227,7 +238,7 @@ mod helpers {
227 target: &str, 238 target: &str,
228 ) { 239 ) {
229 let (before_cursor_pos, before) = extract_offset(before); 240 let (before_cursor_pos, before) = extract_offset(before);
230 let (db, file_id) = RootDatabase::with_single_file(&before); 241 let (db, file_id) = with_single_file(&before);
231 let frange = 242 let frange =
232 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 243 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
233 let assist = 244 let assist =
@@ -247,7 +258,7 @@ mod helpers {
247 target: &str, 258 target: &str,
248 ) { 259 ) {
249 let (range, before) = extract_range(before); 260 let (range, before) = extract_range(before);
250 let (db, file_id) = RootDatabase::with_single_file(&before); 261 let (db, file_id) = with_single_file(&before);
251 let frange = FileRange { file_id, range }; 262 let frange = FileRange { file_id, range };
252 let assist = 263 let assist =
253 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 264 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -265,7 +276,7 @@ mod helpers {
265 before: &str, 276 before: &str,
266 ) { 277 ) {
267 let (before_cursor_pos, before) = extract_offset(before); 278 let (before_cursor_pos, before) = extract_offset(before);
268 let (db, file_id) = RootDatabase::with_single_file(&before); 279 let (db, file_id) = with_single_file(&before);
269 let frange = 280 let frange =
270 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 281 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
271 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 282 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
@@ -277,7 +288,7 @@ mod helpers {
277 before: &str, 288 before: &str,
278 ) { 289 ) {
279 let (range, before) = extract_range(before); 290 let (range, before) = extract_range(before);
280 let (db, file_id) = RootDatabase::with_single_file(&before); 291 let (db, file_id) = with_single_file(&before);
281 let frange = FileRange { file_id, range }; 292 let frange = FileRange { file_id, range };
282 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 293 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
283 assert!(assist.is_none()); 294 assert!(assist.is_none());
@@ -286,17 +297,17 @@ mod helpers {
286 297
287#[cfg(test)] 298#[cfg(test)]
288mod tests { 299mod tests {
289 use ra_db::{fixture::WithFixture, FileRange}; 300 use ra_db::FileRange;
290 use ra_syntax::TextRange; 301 use ra_syntax::TextRange;
291 use test_utils::{extract_offset, extract_range}; 302 use test_utils::{extract_offset, extract_range};
292 303
293 use ra_ide_db::RootDatabase; 304 use crate::helpers;
294 305
295 #[test] 306 #[test]
296 fn assist_order_field_struct() { 307 fn assist_order_field_struct() {
297 let before = "struct Foo { <|>bar: u32 }"; 308 let before = "struct Foo { <|>bar: u32 }";
298 let (before_cursor_pos, before) = extract_offset(before); 309 let (before_cursor_pos, before) = extract_offset(before);
299 let (db, file_id) = RootDatabase::with_single_file(&before); 310 let (db, file_id) = helpers::with_single_file(&before);
300 let frange = 311 let frange =
301 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 312 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
302 let assists = super::assists(&db, frange); 313 let assists = super::assists(&db, frange);
@@ -320,7 +331,7 @@ mod tests {
320 } 331 }
321 }"; 332 }";
322 let (range, before) = extract_range(before); 333 let (range, before) = extract_range(before);
323 let (db, file_id) = RootDatabase::with_single_file(&before); 334 let (db, file_id) = helpers::with_single_file(&before);
324 let frange = FileRange { file_id, range }; 335 let frange = FileRange { file_id, range };
325 let assists = super::assists(&db, frange); 336 let assists = super::assists(&db, frange);
326 let mut assists = assists.iter(); 337 let mut assists = assists.iter();
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index cee6dde8e..788906723 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -92,7 +92,7 @@ pub fn classify_name(
92 ast::FnDef(it) => { 92 ast::FnDef(it) => {
93 let src = name.with_value(it); 93 let src = name.with_value(it);
94 let def: hir::Function = sb.to_def(src)?; 94 let def: hir::Function = sb.to_def(src)?;
95 if parent.parent().and_then(ast::ItemList::cast).is_some() { 95 if parent.parent().and_then(ast::ItemList::cast).map_or(false, |it| it.syntax().parent().and_then(ast::Module::cast).is_none()) {
96 Some(from_assoc_item(sb.db, def.into())) 96 Some(from_assoc_item(sb.db, def.into()))
97 } else { 97 } else {
98 Some(from_module_def(sb.db, def.into(), None)) 98 Some(from_module_def(sb.db, def.into(), None))