From a5e92895fa1e081aff7f12d61b50d964a5ce28e3 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Fri, 28 Jan 2022 19:40:33 -0500 Subject: [PATCH] feat: multiline strings done right --- README.md | 3 ++- src/rules/string.rs | 30 ++++++++++++++++++++++++------ tests/cases/string/in | 9 +++++++++ tests/cases/string/out | 17 +++++++++++++---- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fd36549..2fb41eb 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ It's written in [Rust](https://www.rust-lang.org/) and formats [Nixpkgs](https://github.com/NixOS/nixpkgs) - in just a few seconds[^benchmark-specs]. + in just a few seconds. + [^benchmark-specs] | Cores | Seconds | |:-----:|:--------: diff --git a/src/rules/string.rs b/src/rules/string.rs index 54794f9..5406a79 100644 --- a/src/rules/string.rs +++ b/src/rules/string.rs @@ -38,7 +38,7 @@ pub fn rule( .iter() .filter(|e| e.kind() != rnix::SyntaxKind::TOKEN_STRING_CONTENT); - let mut lines: Vec = elements[0..elements.len() - 1] + let content: String = elements[0..elements.len() - 1] .iter() .map(|element| match element.kind() { rnix::SyntaxKind::TOKEN_STRING_CONTENT => { @@ -47,9 +47,23 @@ pub fn rule( } _ => placeholder.to_string(), }) - .collect::() - .split('\n') - .map(|line| line.trim_end().to_string()) + .collect(); + + let lines: Vec = + content.split('\n').map(|line| line.to_string()).collect(); + + let should_trim_end: bool = + lines.len() >= 1 && lines[lines.len() - 1].trim().len() == 0; + + let mut lines: Vec = lines + .iter() + .map(|line| { + if should_trim_end { + line.trim_end().to_string() + } else { + line.to_string() + } + }) .collect(); // eprintln!("0: {:?}", lines); @@ -92,14 +106,18 @@ pub fn rule( if portions.len() == 1 { if portions[0].len() > 0 || index + 1 == lines.len() { - steps.push_back(crate::builder::Step::Pad); + if lines.len() > 1 { + steps.push_back(crate::builder::Step::Pad); + } steps.push_back(crate::builder::Step::Token( rnix::SyntaxKind::TOKEN_STRING_CONTENT, portions[0].to_string(), )); } } else { - steps.push_back(crate::builder::Step::Pad); + if lines.len() > 1 { + steps.push_back(crate::builder::Step::Pad); + } for (index, portion) in portions.iter().enumerate() { steps.push_back(crate::builder::Step::Token( rnix::SyntaxKind::TOKEN_STRING_CONTENT, diff --git a/tests/cases/string/in b/tests/cases/string/in index 2b0f7eb..de656f9 100644 --- a/tests/cases/string/in +++ b/tests/cases/string/in @@ -59,4 +59,13 @@ '' ### ''-couch_ini ${ cfg.package }/etc/default.ini ${ configFile } ${ pkgs.writeText "couchdb-extra.ini" cfg.extraConfig } ${ cfg.configFile }'' +### + ''exec i3-input -F "mark %s" -l 1 -P 'Mark: ' '' +### + ''exec i3-input -F '[con_mark="%s"] focus' -l 1 -P 'Go to: ' '' +### + ''"${ pkgs.name or "" }";'' +### + '' + ${pkgs.replace-secret}/bin/replace-secret '${placeholder}' '${secretFile}' '${targetFile}' '' ] diff --git a/tests/cases/string/out b/tests/cases/string/out index 5633549..aff5fd1 100644 --- a/tests/cases/string/out +++ b/tests/cases/string/out @@ -9,11 +9,11 @@ b " ### - '' '' + '''' ### - '' a'' + ''a'' ### - '' ${ "" }'' + ''${ "" }'' ### '' ${ "" } @@ -57,7 +57,16 @@ [${ mkSectionName sectName }] '' ### - '' -couch_ini ${ cfg.package }/etc/default.ini ${ configFile } ${ + ''-couch_ini ${ cfg.package }/etc/default.ini ${ configFile } ${ pkgs.writeText "couchdb-extra.ini" cfg.extraConfig } ${ cfg.configFile }'' + ### + ''exec i3-input -F "mark %s" -l 1 -P 'Mark: ' '' + ### + ''exec i3-input -F '[con_mark="%s"] focus' -l 1 -P 'Go to: ' '' + ### + ''"${ pkgs.name or "" }";'' + ### + '' + ${ pkgs.replace-secret }/bin/replace-secret '${ placeholder }' '${ secretFile }' '${ targetFile }' '' ]