Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to format a file at a specific indentation level #689

Merged
merged 1 commit into from
Nov 26, 2024
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
12 changes: 11 additions & 1 deletion src/configuration/builder.rs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @g-plane, I believe this should be useful for markup_fmt when formatting typescript/javascript in a <script> tag.

Maybe we should come up with a standardized name for this config? dprint/dprint#941

Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ impl ConfigurationBuilder {
self.insert("singleBodyPosition", value.to_string().into())
}

/// Amount of indents to use for the whole file.
///
/// This should only be set by tools that need to indent all the code in the file.
///
/// Default: `0`
pub fn file_indent_level(&mut self, value: u16) -> &mut Self {
self.insert("fileIndentLevel", (value as i32).into())
}

/// If trailing commas should be used.
///
/// Default: `TrailingCommas::OnlyMultiLine`
Expand Down Expand Up @@ -1105,6 +1114,7 @@ mod tests {
.operator_position(OperatorPosition::SameLine)
.single_body_position(SameOrNextLinePosition::SameLine)
.trailing_commas(TrailingCommas::Never)
.file_indent_level(1)
.use_braces(UseBraces::WhenNotSingleLine)
.quote_props(QuoteProps::AsNeeded)
.prefer_hanging(false)
Expand Down Expand Up @@ -1287,7 +1297,7 @@ mod tests {
.while_statement_space_around(true);

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 181);
assert_eq!(inner_config.len(), 182);
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
quote_style,
quote_props,
semi_colons,
file_indent_level: get_value(&mut config, "fileIndentLevel", 0, &mut diagnostics),
/* situational */
arrow_function_use_parentheses: get_value(&mut config, "arrowFunction.useParentheses", UseParentheses::Maintain, &mut diagnostics),
binary_expression_line_per_expression: get_value(&mut config, "binaryExpression.linePerExpression", false, &mut diagnostics),
Expand Down
1 change: 1 addition & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ pub struct Configuration {
pub quote_style: QuoteStyle,
pub quote_props: QuoteProps,
pub semi_colons: SemiColons,
pub file_indent_level: u32,
/* situational */
#[serde(rename = "arrowFunction.useParentheses")]
pub arrow_function_use_parentheses: UseParentheses,
Expand Down
6 changes: 5 additions & 1 deletion src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ pub fn generate(parsed_source: &ParsedSource, config: &Configuration) -> PrintIt
#[cfg(debug_assertions)]
context.assert_end_of_file_state();

items
if config.file_indent_level > 0 {
with_indent_times(items, config.file_indent_level)
} else {
items
}
})
}

Expand Down
16 changes: 16 additions & 0 deletions tests/specs/file_indent_level/FileIndentLevel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
~~ fileIndentLevel: 4 ~~
== formats with indentation ==
const test = `this

is a multiline string

`;
console.log(test);

[expect]
const test = `this

is a multiline string

`;
console.log(test);