Skip to content

Commit 97a29a7

Browse files
authored
Format without compiling (#3103)
1 parent 2eebdbb commit 97a29a7

5 files changed

Lines changed: 64 additions & 62 deletions

File tree

src/compilation.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ pub(crate) struct Compilation<'src> {
55
pub(crate) asts: HashMap<PathBuf, Ast<'src>>,
66
pub(crate) justfile: Justfile<'src>,
77
pub(crate) root: PathBuf,
8-
pub(crate) srcs: HashMap<PathBuf, &'src str>,
98
}
109

1110
impl<'src> Compilation<'src> {
1211
pub(crate) fn root_ast(&self) -> &Ast<'src> {
1312
self.asts.get(&self.root).unwrap()
1413
}
15-
16-
pub(crate) fn root_src(&self) -> &'src str {
17-
self.srcs.get(&self.root).unwrap()
18-
}
1914
}

src/compiler.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ impl Compiler {
1111
let mut asts = HashMap::<PathBuf, Ast>::new();
1212
let mut loaded = Vec::new();
1313
let mut paths = HashMap::<PathBuf, PathBuf>::new();
14-
let mut srcs = HashMap::<PathBuf, &str>::new();
15-
1614
let mut stack = Vec::new();
1715
stack.push(Source::root(root));
1816

@@ -23,18 +21,9 @@ impl Compiler {
2321

2422
let (relative, src) = loader.load(root, &current.path)?;
2523
loaded.push(relative.into());
26-
27-
let tokens = Lexer::lex(relative, src)?;
28-
let mut ast = Parser::parse(
29-
current.file_depth,
30-
&current.import_offsets,
31-
current.namepath.as_ref(),
32-
&tokens,
33-
&current.working_directory,
34-
)?;
24+
let mut ast = Parser::parse_source(relative, src, &current)?;
3525

3626
paths.insert(current.path.clone(), relative.into());
37-
srcs.insert(current.path.clone(), src);
3827

3928
for item in &mut ast.items {
4029
match item {
@@ -107,7 +96,6 @@ impl Compiler {
10796
asts,
10897
justfile,
10998
root: root.into(),
110-
srcs,
11199
})
112100
}
113101

@@ -244,43 +232,6 @@ impl Compiler {
244232
mod tests {
245233
use {super::*, temptree::temptree};
246234

247-
#[test]
248-
fn include_justfile() {
249-
let justfile_a = r#"
250-
# A comment at the top of the file
251-
import "./justfile_b"
252-
253-
#some_recipe: recipe_b
254-
some_recipe:
255-
echo "some recipe"
256-
"#;
257-
258-
let justfile_b = r#"import "./subdir/justfile_c"
259-
260-
recipe_b: recipe_c
261-
echo "recipe b"
262-
"#;
263-
264-
let justfile_c = r#"recipe_c:
265-
echo "recipe c"
266-
"#;
267-
268-
let tmp = temptree! {
269-
justfile: justfile_a,
270-
justfile_b: justfile_b,
271-
subdir: {
272-
justfile_c: justfile_c
273-
}
274-
};
275-
276-
let loader = Loader::new();
277-
278-
let justfile_a_path = tmp.path().join("justfile");
279-
let compilation = Compiler::compile(&Config::default(), &loader, &justfile_a_path).unwrap();
280-
281-
assert_eq!(compilation.root_src(), justfile_a);
282-
}
283-
284235
#[test]
285236
fn recursive_includes_fail() {
286237
let tmp = temptree! {

src/parser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ impl<'run, 'src> Parser<'run, 'src> {
5858
.parse_ast()
5959
}
6060

61+
pub(crate) fn parse_source(
62+
path: &'src Path,
63+
src: &'src str,
64+
source: &Source<'src>,
65+
) -> CompileResult<'src, Ast<'src>> {
66+
let tokens = Lexer::lex(path, src)?;
67+
68+
Parser::parse(
69+
source.file_depth,
70+
&source.import_offsets,
71+
source.namepath.as_ref(),
72+
&tokens,
73+
&source.working_directory,
74+
)
75+
}
76+
6177
fn error(&self, kind: CompileErrorKind<'src>) -> CompileResult<'src, CompileError<'src>> {
6278
Ok(self.next()?.error(kind))
6379
}

src/subcommand.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl Subcommand {
8787
return Self::edit(&search);
8888
}
8989

90+
if matches!(self, Format) {
91+
return Self::format(config, loader, &search);
92+
}
93+
9094
let compilation = Self::compile(config, loader, &search)?;
9195
let justfile = &compilation.justfile;
9296

@@ -98,15 +102,16 @@ impl Subcommand {
98102
justfile.run(config, &search, &[])?;
99103
}
100104
Dump => Self::dump(config, compilation)?,
101-
Format => Self::format(config, &search, compilation)?,
102105
Groups => Self::groups(config, justfile),
103106
List { path } => Self::list(config, justfile, path)?,
104107
Run { arguments } => Self::run(config, loader, search, compilation, arguments)?,
105108
Show { path } => Self::show(config, justfile, path)?,
106109
Summary => Self::summary(config, justfile),
107110
Usage { path } => Self::usage(config, justfile, path)?,
108111
Variables => Self::variables(justfile),
109-
Changelog | Completions { .. } | Edit | Init | Man | Request { .. } => unreachable!(),
112+
Changelog | Completions { .. } | Edit | Format | Init | Man | Request { .. } => {
113+
unreachable!()
114+
}
110115
}
111116

112117
Ok(())
@@ -324,12 +329,29 @@ impl Subcommand {
324329
Ok(())
325330
}
326331

327-
fn format(config: &Config, search: &Search, compilation: Compilation) -> RunResult<'static> {
328-
let justfile = &compilation.justfile;
329-
let src = compilation.root_src();
330-
let ast = compilation.root_ast();
332+
fn format<'src>(config: &Config, loader: &'src Loader, search: &Search) -> RunResult<'src> {
333+
let root = search.justfile.parent().unwrap();
334+
335+
let (path, src) = loader.load(root, &search.justfile)?;
336+
337+
let ast = Parser::parse_source(path, src, &Source::root(&search.justfile))?;
331338

332-
config.require_unstable(justfile, UnstableFeature::FormatSubcommand)?;
339+
let unstable = config.unstable
340+
|| ast.items.iter().any(|item| {
341+
matches!(
342+
item,
343+
Item::Set(Set {
344+
value: Setting::Unstable(true),
345+
..
346+
})
347+
)
348+
});
349+
350+
if !unstable {
351+
return Err(Error::UnstableFeature {
352+
unstable_feature: UnstableFeature::FormatSubcommand,
353+
});
354+
}
333355

334356
let formatted = ast.to_string();
335357

tests/format.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,3 +1700,21 @@ fn arg_attribute_help() {
17001700
)
17011701
.success();
17021702
}
1703+
1704+
#[test]
1705+
fn missing_import_file() {
1706+
Test::new()
1707+
.args(["--unstable", "--fmt", "--check"])
1708+
.justfile("import 'foo'\n")
1709+
.test_round_trip(false)
1710+
.success();
1711+
}
1712+
1713+
#[test]
1714+
fn missing_module_file() {
1715+
Test::new()
1716+
.args(["--unstable", "--fmt", "--check"])
1717+
.justfile("mod foo\n")
1718+
.test_round_trip(false)
1719+
.success();
1720+
}

0 commit comments

Comments
 (0)