aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/lib.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 01:10:58 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 01:10:58 +0000
commit95c0c8f3986c8b3bcf0052d34d3ace09ebb9fa1b (patch)
tree0e5aa7337c000dd8c6ef3a7fedba68abf7feca8a /crates/ra_analysis/src/lib.rs
parent9f08341aa486ea59cb488635f19e960523568fb8 (diff)
parent59e29aef633e906837f8fed604435976a46be691 (diff)
Merge #247
247: Hir r=matklad a=matklad This doesn't achive anything new, just a big refactoring. The main change is that Descriptors are now called `hir`, and live in a separate crate. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r--crates/ra_analysis/src/lib.rs144
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
21mod arena;
22mod db; 21mod db;
23mod loc2id;
24mod input;
25mod imp; 22mod imp;
26mod completion; 23mod completion;
27mod descriptors;
28mod symbol_index; 24mod symbol_index;
29mod syntax_ptr;
30pub mod mock_analysis; 25pub mod mock_analysis;
31 26
32use std::{fmt, sync::Arc}; 27use std::{fmt, sync::Arc};
33 28
34use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; 29use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit};
30use ra_db::FileResolverImp;
35use rayon::prelude::*; 31use rayon::prelude::*;
36use relative_path::RelativePathBuf; 32use relative_path::RelativePathBuf;
37 33
38use crate::{ 34use crate::{
39 imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, 35 imp::{AnalysisHostImpl, AnalysisImpl},
40 symbol_index::SymbolIndex, 36 symbol_index::SymbolIndex,
41}; 37};
42 38
43pub use crate::{ 39pub use crate::{
44 completion::CompletionItem, 40 completion::CompletionItem,
45 descriptors::function::FnDescriptor,
46 input::{CrateGraph, CrateId, FileId, FileResolver},
47}; 41};
48pub use ra_editor::{ 42pub use ra_editor::{
49 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, 43 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
50}; 44};
45pub use hir::FnSignatureInfo;
51 46
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 47pub use ra_db::{
53pub struct Canceled; 48 Canceled, Cancelable, FilePosition,
54 49 CrateGraph, CrateId, FileId, FileResolver
55pub type Cancelable<T> = Result<T, Canceled>; 50};
56
57impl 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
63impl std::error::Error for Canceled {}
64 51
65#[derive(Default)] 52#[derive(Default)]
66pub struct AnalysisChange { 53pub struct AnalysisChange {
@@ -130,12 +117,6 @@ impl AnalysisHost {
130 } 117 }
131} 118}
132 119
133#[derive(Clone, Copy, Debug)]
134pub struct FilePosition {
135 pub file_id: FileId,
136 pub offset: TextUnit,
137}
138
139#[derive(Debug)] 120#[derive(Debug)]
140pub struct SourceChange { 121pub 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)]
323mod 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}