diff options
Diffstat (limited to 'src/lib_editor.zig')
| -rw-r--r-- | src/lib_editor.zig | 79 |
1 files changed, 58 insertions, 21 deletions
diff --git a/src/lib_editor.zig b/src/lib_editor.zig index 86e494e1..f9149ef0 100644 --- a/src/lib_editor.zig +++ b/src/lib_editor.zig @@ -221,6 +221,7 @@ const Live = struct { multi_count: usize = 1, retired_edit: ?[]u8 = null, failed: bool = false, + syntax: ?*Syntax = null, undo_thawed: usize = 0, redo_thawed: usize = 0, /// The ring records thawed, by their text's address: a snapshot still @@ -241,6 +242,7 @@ const Live = struct { .retired_edit = &l.retired_edit, .p = {}, .failed = &l.failed, + .syntax = l.syntax, }; } }; @@ -318,21 +320,24 @@ pub fn Editor(comptime cap: Capacity) type { /// The ranges in document order and which is the primary. pub fn ranges(ed: *const Self, a: std.mem.Allocator, out: *[max_selections]Selection) error{OutOfMemory}!struct { n: usize, primary: usize } { - const live = try ed.thaw(a); + const live = try ed.thaw(a, null); const got = live.text.ranges(live.text.own.?, 0, out); return .{ .n = got.n, .primary = got.pri }; } - /// A key, as a text pane in normal or insert mode takes it. - pub fn key(ed: *Self, a: std.mem.Allocator, k: Key) Outcome { - const live = ed.thaw(a) catch return .{ .refused = .out_of_memory }; + /// A key, as a text pane in normal or insert mode takes it. With + /// `syn`, the tree-sitter walks (Alt-o and kin, `]f`, `mif`) run on + /// its parse, made again if the text has changed; without, they + /// come back as actions (`mif` does nothing). + pub fn key(ed: *Self, a: std.mem.Allocator, syn: ?*Syntax, k: Key) Outcome { + const live = ed.thaw(a, syn) catch return .{ .refused = .out_of_memory }; const parsed = normal.textKey(live.context(a), &live.text, k); switch (parsed) { .unbound => return ed.keep(a, live, .{ .unbound = k }), .pending => return ed.keep(a, live, .pending), .ignored => return ed.keep(a, live, .done), .action => |act| { - if (!normal.textRole(act.role)) { + if (!normal.textRole(act.role, syn != null)) { // the parse's state goes on; nothing ran ed.cursor.normal = live.text.normal; return .{ .action = act }; @@ -344,9 +349,9 @@ pub fn Editor(comptime cap: Capacity) type { /// A normal-mode action, as its key would run it: across every /// range unless it is the selection's as a whole. - pub fn run(ed: *Self, a: std.mem.Allocator, act: Action) Outcome { - if (!normal.textRole(act.role)) return .{ .action = act }; - const live = ed.thaw(a) catch return .{ .refused = .out_of_memory }; + pub fn run(ed: *Self, a: std.mem.Allocator, syn: ?*Syntax, act: Action) Outcome { + if (!normal.textRole(act.role, syn != null)) return .{ .action = act }; + const live = ed.thaw(a, syn) catch return .{ .refused = .out_of_memory }; normal.runAction(live.context(a), &live.text, act); return ed.keep(a, live, .done); } @@ -354,7 +359,7 @@ pub fn Editor(comptime cap: Capacity) type { /// helix's `s` (select), `S` (split), `K` (keep) and `Alt-K` /// (remove) with `pattern` over the ranges there are. pub fn selectRegex(ed: *Self, a: std.mem.Allocator, pattern: []const u8, how: RegexMode) Outcome { - const live = ed.thaw(a) catch return .{ .refused = .out_of_memory }; + const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory }; const t = &live.text; // as pardes's prompt answers it (look.submitSearch): helix // keeps the pattern in `/` @@ -370,7 +375,7 @@ pub fn Editor(comptime cap: Capacity) type { /// Replace the whole text with `text`, as one step to undo. pub fn setText(ed: *Self, a: std.mem.Allocator, text: []const u8) Outcome { - const live = ed.thaw(a) catch return .{ .refused = .out_of_memory }; + const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory }; const e = live.context(a); const t = &live.text; edit.pushUndo(e, t) catch return .{ .refused = .out_of_memory }; @@ -390,14 +395,15 @@ pub fn Editor(comptime cap: Capacity) type { /// Write register `name` (the system clipboard's `+`, a pattern's /// `/`): one value. pub fn setRegister(ed: *Self, a: std.mem.Allocator, name: u21, value: []const u8) Outcome { - const live = ed.thaw(a) catch return .{ .refused = .out_of_memory }; + const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory }; live.registers.put(a, name, value, 0, 1, true) catch return .{ .refused = .out_of_memory }; return ed.keep(a, live, .done); } - fn thaw(ed: *const Self, a: std.mem.Allocator) error{OutOfMemory}!*Live { + fn thaw(ed: *const Self, a: std.mem.Allocator, syn: ?*Syntax) error{OutOfMemory}!*Live { const live = try a.create(Live); - live.* = .{ .text = .{ .what = .body, .own = try ed.bytes(a), .gpa = a } }; + live.* = .{ .text = .{ .what = .body, .own = try ed.bytes(a), .gpa = a }, .syntax = syn }; + if (syn) |s| s.at(ed.revision); const t = &live.text; inline for (@typeInfo(Cursor).@"struct".field_names) |name| @field(t, name) = @field(ed.cursor, name); if (ed.cursor.nsel > 0) { @@ -523,19 +529,50 @@ pub fn Editor(comptime cap: Capacity) type { pub const Syntax = struct { tree: ?*syntax.ts.Tree = null, revision: u32 = 0, + /// The grammar the tree is parsed in: the last `parse`'s, or set by a + /// caller that hands this to `key` before parsing. None, and a walk + /// does nothing, as in a text without a grammar. + lang: ?Language = null, + /// helix's object_selections, what Alt-o grew from for Alt-i to go back + /// to (normal.nodeSelect): the tree's, dropped with it. In the page + /// allocator, which holds nothing of the code that made it. + history: std.ArrayList(Context.NodeStep) = .empty, + path: [64]u8 = undefined, /// The tree of `ed`'s text in `lang`, parsed again when the text has /// changed since the last; null when the language cannot parse. pub fn parse(s: *Syntax, a: std.mem.Allocator, ed: anytype, lang: Language) ?*syntax.ts.Tree { - if (s.tree != null and s.revision == ed.revision) return s.tree; + s.lang = lang; + s.at(ed.revision); + if (s.tree == null) _ = s.walkTree(ed.bytes(a) catch return null); + return s.tree; + } + + /// What is kept of another revision of the text dropped: the tree and + /// Alt-o's history. + fn at(s: *Syntax, revision: u32) void { + if (s.revision == revision) return; + s.drop(); + s.revision = revision; + } + + fn drop(s: *Syntax) void { if (s.tree) |tree| tree.destroy(); s.tree = null; - const text = ed.bytes(a) catch return null; - if (!syntax_impl.addLanguage(lang)) return null; - var buf: [64]u8 = undefined; - s.tree = @ptrCast(@alignCast(syntax_impl.parseTree(lang.path(&buf), text, null) orelse return null)); - s.revision = ed.revision; - return s.tree; + for (s.history.items) |step| std.heap.page_allocator.free(step.ranges); + s.history.clearAndFree(std.heap.page_allocator); + } + + /// The parse of `text`, the text at `revision`, as a key's walks read + /// it (normal.nodeTree): made the first time one asks for it. + pub fn walkTree(s: *Syntax, text: []const u8) ?Context.Tree { + const lang = s.lang orelse return null; + const path = lang.path(&s.path); + if (s.tree == null) { + if (!syntax_impl.addLanguage(lang)) return null; + s.tree = @ptrCast(@alignCast(syntax_impl.parseTree(path, text, null) orelse return null)); + } + return .{ .root = s.tree.?, .path = path, .history = &s.history, .gpa = std.heap.page_allocator }; } /// One `Syn` a byte of `ed`'s text from `from` to `to`, in `a`. @@ -549,7 +586,7 @@ pub const Syntax = struct { /// The tree gone, and every language handed in (at a reload: their /// code is about to go). pub fn forget(s: *Syntax) void { - if (s.tree) |tree| tree.destroy(); + s.drop(); s.* = .{}; syntax_impl.forgetLanguages(); } |
