Skip to content

Commit 67be473

Browse files
authored
feat(forge fmt): add watch mode (foundry-rs#9838)
* feat(forge): add watch mode to forge fmt * keen fmt run sync
1 parent 2b107e5 commit 67be473

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

crates/forge/bin/cmd/fmt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::watch::WatchArgs;
12
use clap::{Parser, ValueHint};
23
use eyre::{Context, Result};
34
use forge_fmt::{format_to, parse};
@@ -39,6 +40,9 @@ pub struct FmtArgs {
3940
/// In 'check' and stdin modes, outputs raw formatted code instead of the diff.
4041
#[arg(long, short)]
4142
raw: bool,
43+
44+
#[command(flatten)]
45+
pub watch: WatchArgs,
4246
}
4347

4448
impl_figment_convert_basic!(FmtArgs);
@@ -186,6 +190,11 @@ impl FmtArgs {
186190

187191
Ok(())
188192
}
193+
194+
/// Returns whether `FmtArgs` was configured with `--watch`
195+
pub fn is_watch(&self) -> bool {
196+
self.watch.watch.is_some()
197+
}
189198
}
190199

191200
struct Line(Option<usize>);

crates/forge/bin/cmd/watch.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
build::BuildArgs, coverage::CoverageArgs, doc::DocArgs, snapshot::GasSnapshotArgs,
3-
test::TestArgs,
2+
build::BuildArgs, coverage::CoverageArgs, doc::DocArgs, fmt::FmtArgs,
3+
snapshot::GasSnapshotArgs, test::TestArgs,
44
};
55
use alloy_primitives::map::HashSet;
66
use clap::Parser;
@@ -325,6 +325,14 @@ pub async fn watch_coverage(args: CoverageArgs) -> Result<()> {
325325
run(config).await
326326
}
327327

328+
pub async fn watch_fmt(args: FmtArgs) -> Result<()> {
329+
let config = args.watch.watchexec_config(|| {
330+
let config = args.load_config()?;
331+
Ok([config.src, config.test, config.script])
332+
})?;
333+
run(config).await
334+
}
335+
328336
/// Executes a [`Watchexec`] that listens for changes in the project's sources directory
329337
pub async fn watch_doc(args: DocArgs) -> Result<()> {
330338
let config = args.watch.watchexec_config(|| {

crates/forge/bin/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ fn run() -> Result<()> {
104104
utils::block_on(cmd.run())
105105
}
106106
}
107-
ForgeSubcommand::Fmt(cmd) => cmd.run(),
107+
ForgeSubcommand::Fmt(cmd) => {
108+
if cmd.is_watch() {
109+
utils::block_on(watch::watch_fmt(cmd))
110+
} else {
111+
cmd.run()
112+
}
113+
}
108114
ForgeSubcommand::Config(cmd) => cmd.run(),
109115
ForgeSubcommand::Flatten(cmd) => cmd.run(),
110116
ForgeSubcommand::Inspect(cmd) => cmd.run(),

0 commit comments

Comments
 (0)