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.
13 use std::ops::{Add, Sub};
17 #[derive(Copy, Clone, Debug)]
19 // Width of the block indent, in characters. Must be a multiple of
20 // Config::tab_spaces.
21 pub block_indent: usize,
22 // Alignment in characters.
26 // INDENT_BUFFER.len() = 81
27 const INDENT_BUFFER_LEN: usize = 80;
28 const INDENT_BUFFER: &str =
31 pub fn new(block_indent: usize, alignment: usize) -> Indent {
38 pub fn from_width(config: &Config, width: usize) -> Indent {
39 if config.hard_tabs() {
40 let tab_num = width / config.tab_spaces();
41 let alignment = width % config.tab_spaces();
42 Indent::new(config.tab_spaces() * tab_num, alignment)
48 pub fn empty() -> Indent {
52 pub fn block_only(&self) -> Indent {
54 block_indent: self.block_indent,
59 pub fn block_indent(mut self, config: &Config) -> Indent {
60 self.block_indent += config.tab_spaces();
64 pub fn block_unindent(mut self, config: &Config) -> Indent {
65 if self.block_indent < config.tab_spaces() {
66 Indent::new(self.block_indent, 0)
68 self.block_indent -= config.tab_spaces();
73 pub fn width(&self) -> usize {
74 self.block_indent + self.alignment
77 pub fn to_string(&self, config: &Config) -> Cow<'static, str> {
78 self.to_string_inner(config, 1)
81 pub fn to_string_with_newline(&self, config: &Config) -> Cow<'static, str> {
82 self.to_string_inner(config, 0)
85 fn to_string_inner(&self, config: &Config, offset: usize) -> Cow<'static, str> {
86 let (num_tabs, num_spaces) = if config.hard_tabs() {
87 (self.block_indent / config.tab_spaces(), self.alignment)
91 let num_chars = num_tabs + num_spaces;
92 if num_tabs == 0 && num_chars + offset <= INDENT_BUFFER_LEN {
93 Cow::from(&INDENT_BUFFER[offset..num_chars + 1])
95 let mut indent = String::with_capacity(num_chars + if offset == 0 { 1 } else { 0 });
99 for _ in 0..num_tabs {
102 for _ in 0..num_spaces {
110 impl Add for Indent {
111 type Output = Indent;
113 fn add(self, rhs: Indent) -> Indent {
115 block_indent: self.block_indent + rhs.block_indent,
116 alignment: self.alignment + rhs.alignment,
121 impl Sub for Indent {
122 type Output = Indent;
124 fn sub(self, rhs: Indent) -> Indent {
126 self.block_indent - rhs.block_indent,
127 self.alignment - rhs.alignment,
132 impl Add<usize> for Indent {
133 type Output = Indent;
135 fn add(self, rhs: usize) -> Indent {
136 Indent::new(self.block_indent, self.alignment + rhs)
140 impl Sub<usize> for Indent {
141 type Output = Indent;
143 fn sub(self, rhs: usize) -> Indent {
144 Indent::new(self.block_indent, self.alignment - rhs)
148 #[derive(Copy, Clone, Debug)]
151 // The current indentation of code.
153 // Indentation + any already emitted text on the first line of the current
159 /// `indent` is the indentation of the first line. The next lines
160 /// should begin with at least `indent` spaces (except backwards
161 /// indentation). The first line should not begin with indentation.
162 /// `width` is the maximum number of characters on the last line
163 /// (excluding `indent`). The width of other lines is not limited by
165 /// Note that in reality, we sometimes use width for lines other than the
166 /// last (i.e., we are conservative).
171 // |<------------>| max width
174 pub fn legacy(width: usize, indent: Indent) -> Shape {
178 offset: indent.alignment,
182 pub fn indented(indent: Indent, config: &Config) -> Shape {
184 width: config.max_width().saturating_sub(indent.width()),
186 offset: indent.alignment,
190 pub fn with_max_width(&self, config: &Config) -> Shape {
192 width: config.max_width().saturating_sub(self.indent.width()),
197 pub fn visual_indent(&self, extra_width: usize) -> Shape {
198 let alignment = self.offset + extra_width;
201 indent: Indent::new(self.indent.block_indent, alignment),
206 pub fn block_indent(&self, extra_width: usize) -> Shape {
207 if self.indent.alignment == 0 {
210 indent: Indent::new(self.indent.block_indent + extra_width, 0),
216 indent: self.indent + extra_width,
217 offset: self.indent.alignment + extra_width,
222 pub fn block_left(&self, width: usize) -> Option<Shape> {
223 self.block_indent(width).sub_width(width)
226 pub fn add_offset(&self, extra_width: usize) -> Shape {
228 offset: self.offset + extra_width,
233 pub fn block(&self) -> Shape {
235 indent: self.indent.block_only(),
240 pub fn sub_width(&self, width: usize) -> Option<Shape> {
242 width: self.width.checked_sub(width)?,
247 pub fn shrink_left(&self, width: usize) -> Option<Shape> {
249 width: self.width.checked_sub(width)?,
250 indent: self.indent + width,
251 offset: self.offset + width,
255 pub fn offset_left(&self, width: usize) -> Option<Shape> {
256 self.add_offset(width).sub_width(width)
259 pub fn used_width(&self) -> usize {
260 self.indent.block_indent + self.offset
263 pub fn rhs_overhead(&self, config: &Config) -> usize {
266 .saturating_sub(self.used_width() + self.width)
269 pub fn comment(&self, config: &Config) -> Shape {
272 config.comment_width().saturating_sub(self.indent.width()),
274 Shape { width, ..*self }
283 fn indent_add_sub() {
284 let indent = Indent::new(4, 8) + Indent::new(8, 12);
285 assert_eq!(12, indent.block_indent);
286 assert_eq!(20, indent.alignment);
288 let indent = indent - Indent::new(4, 4);
289 assert_eq!(8, indent.block_indent);
290 assert_eq!(16, indent.alignment);
294 fn indent_add_sub_alignment() {
295 let indent = Indent::new(4, 8) + 4;
296 assert_eq!(4, indent.block_indent);
297 assert_eq!(12, indent.alignment);
299 let indent = indent - 4;
300 assert_eq!(4, indent.block_indent);
301 assert_eq!(8, indent.alignment);
305 fn indent_to_string_spaces() {
306 let config = Config::default();
307 let indent = Indent::new(4, 8);
310 assert_eq!(" ", indent.to_string(&config));
314 fn indent_to_string_hard_tabs() {
315 let mut config = Config::default();
316 config.set().hard_tabs(true);
317 let indent = Indent::new(8, 4);
320 assert_eq!("\t\t ", indent.to_string(&config));
324 fn shape_visual_indent() {
325 let config = Config::default();
326 let indent = Indent::new(4, 8);
327 let shape = Shape::legacy(config.max_width(), indent);
328 let shape = shape.visual_indent(20);
330 assert_eq!(config.max_width(), shape.width);
331 assert_eq!(4, shape.indent.block_indent);
332 assert_eq!(28, shape.indent.alignment);
333 assert_eq!(28, shape.offset);
337 fn shape_block_indent_without_alignment() {
338 let config = Config::default();
339 let indent = Indent::new(4, 0);
340 let shape = Shape::legacy(config.max_width(), indent);
341 let shape = shape.block_indent(20);
343 assert_eq!(config.max_width(), shape.width);
344 assert_eq!(24, shape.indent.block_indent);
345 assert_eq!(0, shape.indent.alignment);
346 assert_eq!(0, shape.offset);
350 fn shape_block_indent_with_alignment() {
351 let config = Config::default();
352 let indent = Indent::new(4, 8);
353 let shape = Shape::legacy(config.max_width(), indent);
354 let shape = shape.block_indent(20);
356 assert_eq!(config.max_width(), shape.width);
357 assert_eq!(4, shape.indent.block_indent);
358 assert_eq!(28, shape.indent.alignment);
359 assert_eq!(28, shape.offset);