diff options
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 89ed1a5..1b0ce7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,8 +100,10 @@ fn apply_changes(content: &str, mut changes: Vec<Change>, debug: bool) -> String last_pos = after; } ChangeKind::Keep => { - // let (before, after) = surrounding_whitespace(&change.node); - let (before, after) = (change.node.text_range().start().into(), change.node.text_range().end().into()); + let (before, after) = surrounding_noise(&change.node); + let (before, after) = eat_whitespace(&before, &after); + // let (before, after) = (change.node.text_range().start().into(), change.node.text_range().end().into()); + let (before, after) = (before.text_range().start().into(), after.text_range().end().into()); if debug { println!("keeping: {}", &content[before..after]) }; @@ -168,7 +170,11 @@ fn surrounding_noise(node: &rnix::SyntaxNode) -> (NixNodeOrToken, NixNodeOrToken loop { let (a, b) = eat_whitespace(&begin, &end); + // println!("loop: {begin}, {end}"); + // println!("loop: {a}, {b}"); + // println!("{:?}", (begin.prev_sibling_or_token(), end.next_sibling_or_token())); if let Some(res) = eat_matching_parens(&a, &b) { + // println!("ate parens!"); (begin, end) = res; } else { break @@ -182,17 +188,37 @@ fn surrounding_noise(node: &rnix::SyntaxNode) -> (NixNodeOrToken, NixNodeOrToken fn remove_node(node: &rnix::SyntaxNode) -> (usize, usize) { let (before, after) = surrounding_noise(node); - let (prev, next) = + let (mut prev, mut next) = eat_whitespace(&before, &after); + // println!("{prev:?}, {next:?}"); + if let Some(t) = prev.as_token().map(|t| t.prev_token()).flatten() { + if t.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE { + prev = NodeOrToken::Token(t); + } + }; + // if let Some(t) = next.as_token().map(|t| t.next_token()).flatten() { + // if t.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE { + // next = NodeOrToken::Token(t); + // } + // }; + // println!("{prev:?}, {next:?}"); + + if prev.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE && next.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE { + // println!("inside if!"); if prev.to_string().lines().count() < next.to_string().lines().count() { - (Into::<usize>::into(node.text_range().start() - prev.text_range().len()) + previous_indentation(&node).unwrap_or(0) - , Into::<usize>::into(node.text_range().end()) + next_indentation(&node).unwrap_or(0)) + let start: usize = (node.text_range().start() - prev.text_range().len()).into(); + let end: usize = node.text_range().end().into(); + (start + previous_indentation(&node).unwrap_or(0) + , end + next_indentation(&node).unwrap_or(0)) } else { - (Into::<usize>::into(node.text_range().start()) - previous_indentation(&node).unwrap_or(0), - Into::<usize>::into(node.text_range().end() + next.text_range().len()) - next_indentation(&node).unwrap_or(0)) + let start: usize = node.text_range().start().into(); + let end: usize = (node.text_range().end() + next.text_range().len()).into(); + (start, end) + // (start - previous_indentation(&node).unwrap_or(0), + // end - next_indentation(&node).unwrap_or(0)) } } else { @@ -200,12 +226,19 @@ fn remove_node(node: &rnix::SyntaxNode) -> (usize, usize) { } } +fn indentation_of_string(string: &str) -> Option<usize> { + if string.contains("\n") { + None + } else { + Some(string.lines().last().unwrap().len()) + } +} fn previous_indentation(node: &rnix::SyntaxNode) -> Option<usize> { let whitespace_token = node.prev_sibling_or_token()?; if whitespace_token.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE { - Some(whitespace_token.to_string().lines().last().unwrap().len()) + indentation_of_string(&whitespace_token.to_string()) } else { None } @@ -215,7 +248,8 @@ fn next_indentation(node: &rnix::SyntaxNode) -> Option<usize> { let whitespace_token = node.next_sibling_or_token()?; if whitespace_token.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE { - Some(whitespace_token.to_string().lines().last().unwrap().len()) + indentation_of_string(&whitespace_token.to_string()) + // Some(whitespace_token.to_string().lines().last().unwrap().len()) } else { None } |