Common ways of representing a Text Buffer in Rust

Say we are building a minimal text editor.

At the far extreme of complexity, we have rope Rope science - Introduction

At the other extreme, we have Vec<Vec<u8>>

We can improve on Vec<Vec<u8>> with

pub struct Buffer {
  cur_line: Vec<u8>,
  prev_lines: Vec<Vec<u8>>,
  post_lines_rev: Vec<Vec<u8>>

where inserting/deleting lines directly above/below current line is O(1) push/pop instead of rebuilding entire vector of all lines.

Question: are there interesting data structures between the "cur_line + prev_lines + post_lines_rev" and Xi ?

A gap buffer is used by many editors AFAIK. It doesn't behave the best with multiple cursors, but in most cases the perf hit for that is small enough to not matter.


You may want to cache some layout metrics in your data structure in addition to the raw text data. If nothing else, long lines/paragraphs may need to be wrapped and it's useful to track how much vertical space each needs.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.