aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-25 22:11:59 +0100
committerAleksey Kladov <[email protected]>2020-06-25 23:27:57 +0100
commit73d73077febba921918b5611574bf514eae63006 (patch)
tree602383421d3c1c624f9536e8b730216af4cf15f6 /crates/rust-analyzer
parent3615347fcebebaa58a5cbf675ae062aae149d9c3 (diff)
Separate creation and initialization of global state
Diffstat (limited to 'crates/rust-analyzer')
-rw-r--r--crates/rust-analyzer/src/global_state.rs121
1 files changed, 65 insertions, 56 deletions
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 149b1b5f9..be5a3f8a7 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -81,7 +81,7 @@ pub(crate) struct GlobalState {
81 pub(crate) req_queue: ReqQueue, 81 pub(crate) req_queue: ReqQueue,
82 latest_requests: Arc<RwLock<LatestRequests>>, 82 latest_requests: Arc<RwLock<LatestRequests>>,
83 source_root_config: SourceRootConfig, 83 source_root_config: SourceRootConfig,
84 _proc_macro_client: ProcMacroClient, 84 proc_macro_client: ProcMacroClient,
85 workspaces: Arc<Vec<ProjectWorkspace>>, 85 workspaces: Arc<Vec<ProjectWorkspace>>,
86} 86}
87 87
@@ -103,61 +103,14 @@ impl GlobalState {
103 config: Config, 103 config: Config,
104 req_queue: ReqQueue, 104 req_queue: ReqQueue,
105 ) -> GlobalState { 105 ) -> GlobalState {
106 let mut change = AnalysisChange::new();
107
108 let project_folders = ProjectFolders::new(&workspaces);
109
110 let (task_sender, task_receiver) = unbounded::<vfs::loader::Message>(); 106 let (task_sender, task_receiver) = unbounded::<vfs::loader::Message>();
111 let mut vfs = vfs::Vfs::default();
112
113 let proc_macro_client = match &config.proc_macro_srv {
114 None => ProcMacroClient::dummy(),
115 Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
116 Ok(it) => it,
117 Err(err) => {
118 log::error!(
119 "Failed to run ra_proc_macro_srv from path {}, error: {:?}",
120 path.display(),
121 err
122 );
123 ProcMacroClient::dummy()
124 }
125 },
126 };
127 107
128 let mut loader = { 108 let loader = {
129 let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| { 109 let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| {
130 task_sender.send(msg).unwrap() 110 task_sender.send(msg).unwrap()
131 })); 111 }));
132 Box::new(loader) 112 Box::new(loader)
133 }; 113 };
134 let watch = match config.files.watcher {
135 FilesWatcher::Client => vec![],
136 FilesWatcher::Notify => project_folders.watch,
137 };
138 loader.set_config(vfs::loader::Config { load: project_folders.load, watch });
139
140 // Create crate graph from all the workspaces
141 let mut crate_graph = CrateGraph::default();
142 let mut load = |path: &AbsPath| {
143 let contents = loader.load_sync(path);
144 let path = vfs::VfsPath::from(path.to_path_buf());
145 vfs.set_file_contents(path.clone(), contents);
146 vfs.file_id(&path)
147 };
148 for ws in workspaces.iter() {
149 crate_graph.extend(ws.to_crate_graph(
150 config.cargo.target.as_deref(),
151 &proc_macro_client,
152 &mut load,
153 ));
154 }
155 change.set_crate_graph(crate_graph);
156
157 let flycheck = config.check.as_ref().and_then(|c| create_flycheck(&workspaces, c));
158
159 let mut analysis_host = AnalysisHost::new(lru_capacity);
160 analysis_host.apply_change(change);
161 114
162 let task_pool = { 115 let task_pool = {
163 let (sender, receiver) = unbounded(); 116 let (sender, receiver) = unbounded();
@@ -168,24 +121,80 @@ impl GlobalState {
168 sender, 121 sender,
169 config, 122 config,
170 task_pool, 123 task_pool,
171 analysis_host, 124 analysis_host: AnalysisHost::new(lru_capacity),
172 loader, 125 loader,
173 task_receiver, 126 task_receiver,
174 flycheck, 127 flycheck: None,
175 diagnostics: Default::default(), 128 diagnostics: Default::default(),
176 mem_docs: FxHashSet::default(), 129 mem_docs: FxHashSet::default(),
177 vfs: Arc::new(RwLock::new((vfs, FxHashMap::default()))), 130 vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
178 status: Status::default(), 131 status: Status::default(),
179 req_queue, 132 req_queue,
180 latest_requests: Default::default(), 133 latest_requests: Default::default(),
181 source_root_config: project_folders.source_root_config, 134 source_root_config: SourceRootConfig::default(),
182 _proc_macro_client: proc_macro_client, 135 proc_macro_client: ProcMacroClient::dummy(),
183 workspaces: Arc::new(workspaces), 136 workspaces: Arc::new(Vec::new()),
184 }; 137 };
185 res.process_changes(); 138 res.reload(workspaces);
186 res 139 res
187 } 140 }
188 141
142 pub(crate) fn reload(&mut self, workspaces: Vec<ProjectWorkspace>) {
143 let mut change = AnalysisChange::new();
144
145 let project_folders = ProjectFolders::new(&workspaces);
146
147 self.proc_macro_client = match &self.config.proc_macro_srv {
148 None => ProcMacroClient::dummy(),
149 Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
150 Ok(it) => it,
151 Err(err) => {
152 log::error!(
153 "Failed to run ra_proc_macro_srv from path {}, error: {:?}",
154 path.display(),
155 err
156 );
157 ProcMacroClient::dummy()
158 }
159 },
160 };
161 let watch = match self.config.files.watcher {
162 FilesWatcher::Client => vec![],
163 FilesWatcher::Notify => project_folders.watch,
164 };
165 self.loader.set_config(vfs::loader::Config { load: project_folders.load, watch });
166
167 // Create crate graph from all the workspaces
168 let crate_graph = {
169 let mut crate_graph = CrateGraph::default();
170 let vfs = &mut self.vfs.write().0;
171 let loader = &mut self.loader;
172 let mut load = |path: &AbsPath| {
173 let contents = loader.load_sync(path);
174 let path = vfs::VfsPath::from(path.to_path_buf());
175 vfs.set_file_contents(path.clone(), contents);
176 vfs.file_id(&path)
177 };
178 for ws in workspaces.iter() {
179 crate_graph.extend(ws.to_crate_graph(
180 self.config.cargo.target.as_deref(),
181 &self.proc_macro_client,
182 &mut load,
183 ));
184 }
185
186 crate_graph
187 };
188 change.set_crate_graph(crate_graph);
189
190 self.flycheck = self.config.check.as_ref().and_then(|c| create_flycheck(&workspaces, c));
191 self.source_root_config = project_folders.source_root_config;
192 self.workspaces = Arc::new(workspaces);
193
194 self.analysis_host.apply_change(change);
195 self.process_changes();
196 }
197
189 pub(crate) fn update_configuration(&mut self, config: Config) { 198 pub(crate) fn update_configuration(&mut self, config: Config) {
190 self.analysis_host.update_lru_capacity(config.lru_capacity); 199 self.analysis_host.update_lru_capacity(config.lru_capacity);
191 if config.check != self.config.check { 200 if config.check != self.config.check {