Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piyoppi committed Oct 21, 2024
1 parent 56ea89d commit 0077011
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 270 deletions.
84 changes: 40 additions & 44 deletions src/code/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,43 @@ pub fn format(
formatters: &[Box<dyn self::Formatter>],
structure_formatters: &[Box<dyn self::StructureFormatter>],
) -> String {
let mut removed_pos_iter = removed_pos.iter().rev().peekable();

let mut content = content.to_string();
let mut ranges: Vec<Range<usize>> = vec![];
let mut open_structure_remove_range: Vec<Range<usize>> = vec![];

while let Some(pos) = removed_pos_iter.next() {
match pos {
RemovedMarker::Block(pos) => {
let next_pos: usize = removed_pos_iter.peek().map_or(0, |p| match p {
RemovedMarker::Block(pos) => *pos,
RemovedMarker::OpenStructure(_, p2) => *p2
});
format_block(&mut content, *pos, next_pos, formatters);
},
RemovedMarker::OpenStructure(start, end) => {
let ranges = structure_formatters.iter().fold(vec![], |mut v, f| {
v.extend(f.format(&content, *end, *start));
v
});
merge_ranges(&mut open_structure_remove_range, ranges);
merge_overlapped_ranges(&mut open_structure_remove_range);
}
let mut removed_pos_iter = removed_pos.iter().rev().peekable();
while let Some((pos, pair_idx)) = removed_pos_iter.next() {
let (next_pos, _) = removed_pos_iter.peek().map_or((0, None), |(p, i)| (*p, *i));
let range = format_block(content, *pos, next_pos, formatters);
ranges.push(range);

if let Some(pair_idx) = pair_idx {
let (pair_start_pos, _) = removed_pos[*pair_idx];
let ranges = structure_formatters.iter().fold(vec![], |mut v, f| {
v.extend(f.format(content, *pos, pair_start_pos));
v
});
open_structure_remove_range.extend(ranges);
}
}

content
}
merge_ranges(&mut ranges, open_structure_remove_range);
merge_overlapped_ranges(&mut ranges);

fn format_block(content: &mut String, pos: usize, next_pos: usize, formatters: &[Box<dyn Formatter>]) {
if !content.is_char_boundary(pos) {
panic!("Invalid byte position: {}", pos);
}
ranges.into_iter().fold(content.to_string(), |mut content, range| {
content.replace_range(range, "");

formatters.iter().fold(pos, |pos, f| {
content
})
}

fn format_block(content: &str, pos: usize, next_pos: usize, formatters: &[Box<dyn Formatter>]) -> Range<usize> {
formatters.iter().fold(pos..pos, |range, f| {
let (start, end) = f.format(content, pos, next_pos);
let start = std::cmp::max(start, next_pos);
content.replace_range(start..end, "");
let start = std::cmp::max(std::cmp::min(start, range.start), next_pos);
let end = std::cmp::max(end.max(range.end), pos);

start
});
start..end
})
}

fn merge_ranges(ranges: &mut Vec<Range<usize>>, new_ranges: Vec<Range<usize>>) {
Expand Down Expand Up @@ -100,19 +97,18 @@ fn merge_ranges(ranges: &mut Vec<Range<usize>>, new_ranges: Vec<Range<usize>>) {
fn merge_overlapped_ranges(ranges: &mut Vec<Range<usize>>) {
let mut write_cursor = 0;
for read_cursor in 1..ranges.len() {
if ranges[write_cursor].end >= ranges[read_cursor].start {
ranges[write_cursor].end = ranges[write_cursor].end.max(ranges[read_cursor].end)
if ranges[write_cursor].start <= ranges[read_cursor].end {
ranges[write_cursor].start = ranges[write_cursor].start.min(ranges[read_cursor].start);
} else {
write_cursor += 1;
if write_cursor != read_cursor {
ranges[write_cursor] = std::mem::replace(&mut ranges[read_cursor], Range { start: 0, end: 0 });
ranges[write_cursor] = ranges[read_cursor].clone();
}
}
}
ranges.truncate(write_cursor + 1);
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -141,7 +137,7 @@ mod tests {
// 01234567890123456789012345678901234567890123456789012345
// ^ ^
let content = "<div>+ hoge+ + foo+ bar+ baz++ +</div>".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(19), RemovedMarker::Block(49)];
let removed_pos = [(19, None), (49, None)];
assert_eq!(
format(&content, &removed_pos, &strategy, &[]),
//12345678901234567890123456789012345678901234567
Expand All @@ -157,7 +153,7 @@ mod tests {
// 0123456789012345678901
// ^
let content = " hoge+ + foo+".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(13)];
let removed_pos = [(13, None)];
assert_eq!(
format(&content, &removed_pos, &strategy, &[]),
//12345678901234567890123456789012345678901234567
Expand All @@ -174,7 +170,7 @@ mod tests {
// 01234567890123456789012
// ^
let content = " hoge++ + foo+".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(14)];
let removed_pos = [(14, None)];
assert_eq!(
format(&content, &removed_pos, &strategy, &[]),
//12345678901234567890123456789012345678901234567
Expand All @@ -191,7 +187,7 @@ mod tests {
// 01234567890123456789012
// ^
let content = " hoge+ ++ foo+".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(14)];
let removed_pos = [(14, None)];
assert_eq!(
format(&content, &removed_pos, &strategy, &[]),
//12345678901234567890123456789012345678901234567
Expand All @@ -208,7 +204,7 @@ mod tests {
// 01234567890123456789012
// ^
let content = " hoge+ + + + foo+".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(15)];
let removed_pos = [(15, None)];
assert_eq!(
format(&content, &removed_pos, &strategy, &[]),
//12345678901234567890123456789012345678901234567
Expand All @@ -221,7 +217,7 @@ mod tests {
// 0123456789012345678901234567890123456789012
// ^ ^
let content = "+<div>+ + + + + +</div>".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(26), RemovedMarker::Block(31)];
let removed_pos = [(26, None), (31, None)];
assert_eq!(
format(
&content,
Expand All @@ -237,7 +233,7 @@ mod tests {
// 012345678901234567890123
// ^
let content = "+<div>+hoge++++baz</div>".replace('+', "\n");
let removed_pos = [RemovedMarker::Block(13)];
let removed_pos = [(13, None)];
assert_eq!(
format(
&content,
Expand All @@ -262,9 +258,9 @@ mod tests {

#[test]
fn test_merge_overlapped_ranges() {
let mut ranges = vec![1..5, 2..6, 8..10, 9..12, 15..18, 20..24];
let mut ranges = vec![20..24, 15..18, 9..12, 8..10, 2..6, 1..5];
merge_overlapped_ranges(&mut ranges);
assert_eq!(ranges, vec![1..6, 8..12, 15..18, 20..24]);
assert_eq!(ranges, vec![20..24, 15..18, 8..12, 1..6]);

let mut ranges = vec![];
merge_overlapped_ranges(&mut ranges);
Expand Down
6 changes: 3 additions & 3 deletions src/code/formatter/empty_line_remover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ impl Formatter for EmptyLineRemover {
return (byte_pos, byte_pos);
}

let is_next_line_empty = find_next_line_break_pos(content, bytes, byte_pos, true)
let is_not_next_line_empty = find_next_line_break_pos(content, bytes, byte_pos, true)
.and_then(|pos| find_next_line_break_pos(content, bytes, pos + 1, true))
.is_none();
let is_prev_line_empty = find_prev_line_break_pos(content, bytes, byte_pos, true)
let is_not_prev_line_empty = find_prev_line_break_pos(content, bytes, byte_pos, true)
.and_then(|pos| find_prev_line_break_pos(content, bytes, pos, true))
.map_or(true, |pos| pos <= next_byte_pos);

if is_next_line_empty && is_prev_line_empty {
if is_not_next_line_empty && is_not_prev_line_empty {
(byte_pos, byte_pos + 1)
} else {
(byte_pos, byte_pos)
Expand Down
Loading

0 comments on commit 0077011

Please sign in to comment.