From d9d41a0d919d90008e4c0db31cc86786ca502aa1 Mon Sep 17 00:00:00 2001
From: Andrew Langmeier <raymi306@gmail.com>
Date: Sun, 13 Aug 2023 17:34:32 +0000
Subject: [PATCH] Add fix for detecting changes on files by editors which
 replace files on save, such as vi

---
 src/cmd/serve.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs
index 46e8d9345..35fd85f1f 100644
--- a/src/cmd/serve.rs
+++ b/src/cmd/serve.rs
@@ -653,6 +653,29 @@ pub fn serve(
                         };
                         messages::report_elapsed_time(start);
                     }
+                    NoticeRemove(path) => {
+                        // Some editors, like those in the vi family, replace the file you're
+                        // working on with a copy, breaking the watcher.
+                        //
+                        // See https://github.com/notify-rs/notify/issues/247
+                        //
+                        // If we need to watch other single required files, should handle them
+                        // here. For now, just config.toml needs special handling
+                        match detect_change_kind(root_dir, &path, &config_path) {
+                            (ChangeKind::Config, _) => {
+                                console::info("-> Config changed. The browser needs to be refreshed to make the changes visible.");
+
+                                if let Some(s) = recreate_site() {
+                                    site = s;
+                                }
+                                let entry = config_path_rel.to_str().unwrap_or("config.toml");
+                                watcher
+                                    .watch(root_dir.join(entry), RecursiveMode::Recursive)
+                                    .with_context(|| format!("Can't watch `{}` for changes in folder `{}`. Does it exist, and do you have correct permissions?", entry, root_dir.display()))?;
+                            }
+                            _ => {}
+                        }
+                    }
                     _ => {}
                 }
             }
