Sitelet https://github.com/casey/just/commit/56ccb1c9a9f42421079c787c904bfef2fbe4bc8b
Skip to content

Commit 56ccb1c

Browse files
authored
Add indentation setting (#3530)
1 parent c613447 commit 56ccb1c

21 files changed

Lines changed: 285 additions & 37 deletions

‎GRAMMAR.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ setting : 'allow-duplicate-recipes' boolean?
8484
| 'fallback' boolean?
8585
| 'guards' boolean?
8686
| 'ignore-comments' boolean?
87+
| 'indentation' ':=' string
8788
| 'lazy' boolean?
8889
| 'lists' boolean?
8990
| 'minimum-version' ':=' string

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4502,6 +4502,14 @@ stdout.
45024502
Note that formatting is not covered by any backwards compatibility guarantee
45034503
and is subject to change from time to time.
45044504

4505+
Recipe bodies are indented with four spaces by default. This can be changed
4506+
with the `--indentation` command-line option, the `JUST_INDENTATION`
4507+
environment variable, or the `indentation` setting:
4508+
4509+
```just
4510+
set indentation := " "
4511+
```
4512+
45054513
Invoking `just --fmt --check` runs `--fmt` in check mode. Instead of
45064514
overwriting the `justfile`, `just` will exit with an exit code of 0 if it is
45074515
formatted correctly, and will exit with 1 and print a diff if it is not.
@@ -4794,6 +4802,7 @@ foo:
47944802
| `fallback` | boolean | `false` | Search for `justfile` in parent directory if the first recipe on the command line is not found. |
47954803
| `guards`<sup>1.47.0</sup> | boolean | `false` | Enable the `?` guard sigil on recipe lines. See [sigils](#sigils). |
47964804
| `ignore-comments` | boolean | `false` | Ignore recipe lines beginning with `#`. |
4805+
| `indentation`<sup>master</sup> | string | - | Set recipe body indentation used when formatting with `--fmt` or `--dump`. |
47974806
| `lazy`<sup>1.47.0</sup> | boolean | `false` | Don't evaluate unused variables. |
47984807
| `lists`<sup>1.53.0</sup> | boolean | `false` | Values may be lists of strings instead of strings. Currently unstable. |
47994808
| `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"`. |

‎src/arguments.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,11 @@ pub struct Arguments {
186186
)]
187187
pub(crate) highlight: bool,
188188
#[arg(
189-
default_value = " ",
190189
env = "JUST_INDENTATION",
191190
help = "Indent recipes bodies with <INDENTATION>",
192191
long
193192
)]
194-
pub(crate) indentation: Indentation,
193+
pub(crate) indentation: Option<Indentation>,
195194
#[arg(
196195
add = ArgValueCompleter::new(PathCompleter::file()),
197196
env = "JUST_JUSTFILE",

‎src/ast.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ pub(crate) struct Ast<'src> {
1313
pub(crate) working_directory: PathBuf,
1414
}
1515

16+
impl Ast<'_> {
17+
pub(crate) fn indentation(&self) -> Option<Indentation> {
18+
self.items.iter().find_map(|item| {
19+
if let Item::Setting(set) = item
20+
&& let Setting::Indentation(_, indentation) = set.value
21+
{
22+
Some(indentation)
23+
} else {
24+
None
25+
}
26+
})
27+
}
28+
}
29+
1630
impl ColorDisplay for Ast<'_> {
1731
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
1832
let mut newlines = 0;

‎src/color.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ impl Color {
175175
}
176176
}
177177

178-
pub(crate) fn use_color(self, use_color: UseColor) -> Self {
178+
pub(crate) fn with_use_color(self, use_color: UseColor) -> Self {
179179
Self { use_color, ..self }
180180
}
181+
182+
pub(crate) fn with_indentation(self, indentation: Indentation) -> Self {
183+
Self {
184+
indentation,
185+
..self
186+
}
187+
}
181188
}

‎src/compile_error.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ impl Display for CompileError<'_> {
278278
"shell recipe `{recipe}` has script recipe attribute `{}`",
279279
attribute.name(),
280280
),
281+
InvalidIndentation { message } => {
282+
write!(f, "{message}")
283+
}
281284
InvalidMinimumVersion { source, version } => {
282285
write!(
283286
f,
@@ -305,12 +308,6 @@ impl Display for CompileError<'_> {
305308
f,
306309
"justfile requires just {minimum} or later, but using {current}",
307310
),
308-
MinimumVersionExpression => {
309-
write!(
310-
f,
311-
"`minimum-version` setting must be a plain string literal"
312-
)
313-
}
314311
MismatchedClosingDelimiter {
315312
open,
316313
open_line,
@@ -372,6 +369,9 @@ impl Display for CompileError<'_> {
372369
f,
373370
"recipe `{recipe}` has both `[script]` and `[shell]` attributes"
374371
),
372+
SettingExpression { setting } => {
373+
write!(f, "`{setting}` setting must be a plain string literal")
374+
}
375375
ShellExpansion { err } => write!(f, "shell expansion failed: {err}"),
376376
ShortOptionWithMultipleCharacters { parameter } => {
377377
write!(

‎src/compile_error_kind.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ pub(crate) enum CompileErrorKind<'src> {
139139
InvalidEscapeSequence {
140140
character: char,
141141
},
142+
InvalidIndentation {
143+
message: &'static str,
144+
},
142145
InvalidMinimumVersion {
143146
source: &'static str,
144147
version: String,
@@ -158,7 +161,6 @@ pub(crate) enum CompileErrorKind<'src> {
158161
current: Version,
159162
minimum: Version,
160163
},
161-
MinimumVersionExpression,
162164
MismatchedClosingDelimiter {
163165
close: Delimiter,
164166
open: Delimiter,
@@ -192,6 +194,9 @@ pub(crate) enum CompileErrorKind<'src> {
192194
ScriptAndShellAttribute {
193195
recipe: &'src str,
194196
},
197+
SettingExpression {
198+
setting: Keyword,
199+
},
195200
ShellExpansion {
196201
err: shellexpand::LookupError<env::VarError>,
197202
},

‎src/config.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) struct Config {
1818
pub(crate) explain: bool,
1919
pub(crate) groups: Vec<String>,
2020
pub(crate) highlight: bool,
21+
pub(crate) indentation: Option<Indentation>,
2122
pub(crate) invocation_directory: PathBuf,
2223
pub(crate) justfile_names: Option<Vec<String>>,
2324
pub(crate) list_heading: String,
@@ -63,6 +64,7 @@ impl Config {
6364
explain: false,
6465
groups: Vec::new(),
6566
highlight: true,
67+
indentation: None,
6668
invocation_directory: env::current_dir().context(config_error::CurrentDir)?,
6769
justfile_names: None,
6870
list_heading: Arguments::DEFAULT_LIST_HEADING.into(),
@@ -315,7 +317,7 @@ impl Config {
315317
}
316318

317319
let unstable = arguments.unstable || subcommand == Subcommand::Summary;
318-
let color = Color::new(arguments.indentation, arguments.color);
320+
let color = Color::new(arguments.indentation.unwrap_or_default(), arguments.color);
319321

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

@@ -338,6 +340,7 @@ impl Config {
338340
explain: arguments.explain,
339341
groups: arguments.group,
340342
highlight: !arguments.no_highlight,
343+
indentation: arguments.indentation,
341344
invocation_directory,
342345
justfile_names: arguments.justfile_names,
343346
list_heading: arguments.list_heading,

‎src/evaluator.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ impl<'src, 'run> Evaluator<'src, 'run> {
107107
Setting::IgnoreComments(value) => {
108108
settings.ignore_comments = value;
109109
}
110+
Setting::Indentation(_, indentation) => {
111+
settings.indentation = Some(indentation);
112+
}
110113
Setting::Lazy(value) => {
111114
settings.lazy = value;
112115
}

‎src/indentation.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ impl Display for Indentation {
2424
}
2525
}
2626

27+
impl Serialize for Indentation {
28+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29+
where
30+
S: Serializer,
31+
{
32+
serializer.collect_str(self)
33+
}
34+
}
35+
2736
impl FromStr for Indentation {
2837
type Err = &'static str;
2938

0 commit comments

Comments
 (0)