1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std::ops::{Add, Sub};
16 #[derive(Copy, Clone, Debug)]
18 // Width of the block indent, in characters. Must be a multiple of
19 // Config::tab_spaces.
20 pub block_indent: usize,
21 // Alignment in characters.
25 // INDENT_BUFFER.len() = 80
26 const INDENT_BUFFER_LEN: usize = 80;
27 const INDENT_BUFFER: &str =
30 pub fn new(block_indent: usize, alignment: usize) -> Indent {
37 pub fn from_width(config: &Config, width: usize) -> Indent {
38 if config.hard_tabs() {
39 let tab_num = width / config.tab_spaces();
40 let alignment = width % config.tab_spaces();
41 Indent::new(config.tab_spaces() * tab_num, alignment)
47 pub fn empty() -> Indent {
51 pub fn block_only(&self) -> Indent {
53 block_indent: self.block_indent,
58 pub fn block_indent(mut self, config: &Config) -> Indent {
59 self.block_indent += config.tab_spaces();
63 pub fn block_unindent(mut self, config: &Config) -> Indent {
64 if self.block_indent < config.tab_spaces() {
65 Indent::new(self.block_indent, 0)
67 self.block_indent -= config.tab_spaces();
72 pub fn width(&self) -> usize {
73 self.block_indent + self.alignment
76 pub fn to_string(&self, config: &Config) -> Cow<'static, str> {
77 self.to_string_inner(config, 1)
80 pub fn to_string_with_newline(&self, config: &Config) -> Cow<'static, str> {
81 self.to_string_inner(config, 0)
84 pub fn to_string_inner(&self, config: &Config, offset: usize) -> Cow<'static, str> {
85 let (num_tabs, num_spaces) = if config.hard_tabs() {
86 (self.block_indent / config.tab_spaces(), self.alignment)
90 let num_chars = num_tabs + num_spaces;
91 if num_tabs == 0 && num_chars + offset <= INDENT_BUFFER_LEN {
92 Cow::from(&INDENT_BUFFER[offset..num_chars + 1])
94 let mut indent = String::with_capacity(num_chars + if offset == 0 { 1 } else { 0 });
98 for _ in 0..num_tabs {
101 for _ in 0..num_spaces {
109 impl Add for Indent {
110 type Output = Indent;
112 fn add(self, rhs: Indent) -> Indent {
114 block_indent: self.block_indent + rhs.block_indent,
115 alignment: self.alignment + rhs.alignment,
120 impl Sub for Indent {
121 type Output = Indent;
123 fn sub(self, rhs: Indent) -> Indent {
125 self.block_indent - rhs.block_indent,
126 self.alignment - rhs.alignment,
131 impl Add<usize> for Indent {
132 type Output = Indent;
134 fn add(self, rhs: usize) -> Indent {
135 Indent::new(self.block_indent, self.alignment + rhs)
139 impl Sub<usize> for Indent {
140 type Output = Indent;
142 fn sub(self, rhs: usize) -> Indent {
143 Indent::new(self.block_indent, self.alignment - rhs)
147 #[derive(Copy, Clone, Debug)]
150 // The current indentation of code.
152 // Indentation + any already emitted text on the first line of the current
158 /// `indent` is the indentation of the first line. The next lines
159 /// should begin with at least `indent` spaces (except backwards
160 /// indentation). The first line should not begin with indentation.
161 /// `width` is the maximum number of characters on the last line
162 /// (excluding `indent`). The width of other lines is not limited by
164 /// Note that in reality, we sometimes use width for lines other than the
165 /// last (i.e., we are conservative).
170 // |<------------>| max width
173 pub fn legacy(width: usize, indent: Indent) -> Shape {
177 offset: indent.alignment,
181 pub fn indented(indent: Indent, config: &Config) -> Shape {
183 width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
185 offset: indent.alignment,
189 pub fn with_max_width(&self, config: &Config) -> Shape {
193 .checked_sub(self.indent.width())
199 pub fn offset(width: usize, indent: Indent, offset: usize) -> Shape {
207 pub fn visual_indent(&self, extra_width: usize) -> Shape {
208 let alignment = self.offset + extra_width;
211 indent: Indent::new(self.indent.block_indent, alignment),
216 pub fn block_indent(&self, extra_width: usize) -> Shape {
217 if self.indent.alignment == 0 {
220 indent: Indent::new(self.indent.block_indent + extra_width, 0),
226 indent: self.indent + extra_width,
227 offset: self.indent.alignment + extra_width,
232 pub fn block_left(&self, width: usize) -> Option<Shape> {
233 self.block_indent(width).sub_width(width)
236 pub fn add_offset(&self, extra_width: usize) -> Shape {
238 offset: self.offset + extra_width,
243 pub fn block(&self) -> Shape {
245 indent: self.indent.block_only(),
250 pub fn sub_width(&self, width: usize) -> Option<Shape> {
252 width: self.width.checked_sub(width)?,
257 pub fn shrink_left(&self, width: usize) -> Option<Shape> {
259 width: self.width.checked_sub(width)?,
260 indent: self.indent + width,
261 offset: self.offset + width,
265 pub fn offset_left(&self, width: usize) -> Option<Shape> {
266 self.add_offset(width).sub_width(width)
269 pub fn used_width(&self) -> usize {
270 self.indent.block_indent + self.offset
273 pub fn rhs_overhead(&self, config: &Config) -> usize {
276 .checked_sub(self.used_width() + self.width)
286 fn indent_add_sub() {
287 let indent = Indent::new(4, 8) + Indent::new(8, 12);
288 assert_eq!(12, indent.block_indent);
289 assert_eq!(20, indent.alignment);
291 let indent = indent - Indent::new(4, 4);
292 assert_eq!(8, indent.block_indent);
293 assert_eq!(16, indent.alignment);
297 fn indent_add_sub_alignment() {
298 let indent = Indent::new(4, 8) + 4;
299 assert_eq!(4, indent.block_indent);
300 assert_eq!(12, indent.alignment);
302 let indent = indent - 4;
303 assert_eq!(4, indent.block_indent);
304 assert_eq!(8, indent.alignment);
308 fn indent_to_string_spaces() {
309 let config = Config::default();
310 let indent = Indent::new(4, 8);
313 assert_eq!(" ", indent.to_string(&config));
317 fn indent_to_string_hard_tabs() {
318 let mut config = Config::default();
319 config.set().hard_tabs(true);
320 let indent = Indent::new(8, 4);
323 assert_eq!("\t\t ", indent.to_string(&config));
327 fn shape_visual_indent() {
328 let config = Config::default();
329 let indent = Indent::new(4, 8);
330 let shape = Shape::legacy(config.max_width(), indent);
331 let shape = shape.visual_indent(20);
333 assert_eq!(config.max_width(), shape.width);
334 assert_eq!(4, shape.indent.block_indent);
335 assert_eq!(28, shape.indent.alignment);
336 assert_eq!(28, shape.offset);
340 fn shape_block_indent_without_alignment() {
341 let config = Config::default();
342 let indent = Indent::new(4, 0);
343 let shape = Shape::legacy(config.max_width(), indent);
344 let shape = shape.block_indent(20);
346 assert_eq!(config.max_width(), shape.width);
347 assert_eq!(24, shape.indent.block_indent);
348 assert_eq!(0, shape.indent.alignment);
349 assert_eq!(0, shape.offset);
353 fn shape_block_indent_with_alignment() {
354 let config = Config::default();
355 let indent = Indent::new(4, 8);
356 let shape = Shape::legacy(config.max_width(), indent);
357 let shape = shape.block_indent(20);
359 assert_eq!(config.max_width(), shape.width);
360 assert_eq!(4, shape.indent.block_indent);
361 assert_eq!(28, shape.indent.alignment);
362 assert_eq!(28, shape.offset);