-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarks.zig
More file actions
311 lines (280 loc) · 12.2 KB
/
Copy pathbookmarks.zig
File metadata and controls
311 lines (280 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Named bookmarks: 26 slots `a..z`, each pointing at a file + cursor
//! position. `mx` sets slot `x`, `'x` jumps to it. Persisted per-project
//! to `~/.stem/cache/bookmarks/<hash>.json` so they survive restart.
const std = @import("std");
pub const Bookmark = struct {
file_path: []const u8,
row: usize,
col: usize,
/// ms since epoch, used to sort the list view newest-first.
timestamp: i64,
};
pub const BookmarkStore = struct {
allocator: std.mem.Allocator,
/// Index is slot - 'a'. `null` = unset.
slots: [26]?Bookmark = [_]?Bookmark{null} ** 26,
/// Absolute path to the persistence file for the current project.
/// `null` until `attachProject` is called.
persist_path: ?[]u8 = null,
pub fn init(allocator: std.mem.Allocator) BookmarkStore {
return .{ .allocator = allocator };
}
pub fn deinit(self: *BookmarkStore) void {
for (&self.slots) |*slot_ptr| {
if (slot_ptr.*) |*bm| self.allocator.free(bm.file_path);
slot_ptr.* = null;
}
if (self.persist_path) |p| self.allocator.free(p);
self.persist_path = null;
}
fn slotIndex(slot: u8) ?usize {
if (slot >= 'a' and slot <= 'z') return @intCast(slot - 'a');
return null;
}
pub fn set(
self: *BookmarkStore,
slot: u8,
file_path: []const u8,
row: usize,
col: usize,
now_ms: i64,
) !void {
const idx = slotIndex(slot) orelse return error.InvalidBookmarkSlot;
if (self.slots[idx]) |*old| self.allocator.free(old.file_path);
self.slots[idx] = .{
.file_path = try self.allocator.dupe(u8, file_path),
.row = row,
.col = col,
.timestamp = now_ms,
};
}
pub fn get(self: *BookmarkStore, slot: u8) ?Bookmark {
const idx = slotIndex(slot) orelse return null;
return self.slots[idx];
}
pub fn remove(self: *BookmarkStore, slot: u8) bool {
const idx = slotIndex(slot) orelse return false;
if (self.slots[idx]) |*old| {
self.allocator.free(old.file_path);
self.slots[idx] = null;
return true;
}
return false;
}
pub fn clearAll(self: *BookmarkStore) void {
for (&self.slots) |*slot_ptr| {
if (slot_ptr.*) |*old| self.allocator.free(old.file_path);
slot_ptr.* = null;
}
}
/// Number of occupied slots.
pub fn count(self: *BookmarkStore) usize {
var n: usize = 0;
for (self.slots) |slot| {
if (slot != null) n += 1;
}
return n;
}
/// Bind to a project root. Computes the persistence file path and
/// best-effort-loads any pre-existing bookmark file. Failing to load
/// is non-fatal — we just start with an empty store.
pub fn attachProject(self: *BookmarkStore, io: std.Io, home_dir: []const u8, project_root: []const u8) !void {
if (self.persist_path) |p| self.allocator.free(p);
self.persist_path = try buildPersistPath(self.allocator, home_dir, project_root);
// Pre-create the parent dir so save can blindly write.
if (std.fs.path.dirname(self.persist_path.?)) |dir_path| {
std.Io.Dir.cwd().createDirPath(io, dir_path) catch {};
}
self.load(io) catch |err| switch (err) {
error.FileNotFound => {},
else => return err,
};
}
/// Save bookmarks to disk. Atomic: writes to .tmp then renames.
pub fn save(self: *BookmarkStore, io: std.Io) !void {
const path = self.persist_path orelse return;
var json = std.ArrayListUnmanaged(u8).empty;
defer json.deinit(self.allocator);
try json.appendSlice(self.allocator, "{\n \"bookmarks\": [\n");
var first = true;
for (self.slots, 0..) |slot, i| {
if (slot) |bm| {
if (!first) try json.appendSlice(self.allocator, ",\n");
first = false;
const slot_char: u8 = @intCast('a' + i);
var prefix_buf: [64]u8 = undefined;
const prefix = try std.fmt.bufPrint(&prefix_buf, " {{\"slot\": \"{c}\", \"file\": \"", .{slot_char});
try json.appendSlice(self.allocator, prefix);
try appendJsonEscaped(self.allocator, &json, bm.file_path);
var suffix_buf: [128]u8 = undefined;
const suffix = try std.fmt.bufPrint(&suffix_buf, "\", \"row\": {d}, \"col\": {d}, \"ts\": {d}}}", .{ bm.row, bm.col, bm.timestamp });
try json.appendSlice(self.allocator, suffix);
}
}
try json.appendSlice(self.allocator, "\n ]\n}\n");
const tmp_path = try std.fmt.allocPrint(self.allocator, "{s}.tmp", .{path});
defer self.allocator.free(tmp_path);
{
var file = try std.Io.Dir.createFileAbsolute(io, tmp_path, .{});
defer file.close(io);
try file.writePositionalAll(io, json.items, 0);
}
try std.Io.Dir.renameAbsolute(tmp_path, path, io);
}
fn appendJsonEscaped(allocator: std.mem.Allocator, out: *std.ArrayListUnmanaged(u8), s: []const u8) !void {
for (s) |c| {
switch (c) {
'"' => try out.appendSlice(allocator, "\\\""),
'\\' => try out.appendSlice(allocator, "\\\\"),
'\n' => try out.appendSlice(allocator, "\\n"),
'\r' => try out.appendSlice(allocator, "\\r"),
'\t' => try out.appendSlice(allocator, "\\t"),
else => try out.append(allocator, c),
}
}
}
pub fn load(self: *BookmarkStore, io: std.Io) !void {
const path = self.persist_path orelse return;
const file = try std.Io.Dir.openFileAbsolute(io, path, .{});
defer file.close(io);
const max_size: usize = 64 * 1024;
const stat = try file.stat(io);
const size = @min(stat.size, max_size);
const bytes = try self.allocator.alloc(u8, @intCast(size));
defer self.allocator.free(bytes);
const n = try file.readPositionalAll(io, bytes, 0);
try self.parseAndPopulate(bytes[0..n]);
}
fn parseAndPopulate(self: *BookmarkStore, json_text: []const u8) !void {
// Minimal hand-rolled parser: we wrote the file, so the shape is
// fixed. Avoids pulling std.json into the kernel surface.
var i: usize = 0;
while (i < json_text.len) {
// Skip to next `{"slot":` marker.
const slot_marker = "\"slot\":";
if (i + slot_marker.len > json_text.len) break;
if (!std.mem.eql(u8, json_text[i .. i + slot_marker.len], slot_marker)) {
i += 1;
continue;
}
// Slot value: next quoted char.
i = std.mem.indexOfScalarPos(u8, json_text, i, '"') orelse break;
i = std.mem.indexOfScalarPos(u8, json_text, i + 1, '"') orelse break;
if (i + 2 >= json_text.len) break;
const slot_char: u8 = json_text[i + 1];
const file_key = std.mem.indexOfPos(u8, json_text, i, "\"file\":") orelse break;
i = std.mem.indexOfScalarPos(u8, json_text, file_key + 7, '"') orelse break;
const file_start = i + 1;
// Find end quote, handle \" escapes.
var j = file_start;
while (j < json_text.len) : (j += 1) {
if (json_text[j] == '\\' and j + 1 < json_text.len) {
j += 1;
continue;
}
if (json_text[j] == '"') break;
}
if (j >= json_text.len) break;
const file_raw = json_text[file_start..j];
i = j + 1;
const row_key = std.mem.indexOfPos(u8, json_text, i, "\"row\":") orelse break;
const row_start = row_key + 6;
const row_end = std.mem.indexOfAnyPos(u8, json_text, row_start, ",}") orelse break;
const row = std.fmt.parseInt(usize, std.mem.trim(u8, json_text[row_start..row_end], " "), 10) catch {
i = row_end;
continue;
};
const col_key = std.mem.indexOfPos(u8, json_text, row_end, "\"col\":") orelse break;
const col_start = col_key + 6;
const col_end = std.mem.indexOfAnyPos(u8, json_text, col_start, ",}") orelse break;
const col = std.fmt.parseInt(usize, std.mem.trim(u8, json_text[col_start..col_end], " "), 10) catch {
i = col_end;
continue;
};
const ts_key = std.mem.indexOfPos(u8, json_text, col_end, "\"ts\":") orelse break;
const ts_start = ts_key + 5;
const ts_end = std.mem.indexOfAnyPos(u8, json_text, ts_start, ",}") orelse break;
const ts = std.fmt.parseInt(i64, std.mem.trim(u8, json_text[ts_start..ts_end], " "), 10) catch 0;
const unescaped = try unescapeJsonString(self.allocator, file_raw);
defer self.allocator.free(unescaped);
self.set(slot_char, unescaped, row, col, ts) catch {};
i = ts_end;
}
}
fn unescapeJsonString(allocator: std.mem.Allocator, s: []const u8) ![]u8 {
var out = std.ArrayListUnmanaged(u8).empty;
defer out.deinit(allocator);
var i: usize = 0;
while (i < s.len) : (i += 1) {
if (s[i] == '\\' and i + 1 < s.len) {
switch (s[i + 1]) {
'n' => try out.append(allocator, '\n'),
't' => try out.append(allocator, '\t'),
'r' => try out.append(allocator, '\r'),
'"' => try out.append(allocator, '"'),
'\\' => try out.append(allocator, '\\'),
'/' => try out.append(allocator, '/'),
else => {
try out.append(allocator, s[i]);
try out.append(allocator, s[i + 1]);
},
}
i += 1;
} else {
try out.append(allocator, s[i]);
}
}
return out.toOwnedSlice(allocator);
}
fn buildPersistPath(allocator: std.mem.Allocator, home_dir: []const u8, project_root: []const u8) ![]u8 {
// Stable-ish per-project key: full project path → 64-bit hash → hex.
// Collisions theoretically possible but practically negligible.
var h = std.hash.Wyhash.init(0);
h.update(project_root);
const digest = h.final();
return std.fmt.allocPrint(
allocator,
"{s}/.stem/cache/bookmarks/{x}.json",
.{ home_dir, digest },
);
}
};
test "bookmark store set/get/remove" {
const allocator = std.testing.allocator;
var store = BookmarkStore.init(allocator);
defer store.deinit();
try store.set('a', "/foo/bar.zig", 10, 5, 1000);
const bm = store.get('a').?;
try std.testing.expectEqual(@as(usize, 10), bm.row);
try std.testing.expectEqual(@as(usize, 5), bm.col);
try std.testing.expectEqualStrings("/foo/bar.zig", bm.file_path);
try std.testing.expect(store.remove('a'));
try std.testing.expect(store.get('a') == null);
}
test "bookmark store rejects out-of-range slots" {
const allocator = std.testing.allocator;
var store = BookmarkStore.init(allocator);
defer store.deinit();
try std.testing.expectError(error.InvalidBookmarkSlot, store.set('1', "x", 0, 0, 0));
try std.testing.expectError(error.InvalidBookmarkSlot, store.set('A', "x", 0, 0, 0));
}
test "bookmark store overwrites existing slot" {
const allocator = std.testing.allocator;
var store = BookmarkStore.init(allocator);
defer store.deinit();
try store.set('a', "/a", 1, 1, 1);
try store.set('a', "/b", 2, 2, 2);
const bm = store.get('a').?;
try std.testing.expectEqualStrings("/b", bm.file_path);
try std.testing.expectEqual(@as(usize, 2), bm.row);
}
test "bookmark store clearAll" {
const allocator = std.testing.allocator;
var store = BookmarkStore.init(allocator);
defer store.deinit();
try store.set('a', "/a", 0, 0, 0);
try store.set('b', "/b", 0, 0, 0);
try std.testing.expectEqual(@as(usize, 2), store.count());
store.clearAll();
try std.testing.expectEqual(@as(usize, 0), store.count());
}