🎨 Make expand_tabs and collapse_tabs instance methods on TabSnapshot

This commit is contained in:
Max Brunsfeld 2023-03-28 14:10:43 -07:00
parent 35b2aceffb
commit c23feeab3a
2 changed files with 21 additions and 27 deletions

View file

@ -19,7 +19,7 @@ use std::{any::TypeId, fmt::Debug, num::NonZeroU32, ops::Range, sync::Arc};
pub use suggestion_map::Suggestion; pub use suggestion_map::Suggestion;
use suggestion_map::SuggestionMap; use suggestion_map::SuggestionMap;
use sum_tree::{Bias, TreeMap}; use sum_tree::{Bias, TreeMap};
use tab_map::{TabMap, TabSnapshot}; use tab_map::TabMap;
use wrap_map::WrapMap; use wrap_map::WrapMap;
pub use block_map::{ pub use block_map::{
@ -637,7 +637,7 @@ impl DisplaySnapshot {
let chars = buffer.chars_at(Point::new(range.start.row, 0)); let chars = buffer.chars_at(Point::new(range.start.row, 0));
let mut is_blank = false; let mut is_blank = false;
let indent_size = TabSnapshot::expand_tabs( let indent_size = self.tab_snapshot.expand_tabs(
chars.take_while(|c| { chars.take_while(|c| {
if *c == ' ' || *c == '\t' { if *c == ' ' || *c == '\t' {
true true
@ -649,7 +649,6 @@ impl DisplaySnapshot {
} }
}), }),
buffer.line_len(buffer_row) as usize, // Never collapse buffer.line_len(buffer_row) as usize, // Never collapse
self.tab_snapshot.tab_size,
); );
(indent_size as u32, is_blank) (indent_size as u32, is_blank)

View file

@ -245,7 +245,7 @@ impl TabSnapshot {
let chars = self let chars = self
.suggestion_snapshot .suggestion_snapshot
.chars_at(SuggestionPoint::new(input.row(), 0)); .chars_at(SuggestionPoint::new(input.row(), 0));
let expanded = Self::expand_tabs(chars, input.column() as usize, self.tab_size); let expanded = self.expand_tabs(chars, input.column() as usize);
TabPoint::new(input.row(), expanded as u32) TabPoint::new(input.row(), expanded as u32)
} }
@ -259,7 +259,7 @@ impl TabSnapshot {
.chars_at(SuggestionPoint::new(output.row(), 0)); .chars_at(SuggestionPoint::new(output.row(), 0));
let expanded = output.column() as usize; let expanded = output.column() as usize;
let (collapsed, expanded_char_column, to_next_stop) = let (collapsed, expanded_char_column, to_next_stop) =
Self::collapse_tabs(chars, expanded, bias, self.tab_size); self.collapse_tabs(chars, expanded, bias);
( (
SuggestionPoint::new(output.row(), collapsed as u32), SuggestionPoint::new(output.row(), collapsed as u32),
expanded_char_column, expanded_char_column,
@ -282,11 +282,9 @@ impl TabSnapshot {
fold_point.to_buffer_point(&self.suggestion_snapshot.fold_snapshot) fold_point.to_buffer_point(&self.suggestion_snapshot.fold_snapshot)
} }
pub fn expand_tabs( pub fn expand_tabs(&self, chars: impl Iterator<Item = char>, column: usize) -> usize {
chars: impl Iterator<Item = char>, let tab_size = self.tab_size.get() as usize;
column: usize,
tab_size: NonZeroU32,
) -> usize {
let mut expanded_chars = 0; let mut expanded_chars = 0;
let mut expanded_bytes = 0; let mut expanded_bytes = 0;
let mut collapsed_bytes = 0; let mut collapsed_bytes = 0;
@ -295,7 +293,6 @@ impl TabSnapshot {
break; break;
} }
if c == '\t' { if c == '\t' {
let tab_size = tab_size.get() as usize;
let tab_len = tab_size - expanded_chars % tab_size; let tab_len = tab_size - expanded_chars % tab_size;
expanded_bytes += tab_len; expanded_bytes += tab_len;
expanded_chars += tab_len; expanded_chars += tab_len;
@ -309,11 +306,13 @@ impl TabSnapshot {
} }
fn collapse_tabs( fn collapse_tabs(
&self,
chars: impl Iterator<Item = char>, chars: impl Iterator<Item = char>,
column: usize, column: usize,
bias: Bias, bias: Bias,
tab_size: NonZeroU32,
) -> (usize, usize, usize) { ) -> (usize, usize, usize) {
let tab_size = self.tab_size.get() as usize;
let mut expanded_bytes = 0; let mut expanded_bytes = 0;
let mut expanded_chars = 0; let mut expanded_chars = 0;
let mut collapsed_bytes = 0; let mut collapsed_bytes = 0;
@ -323,7 +322,6 @@ impl TabSnapshot {
} }
if c == '\t' { if c == '\t' {
let tab_size = tab_size.get() as usize;
let tab_len = tab_size - (expanded_chars % tab_size); let tab_len = tab_size - (expanded_chars % tab_size);
expanded_chars += tab_len; expanded_chars += tab_len;
expanded_bytes += tab_len; expanded_bytes += tab_len;
@ -508,20 +506,17 @@ mod tests {
}; };
use rand::{prelude::StdRng, Rng}; use rand::{prelude::StdRng, Rng};
#[test] #[gpui::test]
fn test_expand_tabs() { fn test_expand_tabs(cx: &mut gpui::MutableAppContext) {
assert_eq!( let buffer = MultiBuffer::build_simple("", cx);
TabSnapshot::expand_tabs("\t".chars(), 0, 4.try_into().unwrap()), let buffer_snapshot = buffer.read(cx).snapshot(cx);
0 let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
); let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
assert_eq!( let (_, tabs_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
TabSnapshot::expand_tabs("\t".chars(), 1, 4.try_into().unwrap()),
4 assert_eq!(tabs_snapshot.expand_tabs("\t".chars(), 0), 0);
); assert_eq!(tabs_snapshot.expand_tabs("\t".chars(), 1), 4);
assert_eq!( assert_eq!(tabs_snapshot.expand_tabs("\ta".chars(), 2), 5);
TabSnapshot::expand_tabs("\ta".chars(), 2, 4.try_into().unwrap()),
5
);
} }
#[gpui::test(iterations = 100)] #[gpui::test(iterations = 100)]