aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/tests/tests.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-31 17:59:17 +0000
committerAleksey Kladov <[email protected]>2018-10-31 18:01:51 +0000
commitf3fb59d7077801a3a68d2d03eef17d59c2925ae8 (patch)
tree7665461636576d121063d7cc391351b284a399b9 /crates/ra_analysis/tests/tests.rs
parentc02be1502c76cc504ccf7f73dce929585c94377c (diff)
Move completion to ra_analysis
While we should handle completion for isolated file, it's better achieved by using empty Analysis, rather than working only with &File: we need memoization for type inference even inside a single file.
Diffstat (limited to 'crates/ra_analysis/tests/tests.rs')
-rw-r--r--crates/ra_analysis/tests/tests.rs56
1 files changed, 6 insertions, 50 deletions
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs
index 806e1fb34..f5683aec5 100644
--- a/crates/ra_analysis/tests/tests.rs
+++ b/crates/ra_analysis/tests/tests.rs
@@ -5,62 +5,16 @@ extern crate relative_path;
5extern crate rustc_hash; 5extern crate rustc_hash;
6extern crate test_utils; 6extern crate test_utils;
7 7
8use std::{
9 sync::Arc,
10};
11
12use ra_syntax::TextRange; 8use ra_syntax::TextRange;
13use relative_path::{RelativePath, RelativePathBuf};
14use test_utils::{assert_eq_dbg, extract_offset}; 9use test_utils::{assert_eq_dbg, extract_offset};
15 10
16use ra_analysis::{ 11use ra_analysis::{
17 AnalysisChange, Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor, 12 MockAnalysis,
13 AnalysisChange, Analysis, CrateGraph, CrateId, FileId, FnDescriptor,
18}; 14};
19 15
20#[derive(Debug)]
21struct FileMap(Vec<(FileId, RelativePathBuf)>);
22
23impl FileMap {
24 fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a {
25 self.0
26 .iter()
27 .map(|(id, path)| (*id, path.as_relative_path()))
28 }
29
30 fn path(&self, id: FileId) -> &RelativePath {
31 self.iter().find(|&(it, _)| it == id).unwrap().1
32 }
33}
34
35impl FileResolver for FileMap {
36 fn file_stem(&self, id: FileId) -> String {
37 self.path(id).file_stem().unwrap().to_string()
38 }
39 fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> {
40 let path = self.path(id).join(rel).normalize();
41 let id = self.iter().find(|&(_, p)| path == p)?.0;
42 Some(id)
43 }
44}
45
46fn analysis_host(files: &[(&str, &str)]) -> AnalysisHost {
47 let mut host = AnalysisHost::new();
48 let mut file_map = Vec::new();
49 let mut change = AnalysisChange::new();
50 for (id, &(path, contents)) in files.iter().enumerate() {
51 let file_id = FileId((id + 1) as u32);
52 assert!(path.starts_with('/'));
53 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
54 change.add_file(file_id, contents.to_string());
55 file_map.push((file_id, path));
56 }
57 change.set_file_resolver(Arc::new(FileMap(file_map)));
58 host.apply_change(change);
59 host
60}
61
62fn analysis(files: &[(&str, &str)]) -> Analysis { 16fn analysis(files: &[(&str, &str)]) -> Analysis {
63 analysis_host(files).analysis() 17 MockAnalysis::with_files(files).analysis()
64} 18}
65 19
66fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) { 20fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) {
@@ -125,7 +79,9 @@ fn test_resolve_parent_module() {
125 79
126#[test] 80#[test]
127fn test_resolve_crate_root() { 81fn test_resolve_crate_root() {
128 let mut host = analysis_host(&[("/lib.rs", "mod foo;"), ("/foo.rs", "")]); 82 let mut host = MockAnalysis::with_files(
83 &[("/lib.rs", "mod foo;"), ("/foo.rs", "")]
84 ).analysis_host();
129 let snap = host.analysis(); 85 let snap = host.analysis();
130 assert!(snap.crate_for(FileId(2)).unwrap().is_empty()); 86 assert!(snap.crate_for(FileId(2)).unwrap().is_empty());
131 87