Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,15 @@ Available recipes:
foo ... # foo is a great module!
```

Modules can be defined inline. Currently inline modules can not contain further modules or inline modules.

```justfile
# foo is an inline module!
foo::
hello:
@echo "Hello from inside the inline foo module"
```

Modules are still missing a lot of features, for example, the ability to depend
on recipes and refer to variables in other modules. See the
[module improvement tracking issue](https://github.com/casey/just/issues/2252)
Expand Down
16 changes: 16 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ impl<'run, 'src> Analyzer<'run, 'src> {
}
}
}
Item::ModuleInline(ModuleInline {
doc, name, groups, ..
}) => {
Self::define(&mut definitions, *name, "module", false)?;
let absolute = root.join(name.lexeme());

self.modules.insert(Self::analyze(
asts,
doc.clone(),
groups.as_slice(),
loaded,
Some(*name),
paths,
&absolute,
)?);
}
Item::Module {
absolute,
doc,
Expand Down
3 changes: 3 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ impl Display for CompileError<'_> {
ShowWhitespace(expected),
ShowWhitespace(found)
),
InlineModuleCannotBeNested { parent_module } => {
write!(f, "Inline module `{parent_module}` cannot be nested")
}
Internal { message } => write!(
f,
"Internal error, this may indicate a bug in just: {message}\n\
Expand Down
3 changes: 3 additions & 0 deletions src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub(crate) enum CompileErrorKind<'src> {
expected: &'src str,
found: &'src str,
},
InlineModuleCannotBeNested {
parent_module: &'src str,
},
Internal {
message: String,
},
Expand Down
6 changes: 6 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ impl Compiler {
return Err(Error::MissingImportFile { path: *path });
}
}
Item::ModuleInline(ModuleInline { ast, name, .. }) => {
// Don't need to think about circular imports because nesting isn't allowed.
let path = current.path.join(name.lexeme());
paths.insert(path.clone(), relative.join(name.lexeme()));
asts.insert(path, ast.clone());
}
_ => {}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) enum Item<'src> {
optional: bool,
relative: Option<StringLiteral<'src>>,
},
ModuleInline(ModuleInline<'src>),
Recipe(UnresolvedRecipe<'src>),
Set(Set<'src>),
Unexport {
Expand Down Expand Up @@ -64,6 +65,7 @@ impl Display for Item<'_> {

Ok(())
}
Self::ModuleInline(inline_module) => write!(f, "{}::", inline_module.name),
Self::Recipe(recipe) => write!(f, "{}", recipe.color_display(Color::never())),
Self::Set(set) => write!(f, "{set}"),
Self::Unexport { name } => write!(f, "unexport {name}"),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) use {
list::List,
load_dotenv::load_dotenv,
loader::Loader,
module_inline::ModuleInline,
module_path::ModulePath,
name::Name,
namepath::Namepath,
Expand Down Expand Up @@ -229,6 +230,7 @@ mod line;
mod list;
mod load_dotenv;
mod loader;
mod module_inline;
mod module_path;
mod name;
mod namepath;
Expand Down
9 changes: 9 additions & 0 deletions src/module_inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::{Ast, Name};

#[derive(Debug, Clone)]
pub struct ModuleInline<'src> {
pub(crate) ast: Ast<'src>,
pub(crate) doc: Option<String>,
pub(crate) groups: Vec<String>,
pub(crate) name: Name<'src>,
}
11 changes: 11 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<'src> Node<'src> for Item<'src> {

tree
}
Self::ModuleInline(inline_mod) => inline_mod.tree(),
Self::Recipe(recipe) => recipe.tree(),
Self::Set(set) => set.tree(),
Self::Unexport { name } => {
Expand Down Expand Up @@ -345,3 +346,13 @@ impl<'src> Node<'src> for str {
Tree::atom("comment").push(["\"", self, "\""].concat())
}
}

impl<'src> Node<'src> for ModuleInline<'src> {
fn tree(&self) -> Tree<'src> {
Tree::list([
Tree::atom("mod_inline"),
Tree::atom(self.name.lexeme()),
self.ast.tree(),
])
}
}
Loading