Skip to content

Commit

Permalink
fix: use 'tab_spaces' for size of hard tabs instead of hardcoded 4 (#143
Browse files Browse the repository at this point in the history
)

* fix: use 'tab_spaces' for size of hard tabs instead of hardcoded 4

* add comments

* update comment

* fix typo
  • Loading branch information
bram209 authored Sep 7, 2024
1 parent c71652c commit e64aba8
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions printer/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,20 @@ impl Printer {
}

fn print_indent(&mut self) {
if !self.settings.hard_tabs {
self.out.reserve(self.pending_indentation);
let (tabs, spaces) = if self.settings.hard_tabs {
// Note: we have to print the remainder in spaces, as pending_indentation represents the space of _any_ breakable token
// including break tokens with never_break set to 'true', meaning that indentation (e.g. a single space) can be printed in the middle of a line as well.
// see `print_break` for implementation details
let tabs = self.pending_indentation / self.settings.tab_spaces as usize;
let remainder = self.pending_indentation % self.settings.tab_spaces as usize;
(tabs, remainder)
} else {
let tabs = self.pending_indentation / 4;
let remaining_spaces = self.pending_indentation % 4;
self.out.reserve(tabs + remaining_spaces);
self.out.extend(iter::repeat('\t').take(tabs));
self.pending_indentation = remaining_spaces
}
(0, self.pending_indentation)
};

self.out
.extend(iter::repeat(' ').take(self.pending_indentation));
self.out.reserve(tabs + spaces);
self.out.extend(iter::repeat('\t').take(tabs));
self.out.extend(iter::repeat(' ').take(spaces));
self.pending_indentation = 0;
}
}

0 comments on commit e64aba8

Please sign in to comment.