summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
authorPig Fang <g-plane@hotmail.com>2024-08-02 20:12:51 +0800
committerGitHub <noreply@github.com>2024-08-02 12:12:51 +0000
commit124a13280e3d71ed14ef0899b7bfd012f82ceb6e (patch)
treed3cd42098c74c0b665c3ea1090660ae87391be94 /cli/tools/fmt.rs
parentea121c9a0e162a83beeae62ed9e5a7911877bcbc (diff)
feat(fmt): support YAML (#24717)
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs82
1 files changed, 70 insertions, 12 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index baecb482f..43a97a72e 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -233,6 +233,8 @@ fn format_markdown(
| "typescript"
| "json"
| "jsonc"
+ | "yml"
+ | "yaml"
) {
// It's important to tell dprint proper file extension, otherwise
// it might parse the file twice.
@@ -244,19 +246,28 @@ fn format_markdown(
let fake_filename =
PathBuf::from(format!("deno_fmt_stdin.{extension}"));
- if matches!(extension, "json" | "jsonc") {
- let mut json_config = get_resolved_json_config(fmt_options);
- json_config.line_width = line_width;
- dprint_plugin_json::format_text(&fake_filename, text, &json_config)
- } else {
- let mut codeblock_config =
- get_resolved_typescript_config(fmt_options);
- codeblock_config.line_width = line_width;
- dprint_plugin_typescript::format_text(
- &fake_filename,
- text.to_string(),
- &codeblock_config,
+ match extension {
+ "json" | "jsonc" => {
+ let mut json_config = get_resolved_json_config(fmt_options);
+ json_config.line_width = line_width;
+ dprint_plugin_json::format_text(&fake_filename, text, &json_config)
+ }
+ "yml" | "yaml" => pretty_yaml::format_text(
+ text,
+ &get_resolved_yaml_config(fmt_options),
)
+ .map(Some)
+ .map_err(AnyError::from),
+ _ => {
+ let mut codeblock_config =
+ get_resolved_typescript_config(fmt_options);
+ codeblock_config.line_width = line_width;
+ dprint_plugin_typescript::format_text(
+ &fake_filename,
+ text.to_string(),
+ &codeblock_config,
+ )
+ }
}
} else {
Ok(None)
@@ -290,6 +301,12 @@ pub fn format_file(
format_markdown(file_text, fmt_options)
}
"json" | "jsonc" => format_json(file_path, file_text, fmt_options),
+ "yml" | "yaml" => pretty_yaml::format_text(
+ file_text,
+ &get_resolved_yaml_config(fmt_options),
+ )
+ .map(Some)
+ .map_err(AnyError::from),
"ipynb" => dprint_plugin_jupyter::format_text(
file_text,
|file_path: &Path, file_text: String| {
@@ -687,6 +704,41 @@ fn get_resolved_json_config(
builder.build()
}
+fn get_resolved_yaml_config(
+ options: &FmtOptionsConfig,
+) -> pretty_yaml::config::FormatOptions {
+ use pretty_yaml::config::*;
+
+ let layout_options = LayoutOptions {
+ print_width: options.line_width.unwrap_or(80) as usize,
+ use_tabs: options.use_tabs.unwrap_or_default(),
+ indent_width: options.indent_width.unwrap_or(2) as usize,
+ line_break: LineBreak::Lf,
+ };
+
+ let language_options = LanguageOptions {
+ quotes: if let Some(true) = options.single_quote {
+ Quotes::PreferSingle
+ } else {
+ Quotes::PreferDouble
+ },
+ trailing_comma: true,
+ format_comments: false,
+ indent_block_sequence_in_map: true,
+ brace_spacing: true,
+ bracket_spacing: false,
+ dash_spacing: DashSpacing::OneSpace,
+ trim_trailing_whitespaces: true,
+ trim_trailing_zero: false,
+ ignore_comment_directive: "deno-fmt-ignore".into(),
+ };
+
+ FormatOptions {
+ layout: layout_options,
+ language: language_options,
+ }
+}
+
struct FileContents {
text: String,
had_bom: bool,
@@ -785,6 +837,8 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
| "mdwn"
| "mdown"
| "markdown"
+ | "yml"
+ | "yaml"
| "ipynb"
)
})
@@ -819,6 +873,10 @@ mod test {
assert!(is_supported_ext_fmt(Path::new("foo.JSONC")));
assert!(is_supported_ext_fmt(Path::new("foo.json")));
assert!(is_supported_ext_fmt(Path::new("foo.JsON")));
+ assert!(is_supported_ext_fmt(Path::new("foo.yml")));
+ assert!(is_supported_ext_fmt(Path::new("foo.Yml")));
+ assert!(is_supported_ext_fmt(Path::new("foo.yaml")));
+ assert!(is_supported_ext_fmt(Path::new("foo.YaML")));
assert!(is_supported_ext_fmt(Path::new("foo.ipynb")));
}