Skip to content

Commit f493633

Browse files
authored
feat: ability to format a file at a specific indentation level (#689)
1 parent eeefd31 commit f493633

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

src/configuration/builder.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ impl ConfigurationBuilder {
203203
self.insert("singleBodyPosition", value.to_string().into())
204204
}
205205

206+
/// Amount of indents to use for the whole file.
207+
///
208+
/// This should only be set by tools that need to indent all the code in the file.
209+
///
210+
/// Default: `0`
211+
pub fn file_indent_level(&mut self, value: u16) -> &mut Self {
212+
self.insert("fileIndentLevel", (value as i32).into())
213+
}
214+
206215
/// If trailing commas should be used.
207216
///
208217
/// Default: `TrailingCommas::OnlyMultiLine`
@@ -1105,6 +1114,7 @@ mod tests {
11051114
.operator_position(OperatorPosition::SameLine)
11061115
.single_body_position(SameOrNextLinePosition::SameLine)
11071116
.trailing_commas(TrailingCommas::Never)
1117+
.file_indent_level(1)
11081118
.use_braces(UseBraces::WhenNotSingleLine)
11091119
.quote_props(QuoteProps::AsNeeded)
11101120
.prefer_hanging(false)
@@ -1287,7 +1297,7 @@ mod tests {
12871297
.while_statement_space_around(true);
12881298

12891299
let inner_config = config.get_inner_config();
1290-
assert_eq!(inner_config.len(), 181);
1300+
assert_eq!(inner_config.len(), 182);
12911301
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
12921302
assert_eq!(diagnostics.len(), 0);
12931303
}

src/configuration/resolve_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
9292
quote_style,
9393
quote_props,
9494
semi_colons,
95+
file_indent_level: get_value(&mut config, "fileIndentLevel", 0, &mut diagnostics),
9596
/* situational */
9697
arrow_function_use_parentheses: get_value(&mut config, "arrowFunction.useParentheses", UseParentheses::Maintain, &mut diagnostics),
9798
binary_expression_line_per_expression: get_value(&mut config, "binaryExpression.linePerExpression", false, &mut diagnostics),

src/configuration/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ pub struct Configuration {
317317
pub quote_style: QuoteStyle,
318318
pub quote_props: QuoteProps,
319319
pub semi_colons: SemiColons,
320+
pub file_indent_level: u32,
320321
/* situational */
321322
#[serde(rename = "arrowFunction.useParentheses")]
322323
pub arrow_function_use_parentheses: UseParentheses,

src/generation/generate.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ pub fn generate(parsed_source: &ParsedSource, config: &Configuration) -> PrintIt
4242
#[cfg(debug_assertions)]
4343
context.assert_end_of_file_state();
4444

45-
items
45+
if config.file_indent_level > 0 {
46+
with_indent_times(items, config.file_indent_level)
47+
} else {
48+
items
49+
}
4650
})
4751
}
4852

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
~~ fileIndentLevel: 4 ~~
2+
== formats with indentation ==
3+
const test = `this
4+
5+
is a multiline string
6+
7+
`;
8+
console.log(test);
9+
10+
[expect]
11+
const test = `this
12+
13+
is a multiline string
14+
15+
`;
16+
console.log(test);

0 commit comments

Comments
 (0)