Skip to content

Commit

Permalink
feat: only in diff flag (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 authored Apr 3, 2024
1 parent 0100191 commit ea47471
Show file tree
Hide file tree
Showing 23 changed files with 488 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ buildkite-test-collector = "0.1.1"
similar = "2.2.1"
reqwest = { version = "0.11.22", features = ["blocking", "json"] }
marzano-test-utils = { path = "../test_utils" }
insta = { version = "1.30.0", features = ["yaml"] }


[features]
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use grit_cache::paths::cache_for_cwd;
use ignore::Walk;
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};

#[allow(unused_imports)]
use marzano_core::pattern::built_in_functions::BuiltIns;
use marzano_core::pattern::{
api::{AnalysisLog, DoneFile, MatchResult},
compiler::CompilationResult,
Problem,
};
#[allow(unused_imports)]
use marzano_core::pattern::built_in_functions::BuiltIns;
use marzano_language::target_language::PatternLanguage;
use marzano_util::cache::GritCache;
use marzano_util::position::Position;
Expand Down
23 changes: 17 additions & 6 deletions crates/cli/src/commands/apply_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use std::collections::BTreeMap;
use tokio::fs;

use crate::{
analyze::par_apply_pattern, community::parse_eslint_output, error::GoodError,
flags::OutputFormat, messenger_variant::create_emitter, result_formatting::get_human_error,
updater::Updater,
analyze::par_apply_pattern, community::parse_eslint_output, diff::extract_modified_ranges,
error::GoodError, flags::OutputFormat, messenger_variant::create_emitter,
result_formatting::get_human_error, updater::Updater,
};

use marzano_messenger::{
Expand Down Expand Up @@ -92,10 +92,18 @@ pub struct ApplyPatternArgs {
hide = true,
)]
pub visibility: VisibilityLevels,
#[clap(
long = "only-in-diff",
help = "Only rewrite ranges that are inside the provided unified diff",
hide = true,
conflicts_with = "only_in_json"
)]
only_in_diff: Option<PathBuf>,
#[clap(
long = "only-in-json",
help = "Only rewrite ranges that are inside the provided eslint-style JSON file",
hide = true
hide = true,
conflicts_with = "only_in_diff"
)]
only_in_json: Option<PathBuf>,
#[clap(
Expand All @@ -109,7 +117,7 @@ pub struct ApplyPatternArgs {
/// Clear cache before running apply
#[clap(long = "refresh-cache", conflicts_with = "cache")]
pub refresh_cache: bool,
#[clap(long = "language", alias="lang")]
#[clap(long = "language", alias = "lang")]
pub language: Option<PatternLanguage>,
}

Expand All @@ -123,6 +131,7 @@ impl Default for ApplyPatternArgs {
format: Default::default(),
interactive: Default::default(),
visibility: VisibilityLevels::Hidden,
only_in_diff: Default::default(),
only_in_json: Default::default(),
output_file: Default::default(),
cache: Default::default(),
Expand Down Expand Up @@ -188,6 +197,9 @@ pub(crate) async fn run_apply_pattern(
let filter_range = if let Some(json_path) = arg.only_in_json.clone() {
let json_ranges = flushable_unwrap!(emitter, parse_eslint_output(json_path));
Some(json_ranges)
} else if let Some(diff_path) = arg.only_in_diff.clone() {
let diff_ranges = flushable_unwrap!(emitter, extract_modified_ranges(&diff_path));
Some(diff_ranges)
} else {
None
};
Expand Down Expand Up @@ -230,7 +242,6 @@ pub(crate) async fn run_apply_pattern(

let pattern_libs = flushable_unwrap!(emitter, get_grit_files_from_cwd().await);
let (mut lang, pattern_body) = if pattern.ends_with(".grit") || pattern.ends_with(".md") {

match fs::read_to_string(pattern.clone()).await {
Ok(pb) => {
if pattern.ends_with(".grit") {
Expand Down
16 changes: 15 additions & 1 deletion crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use marzano_messenger::emit::{Messager, VisibilityLevels};
use cli_server::check::CheckMessenger;

use crate::{
diff::extract_modified_ranges,
error::GoodError,
flags::{GlobalFormatFlags, OutputFormat},
github::{log_check_annotations, write_check_summary},
Expand Down Expand Up @@ -69,6 +70,12 @@ pub struct CheckArg {
/// Output annotations for a GitHub actions workflow
#[clap(long = "github-actions")]
pub github_actions: bool,
#[clap(
long = "only-in-diff",
help = "Only check ranges that are inside the provided unified diff",
hide = true
)]
pub only_in_diff: Option<PathBuf>,
}

pub(crate) async fn run_check(
Expand Down Expand Up @@ -117,6 +124,13 @@ pub(crate) async fn run_check(
std::env::current_dir()?
};

let filter_range = if let Some(diff_path) = arg.only_in_diff.clone() {
let diff_ranges = extract_modified_ranges(&diff_path)?;
Some(diff_ranges)
} else {
None
};

// Construct a resolver
let resolver = GritModuleResolver::new(current_dir.to_str().unwrap());

Expand All @@ -132,7 +146,7 @@ pub(crate) async fn run_check(
.make_pattern(&body, Some(p.local_name.to_string()))
.unwrap();
let lang = PatternLanguage::get_language(&p.body);
match rich_pattern.compile(&grit_files, lang, None) {
match rich_pattern.compile(&grit_files, lang, filter_range.clone()) {
Ok(c) => Ok((p.local_name.clone(), c.problem)),
Err(e) => {
bail!("Unable to compile pattern {}:\n{}", p.local_name, e);
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{
flags::{GlobalFormatFlags, OutputFormat},
updater::Updater,
};
use anyhow::{Result};
use anyhow::Result;
use apply::ApplyArgs;
use auth::{Auth, AuthCommands};
use check::CheckArg;
Expand All @@ -52,7 +52,7 @@ use indicatif_log_bridge::LogWrapper;
use init::InitArgs;
use install::InstallArgs;
use list::ListArgs;
use log::{LevelFilter};
use log::LevelFilter;
use lsp::LspArgs;
use marzano_messenger::emit::ApplyDetails;
use parse::ParseArgs;
Expand Down
5 changes: 2 additions & 3 deletions crates/cli/src/commands/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ use std::io::{stdin, Read};
use std::path::Path;
use std::path::PathBuf;

use crate::analytics::{track_event_line};
use crate::analytics::track_event_line;
use crate::flags::GlobalFormatFlags;
use crate::lister::list_applyables;
use crate::resolver::{get_grit_files_from, resolve_from, Source};
use crate::utils::is_pattern_name;


use super::super::analytics::{AnalyticsArgs};
use super::super::analytics::AnalyticsArgs;
use super::apply_pattern::{run_apply_pattern, ApplyPatternArgs};
use super::check::{run_check, CheckArg};
use super::init::{init_config_from_cwd, init_global_grit_modules};
Expand Down
Loading

0 comments on commit ea47471

Please sign in to comment.