summaryrefslogtreecommitdiff
path: root/core/Editor.zig
blob: 9e490c41939fc7147ac869cc93428a10ac456bd7 (plain) (blame)
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
//! What an edit of a text runs with (normal.zig, edit.zig): the allocators,
//! the registers, the register the running command names, the replay of a
//! key across the ranges, the text an edit made that nothing kept, and the
//! core the text's pane is in. Gathered for each call from where pardes
//! keeps them (`Pardes.editor`); the library gathers it from the value a
//! sketch stores (core/lib_editor.zig). Nothing here is kept between calls.
const std = @import("std");
const Registers = @import("Registers.zig");

/// The library's build (build.zig `library`): a text with no pane and no
/// core, its characters its own. Where an edit reaches a pane it branches
/// 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");

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. `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,
/// What an edit makes and drops: the update's scratch.
scratch: std.mem.Allocator,
registers: *Registers,
/// The register the running normal command names (`"<reg>`); 0 for the
/// default one.
register: *u21,
/// While a key is replayed across the ranges (normal.replaySels): whether
/// this is the first pass, whether a pass stopped the replay, which range
/// this pass is, in document order, and how many there are; outside a
/// replay, the one range and its first pass. A register value belongs to
/// its range by `multi_index`.
multi_first: *bool,
multi_stop: *bool,
multi_index: *usize,
multi_count: *usize,
/// An edited text nothing kept (a tag's shown text): see
/// edit.retireEditText.
retired_edit: *?[]u8,
/// The core the text's pane is in: what an edit reaches past its text with.
p: if (lib) void else *Pardes,
/// A tab's width in cells (the core's `settings.tab`; the library's, the
/// default).
tab: u16,
/// The library's: an edit failed (its allocator ran out) and the key is
/// refused.
failed: if (lib) *bool else void,
/// The library's: the parse its caller handed in, which the tree-sitter
/// walks read; null, and they are the caller's (`textRole`).
syntax: if (lib) ?*Syntax else void,

pub fn of(p: *Pardes) Editor {
    return .{
        .gpa = p.gpa,
        .scratch = p.scratch.allocator(),
        .registers = &p.registers,
        .register = &p.view().register,
        .multi_first = &p.multi_first,
        .multi_stop = &p.multi_stop,
        .multi_index = &p.multi_index,
        .multi_count = &p.multi_count,
        .retired_edit = &p.retired_edit,
        .p = p,
        .tab = p.settings.tab,
        .failed = {},
        .syntax = {},
    };
}

/// An edit that failed, said on the message row of the pane the keyboard
/// is in.
pub fn reportError(e: Editor, err: anyerror) void {
    if (comptime lib) {
        e.failed.* = true;
        return;
    }
    e.p.reportError(.at(e.p.view().active), "edit", err);
}