diff options
Diffstat (limited to 'crates/ra_lsp_server/src/cli/load_cargo.rs')
-rw-r--r-- | crates/ra_lsp_server/src/cli/load_cargo.rs | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/crates/ra_lsp_server/src/cli/load_cargo.rs b/crates/ra_lsp_server/src/cli/load_cargo.rs deleted file mode 100644 index 8cd08ecb6..000000000 --- a/crates/ra_lsp_server/src/cli/load_cargo.rs +++ /dev/null | |||
@@ -1,155 +0,0 @@ | |||
1 | //! Loads a Cargo project into a static instance of analysis, without support | ||
2 | //! for incorporating changes. | ||
3 | |||
4 | use std::path::Path; | ||
5 | |||
6 | use anyhow::Result; | ||
7 | use crossbeam_channel::{unbounded, Receiver}; | ||
8 | use ra_db::{CrateGraph, FileId, SourceRootId}; | ||
9 | use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags}; | ||
10 | use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace}; | ||
11 | use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; | ||
12 | use rustc_hash::{FxHashMap, FxHashSet}; | ||
13 | |||
14 | use crate::vfs_glob::RustPackageFilterBuilder; | ||
15 | |||
16 | fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId { | ||
17 | FileId(f.0) | ||
18 | } | ||
19 | fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId { | ||
20 | SourceRootId(r.0) | ||
21 | } | ||
22 | |||
23 | pub(crate) fn load_cargo( | ||
24 | root: &Path, | ||
25 | ) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> { | ||
26 | let root = std::env::current_dir()?.join(root); | ||
27 | let ws = ProjectWorkspace::discover(root.as_ref(), &Default::default())?; | ||
28 | let project_roots = ws.to_roots(); | ||
29 | let (sender, receiver) = unbounded(); | ||
30 | let sender = Box::new(move |t| sender.send(t).unwrap()); | ||
31 | let (mut vfs, roots) = Vfs::new( | ||
32 | project_roots | ||
33 | .iter() | ||
34 | .map(|pkg_root| { | ||
35 | RootEntry::new( | ||
36 | pkg_root.path().clone(), | ||
37 | RustPackageFilterBuilder::default() | ||
38 | .set_member(pkg_root.is_member()) | ||
39 | .into_vfs_filter(), | ||
40 | ) | ||
41 | }) | ||
42 | .collect(), | ||
43 | sender, | ||
44 | Watch(false), | ||
45 | ); | ||
46 | |||
47 | // FIXME: cfg options? | ||
48 | let default_cfg_options = { | ||
49 | let mut opts = get_rustc_cfg_options(); | ||
50 | opts.insert_atom("test".into()); | ||
51 | opts.insert_atom("debug_assertion".into()); | ||
52 | opts | ||
53 | }; | ||
54 | |||
55 | let (crate_graph, _crate_names) = | ||
56 | ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { | ||
57 | let vfs_file = vfs.load(path); | ||
58 | log::debug!("vfs file {:?} -> {:?}", path, vfs_file); | ||
59 | vfs_file.map(vfs_file_to_id) | ||
60 | }); | ||
61 | log::debug!("crate graph: {:?}", crate_graph); | ||
62 | |||
63 | let source_roots = roots | ||
64 | .iter() | ||
65 | .map(|&vfs_root| { | ||
66 | let source_root_id = vfs_root_to_id(vfs_root); | ||
67 | let project_root = project_roots | ||
68 | .iter() | ||
69 | .find(|it| it.path() == &vfs.root2path(vfs_root)) | ||
70 | .unwrap() | ||
71 | .clone(); | ||
72 | (source_root_id, project_root) | ||
73 | }) | ||
74 | .collect::<FxHashMap<_, _>>(); | ||
75 | let host = load(&source_roots, crate_graph, &mut vfs, receiver); | ||
76 | Ok((host, source_roots)) | ||
77 | } | ||
78 | |||
79 | pub(crate) fn load( | ||
80 | source_roots: &FxHashMap<SourceRootId, PackageRoot>, | ||
81 | crate_graph: CrateGraph, | ||
82 | vfs: &mut Vfs, | ||
83 | receiver: Receiver<VfsTask>, | ||
84 | ) -> AnalysisHost { | ||
85 | let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok()); | ||
86 | let mut host = AnalysisHost::new(lru_cap, FeatureFlags::default()); | ||
87 | let mut analysis_change = AnalysisChange::new(); | ||
88 | analysis_change.set_crate_graph(crate_graph); | ||
89 | |||
90 | // wait until Vfs has loaded all roots | ||
91 | let mut roots_loaded = FxHashSet::default(); | ||
92 | for task in receiver { | ||
93 | vfs.handle_task(task); | ||
94 | let mut done = false; | ||
95 | for change in vfs.commit_changes() { | ||
96 | match change { | ||
97 | VfsChange::AddRoot { root, files } => { | ||
98 | let source_root_id = vfs_root_to_id(root); | ||
99 | let is_local = source_roots[&source_root_id].is_member(); | ||
100 | log::debug!( | ||
101 | "loaded source root {:?} with path {:?}", | ||
102 | source_root_id, | ||
103 | vfs.root2path(root) | ||
104 | ); | ||
105 | analysis_change.add_root(source_root_id, is_local); | ||
106 | analysis_change.set_debug_root_path( | ||
107 | source_root_id, | ||
108 | source_roots[&source_root_id].path().display().to_string(), | ||
109 | ); | ||
110 | |||
111 | let mut file_map = FxHashMap::default(); | ||
112 | for (vfs_file, path, text) in files { | ||
113 | let file_id = vfs_file_to_id(vfs_file); | ||
114 | analysis_change.add_file(source_root_id, file_id, path.clone(), text); | ||
115 | file_map.insert(path, file_id); | ||
116 | } | ||
117 | roots_loaded.insert(source_root_id); | ||
118 | if roots_loaded.len() == vfs.n_roots() { | ||
119 | done = true; | ||
120 | } | ||
121 | } | ||
122 | VfsChange::AddFile { root, file, path, text } => { | ||
123 | let source_root_id = vfs_root_to_id(root); | ||
124 | let file_id = vfs_file_to_id(file); | ||
125 | analysis_change.add_file(source_root_id, file_id, path, text); | ||
126 | } | ||
127 | VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => { | ||
128 | // We just need the first scan, so just ignore these | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | if done { | ||
133 | break; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | host.apply_change(analysis_change); | ||
138 | host | ||
139 | } | ||
140 | |||
141 | #[cfg(test)] | ||
142 | mod tests { | ||
143 | use super::*; | ||
144 | |||
145 | use hir::Crate; | ||
146 | |||
147 | #[test] | ||
148 | fn test_loading_rust_analyzer() { | ||
149 | let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); | ||
150 | let (host, _roots) = load_cargo(path).unwrap(); | ||
151 | let n_crates = Crate::all(host.raw_database()).len(); | ||
152 | // RA has quite a few crates, but the exact count doesn't matter | ||
153 | assert!(n_crates > 20); | ||
154 | } | ||
155 | } | ||