1pub mod history;
5pub mod id;
6pub mod layout;
7pub mod selection;
8
9use history::{Edit, EditKind, History};
10use ropey::Rope;
11
12pub struct Buffer {
14 pub rope: Rope,
15 pub path: Option<std::path::PathBuf>,
19 pub dirty: bool,
20 pub epoch: u64,
22 pub readonly: bool,
24 pub name: Option<String>,
27 pub history: History,
30 pub replaying: bool,
32 disk_stamp: Option<std::time::SystemTime>,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum MotionShape {
41 Characterwise { inclusive: bool },
42 Linewise,
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct Range {
49 pub start: usize,
50 pub end: usize,
51 pub shape: MotionShape,
52}
53
54impl Range {
55 pub fn charwise(start: impl Into<id::ByteOffset>, end: impl Into<id::ByteOffset>) -> Self {
56 let (start, end) = (start.into().get(), end.into().get());
57 debug_assert!(start <= end);
58 Self {
59 start,
60 end,
61 shape: MotionShape::Characterwise { inclusive: false },
62 }
63 }
64 pub fn linewise(start: impl Into<id::ByteOffset>, end: impl Into<id::ByteOffset>) -> Self {
65 let (start, end) = (start.into().get(), end.into().get());
66 debug_assert!(start <= end);
67 Self {
68 start,
69 end,
70 shape: MotionShape::Linewise,
71 }
72 }
73 pub fn is_linewise(&self) -> bool {
74 matches!(self.shape, MotionShape::Linewise)
75 }
76 pub fn with_inclusive(mut self, inclusive: bool) -> Self {
78 if let MotionShape::Characterwise { inclusive: i } = &mut self.shape {
79 *i = inclusive;
80 }
81 self
82 }
83 pub fn inclusive(&self) -> bool {
84 matches!(self.shape, MotionShape::Characterwise { inclusive: true })
85 }
86 pub fn len(&self) -> usize {
88 self.end - self.start
89 }
90 pub fn is_empty(&self) -> bool {
91 self.start == self.end
92 }
93}
94
95impl Buffer {
96 pub fn from_text(text: &str) -> Self {
97 Self {
98 rope: Rope::from_str(text),
99 path: None,
100 dirty: false,
101 epoch: 0,
102 readonly: false,
103 name: None,
104 history: History::default(),
105 replaying: false,
106 disk_stamp: None,
107 }
108 }
109
110 pub fn open(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
113 let path = path.as_ref();
114 let text = match std::fs::read_to_string(path) {
115 Ok(t) => t,
116 Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
117 Err(e) => return Err(e),
118 };
119 let disk_stamp = std::fs::metadata(path).and_then(|m| m.modified()).ok();
120 Ok(Self {
121 rope: Rope::from_str(&text),
122 path: Some(path.to_path_buf()),
123 dirty: false,
124 epoch: 0,
125 readonly: false,
126 name: None,
127 history: History::default(),
128 replaying: false,
129 disk_stamp,
130 })
131 }
132
133 pub fn save(&mut self, force: bool) -> std::io::Result<()> {
137 let Some(path) = self.path.clone() else {
138 return Err(std::io::Error::new(
141 std::io::ErrorKind::NotFound,
142 "no file name — :w {path} to name it",
143 ));
144 };
145 let current = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
146 if !force && current.is_some() && current != self.disk_stamp {
147 return Err(std::io::Error::new(
148 std::io::ErrorKind::PermissionDenied,
149 "file changed on disk — :w! to force",
150 ));
151 }
152 write_atomic(std::path::Path::new(&path), &self.rope.to_string())?;
153 self.disk_stamp = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
154 self.dirty = false;
155 Ok(())
156 }
157
158 pub fn save_as(&mut self, path: &str, force: bool) -> std::io::Result<()> {
163 let target = std::path::Path::new(path);
164 if !force && target.exists() {
165 return Err(std::io::Error::new(
166 std::io::ErrorKind::PermissionDenied,
167 "file exists — :w! to overwrite",
168 ));
169 }
170 write_atomic(target, &self.rope.to_string())?;
171 self.path = Some(std::path::PathBuf::from(path));
173 self.disk_stamp = std::fs::metadata(target).and_then(|m| m.modified()).ok();
174 self.dirty = false;
175 Ok(())
176 }
177 pub fn cell_col_of(&self, offset: impl Into<id::ByteOffset>) -> u16 {
182 let offset = offset.into().get();
183 if self.len_bytes() == 0 {
184 return 0;
185 }
186 let line = self.line_of(offset);
187 let (s, e) = (self.line_start(line), self.line_end(line));
188 let text = self.rope.byte_slice(s..e).to_string();
189 let col = offset.saturating_sub(s);
190 let layout = layout::LineLayout::build(text.trim_end_matches('\n'), 8);
191 layout.cell_at_byte(col.min(layout.len_bytes))
192 }
193
194 pub fn len_bytes(&self) -> usize {
195 self.rope.len_bytes()
196 }
197 pub fn len_lines(&self) -> usize {
198 self.rope.len_lines()
199 }
200
201 pub fn last_content_line(&self) -> usize {
204 let mut l = self.len_lines().saturating_sub(1);
205 if self.len_bytes() > 0 && self.byte(self.len_bytes() - 1) == b'\n' && l > 0 {
206 l -= 1;
207 }
208 l
209 }
210
211 pub fn line_start(&self, line: impl Into<id::LineIndex>) -> usize {
213 self.rope
214 .line_to_byte(line.into().get().min(self.len_lines().saturating_sub(1)))
215 }
216
217 pub fn line_end(&self, line: impl Into<id::LineIndex>) -> usize {
219 let line = line.into().get();
220 let start = self.line_start(line);
221 let mut end = self.line_start((line + 1).min(self.len_lines().saturating_sub(1)));
222 if line + 1 >= self.len_lines() {
223 end = self.len_bytes();
224 }
225 if end > start && self.byte(end - 1) == b'\n' {
227 end -= 1;
228 }
229 end
230 }
231
232 pub fn line_of(&self, offset: impl Into<id::ByteOffset>) -> usize {
233 self.rope
234 .byte_to_line(offset.into().get().min(self.len_bytes()))
235 }
236
237 pub fn col_of(&self, offset: impl Into<id::ByteOffset>) -> usize {
239 let offset = offset.into();
240 offset.get() - self.line_start(self.line_of(offset))
241 }
242 pub fn byte(&self, offset: impl Into<id::ByteOffset>) -> u8 {
247 if self.len_bytes() == 0 {
248 return 0;
249 }
250 self.rope
251 .byte(offset.into().get().min(self.len_bytes().saturating_sub(1)))
252 }
253
254 pub fn byte_at(&self, offset: impl Into<id::ByteOffset>) -> Option<u8> {
255 let off = offset.into().get();
256 if off < self.len_bytes() {
257 Some(self.rope.byte(off))
258 } else {
259 None
260 }
261 }
262
263 pub fn is_boundary(&self, offset: impl Into<id::ByteOffset>) -> bool {
268 let off = offset.into().get();
269 if off == 0 || off == self.len_bytes() {
270 return true;
271 }
272 if off > self.len_bytes() {
273 return false;
274 }
275 match self.rope.try_byte_to_char(off) {
276 Ok(c) => self.rope.try_char_to_byte(c).is_ok_and(|b| b == off),
277 Err(_) => false,
278 }
279 }
280
281 pub fn clamp_boundary(&self, offset: impl Into<id::ByteOffset>) -> usize {
284 let mut offset = offset.into().get().min(self.len_bytes());
285 while offset > 0 && !self.is_boundary(offset) {
286 offset -= 1;
287 }
288 offset
289 }
290
291 pub fn ceil_boundary(&self, offset: impl Into<id::ByteOffset>) -> usize {
296 let mut offset = offset.into().get().min(self.len_bytes());
297 while offset < self.len_bytes() && !self.is_boundary(offset) {
298 offset += 1;
299 }
300 offset
301 }
302
303 pub fn slice_string(&self, range: Range) -> String {
306 let start = range.start.min(self.len_bytes());
307 let end = range.end.min(self.len_bytes());
308 self.rope.byte_slice(start..end.max(start)).to_string()
309 }
310
311 pub fn apply_history(&mut self, ops: Vec<Edit>) {
313 self.replaying = true;
314 for op in ops {
315 match op.kind {
316 EditKind::Insert => {
317 let at = self.clamp_boundary(op.at.min(self.len_bytes()));
318 self.rope.insert(self.rope.byte_to_char(at), &op.text);
319 }
320 EditKind::Delete => {
321 let end = self.clamp_boundary((op.at + op.text.len()).min(self.len_bytes()));
324 let start = self.clamp_boundary(op.at.min(end));
325 if start < end {
326 self.rope
327 .remove(self.rope.byte_to_char(start)..self.rope.byte_to_char(end));
328 }
329 }
330 }
331 }
332 self.replaying = false;
333 self.dirty = true;
334 self.epoch += 1;
335 }
336
337 pub fn replace_all(&mut self, text: &str) {
341 if self.readonly {
342 return;
343 }
344 self.replace_all_system(text);
345 }
346
347 pub fn replace_all_system(&mut self, text: &str) {
351 self.rope = Rope::from_str(text);
352 self.epoch += 1;
353 }
354
355 pub fn delete(&mut self, range: Range) -> String {
359 if self.readonly && !self.replaying {
360 return String::new();
361 }
362 let start = self.clamp_boundary(range.start.min(self.len_bytes()));
364 let end = self.clamp_boundary(range.end.min(self.len_bytes()));
365 if start >= end {
366 return String::new();
367 }
368 let text = self.rope.byte_slice(start..end).to_string();
369 let cstart = self.rope.byte_to_char(start);
371 let cend = self.rope.byte_to_char(end);
372 self.rope.remove(cstart..cend);
373 self.dirty = true;
374 self.epoch += 1;
375 if !self.replaying && !self.readonly {
376 self.history.record(
377 Edit {
378 at: range.start,
379 text: text.clone(),
380 kind: EditKind::Insert,
381 },
382 Edit {
383 at: range.start,
384 text: text.clone(),
385 kind: EditKind::Delete,
386 },
387 );
388 }
389 text
390 }
391
392 pub fn insert(&mut self, at: impl Into<id::ByteOffset>, text: &str) {
393 if self.readonly && !self.replaying {
394 return;
395 }
396 let at = self.clamp_boundary(at);
397 self.rope.insert(self.rope.byte_to_char(at), text);
398 self.dirty = true;
399 self.epoch += 1;
400 if !self.replaying && !self.readonly {
401 self.history.record(
402 Edit {
403 at,
404 text: text.into(),
405 kind: EditKind::Delete,
406 },
407 Edit {
408 at,
409 text: text.into(),
410 kind: EditKind::Insert,
411 },
412 );
413 }
414 }
415
416 pub fn line_text(&self, line: impl Into<id::LineIndex>) -> String {
417 let line = line.into().get();
418 let start = self.line_start(line);
419 let end = self.line_end(line);
420 self.rope.byte_slice(start..end).to_string()
421 }
422}
423
424fn write_atomic(target: &std::path::Path, contents: &str) -> std::io::Result<()> {
427 let tmp = target.with_file_name(format!(
428 ".strop-tmp-{}-{}",
429 std::process::id(),
430 target.file_name().and_then(|n| n.to_str()).unwrap_or("x")
431 ));
432 std::fs::write(&tmp, contents)?;
433 if let Ok(meta) = std::fs::metadata(target) {
434 let _ = std::fs::set_permissions(&tmp, meta.permissions());
436 }
437 std::fs::rename(&tmp, target)
438}
439
440#[cfg(test)]
441mod safety_tests {
442 use super::*;
443
444 #[test]
445 fn save_refuses_external_change_unless_forced() {
446 let dir = tempfile::tempdir().unwrap();
447 let f = dir.path().join("f.txt");
448 std::fs::write(&f, "original\n").unwrap();
449 let mut b = Buffer::open(f.to_str().unwrap()).unwrap();
450 b.insert(id::ByteOffset::new(0), "mine ");
451 std::thread::sleep(std::time::Duration::from_millis(5));
453 std::fs::write(&f, "theirs\n").unwrap();
454 let err = b.save(false).unwrap_err();
455 assert!(err.to_string().contains("changed on disk"));
456 assert_eq!(std::fs::read_to_string(&f).unwrap(), "theirs\n");
457 b.save(true).unwrap(); assert_eq!(std::fs::read_to_string(&f).unwrap(), "mine original\n");
459 assert!(!b.dirty);
460 }
461
462 #[test]
463 fn save_is_atomic_and_keeps_permissions() {
464 use std::os::unix::fs::PermissionsExt;
465 let dir = tempfile::tempdir().unwrap();
466 let f = dir.path().join("x.sh");
467 std::fs::write(&f, "#!/bin/sh\n").unwrap();
468 std::fs::set_permissions(&f, std::fs::Permissions::from_mode(0o750)).unwrap();
469 let mut b = Buffer::open(f.to_str().unwrap()).unwrap();
470 b.insert(id::ByteOffset::new(b.len_bytes()), "echo hi\n");
471 b.save(false).unwrap();
472 assert_eq!(std::fs::read_to_string(&f).unwrap(), "#!/bin/sh\necho hi\n");
473 let mode = std::fs::metadata(&f).unwrap().permissions().mode() & 0o777;
474 assert_eq!(mode, 0o750, "permissions survive the swap");
475 assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1);
477 }
478
479 #[test]
480 fn readonly_refuses_mutation_at_the_boundary() {
481 let mut b = Buffer::from_text("abc\n");
483 b.readonly = true;
484 b.insert(id::ByteOffset::new(0), "nope");
485 let gone = b.delete(Range::charwise(0, 2));
486 assert_eq!(gone, "");
487 assert_eq!(b.rope.to_string(), "abc\n", "untouched");
488 b.replace_all_system("gen\n");
490 assert_eq!(b.rope.to_string(), "gen\n");
491 }
492 #[test]
493 fn non_utf8_filename_opens_and_roundtrips() {
494 use std::os::unix::ffi::OsStrExt;
497 let dir = tempfile::tempdir().unwrap();
498 let weird = dir
499 .path()
500 .join(std::ffi::OsStr::from_bytes(b"weird-\xff.rs"));
501 std::fs::write(&weird, "fn main() {}\n").unwrap();
502 let mut b = Buffer::open(&weird).unwrap();
503 assert_eq!(b.path.as_deref(), Some(weird.as_path()));
504 b.insert(0, "// x\n");
505 b.save(false).unwrap();
506 assert!(std::fs::read_to_string(&weird).unwrap().starts_with("// x"));
507 }
508}