Support expanding tabs

This commit is contained in:
Jeremy Soller 2024-06-06 21:01:46 -06:00
parent 56812a8348
commit 3c94352f3f
9 changed files with 82 additions and 25 deletions

View file

@ -210,6 +210,7 @@ pub struct Buffer {
redraw: bool,
wrap: Wrap,
monospace_width: Option<f32>,
tab_width: u16,
/// Scratch buffer for shaping and laying out.
scratch: ShapeBuffer,
@ -226,6 +227,7 @@ impl Clone for Buffer {
redraw: self.redraw,
wrap: self.wrap,
monospace_width: self.monospace_width,
tab_width: self.tab_width,
scratch: ShapeBuffer::default(),
}
}
@ -255,6 +257,7 @@ impl Buffer {
wrap: Wrap::WordOrGlyph,
scratch: ShapeBuffer::default(),
monospace_width: None,
tab_width: 8,
}
}
@ -294,6 +297,7 @@ impl Buffer {
self.width,
self.wrap,
self.monospace_width,
self.tab_width,
);
}
}
@ -510,7 +514,7 @@ impl Buffer {
line_i: usize,
) -> Option<&ShapeLine> {
let line = self.lines.get_mut(line_i)?;
Some(line.shape_in_buffer(&mut self.scratch, font_system))
Some(line.shape_in_buffer(&mut self.scratch, font_system, self.tab_width))
}
/// Lay out the provided line index and return the result
@ -527,6 +531,7 @@ impl Buffer {
self.width,
self.wrap,
self.monospace_width,
self.tab_width,
))
}
@ -576,6 +581,30 @@ impl Buffer {
}
}
/// Get the current `tab_width`
pub fn tab_width(&self) -> u16 {
self.tab_width
}
/// Set tab width (number of spaces between tab stops)
pub fn set_tab_width(&mut self, font_system: &mut FontSystem, tab_width: u16) {
// A tab width of 0 is not allowed
if tab_width == 0 {
return;
}
if tab_width != self.tab_width {
self.tab_width = tab_width;
// Shaping must be reset when tab width is changed
for line in self.lines.iter_mut() {
if line.text().contains('\t') {
line.reset_shaping();
}
}
self.redraw = true;
self.shape_until_scroll(font_system, false);
}
}
/// Get the current buffer dimensions (width, height)
pub fn size(&self) -> (f32, f32) {
(self.width, self.height)