Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include content in match results #391

Merged
merged 14 commits into from
Jul 1, 2024
4 changes: 2 additions & 2 deletions crates/cli/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub fn extract_rewritten_content(result: &MatchResult) -> Option<&String> {
MatchResult::AnalysisLog(_) => None,
MatchResult::Match(_) => None,
MatchResult::InputFile(_) => None,
MatchResult::CreateFile(c) => Some(&c.rewritten.content),
MatchResult::CreateFile(c) => c.rewritten.content.as_ref(),
MatchResult::RemoveFile(_) => None,
MatchResult::Rewrite(r) => Some(&r.rewritten.content),
MatchResult::Rewrite(r) => r.rewritten.content.as_ref(),
MatchResult::DoneFile(_) => None,
MatchResult::AllDone(_) => None,
MatchResult::PatternInfo(_) => None,
Expand Down
11 changes: 5 additions & 6 deletions crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::anyhow;
use colored::Colorize;
use console::style;
use core::fmt;
use fs_err::read_to_string;
use log::{debug, error, info, warn};
use marzano_core::api::{
AllDone, AnalysisLog, AnalysisLogLevel, CreateFile, DoneFile, FileMatchResult, InputFile,
Expand Down Expand Up @@ -168,7 +167,7 @@ impl fmt::Display for FormattedResult {
FormattedResult::Match(m) => {
let path_title = m.file_name().bold();
writeln!(f, "{}", path_title)?;
let source = read_to_string(m.file_name());
let source = m.content();
match source {
Err(e) => {
writeln!(f, "Could not read file: {}", e)?;
Expand Down Expand Up @@ -401,18 +400,18 @@ impl Messager for TransformedMessenger<'_> {
// Write the file contents to the output
if let Some(writer) = &mut self.writer {
let mut writer = writer.lock().map_err(|_| anyhow!("Output lock poisoned"))?;
writeln!(writer, "{}", file.rewritten.content)?;
writeln!(writer, "{}", file.content().unwrap_or_default())?;
} else {
info!("{}", file.rewritten.content);
info!("{}", file.content().unwrap_or_default());
}
}
MatchResult::CreateFile(file) => {
// Write the file contents to the output
if let Some(writer) = &mut self.writer {
let mut writer = writer.lock().map_err(|_| anyhow!("Output lock poisoned"))?;
writeln!(writer, "{}", file.rewritten.content)?;
writeln!(writer, "{}", file.content().unwrap_or_default())?;
} else {
info!("{}", file.rewritten.content);
info!("{}", file.content().unwrap_or_default());
}
}
MatchResult::RemoveFile(file) => {
Expand Down
16 changes: 3 additions & 13 deletions crates/cli/src/ux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use marzano_core::{
fs::extract_ranges,
};
use marzano_gritmodule::{
config::ResolvedGritDefinition, testing::SampleTestResult, utils::extract_path,
config::ResolvedGritDefinition, testing::SampleTestResult,
};
use similar::{ChangeTag, TextDiff};
use std::{collections::HashMap, fs::read_to_string};
use std::{collections::HashMap};

use crate::analyze::{extract_rewritten_content, group_checks};

Expand Down Expand Up @@ -149,20 +149,10 @@ fn format_diff(expected: &str, actual: &str) -> DiffString {
DiffString { diff: output }
}

pub fn extract_original_content(r: &MatchResult) -> Option<String> {
match extract_path(r) {
Some(p) => match read_to_string(p) {
Ok(c) => Some(c),
Err(_) => None,
},
None => None,
}
}

pub fn format_result_diff(r: &MatchResult, src: Option<&str>) -> DiffString {
let old_content = match src {
Some(s) => s.to_string(),
None => extract_original_content(r).unwrap_or_default(),
None => r.extract_original_content().unwrap_or_default().to_string(),
};
let default_rewritten = String::new();
let new_content = match extract_rewritten_content(r) {
Expand Down
4 changes: 2 additions & 2 deletions crates/cli_bin/tests/snapshots/apply__output_jsonl.snap

Large diffs are not rendered by default.

Loading
Loading