From bfc720bf4d8adbb3b1eaff4f3556bc6d1e00849a Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 26 Oct 2021 21:22:41 +0530 Subject: allow stdin input to statix-single --- bin/src/config.rs | 4 ++-- bin/src/fix/single.rs | 23 +++++++++-------------- bin/src/main.rs | 25 +++++++++++++++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) (limited to 'bin/src') diff --git a/bin/src/config.rs b/bin/src/config.rs index c856a59..79bed35 100644 --- a/bin/src/config.rs +++ b/bin/src/config.rs @@ -77,8 +77,8 @@ impl Fix { #[derive(Clap, Debug)] pub struct Single { /// File to run single-fix on - #[clap(default_value = ".", parse(from_os_str))] - pub target: PathBuf, + #[clap(parse(from_os_str))] + pub target: Option, /// Position to attempt a fix at #[clap(short, long, parse(try_from_str = parse_line_col))] diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index c09c710..4d492f8 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs @@ -15,7 +15,7 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result Result Result { - let offset = offset - TextSize::from(1u32); // we don't really need the source to form a completely parsed tree let parsed = rnix::parse(src); @@ -38,22 +37,18 @@ fn find(offset: TextSize, src: &str) -> Result { .preorder_with_tokens() .filter_map(|event| match event { WalkEvent::Enter(child) => { - if child.text_range().start() == offset { - LINTS.get(&child.kind()).map(|rules| { - rules - .iter() - .filter_map(|rule| rule.validate(&child)) - .filter(|report| report.total_suggestion_range().is_some()) - .next() - }) - } else { - None - } + LINTS.get(&child.kind()).map(|rules| { + rules + .iter() + .filter_map(|rule| rule.validate(&child)) + .filter(|report| report.total_suggestion_range().is_some()) + .next() + }) } _ => None, }) .flatten() - .next() + .find(|report| report.total_diagnostic_range().unwrap().contains(offset)) .ok_or(SingleFixErr::NoOp) } diff --git a/bin/src/main.rs b/bin/src/main.rs index 4063621..9e9c8ac 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -4,7 +4,7 @@ mod fix; mod lint; mod traits; -use std::io; +use std::io::{self, BufRead}; use crate::{ err::{FixErr, SingleFixErr, StatixErr}, @@ -55,15 +55,26 @@ fn _main() -> Result<(), StatixErr> { } } } + // FIXME: this block nasty, configure in/out streams in `impl Single` maybe SubCommand::Single(single_config) => { - let path = single_config.target; - let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?; + let src = if let Some(path) = &single_config.target { + std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)? + } else { + io::stdin().lock().lines().map(|l| l.unwrap()).collect::>().join("\n") + }; + + let path_id = if let Some(path) = &single_config.target { + path.display().to_string() + } else { + "".to_owned() + }; + let (line, col) = single_config.position; let single_fix_result = fix::single(line, col, &src)?; if single_config.diff_only { let text_diff = TextDiff::from_lines(src.as_str(), &single_fix_result.src); - let old_file = format!("{}", path.display()); - let new_file = format!("{} [fixed]", path.display()); + let old_file = format!("{}", path_id); + let new_file = format!("{} [fixed]", path_id); println!( "{}", text_diff @@ -71,9 +82,11 @@ fn _main() -> Result<(), StatixErr> { .context_radius(4) .header(&old_file, &new_file) ); - } else { + } else if let Some(path) = single_config.target { std::fs::write(&path, &*single_fix_result.src) .map_err(SingleFixErr::InvalidPath)?; + } else { + print!("{}", &*single_fix_result.src) } } } -- cgit v1.2.3