]> git.lizzy.rs Git - rust.git/blobdiff - crates/rust-analyzer/src/reload.rs
Replaced fold with for loop
[rust.git] / crates / rust-analyzer / src / reload.rs
index fa9f05bc3835466d1acea61dde86917592a354e1..eecc83e02ac499d4b0e40156c4d1db06e3f73002 100644 (file)
@@ -5,10 +5,12 @@
 use hir::db::DefDatabase;
 use ide::Change;
 use ide_db::base_db::{
-    CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroKind, SourceRoot, VfsPath,
+    CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind,
+    SourceRoot, VfsPath,
 };
-use proc_macro_api::ProcMacroClient;
+use proc_macro_api::{MacroDylib, ProcMacroServer};
 use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
+use syntax::SmolStr;
 use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
 
 use crate::{
@@ -47,7 +49,7 @@ pub(crate) fn update_configuration(&mut self, config: Config) {
             self.analysis_host.update_lru_capacity(self.config.lru_capacity());
         }
         if self.config.linked_projects() != old_config.linked_projects() {
-            self.fetch_workspaces_request()
+            self.fetch_workspaces_queue.request_op()
         } else if self.config.flycheck() != old_config.flycheck() {
             self.reload_flycheck();
         }
@@ -57,65 +59,19 @@ pub(crate) fn update_configuration(&mut self, config: Config) {
             .raw_database_mut()
             .set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
     }
-    pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
-        if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
-            return;
-        }
-        tracing::info!(
-            "Requesting workspace reload because of the following changes: {}",
-            itertools::join(
-                changes
-                    .iter()
-                    .filter(|(path, kind)| is_interesting(path, *kind))
-                    .map(|(path, kind)| format!("{}: {:?}", path.display(), kind)),
-                ", "
-            )
-        );
-        self.fetch_workspaces_request();
-
-        fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
-            const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
-            const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
-            let file_name = path.file_name().unwrap_or_default();
-
-            if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
-                return true;
-            }
-            if change_kind == ChangeKind::Modify {
-                return false;
-            }
-            if path.extension().unwrap_or_default() != "rs" {
-                return false;
-            }
-            if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
-                return true;
-            }
-            let parent = match path.parent() {
-                Some(it) => it,
-                None => return false,
-            };
-            if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
-                return true;
-            }
-            if file_name == "main.rs" {
-                let grand_parent = match parent.parent() {
-                    Some(it) => it,
-                    None => return false,
-                };
-                if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
-                    return true;
-                }
-            }
-            false
-        }
-    }
-    pub(crate) fn report_new_status_if_needed(&mut self) {
+
+    pub(crate) fn current_status(&self) -> lsp_ext::ServerStatusParams {
         let mut status = lsp_ext::ServerStatusParams {
             health: lsp_ext::Health::Ok,
             quiescent: self.is_quiescent(),
             message: None,
         };
 
+        if self.proc_macro_changed {
+            status.health = lsp_ext::Health::Warning;
+            status.message =
+                Some("Reload required due to source changes of a procedural macro.".into())
+        }
         if let Some(error) = self.fetch_build_data_error() {
             status.health = lsp_ext::Health::Warning;
             status.message = Some(error)
@@ -132,27 +88,10 @@ pub(crate) fn report_new_status_if_needed(&mut self) {
             status.health = lsp_ext::Health::Error;
             status.message = Some(error)
         }
-
-        if self.last_reported_status.as_ref() != Some(&status) {
-            self.last_reported_status = Some(status.clone());
-
-            if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
-                self.show_message(lsp_types::MessageType::Error, message.clone());
-            }
-
-            if self.config.server_status_notification() {
-                self.send_notification::<lsp_ext::ServerStatusNotification>(status);
-            }
-        }
+        status
     }
 
-    pub(crate) fn fetch_workspaces_request(&mut self) {
-        self.fetch_workspaces_queue.request_op()
-    }
-    pub(crate) fn fetch_workspaces_if_needed(&mut self) {
-        if !self.fetch_workspaces_queue.should_start_op() {
-            return;
-        }
+    pub(crate) fn fetch_workspaces(&mut self) {
         tracing::info!("will fetch workspaces");
 
         self.task_pool.handle.spawn_with_sender({
@@ -203,21 +142,8 @@ pub(crate) fn fetch_workspaces_if_needed(&mut self) {
             }
         });
     }
-    pub(crate) fn fetch_workspaces_completed(
-        &mut self,
-        workspaces: Vec<anyhow::Result<ProjectWorkspace>>,
-    ) {
-        self.fetch_workspaces_queue.op_completed(workspaces)
-    }
-
-    pub(crate) fn fetch_build_data_request(&mut self) {
-        self.fetch_build_data_queue.request_op();
-    }
-    pub(crate) fn fetch_build_data_if_needed(&mut self) {
-        if !self.fetch_build_data_queue.should_start_op() {
-            return;
-        }
 
+    pub(crate) fn fetch_build_data(&mut self) {
         let workspaces = Arc::clone(&self.workspaces);
         let config = self.config.cargo();
         self.task_pool.handle.spawn_with_sender(move |sender| {
@@ -236,12 +162,6 @@ pub(crate) fn fetch_build_data_if_needed(&mut self) {
             sender.send(Task::FetchBuildData(BuildDataProgress::End((workspaces, res)))).unwrap();
         });
     }
-    pub(crate) fn fetch_build_data_completed(
-        &mut self,
-        build_data: (Arc<Vec<ProjectWorkspace>>, Vec<anyhow::Result<WorkspaceBuildScripts>>),
-    ) {
-        self.fetch_build_data_queue.op_completed(build_data)
-    }
 
     pub(crate) fn switch_workspaces(&mut self) {
         let _p = profile::span("GlobalState::switch_workspaces");
@@ -297,7 +217,7 @@ fn eq_ignore_build_data<'a>(
 
         if same_workspaces {
             let (workspaces, build_scripts) = self.fetch_build_data_queue.last_op_result();
-            if Arc::ptr_eq(&workspaces, &self.workspaces) {
+            if Arc::ptr_eq(workspaces, &self.workspaces) {
                 let workspaces = workspaces
                     .iter()
                     .cloned()
@@ -322,38 +242,33 @@ fn eq_ignore_build_data<'a>(
         }
 
         if let FilesWatcher::Client = self.config.files().watcher {
-            if self.config.did_change_watched_files_dynamic_registration() {
-                let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
-                    watchers: self
-                        .workspaces
-                        .iter()
-                        .flat_map(|ws| ws.to_roots())
-                        .filter(|it| it.is_member)
-                        .flat_map(|root| {
-                            root.include.into_iter().flat_map(|it| {
-                                [
-                                    format!("{}/**/*.rs", it.display()),
-                                    format!("{}/**/Cargo.toml", it.display()),
-                                    format!("{}/**/Cargo.lock", it.display()),
-                                ]
-                            })
-                        })
-                        .map(|glob_pattern| lsp_types::FileSystemWatcher {
-                            glob_pattern,
-                            kind: None,
+            let registration_options = lsp_types::DidChangeWatchedFilesRegistrationOptions {
+                watchers: self
+                    .workspaces
+                    .iter()
+                    .flat_map(|ws| ws.to_roots())
+                    .filter(|it| it.is_local)
+                    .flat_map(|root| {
+                        root.include.into_iter().flat_map(|it| {
+                            [
+                                format!("{}/**/*.rs", it.display()),
+                                format!("{}/**/Cargo.toml", it.display()),
+                                format!("{}/**/Cargo.lock", it.display()),
+                            ]
                         })
-                        .collect(),
-                };
-                let registration = lsp_types::Registration {
-                    id: "workspace/didChangeWatchedFiles".to_string(),
-                    method: "workspace/didChangeWatchedFiles".to_string(),
-                    register_options: Some(serde_json::to_value(registration_options).unwrap()),
-                };
-                self.send_request::<lsp_types::request::RegisterCapability>(
-                    lsp_types::RegistrationParams { registrations: vec![registration] },
-                    |_, _| (),
-                );
-            }
+                    })
+                    .map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
+                    .collect(),
+            };
+            let registration = lsp_types::Registration {
+                id: "workspace/didChangeWatchedFiles".to_string(),
+                method: "workspace/didChangeWatchedFiles".to_string(),
+                register_options: Some(serde_json::to_value(registration_options).unwrap()),
+            };
+            self.send_request::<lsp_types::request::RegisterCapability>(
+                lsp_types::RegistrationParams { registrations: vec![registration] },
+                |_, _| (),
+            );
         }
 
         let mut change = Change::new();
@@ -364,7 +279,7 @@ fn eq_ignore_build_data<'a>(
         if self.proc_macro_client.is_none() {
             self.proc_macro_client = match self.config.proc_macro_srv() {
                 None => None,
-                Some((path, args)) => match ProcMacroClient::extern_process(path.clone(), args) {
+                Some((path, args)) => match ProcMacroServer::spawn(path.clone(), args) {
                     Ok(it) => Some(it),
                     Err(err) => {
                         tracing::error!(
@@ -392,8 +307,9 @@ fn eq_ignore_build_data<'a>(
         // Create crate graph from all the workspaces
         let crate_graph = {
             let proc_macro_client = self.proc_macro_client.as_ref();
-            let mut load_proc_macro =
-                move |path: &AbsPath| load_proc_macro(proc_macro_client, path);
+            let mut load_proc_macro = move |path: &AbsPath, dummy_replace: &_| {
+                load_proc_macro(proc_macro_client, path, dummy_replace)
+            };
 
             let vfs = &mut self.vfs.write().0;
             let loader = &mut self.loader;
@@ -414,7 +330,11 @@ fn eq_ignore_build_data<'a>(
 
             let mut crate_graph = CrateGraph::default();
             for ws in self.workspaces.iter() {
-                crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load));
+                crate_graph.extend(ws.to_crate_graph(
+                    self.config.dummy_replacements(),
+                    &mut load_proc_macro,
+                    &mut load,
+                ));
             }
             crate_graph
         };
@@ -503,7 +423,7 @@ fn reload_flycheck(&mut self) {
                     id,
                     Box::new(move |msg| sender.send(msg).unwrap()),
                     config.clone(),
-                    root.to_path_buf().into(),
+                    root.to_path_buf(),
                 )
             })
             .collect();
@@ -548,12 +468,12 @@ pub(crate) fn new(
                 vfs::loader::Entry::Directories(dirs)
             };
 
-            if root.is_member {
+            if root.is_local {
                 res.watch.push(res.load.len());
             }
             res.load.push(entry);
 
-            if root.is_member {
+            if root.is_local {
                 local_filesets.push(fsc.len());
             }
             fsc.add_file_set(file_set_roots)
@@ -591,27 +511,63 @@ pub(crate) fn partition(&self, vfs: &vfs::Vfs) -> Vec<SourceRoot> {
     }
 }
 
-pub(crate) fn load_proc_macro(client: Option<&ProcMacroClient>, path: &AbsPath) -> Vec<ProcMacro> {
+/// Load the proc-macros for the given lib path, replacing all expanders whose names are in `dummy_replace`
+/// with an identity dummy expander.
+pub(crate) fn load_proc_macro(
+    client: Option<&ProcMacroServer>,
+    path: &AbsPath,
+    dummy_replace: &[Box<str>],
+) -> Vec<ProcMacro> {
+    let dylib = match MacroDylib::new(path.to_path_buf()) {
+        Ok(it) => it,
+        Err(err) => {
+            // FIXME: that's not really right -- we store this error in a
+            // persistent status.
+            tracing::warn!("failed to load proc macro: {}", err);
+            return Vec::new();
+        }
+    };
+
     return client
-        .map(|it| it.by_dylib_path(path))
-        .unwrap_or_default()
+        .map(|it| it.load_dylib(dylib))
         .into_iter()
-        .map(expander_to_proc_macro)
+        .flat_map(|it| match it {
+            Ok(Ok(macros)) => macros,
+            Err(err) => {
+                tracing::error!("proc macro server crashed: {}", err);
+                Vec::new()
+            }
+            Ok(Err(err)) => {
+                // FIXME: that's not really right -- we store this error in a
+                // persistent status.
+                tracing::warn!("failed to load proc macro: {}", err);
+                Vec::new()
+            }
+        })
+        .map(|expander| expander_to_proc_macro(expander, dummy_replace))
         .collect();
 
-    fn expander_to_proc_macro(expander: proc_macro_api::ProcMacroProcessExpander) -> ProcMacro {
-        let name = expander.name().into();
+    fn expander_to_proc_macro(
+        expander: proc_macro_api::ProcMacro,
+        dummy_replace: &[Box<str>],
+    ) -> ProcMacro {
+        let name = SmolStr::from(expander.name());
         let kind = match expander.kind() {
             proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
             proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
             proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
         };
-        let expander = Arc::new(Expander(expander));
+        let expander: Arc<dyn ProcMacroExpander> =
+            if dummy_replace.iter().any(|replace| &**replace == name) {
+                Arc::new(DummyExpander)
+            } else {
+                Arc::new(Expander(expander))
+            };
         ProcMacro { name, kind, expander }
     }
 
     #[derive(Debug)]
-    struct Expander(proc_macro_api::ProcMacroProcessExpander);
+    struct Expander(proc_macro_api::ProcMacro);
 
     impl ProcMacroExpander for Expander {
         fn expand(
@@ -619,9 +575,64 @@ fn expand(
             subtree: &tt::Subtree,
             attrs: Option<&tt::Subtree>,
             env: &Env,
-        ) -> Result<tt::Subtree, tt::ExpansionError> {
+        ) -> Result<tt::Subtree, ProcMacroExpansionError> {
             let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
-            self.0.expand(subtree, attrs, env)
+            match self.0.expand(subtree, attrs, env) {
+                Ok(Ok(subtree)) => Ok(subtree),
+                Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
+                Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
+            }
+        }
+    }
+
+    /// Dummy identity expander, used for proc-macros that are deliberately ignored by the user.
+    #[derive(Debug)]
+    struct DummyExpander;
+
+    impl ProcMacroExpander for DummyExpander {
+        fn expand(
+            &self,
+            subtree: &tt::Subtree,
+            _: Option<&tt::Subtree>,
+            _: &Env,
+        ) -> Result<tt::Subtree, ProcMacroExpansionError> {
+            Ok(subtree.clone())
+        }
+    }
+}
+
+pub(crate) fn should_refresh_for_change(path: &AbsPath, change_kind: ChangeKind) -> bool {
+    const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
+    const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
+    let file_name = path.file_name().unwrap_or_default();
+
+    if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
+        return true;
+    }
+    if change_kind == ChangeKind::Modify {
+        return false;
+    }
+    if path.extension().unwrap_or_default() != "rs" {
+        return false;
+    }
+    if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
+        return true;
+    }
+    let parent = match path.parent() {
+        Some(it) => it,
+        None => return false,
+    };
+    if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
+        return true;
+    }
+    if file_name == "main.rs" {
+        let grand_parent = match parent.parent() {
+            Some(it) => it,
+            None => return false,
+        };
+        if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
+            return true;
         }
     }
+    false
 }