fix: rewatch files after atomic saves (#908)
This commit is contained in:
@@ -320,6 +320,14 @@ func (e *Engine) watchPath(path string) error {
|
||||
if !e.isIncludeExt(ev.Name) && !e.checkIncludeFile(ev.Name) {
|
||||
break
|
||||
}
|
||||
// Rewatch the file if the editor is using atomic saving.
|
||||
if renameOrRemoveEvent(ev) {
|
||||
if e.checkIncludeFile(ev.Name) {
|
||||
if err := e.watcher.Add(ev.Name); err != nil {
|
||||
e.watcherLog("error rewatching file: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
e.watcherDebug("%s has changed", e.config.rel(ev.Name))
|
||||
e.eventCh <- ev.Name
|
||||
case err := <-e.watcher.Errors():
|
||||
|
||||
+7
-1
@@ -303,13 +303,19 @@ func isDir(path string) bool {
|
||||
func validEvent(ev fsnotify.Event) bool {
|
||||
return ev.Op&fsnotify.Create == fsnotify.Create ||
|
||||
ev.Op&fsnotify.Write == fsnotify.Write ||
|
||||
ev.Op&fsnotify.Remove == fsnotify.Remove
|
||||
ev.Op&fsnotify.Remove == fsnotify.Remove ||
|
||||
ev.Op&fsnotify.Rename == fsnotify.Rename
|
||||
}
|
||||
|
||||
func removeEvent(ev fsnotify.Event) bool {
|
||||
return ev.Op&fsnotify.Remove == fsnotify.Remove
|
||||
}
|
||||
|
||||
func renameOrRemoveEvent(ev fsnotify.Event) bool {
|
||||
return ev.Op&fsnotify.Remove == fsnotify.Remove ||
|
||||
ev.Op&fsnotify.Rename == fsnotify.Rename
|
||||
}
|
||||
|
||||
func cmdPath(path string) string {
|
||||
return strings.Split(path, " ")[0]
|
||||
}
|
||||
|
||||
+5
-1
@@ -169,10 +169,14 @@ func TestValidAndRemoveEvent(t *testing.T) {
|
||||
assert.True(t, validEvent(fsnotify.Event{Op: fsnotify.Create}))
|
||||
assert.True(t, validEvent(fsnotify.Event{Op: fsnotify.Write}))
|
||||
assert.True(t, validEvent(fsnotify.Event{Op: fsnotify.Remove}))
|
||||
assert.False(t, validEvent(fsnotify.Event{Op: fsnotify.Rename}))
|
||||
assert.True(t, validEvent(fsnotify.Event{Op: fsnotify.Rename}))
|
||||
|
||||
assert.True(t, removeEvent(fsnotify.Event{Op: fsnotify.Remove}))
|
||||
assert.False(t, removeEvent(fsnotify.Event{Op: fsnotify.Write}))
|
||||
|
||||
assert.True(t, renameOrRemoveEvent(fsnotify.Event{Op: fsnotify.Remove}))
|
||||
assert.True(t, renameOrRemoveEvent(fsnotify.Event{Op: fsnotify.Rename}))
|
||||
assert.False(t, renameOrRemoveEvent(fsnotify.Event{Op: fsnotify.Write}))
|
||||
}
|
||||
|
||||
func TestCmdPath(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user