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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! The document. SPEC.md §4.
//!
//! Rope-backed, char-indexed. Line endings are detected on load, normalized to
//! `\n` in the rope, and reapplied on save — the file round-trips byte for byte
//! unless the user actually edited it.
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use anyhow::Result;
use ropey::Rope;
use super::cursor::Cursor;
use super::history::History;
use crate::fs::open;
use crate::fs::save::{self, SavePolicy, SaveOptions};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum LineEnding {
Lf,
Crlf,
}
/// Which styling ruleset applies. Decided from the file extension on open.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Syntax {
/// Full Markdown: `.md`, `.markdown`
Markdown,
/// Reduced ruleset — no wiki links, no tags: `.txt`
PlainProse,
/// No styling at all.
None,
}
pub struct Buffer {
pub rope: Rope,
pub cursor: Cursor,
pub history: History,
pub path: Option<PathBuf>,
pub syntax: Syntax,
pub line_ending: LineEnding,
/// Whether the file on disk ended with a newline. Preserved on save so we
/// never silently append one.
pub final_newline: bool,
/// mtime at open/save. Compared before writing to catch external edits.
pub disk_mtime: Option<SystemTime>,
/// Unsaved changes present. Derived from `saved_revision`, never set
/// directly, so undoing back to the last saved state clears it.
pub modified: bool,
/// The history position the file on disk holds. Compared with
/// `history.state()`, so undoing back to it clears `modified` again.
pub saved_state: u64,
/// Bumped on every mutation. All render caches key off this.
pub revision: u64,
/// The last word count taken, and the revision it was taken at.
///
/// `[status] show` lists `words` by DEFAULT, and the status line is drawn
/// every frame — so without this, moving the cursor one line re-counted
/// every word in the document. A `Cell` rather than a field set by the
/// edit primitives, because counting is worth doing lazily: a session that
/// never shows the segment never pays for it at all.
words: std::cell::Cell<Option<(u64, usize)>>,
/// Lowest line touched since a consumer last cleared it — the seam that
/// lets `BlockCache::invalidate_from` rescan from the edit instead of
/// rebuilding the document. `None` means "nothing since the last take";
/// after an undo/redo or a multi-part edit it holds the MINIMUM line, so a
/// single rescan from there covers every change.
pub dirty_line: Option<usize>,
/// The file changed on disk while this buffer had unsaved work.
///
/// Only ever true for a MODIFIED buffer: a clean one is reloaded the moment
/// the change is noticed, so there is no lasting state to hold. It is
/// therefore not "is the buffer stale" but "the two diverged and only you
/// can say which wins" — `:w!` keeps yours, `:revert!` takes theirs.
pub conflict: bool,
/// Char ranges that refuse edits. Always EMPTY, and deliberately kept.
///
/// It was built for transclusion, which then did not need it: an embed
/// occupies exactly one rope line and the active line always renders raw,
/// so the cursor can never be inside expanded content and there is nothing
/// to protect (SPEC.md §14.5, `transclude/preview.rs`). It stays because it
/// costs one branch in `edit.rs` and is the escape hatch if embeds ever
/// become editable — auditing every mutation site later would not be.
pub readonly_ranges: Vec<std::ops::Range<usize>>,
}
impl Buffer {
pub fn empty() -> Self {
Buffer {
rope: Rope::new(),
cursor: Cursor::default(),
history: History::default(),
path: None,
syntax: Syntax::Markdown,
line_ending: LineEnding::Lf,
final_newline: true,
disk_mtime: None,
modified: false,
saved_state: 0,
revision: 0,
dirty_line: None,
conflict: false,
readonly_ranges: Vec::new(),
words: std::cell::Cell::new(None),
}
}
/// UTF-8 only. Invalid bytes are refused rather than lossily converted —
/// we are about to let the user overwrite this file. SPEC.md §10.
///
/// A path that does not exist is not an error: it opens an empty buffer
/// that will create the file on first `:w`.
pub fn open(path: PathBuf, plain_text_exts: &[String]) -> Result<Self> {
if !path.exists() {
let mut b = Buffer::empty();
b.syntax = open::syntax_for(&path, plain_text_exts);
b.path = Some(path);
return Ok(b);
}
let loaded = open::load(&path, plain_text_exts)?;
let mtime = std::fs::metadata(&path).ok().and_then(|m| m.modified().ok());
Ok(Buffer {
rope: loaded.rope,
cursor: Cursor::default(),
history: History::default(),
path: Some(path),
syntax: loaded.syntax,
line_ending: loaded.line_ending,
final_newline: loaded.final_newline,
disk_mtime: mtime,
modified: false,
saved_state: 0,
revision: 0,
dirty_line: None,
conflict: false,
readonly_ranges: Vec::new(),
words: std::cell::Cell::new(None),
})
}
/// Atomic: temp file in the same directory, fsync, rename. SPEC.md §10.
///
/// `force` is `:w!` — write even though the file changed underneath us.
pub fn save(&mut self, policy: SavePolicy, force: bool) -> Result<()> {
let path = match &self.path {
Some(p) => p.clone(),
None => anyhow::bail!("no filename — use :w <path>"),
};
self.write_to(&path, policy, force)
}
pub fn save_as(&mut self, path: PathBuf, policy: SavePolicy, force: bool) -> Result<()> {
self.write_to(&path, policy, force)?;
self.path = Some(path);
Ok(())
}
fn write_to(&mut self, path: &Path, policy: SavePolicy, force: bool) -> Result<()> {
// Refuse to clobber a file something else has written since we read it.
// Only meaningful when this IS the file we read: `:w other.md` records
// an mtime for a different path, and comparing the two would refuse a
// perfectly ordinary write. SPEC.md §10.
let same_file = self.path.as_deref().is_some_and(|ours| same_path(ours, path));
if !force && same_file && save::changed_externally(path, self.disk_mtime) {
anyhow::bail!("{} changed on disk — :w! to overwrite", name_of(path));
}
// Nor one that is not ours at all: `:w notes.md` over an existing
// notes.md would destroy it. vim's E13, and `:w!` the same way past.
if !force && !same_file && path.exists() {
anyhow::bail!("{} exists — :w! to overwrite", name_of(path));
}
let opts = SaveOptions {
line_ending: self.line_ending,
// `preserve` answers with what the file arrived with; the other two
// overrule it. This is the only place that distinction is made.
final_newline: policy.final_newline.resolve(self.final_newline),
trim_trailing_whitespace: policy.trim_trailing_whitespace,
};
let mtime = save::write_atomic(path, &self.rope, &opts)?;
self.disk_mtime = Some(mtime);
// Close any in-flight step first, so what was written corresponds to a
// sealed position and the next keystroke starts a new one.
self.history.split();
self.saved_state = self.history.state();
self.modified = false;
// The write settled whichever divergence there was: this text IS the
// file now. `:w!` is how a reader says "keep mine", so the marker has
// to come down here rather than needing its own command.
self.conflict = false;
Ok(())
}
/// Re-read the file from disk, replacing the buffer's text.
///
/// `Ok(false)` means the file's mtime moved but its CONTENT did not — a
/// `touch`, or a writer that rewrote identical bytes. The new mtime is
/// absorbed and nothing else happens, because throwing away the cursor to
/// replace text with itself is a worse outcome than the stale mtime was.
///
/// Two things make this safe to do behind the user's back (`App::check_disk`
/// only ever calls it on a CLEAN buffer):
///
/// - It reads through `fs::open::load`, the same function `Buffer::open`
/// uses. Line-ending detection, the `final_newline` flag and the syntax
/// choice therefore cannot drift from what opening the file would have
/// given — a second read path would drift, and this repo has that scar
/// already (preview and export once had two and the preview lied).
/// - The replacement is ONE undo step, so `u` gives back the pre-reload
/// text and lands the cursor where the step began. That is what makes an
/// automatic reload reversible rather than merely fast.
///
/// Afterwards the buffer matches disk, so it takes the same three lines a
/// save does: seal the step, record it as the saved state, clear `modified`.
/// Undoing the reload therefore marks the buffer modified again, which is
/// correct — you are once more holding something the file does not have.
pub fn reload(&mut self, plain_text_exts: &[String]) -> Result<bool> {
let path = match &self.path {
Some(p) => p.clone(),
None => anyhow::bail!("no file to reload"),
};
let loaded = open::load(&path, plain_text_exts)?;
let mtime = std::fs::metadata(&path).ok().and_then(|m| m.modified().ok());
if loaded.rope == self.rope {
// Same text, but perhaps not the same bytes: a tool that turned
// LF into CRLF leaves the rope identical, and keeping the old
// ending would turn it back on the next save.
self.line_ending = loaded.line_ending;
self.final_newline = loaded.final_newline;
self.disk_mtime = mtime;
self.conflict = false;
return Ok(false);
}
self.replace_all(&loaded.rope.to_string());
self.line_ending = loaded.line_ending;
self.final_newline = loaded.final_newline;
self.syntax = loaded.syntax;
self.disk_mtime = mtime;
self.saved_state = self.history.state();
self.modified = false;
self.conflict = false;
Ok(true)
}
/// Replace the whole document, as ONE undo step.
///
/// Shared by `reload` and the `:diff` merge because both replace everything
/// and both must be a single `u` — two copies of this would be two chances
/// for one of them to stop grouping, and the group is the whole reason an
/// automatic reload is safe to do behind the reader's back.
///
/// The cursor is left where it was; callers clamp (`App::sync_after_input`
/// does it for every input path anyway), and the group records the
/// pre-replacement cursor so undo lands where the reader was.
pub fn replace_all(&mut self, text: &str) {
self.history.begin_group(Some(self.cursor));
let len = self.rope.len_chars();
if len > 0 {
self.delete_chars(0, len);
}
if !text.is_empty() {
self.insert_str(Cursor::new(0, 0), text);
}
self.history.end_group();
}
/// Recompute `modified` from the revision the disk holds. Undoing back to
/// the saved state marks the buffer clean again, exactly as vim does.
pub fn sync_modified(&mut self) {
self.modified = self.history.state() != self.saved_state;
}
pub fn line_count(&self) -> usize {
self.rope.len_lines()
}
/// The last line the cursor can rest on outside Insert mode — vim's last
/// line.
///
/// Ropey counts the empty string after a trailing newline as a line of its
/// own, so `line_count() - 1` is one past the end for any file ending in
/// `\n`. That line is not part of the document: saving writes the same
/// bytes whether or not it is there. Insert mode may still sit on it (an
/// Enter at the end of the text lands there), so `line_count` keeps
/// ropey's meaning and this is what motions and clamps use instead.
pub fn last_line(&self) -> usize {
let n = self.rope.len_lines();
let len = self.rope.len_chars();
if n > 1 && self.rope.char(len - 1) == '\n' {
n - 2
} else {
n - 1
}
}
/// Line content WITHOUT its trailing newline.
pub fn line_text(&self, line: usize) -> String {
if line >= self.rope.len_lines() {
return String::new();
}
let slice = self.rope.line(line);
let mut s = slice.to_string();
if s.ends_with('\n') {
s.pop();
if s.ends_with('\r') {
s.pop();
}
}
s
}
/// Char count of a line, excluding its trailing newline.
pub fn line_len(&self, line: usize) -> usize {
if line >= self.rope.len_lines() {
return 0;
}
let slice = self.rope.line(line);
let mut n = slice.len_chars();
if n > 0 && slice.char(n - 1) == '\n' {
n -= 1;
if n > 0 && slice.char(n - 1) == '\r' {
n -= 1;
}
}
n
}
/// Absolute char index of a cursor position.
pub fn char_index(&self, at: Cursor) -> usize {
let line = at.line.min(self.rope.len_lines().saturating_sub(1));
let base = self.rope.line_to_char(line);
(base + at.col.min(self.line_len(line))).min(self.rope.len_chars())
}
/// Word count over the whole buffer, for the status line.
pub fn word_count(&self) -> usize {
if let Some((rev, n)) = self.words.get() {
if rev == self.revision {
return n;
}
}
let mut n = 0;
let mut in_word = false;
for c in self.rope.chars() {
if c.is_whitespace() {
in_word = false;
} else if !in_word {
in_word = true;
n += 1;
}
}
self.words.set(Some((self.revision, n)));
n
}
pub fn display_name(&self) -> String {
match &self.path {
Some(p) => name_of(p),
None => "[no name]".to_string(),
}
}
}
/// A path as the status line and error messages name it: the file name alone,
/// falling back to the whole path when there isn't one.
fn name_of(path: &Path) -> String {
path.file_name()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| path.to_string_lossy().into_owned())
}
/// Whether two spellings name one file: `notes.md` and `./notes.md` do, and
/// so does a symlink and what it points at.
fn same_path(a: &Path, b: &Path) -> bool {
a == b
|| matches!(
(std::fs::canonicalize(a), std::fs::canonicalize(b)),
(Ok(x), Ok(y)) if x == y
)
}
#[cfg(test)]
mod tests {
use super::*;
/// Words are counted on runs of non-whitespace, so punctuation and CJK
/// come out the way a writer expects to be told.
#[test]
fn words_are_runs_of_non_whitespace() {
let mut b = Buffer::empty();
b.rope = Rope::from_str("one two\n\nthree four\tfive\n");
assert_eq!(b.word_count(), 5);
b.rope = Rope::from_str("");
b.revision += 1;
assert_eq!(b.word_count(), 0);
b.rope = Rope::from_str(" \n \t \n");
b.revision += 1;
assert_eq!(b.word_count(), 0, "whitespace alone is no words");
}
/// The count is memoized against the revision. Without it the status line
/// re-counted the whole document on every frame, including the frames where
/// only the cursor moved.
#[test]
fn the_word_count_is_taken_once_per_revision() {
let mut b = Buffer::empty();
b.rope = Rope::from_str("alpha beta gamma\n");
assert_eq!(b.word_count(), 3);
// Swap the text WITHOUT bumping the revision: the memo is expected to
// answer, which is what proves it answered rather than re-counted.
b.rope = Rope::from_str("one two three four five\n");
assert_eq!(b.word_count(), 3, "same revision, memoized answer");
b.revision += 1;
assert_eq!(b.word_count(), 5, "a new revision re-counts");
}
/// An edit through the primitives bumps the revision, so the count follows
/// the text without anyone having to invalidate it by hand.
#[test]
fn editing_moves_the_count_along() {
let mut b = Buffer::empty();
b.insert_str(Cursor::new(0, 0), "one two\n");
assert_eq!(b.word_count(), 2);
b.insert_str(Cursor::new(0, 7), " three");
assert_eq!(b.word_count(), 3);
let idx = b.rope.len_chars();
b.delete_chars(idx.saturating_sub(6), idx);
assert_eq!(b.word_count(), 2);
}
/// `line_len` and `line_text` agree, and both drop the terminator — the
/// pair every motion and every render measurement is built on.
#[test]
fn a_lines_length_excludes_its_terminator() {
let mut b = Buffer::empty();
b.rope = Rope::from_str("abc\nde\n");
assert_eq!(b.line_text(0), "abc");
assert_eq!(b.line_len(0), 3);
assert_eq!(b.line_text(1), "de");
assert_eq!(b.line_len(1), 2);
// Past the end is empty rather than a panic: motions clamp against it.
assert_eq!(b.line_text(99), "");
assert_eq!(b.line_len(99), 0);
}
/// A trailing newline ends the last line rather than opening another, so
/// `last_line` is one short of ropey's count exactly when the text ends
/// in `\n` — and never below line 0.
#[test]
fn the_last_line_ignores_the_empty_string_after_a_trailing_newline() {
let mut b = Buffer::empty();
let cases = [
("", 0),
("\n", 0),
("one", 0),
("one\ntwo", 1),
("one\ntwo\n", 1),
("one\n\n", 1),
];
for (text, last) in cases {
b.rope = Rope::from_str(text);
assert_eq!(b.last_line(), last, "{text:?}");
}
}
/// `char_index` clamps on both axes. Every edit primitive goes through it,
/// so an out-of-range cursor must land somewhere valid rather than panic
/// inside ropey.
#[test]
fn char_index_clamps_rather_than_panics() {
let mut b = Buffer::empty();
b.rope = Rope::from_str("abc\nde\n");
assert_eq!(b.char_index(Cursor::new(0, 0)), 0);
assert_eq!(b.char_index(Cursor::new(1, 1)), 5);
assert_eq!(b.char_index(Cursor::new(0, 99)), 3, "column clamps to the line");
assert!(b.char_index(Cursor::new(99, 99)) <= b.rope.len_chars());
}
}