Repository: denoland/deno
File: cli/tools/fmt.rs (Rust)

Code before:
              format!("{error}")
            } else {
              format!("{error}\n\n")
            }
          })
          .collect::<String>(),
      )
    }
  });

  let formatted_str = format_result?;

  Ok(if formatted_str == file_text {
    None
  } else {
    Some(formatted_str)
  })
}

/// A function for formatting embedded code blocks in JavaScript and TypeScript.
fn create_external_formatter_for_typescript(
  unstable_options: &UnstableFmtOptions,
) -> impl Fn(
  MediaType,
  String,
  &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  let unstable_sql = unstable_options.sql;
  move |media_type, text, config| {
    match media_type {
      MediaType::Css => format_embedded_css(&text, config),
      MediaType::Html => format_embedded_html(&text, config),
      MediaType::Sql => {
        if unstable_sql {
          format_embedded_sql(&text, config)
        } else {
          None
        }
      }
      // dprint-plugin-typescript only detects css, html, and sql for now
      _ => unreachable!(),
    }
  }
}

/// Formats embedded CSS code blocks in JavaScript and TypeScript.
///
/// This function supports properties only CSS expressions, like:
/// ```css
/// margin: 10px;
/// padding: 10px;

Review comment (suggestion):
Question I was just thinking about and didn't look into enough during the first review. I think markup_fmt exposes a `fileIndentLevel` that allows figuring out what the starting indentation level should be when formatting: https://github.com/denoland/deno/pull/27164/files#diff-922e01000758964a3d0058e0ce425faeef65a5706e990516329b9ba62db5e369R445

So for example, it might say the current indent level is level 5 and so any file being formatted will format with an initial indent level of 5 rather than 0.

Do we need to use that here or is that currently handled internally within dprint-plugin-typescript? In the future we should probably do something similar.

Code after:
              format!("{error}")
            } else {
              format!("{error}\n\n")
            }
          })
          .collect::<String>(),
      )
    }
  });

  let formatted_str = format_result?;

  Ok(if formatted_str == file_text {
    None
  } else {
    Some(formatted_str)
  })
}

/// A function for formatting embedded code blocks in JavaScript and TypeScript.
fn create_external_formatter_for_typescript(
  unstable_options: &UnstableFmtOptions,
) -> impl Fn(
  MediaType,
  String,
  &dprint_plugin_typescript::configuration::Configuration,
) -> Option<String> {
  let unstable_sql = unstable_options.sql;
  move |media_type, text, config| match media_type {
    MediaType::Css => format_embedded_css(&text, config),
    MediaType::Html => format_embedded_html(&text, config),
    MediaType::Sql => {
      if unstable_sql {
        format_embedded_sql(&text, config)
      } else {
        None
      }
    }
    _ => None,
  }
}

/// Formats embedded CSS code blocks in JavaScript and TypeScript.
///
/// This function supports properties only CSS expressions, like:
/// ```css
/// margin: 10px;
/// padding: 10px;
/// ```
///
/// To support this scenario, this function first wraps the text with `a { ... }`,