2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
2012-09-20 18:09:46 -05:00
|
|
|
Removes the common level of indention from description strings. For
|
|
|
|
instance, if an entire doc comment is indented 8 spaces we want to
|
|
|
|
remove those 8 spaces from every line.
|
|
|
|
|
|
|
|
The first line of a string is allowed to be intend less than
|
|
|
|
subsequent lines in the same paragraph in order to account for
|
|
|
|
instances where the string containing the doc comment is opened in the
|
|
|
|
middle of a line, and each of the following lines is indented.
|
|
|
|
*/
|
2012-01-25 00:02:36 -06:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
|
|
|
use pass::Pass;
|
2012-12-23 16:41:37 -06:00
|
|
|
use text_pass;
|
|
|
|
|
|
|
|
use core::str;
|
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
|
|
|
|
2012-11-19 20:00:12 -06:00
|
|
|
pub fn mk_pass() -> Pass {
|
2012-07-14 00:57:48 -05:00
|
|
|
text_pass::mk_pass(~"unindent", unindent)
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 20:52:31 -06:00
|
|
|
fn unindent(s: &str) -> ~str {
|
2012-01-25 00:02:36 -06:00
|
|
|
let lines = str::lines_any(s);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut saw_first_line = false;
|
|
|
|
let mut saw_second_line = false;
|
2012-06-30 18:19:07 -05:00
|
|
|
let min_indent = do vec::foldl(uint::max_value, lines)
|
|
|
|
|min_indent, line| {
|
2012-01-25 00:02:36 -06:00
|
|
|
|
|
|
|
// After we see the first non-whitespace line, look at
|
|
|
|
// the line we have. If it is not whitespace, and therefore
|
|
|
|
// part of the first paragraph, then ignore the indentation
|
|
|
|
// level of the first line
|
|
|
|
let ignore_previous_indents =
|
|
|
|
saw_first_line &&
|
|
|
|
!saw_second_line &&
|
2012-09-28 00:20:47 -05:00
|
|
|
!str::is_whitespace(*line);
|
2012-01-25 00:02:36 -06:00
|
|
|
|
|
|
|
let min_indent = if ignore_previous_indents {
|
|
|
|
uint::max_value
|
|
|
|
} else {
|
|
|
|
min_indent
|
|
|
|
};
|
|
|
|
|
|
|
|
if saw_first_line {
|
|
|
|
saw_second_line = true;
|
|
|
|
}
|
|
|
|
|
2012-09-28 00:20:47 -05:00
|
|
|
if str::is_whitespace(*line) {
|
2012-01-25 00:02:36 -06:00
|
|
|
min_indent
|
|
|
|
} else {
|
|
|
|
saw_first_line = true;
|
2012-08-30 14:54:50 -05:00
|
|
|
let mut spaces = 0;
|
2012-09-28 00:20:47 -05:00
|
|
|
do str::all(*line) |char| {
|
2012-01-25 00:02:36 -06:00
|
|
|
// Only comparing against space because I wouldn't
|
|
|
|
// know what to do with mixed whitespace chars
|
|
|
|
if char == ' ' {
|
2012-08-30 14:54:50 -05:00
|
|
|
spaces += 1;
|
2012-01-25 00:02:36 -06:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2012-08-30 14:54:50 -05:00
|
|
|
uint::min(min_indent, spaces)
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-24 22:24:57 -06:00
|
|
|
if !lines.is_empty() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let unindented = ~[str::trim(vec::head(lines))]
|
2013-01-31 19:12:29 -06:00
|
|
|
+ do vec::tail(lines).map |line| {
|
2012-09-28 19:17:20 -05:00
|
|
|
if str::is_whitespace(*line) {
|
2013-01-30 15:14:35 -06:00
|
|
|
copy *line
|
2012-01-25 00:02:36 -06:00
|
|
|
} else {
|
2012-09-28 19:17:20 -05:00
|
|
|
assert str::len(*line) >= min_indent;
|
|
|
|
str::slice(*line, min_indent, str::len(*line))
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
};
|
2012-07-14 00:57:48 -05:00
|
|
|
str::connect(unindented, ~"\n")
|
2012-01-25 00:02:36 -06:00
|
|
|
} else {
|
2013-01-30 20:52:31 -06:00
|
|
|
s.to_str()
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_unindent() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~" line1\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
let r = unindent(s);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert r == ~"line1\nline2";
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_unindent_multiple_paragraphs() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~" line1\n\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
let r = unindent(s);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert r == ~"line1\n\nline2";
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_leave_multiple_indent_levels() {
|
|
|
|
// Line 2 is indented another level beyond the
|
|
|
|
// base indentation and should be preserved
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~" line1\n\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
let r = unindent(s);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert r == ~"line1\n\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_ignore_first_line_indent() {
|
|
|
|
// Thi first line of the first paragraph may not be indented as
|
|
|
|
// far due to the way the doc string was written:
|
|
|
|
//
|
|
|
|
// #[doc = "Start way over here
|
|
|
|
// and continue here"]
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~"line1\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
let r = unindent(s);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert r == ~"line1\nline2";
|
2012-01-25 00:02:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_ignore_first_line_indent_in_a_single_line_para() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let s = ~"line1\n\n line2";
|
2012-01-25 00:02:36 -06:00
|
|
|
let r = unindent(s);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert r == ~"line1\n\n line2";
|
2012-01-30 22:27:16 -06:00
|
|
|
}
|