3 use std::ops::{Add, Sub};
7 #[derive(Copy, Clone, Debug)]
8 pub(crate) struct Indent {
9 // Width of the block indent, in characters. Must be a multiple of
10 // Config::tab_spaces.
11 pub(crate) block_indent: usize,
12 // Alignment in characters.
13 pub(crate) alignment: usize,
16 // INDENT_BUFFER.len() = 81
17 const INDENT_BUFFER_LEN: usize = 80;
18 const INDENT_BUFFER: &str =
22 pub(crate) fn new(block_indent: usize, alignment: usize) -> Indent {
29 pub(crate) fn from_width(config: &Config, width: usize) -> Indent {
30 if config.hard_tabs() {
31 let tab_num = width / config.tab_spaces();
32 let alignment = width % config.tab_spaces();
33 Indent::new(config.tab_spaces() * tab_num, alignment)
39 pub(crate) fn empty() -> Indent {
43 pub(crate) fn block_only(&self) -> Indent {
45 block_indent: self.block_indent,
50 pub(crate) fn block_indent(mut self, config: &Config) -> Indent {
51 self.block_indent += config.tab_spaces();
55 pub(crate) fn block_unindent(mut self, config: &Config) -> Indent {
56 if self.block_indent < config.tab_spaces() {
57 Indent::new(self.block_indent, 0)
59 self.block_indent -= config.tab_spaces();
64 pub(crate) fn width(&self) -> usize {
65 self.block_indent + self.alignment
68 pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> {
69 self.to_string_inner(config, 1)
72 pub(crate) fn to_string_with_newline(&self, config: &Config) -> Cow<'static, str> {
73 self.to_string_inner(config, 0)
76 fn to_string_inner(&self, config: &Config, offset: usize) -> Cow<'static, str> {
77 let (num_tabs, num_spaces) = if config.hard_tabs() {
78 (self.block_indent / config.tab_spaces(), self.alignment)
82 let num_chars = num_tabs + num_spaces;
83 if num_tabs == 0 && num_chars + offset <= INDENT_BUFFER_LEN {
84 Cow::from(&INDENT_BUFFER[offset..=num_chars])
86 let mut indent = String::with_capacity(num_chars + if offset == 0 { 1 } else { 0 });
90 for _ in 0..num_tabs {
93 for _ in 0..num_spaces {
101 impl Add for Indent {
102 type Output = Indent;
104 fn add(self, rhs: Indent) -> Indent {
106 block_indent: self.block_indent + rhs.block_indent,
107 alignment: self.alignment + rhs.alignment,
112 impl Sub for Indent {
113 type Output = Indent;
115 fn sub(self, rhs: Indent) -> Indent {
117 self.block_indent - rhs.block_indent,
118 self.alignment - rhs.alignment,
123 impl Add<usize> for Indent {
124 type Output = Indent;
126 fn add(self, rhs: usize) -> Indent {
127 Indent::new(self.block_indent, self.alignment + rhs)
131 impl Sub<usize> for Indent {
132 type Output = Indent;
134 fn sub(self, rhs: usize) -> Indent {
135 Indent::new(self.block_indent, self.alignment - rhs)
139 // 8096 is close enough to infinite for rustfmt.
140 const INFINITE_SHAPE_WIDTH: usize = 8096;
142 #[derive(Copy, Clone, Debug)]
143 pub(crate) struct Shape {
144 pub(crate) width: usize,
145 // The current indentation of code.
146 pub(crate) indent: Indent,
147 // Indentation + any already emitted text on the first line of the current
149 pub(crate) offset: usize,
153 /// `indent` is the indentation of the first line. The next lines
154 /// should begin with at least `indent` spaces (except backwards
155 /// indentation). The first line should not begin with indentation.
156 /// `width` is the maximum number of characters on the last line
157 /// (excluding `indent`). The width of other lines is not limited by
159 /// Note that in reality, we sometimes use width for lines other than the
160 /// last (i.e., we are conservative).
165 // |<------------>| max width
168 pub(crate) fn legacy(width: usize, indent: Indent) -> Shape {
172 offset: indent.alignment,
176 pub(crate) fn indented(indent: Indent, config: &Config) -> Shape {
178 width: config.max_width().saturating_sub(indent.width()),
180 offset: indent.alignment,
184 pub(crate) fn with_max_width(&self, config: &Config) -> Shape {
186 width: config.max_width().saturating_sub(self.indent.width()),
191 pub(crate) fn visual_indent(&self, extra_width: usize) -> Shape {
192 let alignment = self.offset + extra_width;
195 indent: Indent::new(self.indent.block_indent, alignment),
200 pub(crate) fn block_indent(&self, extra_width: usize) -> Shape {
201 if self.indent.alignment == 0 {
204 indent: Indent::new(self.indent.block_indent + extra_width, 0),
210 indent: self.indent + extra_width,
211 offset: self.indent.alignment + extra_width,
216 pub(crate) fn block_left(&self, width: usize) -> Option<Shape> {
217 self.block_indent(width).sub_width(width)
220 pub(crate) fn add_offset(&self, extra_width: usize) -> Shape {
222 offset: self.offset + extra_width,
227 pub(crate) fn block(&self) -> Shape {
229 indent: self.indent.block_only(),
234 pub(crate) fn saturating_sub_width(&self, width: usize) -> Shape {
235 self.sub_width(width).unwrap_or(Shape { width: 0, ..*self })
238 pub(crate) fn sub_width(&self, width: usize) -> Option<Shape> {
240 width: self.width.checked_sub(width)?,
245 pub(crate) fn shrink_left(&self, width: usize) -> Option<Shape> {
247 width: self.width.checked_sub(width)?,
248 indent: self.indent + width,
249 offset: self.offset + width,
253 pub(crate) fn offset_left(&self, width: usize) -> Option<Shape> {
254 self.add_offset(width).sub_width(width)
257 pub(crate) fn used_width(&self) -> usize {
258 self.indent.block_indent + self.offset
261 pub(crate) fn rhs_overhead(&self, config: &Config) -> usize {
264 .saturating_sub(self.used_width() + self.width)
267 pub(crate) fn comment(&self, config: &Config) -> Shape {
270 config.comment_width().saturating_sub(self.indent.width()),
272 Shape { width, ..*self }
275 pub(crate) fn to_string_with_newline(&self, config: &Config) -> Cow<'static, str> {
276 let mut offset_indent = self.indent;
277 offset_indent.alignment = self.offset;
278 offset_indent.to_string_inner(config, 0)
281 /// Creates a `Shape` with a virtually infinite width.
282 pub(crate) fn infinite_width(&self) -> Shape {
284 width: INFINITE_SHAPE_WIDTH,
295 fn indent_add_sub() {
296 let indent = Indent::new(4, 8) + Indent::new(8, 12);
297 assert_eq!(12, indent.block_indent);
298 assert_eq!(20, indent.alignment);
300 let indent = indent - Indent::new(4, 4);
301 assert_eq!(8, indent.block_indent);
302 assert_eq!(16, indent.alignment);
306 fn indent_add_sub_alignment() {
307 let indent = Indent::new(4, 8) + 4;
308 assert_eq!(4, indent.block_indent);
309 assert_eq!(12, indent.alignment);
311 let indent = indent - 4;
312 assert_eq!(4, indent.block_indent);
313 assert_eq!(8, indent.alignment);
317 fn indent_to_string_spaces() {
318 let config = Config::default();
319 let indent = Indent::new(4, 8);
322 assert_eq!(" ", indent.to_string(&config));
326 fn indent_to_string_hard_tabs() {
327 let mut config = Config::default();
328 config.set().hard_tabs(true);
329 let indent = Indent::new(8, 4);
332 assert_eq!("\t\t ", indent.to_string(&config));
336 fn shape_visual_indent() {
337 let config = Config::default();
338 let indent = Indent::new(4, 8);
339 let shape = Shape::legacy(config.max_width(), indent);
340 let shape = shape.visual_indent(20);
342 assert_eq!(config.max_width(), shape.width);
343 assert_eq!(4, shape.indent.block_indent);
344 assert_eq!(28, shape.indent.alignment);
345 assert_eq!(28, shape.offset);
349 fn shape_block_indent_without_alignment() {
350 let config = Config::default();
351 let indent = Indent::new(4, 0);
352 let shape = Shape::legacy(config.max_width(), indent);
353 let shape = shape.block_indent(20);
355 assert_eq!(config.max_width(), shape.width);
356 assert_eq!(24, shape.indent.block_indent);
357 assert_eq!(0, shape.indent.alignment);
358 assert_eq!(0, shape.offset);
362 fn shape_block_indent_with_alignment() {
363 let config = Config::default();
364 let indent = Indent::new(4, 8);
365 let shape = Shape::legacy(config.max_width(), indent);
366 let shape = shape.block_indent(20);
368 assert_eq!(config.max_width(), shape.width);
369 assert_eq!(4, shape.indent.block_indent);
370 assert_eq!(28, shape.indent.alignment);
371 assert_eq!(28, shape.offset);