Skip to content

Commit

Permalink
WIP: Revert this commit and break into individual commits
Browse files Browse the repository at this point in the history
  • Loading branch information
ytmimi committed Jul 19, 2024
1 parent ea02de2 commit 379503d
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 38 deletions.
14 changes: 14 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,20 @@ By default this option is set as a percentage of [`max_width`](#max_width) provi

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `style_edition`

The style edition used to format your code. rustfmt output may differ between style editions.

- **Default value**: `"2015"`
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"`
- **Stable**: Yes

You can specify which style edition to use via the `--style-edition` CLI option or via your `rustfmt.toml`.

```toml
style_edition = "2021"
```

## `tab_spaces`

Number of spaces per tab
Expand Down
42 changes: 37 additions & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::str::FromStr;
use getopts::{Matches, Options};

use crate::rustfmt::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
load_config, CliOptions, Color, Config, Edition, StyleEdition, EmitMode, FileLines, FileName,
FormatReportFormatterBuilder, Input, Session, Verbosity,
};

Expand Down Expand Up @@ -130,6 +130,15 @@ fn make_opts() -> Options {
"[Path for the configuration file]",
);
opts.optopt("", "edition", "Rust edition to use", "[2015|2018|2021]");
opts.optopt(
"",
"style-edition",
"Rustfmt style edition used to format your code. If specified, this style edition will \
take precedence over the value configured in any `rustfmt.toml`. rustfmt output may \
differ between style editions. If not specified, style-edition will be inferred from the \
configured `--edition`",
"[2015|2018|2021|2024]",
);
opts.optopt(
"",
"color",
Expand Down Expand Up @@ -207,6 +216,8 @@ fn is_nightly() -> bool {
fn execute(opts: &Options) -> Result<i32> {
let matches = opts.parse(env::args().skip(1))?;
let options = GetOptsOptions::from_matches(&matches)?;
let style_edition = options.style_edition;
let edition = options.edition;

match determine_operation(&matches)? {
Operation::Help(HelpOp::None) => {
Expand Down Expand Up @@ -244,7 +255,7 @@ fn execute(opts: &Options) -> Result<i32> {
let file = PathBuf::from(path);
let file = file.canonicalize().unwrap_or(file);

let (config, _) = load_config(Some(file.parent().unwrap()), Some(options))?;
let (config, _) = load_config(style_edition, edition, Some(file.parent().unwrap()), Some(options))?;
let toml = config.all_options().to_toml()?;
io::stdout().write_all(toml.as_bytes())?;

Expand All @@ -259,8 +270,11 @@ fn execute(opts: &Options) -> Result<i32> {
}

fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
let style_edition = options.style_edition;
let edition = options.edition;

// try to read config from local directory
let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?;
let (mut config, _) = load_config(style_edition, edition, Some(Path::new(".")), Some(options.clone()))?;

if options.check {
config.set().emit_mode(EmitMode::Diff);
Expand Down Expand Up @@ -306,7 +320,10 @@ fn format(
options: &GetOptsOptions,
) -> Result<i32> {
options.verify_file_lines(&files);
let (config, config_path) = load_config(None, Some(options.clone()))?;
let style_edition = options.style_edition;
let edition = options.edition;

let (config, config_path) = load_config(style_edition, edition, None, Some(options.clone()))?;

if config.verbose() == Verbosity::Verbose {
if let Some(path) = config_path.as_ref() {
Expand All @@ -328,7 +345,7 @@ fn format(
// Check the file directory if the config-path could not be read or not provided
if config_path.is_none() {
let (local_config, config_path) =
load_config(Some(file.parent().unwrap()), Some(options.clone()))?;
load_config(style_edition, edition, Some(file.parent().unwrap()), Some(options.clone()))?;
if local_config.verbose() == Verbosity::Verbose {
if let Some(path) = config_path {
println!(
Expand Down Expand Up @@ -514,6 +531,7 @@ struct GetOptsOptions {
backup: bool,
check: bool,
edition: Option<Edition>,
style_edition: Option<StyleEdition>,
color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files.
unstable_features: bool,
Expand Down Expand Up @@ -605,6 +623,10 @@ impl GetOptsOptions {
options.edition = Some(edition_from_edition_str(edition_str)?);
}

if let Some(ref style_edition) = matches.opt_str("style-edition") {
options.style_edition = Some(style_edition_from_edition_str(style_edition)?);
}

if matches.opt_present("backup") {
options.backup = true;
}
Expand Down Expand Up @@ -702,6 +724,16 @@ fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
}
}

fn style_edition_from_edition_str(edition_str: &str) -> Result<StyleEdition> {
match edition_str {
"2015" => Ok(StyleEdition::Edition2015),
"2018" => Ok(StyleEdition::Edition2018),
"2021" => Ok(StyleEdition::Edition2021),
"2024" => Ok(StyleEdition::Edition2024),
_ => Err(format_err!("Invalid value for `--style-edition`")),
}
}

fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
match emit_str {
"files" => Ok(EmitMode::Files),
Expand Down
Loading

0 comments on commit 379503d

Please sign in to comment.