diff options
| -rw-r--r-- | core/Editor.zig | 27 | ||||
| -rw-r--r-- | core/File.zig | 6 | ||||
| -rw-r--r-- | core/edit.zig | 9 | ||||
| -rw-r--r-- | core/jj.zig | 53 | ||||
| -rw-r--r-- | core/lib_editor.zig | 2 | ||||
| -rw-r--r-- | core/normal.zig | 16 |
6 files changed, 100 insertions, 13 deletions
diff --git a/core/Editor.zig b/core/Editor.zig index aaf6ab05..9e490c41 100644 --- a/core/Editor.zig +++ b/core/Editor.zig @@ -12,6 +12,7 @@ const Registers = @import("Registers.zig"); /// on this at compile time, so the library never compiles the core. pub const lib = @import("pardes_options").lib; const Pardes = if (lib) void else @import("Core.zig"); +const File = if (lib) void else @import("File.zig"); const Syntax = if (lib) @import("lib_editor.zig").Syntax else void; const modal = @import("modal.zig"); @@ -20,8 +21,30 @@ const Editor = @This(); /// A text's parse as the tree-sitter walks read it (normal.nodeTree): the /// tree, a path its grammar's queries are found by, and helix's /// object_selections, what Alt-o grew from for Alt-i to go back to, its -/// ranges in `gpa`. Dropped with the tree when the text changes. -pub const Tree = struct { root: *anyopaque, path: []const u8, history: *std.ArrayList(NodeStep), gpa: std.mem.Allocator }; +/// ranges in `gpa`. Dropped with the tree when the text changes. `text` is +/// what it parsed: a file's text (File.fileText: never the `Changes` +/// layer's lines, which are the buffer's), whose places `in` and `out` map +/// from and to the buffer's; the buffer itself with no layer shown, and +/// the library's text. +pub const Tree = struct { + root: *anyopaque, + path: []const u8, + history: *std.ArrayList(NodeStep), + gpa: std.mem.Allocator, + text: []const u8, + file: if (lib) void else *const File.State, + + /// A place in the buffer as the tree's text's (File.fileOffset). + pub fn in(tree: Tree, at: usize) usize { + return if (lib) at else File.fileOffset(tree.file, at); + } + + /// A place in the tree's text as the buffer's; `end`, a range's end + /// (File.bufferOffset). + pub fn out(tree: Tree, at: usize, end: bool) usize { + return if (lib) at else File.bufferOffset(tree.file, at, end); + } +}; pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; gpa: std.mem.Allocator, diff --git a/core/File.zig b/core/File.zig index 0c742eee..d36af5c2 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1137,8 +1137,10 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who } // The kept tree follows the edit (normal.fileTree parses with it), but // not while a rollback copy holds it: an edit in place would change the - // copy's tree under it, so that one is left as it is and parsed anew. - if (f.node_tree) |tree| if (syntax.held_trees == null and f.node_tree_edits == f.revision) { + // copy's tree under it, so that one is left as it is and parsed anew; + // nor with the `Changes` layer's lines in or going (the tree is of the + // file's text, the edit of the buffer's): parsed anew. + if (f.node_tree) |tree| if (syntax.held_trees == null and f.node_tree_edits == f.revision and told and f.read_only.len == 0) { syntax.editTree(tree, f.content[0..off], f.content[off..][0..len], bytes); f.node_tree_edits = f.revision +% 1; }; diff --git a/core/edit.zig b/core/edit.zig index 3517fe80..0b08dc9e 100644 --- a/core/edit.zig +++ b/core/edit.zig @@ -775,7 +775,10 @@ fn treeIndent(e: Editor, t: *Text, text: []const u8, row: usize, at: usize) Erro const f = bodyFile(t) orelse return null; const l = indents.lang(f.path) orelse return null; const tree = normal.fileTree(e.p, f) orelse return null; - return indents.forNewline(e.scratch, l, tree, text, row, at); + // The tree is of the file's text, in its lines and offsets (the + // `Changes` layer's lines are none of it). + if (f.read_only.len == 0) return indents.forNewline(e.scratch, l, tree, text, row, at); + return indents.forNewline(e.scratch, l, tree, try panes.File.fileText(f, e.scratch), panes.File.fileRow(f, row), panes.File.fileOffset(f, at)); } /// helix's default auto-pairs (auto_pairs.rs DEFAULT_PAIRS): `o` opens one @@ -1970,8 +1973,8 @@ pub fn textobjectSelect(e: Editor, t: *Text, text: []const u8, obj: u21, around: }; if (object) |o| { const tree = normal.nodeTree(e, t, text) orelse return; - const s = syntax.objectAt(tree.path, tree.root, text, o, around, modal.selectionCursor(text, range)) orelse return; - return t.setRange(e.tab, text, 0, .{ .anchor = s.from, .head = s.to }, false); + const s = syntax.objectAt(tree.path, tree.root, tree.text, o, around, tree.in(modal.selectionCursor(text, range))) orelse return; + return t.setRange(e.tab, text, 0, .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) }, false); } switch (obj) { 'w', 'W' => { diff --git a/core/jj.zig b/core/jj.zig index 963e4c42..78c0e857 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -3498,6 +3498,59 @@ test "the layer's lines are the pane's text, read-only: j and k go onto them, a } } +test "the node keys and the indent read the file's text: Alt-o and a function's text object beside the layer's lines select what they select with the layer hidden (the layer's lines inside the node with it), and o opens a line indented as there" { + const gpa = testing.allocator; + var repo = try Lines.repo() orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 44 }); + defer p.deinit(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const pane = try Lines.open(p, repo.file); + const id = p.view().active; + const f = &pane.file.?; + const alt_o: pardes.Event = .{ .key = .{ .cp = 'o', .alt = true } }; + // What each gesture selects, in the file's offsets (9P's dot), with the + // cursor on `c2();`'s `c`: buffer line 4 with the layer shown, the + // file's line 2 hidden. + const Gesture = enum { node1, node2, node3, function }; + var shown: [4]pardes.ctlfs.pane.State.Range = undefined; + var hidden: [4]pardes.ctlfs.pane.State.Range = undefined; + for ([_]bool{ true, false }) |layer| { + p.view().active = id; + if (!layer) p.update(.{ .command = "Changes" }); + try testing.expectEqual(layer, f.read_only.len > 0); + const row: i32 = if (layer) 4 else 2; + for (std.enums.values(Gesture), 0..) |g, k| { + p.view().active = id; + Lines.at(pane, row, 4); + switch (g) { + .node1, .node2, .node3 => for (0..@intFromEnum(g) + 1) |_| p.update(alt_o), + .function => Lines.send(p, "maf"), + } + (if (layer) &shown else &hidden)[k] = pardes.ctlfs.pane.fileDot(pane); + Lines.send(p, "\x1b;"); + } + } + errdefer for (shown, hidden) |s, h| std.debug.print("shown {d}..{d} hidden {d}..{d}\n", .{ s.q0, s.q1, h.q0, h.q1 }); + for (shown, hidden) |s, h| try testing.expectEqual(h, s); + // The function, the whole file less its last newline: hidden, the text. + try testing.expectEqualStrings("fn f() void {\n a();\n c2();\n}", Lines.cut_text[hidden[3].q0..hidden[3].q1]); + // `o` on `c2();`: the line opened with the indent it gets hidden. + var opened: [2][]const u8 = undefined; + for ([_]bool{ false, true }, &opened) |layer, *o| { + p.view().active = id; + if (layer) p.update(.{ .command = "Changes" }); + try testing.expectEqual(layer, f.read_only.len > 0); + Lines.at(pane, if (layer) 4 else 2, 4); + Lines.send(p, "ox\x1b"); + o.* = try arena.allocator().dupe(u8, try File.fileText(f, arena.allocator())); + Lines.send(p, "u"); + } + try testing.expectEqualStrings("fn f() void {\n a();\n c2();\n x\n}\n", opened[0]); + try testing.expectEqualStrings(opened[0], opened[1]); +} + test "THE FILE'S TEXT: with the layer's lines in the buffer, every reader of the file reads what it reads with them out, byte for byte: a Save, a Save to another name, Edit's w, body over 9P, the dirty check, a reread from the disk, a Get, jj's lines asked again, the context declarations, the colours of the file's lines, the language server's text and the place it is asked at" { const gpa = testing.allocator; var repo = try Lines.repo() orelse return error.SkipZigTest; diff --git a/core/lib_editor.zig b/core/lib_editor.zig index 3fe631af..3e4a1f15 100644 --- a/core/lib_editor.zig +++ b/core/lib_editor.zig @@ -585,7 +585,7 @@ pub const Syntax = struct { 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 }; + return .{ .root = s.tree.?, .path = path, .history = &s.history, .gpa = std.heap.page_allocator, .text = text, .file = {} }; } /// One `Syn` a byte of `ed`'s text from `from` to `to`, in `a`. diff --git a/core/normal.zig b/core/normal.zig index bb7824b2..89fd72f6 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -213,10 +213,13 @@ pub fn appendSearchLiteral(out: *std.ArrayList(u8), arena: std.mem.Allocator, te /// The file's parse, made on the first key that walks it after an edit and /// kept until the next: never made again for a key that changed nothing. +/// Of the file's text, as every parse is (File.fileText: the `Changes` +/// layer's lines are none of it). pub fn fileTree(p: *Pardes, f: *panes.File.State) ?*anyopaque { if (f.node_tree == null or f.node_tree_revision != f.revision) { + const text = panes.File.fileText(f, p.scratch.allocator()) catch return null; const old = f.node_tree; - f.node_tree = syntax.parseTree(f.path, f.content, if (f.node_tree_edits == f.revision) old else null); + f.node_tree = syntax.parseTree(f.path, text, if (f.node_tree_edits == f.revision) old else null); if (old) |o| syntax.freeTree(o); f.node_tree_revision = f.revision; f.node_tree_edits = f.revision; @@ -234,7 +237,9 @@ pub fn nodeTree(e: Editor, t: *Text, text: []const u8) ?Editor.Tree { if (comptime Editor.lib) return (e.syntax orelse return null).walkTree(text); const pane = t.pane() orelse return null; const f = if (pane.file) |*file| file else return null; - return .{ .root = fileTree(e.p, f) orelse return null, .path = f.path, .history = &f.node_history, .gpa = e.gpa }; + const root = fileTree(e.p, f) orelse return null; + const parsed = panes.File.fileText(f, e.scratch) catch return null; + return .{ .root = root, .path = f.path, .history = &f.node_history, .gpa = e.gpa, .text = parsed, .file = f }; } /// `]f`/`[f` and kin (helix goto_ts_object): to the next object starting @@ -244,8 +249,8 @@ fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, obj const tree = nodeTree(e, t, text) orelse return; var last = range; for (0..count) |_| { - const s = syntax.objectNext(tree.path, tree.root, text, object, forward, modal.selectionCursor(text, last)) orelse break; - const next: modal.Selection = .{ .anchor = s.from, .head = s.to }; + const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last))) orelse break; + const next: modal.Selection = .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) }; if (std.meta.eql(next, last)) break; last = next; } @@ -292,7 +297,8 @@ pub fn nodeSelect(e: Editor, t: *Text, text: []const u8, walk: modal.Normal.Node const from = @min(r.anchor, r.head); const to = @max(r.anchor, r.head); const back = r.head < r.anchor; - const n = syntax.walkNodes(tree.root, walk, from, to, modal.selectionCursor(text, r), spans[0 .. out.len - m]); + const n = syntax.walkNodes(tree.root, walk, tree.in(from), tree.in(to), tree.in(modal.selectionCursor(text, r)), spans[0 .. out.len - m]); + for (spans[0..n]) |*s| s.* = .{ .from = tree.out(s.from, false), .to = tree.out(s.to, s.to > s.from) }; if (n == 0) { out[m] = r; m += 1; |
