-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3641827
commit 351dac9
Showing
6 changed files
with
106 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,96 @@ | ||
// wrapping | ||
use crate::comments::*; | ||
|
||
const WRAP: usize = 80; | ||
|
||
// any line under WRAP chars must be unchanged | ||
|
||
// while line is long | ||
// find the first break space (should be after non-space character) | ||
// if break is after WRAP, throw warning | ||
// if no comment, replace space with newline | ||
// if comment and break before comment, replace space with newline | ||
// if comment and break after comment | ||
// if spaced comment | ||
// replace space before comment with newline | ||
// if non-spaced comment | ||
// replace comment % with %\n% | ||
|
||
pub fn needs_wrap(file: &str) -> bool { | ||
file.lines().any(|l| l.len() > WRAP) | ||
} | ||
|
||
pub fn line_needs_wrap(line: &str) -> bool { | ||
line.len() > WRAP | ||
} | ||
|
||
pub fn find_wrap_point(line: &str) -> Option<usize> { | ||
let mut wrap_point: Option<usize> = None; | ||
for i in 0..WRAP { | ||
if line.chars().nth(i) == Some(' ') { | ||
wrap_point = Some(i); | ||
} | ||
} | ||
wrap_point | ||
} | ||
|
||
pub fn wrap_line(line: &str) -> String { | ||
let mut remaining_line = line.to_string(); | ||
let mut new_line = "".to_string(); | ||
let mut can_wrap = true; | ||
while line_needs_wrap(&remaining_line) && can_wrap { | ||
let wrap_point = find_wrap_point(&remaining_line); | ||
let comm = find_comment(&remaining_line); | ||
match wrap_point { | ||
Some(p) => { | ||
let line_start = match comm { | ||
Some(ref c) => { | ||
if p > c.idx { | ||
"%" | ||
} | ||
else { | ||
"" | ||
} | ||
} | ||
None => "" | ||
}; | ||
new_line.push_str(&remaining_line[0..p]); | ||
new_line.push('\n'); | ||
remaining_line = remaining_line[p..remaining_line.len()].to_string(); | ||
remaining_line.insert_str(0, line_start); | ||
}, | ||
None => { | ||
can_wrap = false; | ||
println!("long line cannot be wrapped!"); | ||
println!("{}", line); | ||
} | ||
} | ||
} | ||
new_line.push_str(&remaining_line); | ||
new_line | ||
} | ||
|
||
pub fn wrap(file: &str) -> String { | ||
let mut new_file = "".to_string(); | ||
let lines: Vec<&str> = file.lines().collect(); | ||
for line in lines.iter() { | ||
for line in lines { | ||
if line_needs_wrap(line) { | ||
let new_line = wrap_line(line); | ||
new_file.push_str(&new_line); | ||
} else { | ||
new_file.push_str(line); | ||
} | ||
new_file.push('\n'); | ||
} | ||
new_file | ||
} | ||
|
||
#[cfg(test)] | ||
|
||
//dbg!(&file); | ||
file.to_string() | ||
#[test] | ||
fn test_wrap_line() { | ||
// no comment | ||
let s_in = "This line is too long because it has more than eighty characters inside it. \ | ||
Therefore it should be split."; | ||
let s_out = "This line is too long because it has more than eighty characters inside it.\n \ | ||
Therefore it should be split."; | ||
assert_eq!(wrap_line(s_in), s_out); | ||
// break before comment | ||
let s_in = "This line is too long because it has more than eighty characters inside it. \ | ||
Therefore it % should be split."; | ||
let s_out = "This line is too long because it has more than eighty characters inside it.\n \ | ||
Therefore it % should be split."; | ||
assert_eq!(wrap_line(s_in), s_out); | ||
// break after comment | ||
let s_in = "This line is too long because % it has more than eighty characters inside it. \ | ||
Therefore it should be split."; | ||
let s_out = "This line is too long because % it has more than eighty characters inside it.\n\ | ||
% Therefore it should be split."; | ||
assert_eq!(wrap_line(s_in), s_out); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters