Skip to content

Commit

Permalink
indent guides: Use primary buffer language to determine tab size (zed…
Browse files Browse the repository at this point in the history
…-industries#12506)

When indent guides were still WIP, I thought it might be a good idea to
detect the tab size for every line individually, so we can handle files
with mixed indentations. However, while optimizing the performance of
indent guides I found that getting the language at a given anchor was
pretty expensive, therefore I only resolved the language for the first
visible row. However, this could lead to some weird flickering, where
the indent guides would use different tab sizes depending on the first
visible row (see zed-industries#12492). This can be fixed by just using the primary
buffer language size.

So as of right now indent guides cannot handle files with mixed
indentations. Im not sure if anyone actually does/expects this, but one
use case I could imagine is something like this:
User x has a svelte file, where the tab size is set to `4`. However the
svelte code uses typescript inside a script tag, which User x wants to
use a tab size of `2`. The approach used here would not work for this,
but then again I think our formatter does not even support something
like this. Im probably overcomplicating things, so let's stick with the
simple solution for now.

Release Notes:

- Fixed an issue where indent guides would use an incorrect tab size
([zed-industries#12492](zed-industries#12492)).
  • Loading branch information
bennetbo authored May 30, 2024
1 parent 1d46a52 commit 22cf73a
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3153,10 +3153,7 @@ impl BufferSnapshot {
range: Range<Anchor>,
cx: &AppContext,
) -> Vec<IndentGuide> {
fn tab_size_for_row(this: &BufferSnapshot, row: BufferRow, cx: &AppContext) -> u32 {
let language = this.language_at(Point::new(row, 0));
language_settings(language, None, cx).tab_size.get() as u32
}
let tab_size = language_settings(self.language(), None, cx).tab_size.get() as u32;

let start_row = range.start.to_point(self).row;
let end_row = range.end.to_point(self).row;
Expand All @@ -3167,9 +3164,6 @@ impl BufferSnapshot {
let mut result_vec = Vec::new();
let mut indent_stack = SmallVec::<[IndentGuide; 8]>::new();

// TODO: This should be calculated for every row but it is pretty expensive
let tab_size = tab_size_for_row(self, start_row, cx);

while let Some((first_row, mut line_indent)) = row_indents.next() {
let current_depth = indent_stack.len() as u32;

Expand Down

0 comments on commit 22cf73a

Please sign in to comment.