diff options
Diffstat (limited to 'core/File.zig')
| -rw-r--r-- | core/File.zig | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/core/File.zig b/core/File.zig index e246285c..d54d5011 100644 --- a/core/File.zig +++ b/core/File.zig @@ -60,35 +60,39 @@ pub const History = struct { } }; -/// Owned syntax results: a text's declarations at a revision. +/// Owned syntax results: a text's declarations and symbols at a revision. pub const ContextCache = struct { /// Its revision is dumped and digested: a worker's parse is an input, /// which a client joined since never saw, and a step reads what it - /// left (Query.ownerDecls). The declarations are made again from the - /// text at a load (`reload`), with the allocator that made them. - pub const replica = .{ .local = .{"allocator"}, .derived = .{"declarations"} }; + /// left (Query.ownerDecls, the Symbols source). The declarations and + /// symbols are made again from the text at a load (`reload`), with the + /// allocator that made them. + pub const replica = .{ .local = .{"allocator"}, .derived = .{ "declarations", "symbols" } }; allocator: ?std.mem.Allocator = null, declarations: []syntax.ContextDeclaration = &.{}, + symbols: []syntax.Symbol = &.{}, revision: ?u32 = null, pub fn deinit(self: *ContextCache) void { - if (self.allocator) |gpa| gpa.free(self.declarations); + if (self.allocator) |gpa| { + gpa.free(self.declarations); + gpa.free(self.symbols); + } self.* = .{}; } - /// A loaded text's (its revision the dump's): its declarations parsed - /// again where they are of the text it has. + /// A loaded text's (its revision the dump's): its context parsed + /// again where it is of the text it has. pub fn reload(self: *ContextCache, gpa: std.mem.Allocator, path: []const u8, content: []const u8, revision: u32) void { const at = self.revision orelse return; if (at != revision) return; - const declarations = syntax.contextDeclarations(gpa, path, content) catch return; - self.replace(gpa, declarations, at); + self.replace(gpa, syntax.context(gpa, path, content) catch return, at); } - /// Takes ownership of declarations allocated by gpa. - pub fn replace(self: *ContextCache, gpa: std.mem.Allocator, declarations: []syntax.ContextDeclaration, revision: u32) void { + /// Takes ownership of a context allocated by gpa. + pub fn replace(self: *ContextCache, gpa: std.mem.Allocator, made: syntax.Context, revision: u32) void { self.deinit(); - self.* = .{ .allocator = gpa, .declarations = declarations, .revision = revision }; + self.* = .{ .allocator = gpa, .declarations = made.declarations, .symbols = made.symbols, .revision = revision }; } }; @@ -922,7 +926,13 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { pane.body.cur_pinned = true; pane.body.cur_row = @intCast(scroll); p.installPane(id, pane); - if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); + if (filesystem.localPath(path) != null) { + p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); + // Its language's server started now, off the loop: the first + // question asked of it finds it ready. Not a picker's preview's, + // which opens no pane (Pick.preview). + p.emit(.{ .lsp_open = .{ .pane = .at(id) } }); + } return pane; } @@ -2191,12 +2201,17 @@ pub fn unchain(pane: *Pane) void { dropUnfollow(pane); } -/// The Unfollow word Follow gave a pane's tag (builtins.Follow), gone. +/// The Unfollow word Follow gave a pane's tag (builtins.Follow), gone, with +/// the blank before it (after it, first in the tag's own text). fn dropUnfollow(pane: *Pane) void { const own = pane.tag.own orelse return; - const at = std.mem.indexOf(u8, own, " Unfollow") orelse return; - const end = at + " Unfollow".len; - if (end < own.len and std.mem.indexOfScalar(u8, " \t\n", own[end]) == null) return; + const word = std.mem.indexOf(u8, own, "Unfollow") orelse return; + const word_end = word + "Unfollow".len; + const blank_after = word_end < own.len and std.mem.indexOfScalar(u8, " \t\n", own[word_end]) != null; + if (word_end < own.len and !blank_after) return; + if (word > 0 and own[word - 1] != ' ') return; + const at = if (word > 0) word - 1 else word; + const end = if (word == 0 and blank_after) word_end + 1 else word_end; const cut = std.mem.concat(pane.gpa, u8, &.{ own[0..at], own[end..] }) catch return; pane.gpa.free(own); pane.tag.own = cut; @@ -2490,8 +2505,7 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { if (!f.tree_context or !supportsContext(pane) or f.context.revision == f.revision) return; // The file's declarations, in its lines (the layer's are none of it). - const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, fileText(f, p.scratch.allocator()) catch return) catch return; - f.context.replace(p.tree_sitter_gpa, declarations, f.revision); + f.context.replace(p.tree_sitter_gpa, syntax.context(p.tree_sitter_gpa, f.path, fileText(f, p.scratch.allocator()) catch return) catch return, f.revision); } pub fn refreshHighlights(p: *Pardes) void { @@ -3614,12 +3628,13 @@ test "context cache frees each allocation with its original allocator" { defer std.testing.expect(second.deinit() == .ok) catch @panic("second allocator leaked"); var cache: ContextCache = .{}; defer cache.deinit(); - cache.replace(first.allocator(), try first.allocator().alloc(syntax.ContextDeclaration, 1), 1); - cache.replace(second.allocator(), try second.allocator().alloc(syntax.ContextDeclaration, 2), 2); + cache.replace(first.allocator(), .{ .declarations = try first.allocator().alloc(syntax.ContextDeclaration, 1), .symbols = try first.allocator().alloc(syntax.Symbol, 1) }, 1); + cache.replace(second.allocator(), .{ .declarations = try second.allocator().alloc(syntax.ContextDeclaration, 2), .symbols = try second.allocator().alloc(syntax.Symbol, 2) }, 2); try std.testing.expectEqual(@as(?u32, 2), cache.revision); cache.deinit(); try std.testing.expect(cache.allocator == null); try std.testing.expectEqual(@as(usize, 0), cache.declarations.len); + try std.testing.expectEqual(@as(usize, 0), cache.symbols.len); } /// The inks of row `row`'s first `n` text cells of pane `id` as `s` draws them. |
