dtolnay / indoc

Indented document literals for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make indoc work with tab-based indentation

Boscop opened this issue · comments

I use tabs for indentation and spaces for alignment, and would really appreciate it if indoc would also work with tab-based indentation.

That would be a nice improvement! I would accept a PR to make indoc handle tab-based indentation. Here is the relevant code that removes indentation from a string:


pub fn unindent_bytes(s: &[u8]) -> Vec<u8> {
// Document may start either on the same line as opening quote or
// on the next line
let ignore_first_line = s.starts_with(b"\n") ||
s.starts_with(b"\r\n");
// Largest number of spaces that can be removed from every
// non-whitespace-only line after the first
let spaces = s.lines()
.skip(1)
.filter_map(count_spaces)
.min()
.unwrap_or(0);
let mut result = Vec::with_capacity(s.len());
for (i, line) in s.lines().enumerate() {
if i > 1 || (i == 1 && !ignore_first_line) {
result.push(b'\n');
}
if i == 0 {
// Do not un-indent anything on same line as opening quote
result.extend_from_slice(line);
} else if line.len() > spaces {
// Whitespace-only lines may have fewer than the number of spaces
// being removed
result.extend_from_slice(&line[spaces..]);
}
}
result
}

Closing for now because I don't plan to pursue this.