diff options
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 144 |
1 files changed, 117 insertions, 27 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 0fbfd8a40..350a6d627 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -18,49 +18,36 @@ macro_rules! ctry { | |||
18 | }; | 18 | }; |
19 | } | 19 | } |
20 | 20 | ||
21 | mod arena; | ||
22 | mod db; | 21 | mod db; |
23 | mod loc2id; | ||
24 | mod input; | ||
25 | mod imp; | 22 | mod imp; |
26 | mod completion; | 23 | mod completion; |
27 | mod descriptors; | ||
28 | mod symbol_index; | 24 | mod symbol_index; |
29 | mod syntax_ptr; | ||
30 | pub mod mock_analysis; | 25 | pub mod mock_analysis; |
31 | 26 | ||
32 | use std::{fmt, sync::Arc}; | 27 | use std::{fmt, sync::Arc}; |
33 | 28 | ||
34 | use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; | 29 | use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; |
30 | use ra_db::FileResolverImp; | ||
35 | use rayon::prelude::*; | 31 | use rayon::prelude::*; |
36 | use relative_path::RelativePathBuf; | 32 | use relative_path::RelativePathBuf; |
37 | 33 | ||
38 | use crate::{ | 34 | use crate::{ |
39 | imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, | 35 | imp::{AnalysisHostImpl, AnalysisImpl}, |
40 | symbol_index::SymbolIndex, | 36 | symbol_index::SymbolIndex, |
41 | }; | 37 | }; |
42 | 38 | ||
43 | pub use crate::{ | 39 | pub use crate::{ |
44 | completion::CompletionItem, | 40 | completion::CompletionItem, |
45 | descriptors::function::FnDescriptor, | ||
46 | input::{CrateGraph, CrateId, FileId, FileResolver}, | ||
47 | }; | 41 | }; |
48 | pub use ra_editor::{ | 42 | pub use ra_editor::{ |
49 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, | 43 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, |
50 | }; | 44 | }; |
45 | pub use hir::FnSignatureInfo; | ||
51 | 46 | ||
52 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 47 | pub use ra_db::{ |
53 | pub struct Canceled; | 48 | Canceled, Cancelable, FilePosition, |
54 | 49 | CrateGraph, CrateId, FileId, FileResolver | |
55 | pub type Cancelable<T> = Result<T, Canceled>; | 50 | }; |
56 | |||
57 | impl std::fmt::Display for Canceled { | ||
58 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
59 | fmt.write_str("Canceled") | ||
60 | } | ||
61 | } | ||
62 | |||
63 | impl std::error::Error for Canceled {} | ||
64 | 51 | ||
65 | #[derive(Default)] | 52 | #[derive(Default)] |
66 | pub struct AnalysisChange { | 53 | pub struct AnalysisChange { |
@@ -130,12 +117,6 @@ impl AnalysisHost { | |||
130 | } | 117 | } |
131 | } | 118 | } |
132 | 119 | ||
133 | #[derive(Clone, Copy, Debug)] | ||
134 | pub struct FilePosition { | ||
135 | pub file_id: FileId, | ||
136 | pub offset: TextUnit, | ||
137 | } | ||
138 | |||
139 | #[derive(Debug)] | 120 | #[derive(Debug)] |
140 | pub struct SourceChange { | 121 | pub struct SourceChange { |
141 | pub label: String, | 122 | pub label: String, |
@@ -305,7 +286,7 @@ impl Analysis { | |||
305 | pub fn resolve_callable( | 286 | pub fn resolve_callable( |
306 | &self, | 287 | &self, |
307 | position: FilePosition, | 288 | position: FilePosition, |
308 | ) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> { | 289 | ) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> { |
309 | self.imp.resolve_callable(position) | 290 | self.imp.resolve_callable(position) |
310 | } | 291 | } |
311 | } | 292 | } |
@@ -336,3 +317,112 @@ fn analysis_is_send() { | |||
336 | fn is_send<T: Send>() {} | 317 | fn is_send<T: Send>() {} |
337 | is_send::<Analysis>(); | 318 | is_send::<Analysis>(); |
338 | } | 319 | } |
320 | |||
321 | //TODO: move to hir | ||
322 | #[cfg(test)] | ||
323 | mod hir_namres_tests { | ||
324 | use std::sync::Arc; | ||
325 | use ra_db::FilesDatabase; | ||
326 | use ra_syntax::SmolStr; | ||
327 | use hir::{self, db::HirDatabase}; | ||
328 | |||
329 | use crate::{ | ||
330 | AnalysisChange, | ||
331 | mock_analysis::{MockAnalysis, analysis_and_position}, | ||
332 | }; | ||
333 | |||
334 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | ||
335 | let (analysis, pos) = analysis_and_position(fixture); | ||
336 | let db = analysis.imp.db; | ||
337 | let source_root = db.file_source_root(pos.file_id); | ||
338 | let descr = hir::Module::guess_from_position(&*db, pos) | ||
339 | .unwrap() | ||
340 | .unwrap(); | ||
341 | let module_id = descr.module_id; | ||
342 | (db.item_map(source_root).unwrap(), module_id) | ||
343 | } | ||
344 | |||
345 | #[test] | ||
346 | fn test_item_map() { | ||
347 | let (item_map, module_id) = item_map( | ||
348 | " | ||
349 | //- /lib.rs | ||
350 | mod foo; | ||
351 | |||
352 | use crate::foo::bar::Baz; | ||
353 | <|> | ||
354 | |||
355 | //- /foo/mod.rs | ||
356 | pub mod bar; | ||
357 | |||
358 | //- /foo/bar.rs | ||
359 | pub struct Baz; | ||
360 | ", | ||
361 | ); | ||
362 | let name = SmolStr::from("Baz"); | ||
363 | let resolution = &item_map.per_module[&module_id].items[&name]; | ||
364 | assert!(resolution.def_id.is_some()); | ||
365 | } | ||
366 | |||
367 | #[test] | ||
368 | fn typing_inside_a_function_should_not_invalidate_item_map() { | ||
369 | let mock_analysis = MockAnalysis::with_files( | ||
370 | " | ||
371 | //- /lib.rs | ||
372 | mod foo; | ||
373 | |||
374 | use crate::foo::bar::Baz; | ||
375 | |||
376 | fn foo() -> i32 { | ||
377 | 1 + 1 | ||
378 | } | ||
379 | //- /foo/mod.rs | ||
380 | pub mod bar; | ||
381 | |||
382 | //- /foo/bar.rs | ||
383 | pub struct Baz; | ||
384 | ", | ||
385 | ); | ||
386 | |||
387 | let file_id = mock_analysis.id_of("/lib.rs"); | ||
388 | let mut host = mock_analysis.analysis_host(); | ||
389 | |||
390 | let source_root = host.analysis().imp.db.file_source_root(file_id); | ||
391 | |||
392 | { | ||
393 | let db = host.analysis().imp.db; | ||
394 | let events = db.log_executed(|| { | ||
395 | db.item_map(source_root).unwrap(); | ||
396 | }); | ||
397 | assert!(format!("{:?}", events).contains("item_map")) | ||
398 | } | ||
399 | |||
400 | let mut change = AnalysisChange::new(); | ||
401 | |||
402 | change.change_file( | ||
403 | file_id, | ||
404 | " | ||
405 | mod foo; | ||
406 | |||
407 | use crate::foo::bar::Baz; | ||
408 | |||
409 | fn foo() -> i32 { 92 } | ||
410 | " | ||
411 | .to_string(), | ||
412 | ); | ||
413 | |||
414 | host.apply_change(change); | ||
415 | |||
416 | { | ||
417 | let db = host.analysis().imp.db; | ||
418 | let events = db.log_executed(|| { | ||
419 | db.item_map(source_root).unwrap(); | ||
420 | }); | ||
421 | assert!( | ||
422 | !format!("{:?}", events).contains("_item_map"), | ||
423 | "{:#?}", | ||
424 | events | ||
425 | ) | ||
426 | } | ||
427 | } | ||
428 | } | ||