Indent guides (#11503)

Builds on top of existing work from #2249, but here's a showcase:


https://github.com/zed-industries/zed/assets/53836821/4b346965-6654-496c-b379-75425d9b493f

TODO:
- [x] handle line wrapping
- [x] implement handling in multibuffer (crashes currently)
- [x] add configuration option
- [x] new theme properties? What colors to use?
- [x] Possibly support indents with different colors or background
colors
- [x] investigate edge cases (e.g. indent guides and folds continue on
empty lines even if the next indent is different)
- [x] add more tests (also test `find_active_indent_index`)
- [x] docs (will do in a follow up PR)
- [x] benchmark performance impact

Release Notes:

- Added indent guides
([#5373](https://github.com/zed-industries/zed/issues/5373))

---------

Co-authored-by: Nate Butler <1714999+iamnbutler@users.noreply.github.com>
Co-authored-by: Remco <djsmits12@gmail.com>
This commit is contained in:
Bennet Bo Fenner 2024-05-23 15:50:59 +02:00 committed by GitHub
parent 3eb0418bda
commit feea607bac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 1705 additions and 65 deletions

View file

@ -1865,6 +1865,87 @@ impl BufferSnapshot {
(row_end_offset - row_start_offset) as u32
}
pub fn line_indents_in_row_range(
&self,
row_range: Range<u32>,
) -> impl Iterator<Item = (u32, u32, bool)> + '_ {
let start = Point::new(row_range.start, 0).to_offset(self);
let end = Point::new(row_range.end, 0).to_offset(self);
let mut lines = self.as_rope().chunks_in_range(start..end).lines();
let mut row = row_range.start;
std::iter::from_fn(move || {
if let Some(line) = lines.next() {
let mut indent_size = 0;
let mut is_blank = true;
for c in line.chars() {
is_blank = false;
if c == ' ' || c == '\t' {
indent_size += 1;
} else {
break;
}
}
row += 1;
Some((row - 1, indent_size, is_blank))
} else {
None
}
})
}
pub fn reversed_line_indents_in_row_range(
&self,
row_range: Range<u32>,
) -> impl Iterator<Item = (u32, u32, bool)> + '_ {
let start = Point::new(row_range.start, 0).to_offset(self);
let end = Point::new(row_range.end, 0)
.to_offset(self)
.saturating_sub(1);
let mut lines = self.as_rope().reversed_chunks_in_range(start..end).lines();
let mut row = row_range.end;
std::iter::from_fn(move || {
if let Some(line) = lines.next() {
let mut indent_size = 0;
let mut is_blank = true;
for c in line.chars() {
is_blank = false;
if c == ' ' || c == '\t' {
indent_size += 1;
} else {
break;
}
}
row = row.saturating_sub(1);
Some((row, indent_size, is_blank))
} else {
None
}
})
}
pub fn line_indent_for_row(&self, row: u32) -> (u32, bool) {
let mut indent_size = 0;
let mut is_blank = false;
for c in self.chars_at(Point::new(row, 0)) {
if c == ' ' || c == '\t' {
indent_size += 1;
} else {
if c == '\n' {
is_blank = true;
}
break;
}
}
(indent_size, is_blank)
}
pub fn is_line_blank(&self, row: u32) -> bool {
self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
.all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())