Skip to content
Merged
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
1 change: 1 addition & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ setting : 'allow-duplicate-recipes' boolean?
| 'fallback' boolean?
| 'guards' boolean?
| 'ignore-comments' boolean?
| 'indentation' ':=' string
| 'lazy' boolean?
| 'lists' boolean?
| 'minimum-version' ':=' string
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4502,6 +4502,14 @@ stdout.
Note that formatting is not covered by any backwards compatibility guarantee
and is subject to change from time to time.

Recipe bodies are indented with four spaces by default. This can be changed
with the `--indentation` command-line option, the `JUST_INDENTATION`
environment variable, or the `indentation` setting:

```just
set indentation := " "
```

Invoking `just --fmt --check` runs `--fmt` in check mode. Instead of
overwriting the `justfile`, `just` will exit with an exit code of 0 if it is
formatted correctly, and will exit with 1 and print a diff if it is not.
Expand Down Expand Up @@ -4794,6 +4802,7 @@ foo:
| `fallback` | boolean | `false` | Search for `justfile` in parent directory if the first recipe on the command line is not found. |
| `guards`<sup>1.47.0</sup> | boolean | `false` | Enable the `?` guard sigil on recipe lines. See [sigils](#sigils). |
| `ignore-comments` | boolean | `false` | Ignore recipe lines beginning with `#`. |
| `indentation`<sup>master</sup> | string | - | Set recipe body indentation used when formatting with `--fmt` or `--dump`. |
| `lazy`<sup>1.47.0</sup> | boolean | `false` | Don't evaluate unused variables. |
| `lists`<sup>1.53.0</sup> | boolean | `false` | Values may be lists of strings instead of strings. Currently unstable. |
| `minimum-version`<sup>1.55.0</sup> | string | - | Error if `just` is older than `minimum-version`. Accepts a string of the form `MAJOR.MINOR.PATCH`, e.g., `"1.55.0"`. |
Expand Down
3 changes: 1 addition & 2 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,11 @@ pub struct Arguments {
)]
pub(crate) highlight: bool,
#[arg(
default_value = " ",
env = "JUST_INDENTATION",
help = "Indent recipes bodies with <INDENTATION>",
long
)]
pub(crate) indentation: Indentation,
pub(crate) indentation: Option<Indentation>,
#[arg(
add = ArgValueCompleter::new(PathCompleter::file()),
env = "JUST_JUSTFILE",
Expand Down
14 changes: 14 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ pub(crate) struct Ast<'src> {
pub(crate) working_directory: PathBuf,
}

impl Ast<'_> {
pub(crate) fn indentation(&self) -> Option<Indentation> {
self.items.iter().find_map(|item| {
if let Item::Setting(set) = item
&& let Setting::Indentation(_, indentation) = set.value
{
Some(indentation)
} else {
None
}
})
}
}

impl ColorDisplay for Ast<'_> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
let mut newlines = 0;
Expand Down
9 changes: 8 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ impl Color {
}
}

pub(crate) fn use_color(self, use_color: UseColor) -> Self {
pub(crate) fn with_use_color(self, use_color: UseColor) -> Self {
Self { use_color, ..self }
}

pub(crate) fn with_indentation(self, indentation: Indentation) -> Self {
Self {
indentation,
..self
}
}
}
12 changes: 6 additions & 6 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ impl Display for CompileError<'_> {
"shell recipe `{recipe}` has script recipe attribute `{}`",
attribute.name(),
),
InvalidIndentation { message } => {
write!(f, "{message}")
}
InvalidMinimumVersion { source, version } => {
write!(
f,
Expand Down Expand Up @@ -305,12 +308,6 @@ impl Display for CompileError<'_> {
f,
"justfile requires just {minimum} or later, but using {current}",
),
MinimumVersionExpression => {
write!(
f,
"`minimum-version` setting must be a plain string literal"
)
}
MismatchedClosingDelimiter {
open,
open_line,
Expand Down Expand Up @@ -372,6 +369,9 @@ impl Display for CompileError<'_> {
f,
"recipe `{recipe}` has both `[script]` and `[shell]` attributes"
),
SettingExpression { setting } => {
write!(f, "`{setting}` setting must be a plain string literal")
}
ShellExpansion { err } => write!(f, "shell expansion failed: {err}"),
ShortOptionWithMultipleCharacters { parameter } => {
write!(
Expand Down
7 changes: 6 additions & 1 deletion src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ pub(crate) enum CompileErrorKind<'src> {
InvalidEscapeSequence {
character: char,
},
InvalidIndentation {
message: &'static str,
},
InvalidMinimumVersion {
source: &'static str,
version: String,
Expand All @@ -158,7 +161,6 @@ pub(crate) enum CompileErrorKind<'src> {
current: Version,
minimum: Version,
},
MinimumVersionExpression,
MismatchedClosingDelimiter {
close: Delimiter,
open: Delimiter,
Expand Down Expand Up @@ -192,6 +194,9 @@ pub(crate) enum CompileErrorKind<'src> {
ScriptAndShellAttribute {
recipe: &'src str,
},
SettingExpression {
setting: Keyword,
},
ShellExpansion {
err: shellexpand::LookupError<env::VarError>,
},
Expand Down
5 changes: 4 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) struct Config {
pub(crate) explain: bool,
pub(crate) groups: Vec<String>,
pub(crate) highlight: bool,
pub(crate) indentation: Option<Indentation>,
pub(crate) invocation_directory: PathBuf,
pub(crate) justfile_names: Option<Vec<String>>,
pub(crate) list_heading: String,
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Config {
explain: false,
groups: Vec::new(),
highlight: true,
indentation: None,
invocation_directory: env::current_dir().context(config_error::CurrentDir)?,
justfile_names: None,
list_heading: Arguments::DEFAULT_LIST_HEADING.into(),
Expand Down Expand Up @@ -315,7 +317,7 @@ impl Config {
}

let unstable = arguments.unstable || subcommand == Subcommand::Summary;
let color = Color::new(arguments.indentation, arguments.color);
let color = Color::new(arguments.indentation.unwrap_or_default(), arguments.color);

let invocation_directory = env::current_dir().context(config_error::CurrentDir)?;

Expand All @@ -338,6 +340,7 @@ impl Config {
explain: arguments.explain,
groups: arguments.group,
highlight: !arguments.no_highlight,
indentation: arguments.indentation,
invocation_directory,
justfile_names: arguments.justfile_names,
list_heading: arguments.list_heading,
Expand Down
3 changes: 3 additions & 0 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ impl<'src, 'run> Evaluator<'src, 'run> {
Setting::IgnoreComments(value) => {
settings.ignore_comments = value;
}
Setting::Indentation(_, indentation) => {
settings.indentation = Some(indentation);
}
Setting::Lazy(value) => {
settings.lazy = value;
}
Expand Down
9 changes: 9 additions & 0 deletions src/indentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ impl Display for Indentation {
}
}

impl Serialize for Indentation {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl FromStr for Indentation {
type Err = &'static str;

Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) enum Keyword {
If,
IgnoreComments,
Import,
Indentation,
Lazy,
Lists,
MinimumVersion,
Expand Down
4 changes: 3 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,13 @@ impl<'src> Node<'src> for Set<'src> {
Setting::DotenvCommand(value)
| Setting::DotenvFilename(value)
| Setting::DotenvPath(value)
| Setting::MinimumVersion(value)
| Setting::Tempdir(value)
| Setting::WorkingDirectory(value) => {
set.push_mut(value.tree());
}
Setting::Indentation(value, _) | Setting::MinimumVersion(value) => {
set.push_mut(Tree::string(&value.cooked));
}
Setting::ScriptInterpreter(Interpreter { command, arguments })
| Setting::Shell(Interpreter { command, arguments })
| Setting::WindowsShell(Interpreter { command, arguments }) => {
Expand Down
30 changes: 26 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,15 +1614,37 @@ impl<'run, 'src> Parser<'run, 'src> {
Keyword::DotenvCommand => Some(Setting::DotenvCommand(self.parse_expression()?)),
Keyword::DotenvFilename => Some(Setting::DotenvFilename(self.parse_expression()?)),
Keyword::DotenvPath => Some(Setting::DotenvPath(self.parse_expression()?)),
Keyword::Indentation => {
let expression = self.parse_expression()?;

let Expression::StringLiteral { string_literal } = expression else {
return Err(name.error(CompileErrorKind::SettingExpression { setting: keyword }));
};

if string_literal.expand || string_literal.kind.indented || string_literal.part.is_some() {
return Err(name.error(CompileErrorKind::SettingExpression { setting: keyword }));
}

let indentation = string_literal
.cooked
.parse::<Indentation>()
.map_err(|message| {
string_literal
.token
.error(CompileErrorKind::InvalidIndentation { message })
})?;

Some(Setting::Indentation(string_literal, indentation))
}
Keyword::MinimumVersion => {
let expression = self.parse_expression()?;

let Expression::StringLiteral { string_literal } = &expression else {
return Err(name.error(CompileErrorKind::MinimumVersionExpression));
let Expression::StringLiteral { string_literal } = expression else {
return Err(name.error(CompileErrorKind::SettingExpression { setting: keyword }));
};

if string_literal.expand || string_literal.kind.indented || string_literal.part.is_some() {
return Err(name.error(CompileErrorKind::MinimumVersionExpression));
return Err(name.error(CompileErrorKind::SettingExpression { setting: keyword }));
}

let minimum = string_literal.cooked.parse::<Version>().map_err(|source| {
Expand All @@ -1643,7 +1665,7 @@ impl<'run, 'src> Parser<'run, 'src> {
);
}

Some(Setting::MinimumVersion(expression))
Some(Setting::MinimumVersion(string_literal))
}
Keyword::ScriptInterpreter => Some(Setting::ScriptInterpreter(self.parse_interpreter()?)),
Keyword::Shell => Some(Setting::Shell(self.parse_interpreter()?)),
Expand Down
7 changes: 4 additions & 3 deletions src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ pub(crate) enum Setting<'src> {
Fallback(bool),
Guards(bool),
IgnoreComments(bool),
Indentation(StringLiteral<'src>, Indentation),
Lazy(bool),
Lists(bool),
MinimumVersion(Expression<'src>),
MinimumVersion(StringLiteral<'src>),
NoCd(bool),
NoExitMessage(bool),
PositionalArguments(bool),
Expand Down Expand Up @@ -57,9 +58,9 @@ impl<'src> Setting<'src> {
Self::DotenvCommand(_value)
| Self::DotenvFilename(_value)
| Self::DotenvPath(_value)
| Self::MinimumVersion(_value)
| Self::Tempdir(_value)
| Self::WorkingDirectory(_value) => false,
Self::Indentation(..) | Self::MinimumVersion(_) => false,
Self::ScriptInterpreter(_value) | Self::Shell(_value) | Self::WindowsShell(_value) => false,
}
}
Expand Down Expand Up @@ -131,11 +132,11 @@ impl Display for Setting<'_> {
Self::DotenvCommand(value)
| Self::DotenvFilename(value)
| Self::DotenvPath(value)
| Self::MinimumVersion(value)
| Self::Tempdir(value)
| Self::WorkingDirectory(value) => {
write!(f, "{value}")
}
Self::Indentation(value, _) | Self::MinimumVersion(value) => write!(f, "{value}"),
Self::ScriptInterpreter(value) | Self::Shell(value) | Self::WindowsShell(value) => {
write!(f, "[{value}]")
}
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) struct Settings {
pub(crate) fallback: bool,
pub(crate) guards: bool,
pub(crate) ignore_comments: bool,
pub(crate) indentation: Option<Indentation>,
pub(crate) lazy: bool,
pub(crate) lists: bool,
pub(crate) no_cd: bool,
Expand Down
33 changes: 24 additions & 9 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,22 @@ impl Subcommand {
.map_err(|source| Error::DumpJson { source })?;
println!();
}
DumpFormat::Just => print!(
"{}",
compilation
.root_ast()
.color_display(config.color.use_color(UseColor::Never))
),
DumpFormat::Just => {
print!(
"{}",
compilation.root_ast().color_display(
config
.color
.with_use_color(UseColor::Never)
.with_indentation(
config
.indentation
.or(compilation.justfile.settings.indentation)
.unwrap_or_default()
)
)
);
}
}
Ok(())
}
Expand Down Expand Up @@ -476,7 +486,12 @@ impl Subcommand {
)?;

let formatted = ast
.color_display(config.color.use_color(UseColor::Never))
.color_display(
config
.color
.with_use_color(UseColor::Never)
.with_indentation(config.indentation.or(ast.indentation()).unwrap_or_default()),
)
.to_string();

if config.check {
Expand Down Expand Up @@ -516,7 +531,7 @@ impl Subcommand {
})?;

if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
eprintln!("wrote justfile to `{}`", search.justfile.display());
}
}

Expand All @@ -541,7 +556,7 @@ impl Subcommand {
}

if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
eprintln!("wrote justfile to `{}`", search.justfile.display());
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/ceiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ fn justfile_init_search_stops_at_ceiling_dir() {
.current_dir("foo/bar")
.args(["--init", "--ceiling", ceiling.to_str().unwrap()])
.stderr_regex(if cfg!(windows) {
r"Wrote justfile to `.*\\foo\\bar\\justfile`\n"
r"wrote justfile to `.*\\foo\\bar\\justfile`\n"
} else {
"Wrote justfile to `.*/foo/bar/justfile`\n"
"wrote justfile to `.*/foo/bar/justfile`\n"
})
.success();

Expand Down
Loading
Loading