From a0459645638fe1397aa4f5e01a8f9093911ccf6c Mon Sep 17 00:00:00 2001 From: stuebinm Date: Fri, 12 Apr 2024 20:54:52 +0200 Subject: version 0.0.2 since this is apparently enough to edit all of nixpkgs to remove lib.mdDoc without (?) breaking anything, I think it should qualify as a tiny, tiny release. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eac2997..606a0aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,7 +194,7 @@ dependencies = [ [[package]] name = "nixq" -version = "0.1.0" +version = "0.0.2" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 12517b8..8aea1a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nixq" -version = "0.1.0" +version = "0.0.2" edition = "2021" authors = [ "stuebinm " ] 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, 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::::into(node.text_range().start() - prev.text_range().len()) + previous_indentation(&node).unwrap_or(0) - , Into::::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::::into(node.text_range().start()) - previous_indentation(&node).unwrap_or(0), - Into::::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 { + if string.contains("\n") { + None + } else { + Some(string.lines().last().unwrap().len()) + } +} fn previous_indentation(node: &rnix::SyntaxNode) -> Option { 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 { 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 } -- cgit v1.2.3