refactor: update project file index on watcher events
This commit is contained in:
parent
b4d2998425
commit
863c9aade5
2 changed files with 58 additions and 1 deletions
|
|
@ -421,8 +421,11 @@ const Process = struct {
|
|||
var vcs_id: []const u8 = undefined;
|
||||
|
||||
var eol_mode: Buffer.EolModeTag = @intFromEnum(Buffer.EolMode.lf);
|
||||
var event_type: file_watcher.EventType = undefined;
|
||||
|
||||
if (try cbor.match(m.buf, .{ "walk_tree_entry", tp.extract(&project_directory), tp.more })) {
|
||||
if (try cbor.match(m.buf, .{ "FW", "change", tp.extract(&path), tp.extract(&event_type) })) {
|
||||
self.handle_file_watch_event(path, event_type);
|
||||
} else if (try cbor.match(m.buf, .{ "walk_tree_entry", tp.extract(&project_directory), tp.more })) {
|
||||
if (self.projects.get(project_directory)) |project|
|
||||
project.walk_tree_entry(m) catch |e| self.logger.err("walk_tree_entry", e);
|
||||
} else if (try cbor.match(m.buf, .{ "walk_tree_done", tp.extract(&project_directory) })) {
|
||||
|
|
@ -539,6 +542,26 @@ const Process = struct {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_file_watch_event(self: *Process, abs_path: []const u8, event_type: file_watcher.EventType) void {
|
||||
std.log.debug("file_watch_event: {s} {s}", .{ @tagName(event_type), abs_path });
|
||||
var it = self.projects.iterator();
|
||||
while (it.next()) |entry| {
|
||||
const dir = entry.key_ptr.*;
|
||||
if (!std.mem.startsWith(u8, abs_path, dir)) continue;
|
||||
if (abs_path.len <= dir.len or abs_path[dir.len] != std.fs.path.sep) continue;
|
||||
const rel_path = abs_path[dir.len + 1 ..];
|
||||
const project = entry.value_ptr.*;
|
||||
switch (event_type) {
|
||||
.created => project.file_added(rel_path) catch |e| self.logger.err("file_watcher.file_added", e),
|
||||
.modified => project.file_modified(rel_path),
|
||||
.deleted => project.file_deleted(rel_path),
|
||||
.renamed => project.file_deleted(rel_path),
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.parent.send(.{ "FW", "change", abs_path, event_type }) catch {};
|
||||
}
|
||||
|
||||
fn open(self: *Process, project_directory: []const u8) (SpawnError || std.fs.Dir.OpenError)!void {
|
||||
if (self.projects.get(project_directory)) |project| {
|
||||
project.last_used = std.time.nanoTimestamp();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue