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

Impl StyleEditionDefault trait for all configs #5937

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Oct 8, 2023

This PR is a follow up to #5933, but currently includes all the changes from that PR since it hasn't been merged yet.

This PR makes internal changes to define each configuration in terms of the StyleEditionDefault trait, and hard codes StyleEdition::Edition2015 anywhere the new style_edition is needed.

Note users are still unable to configure style edition.

r? @calebcartwright

Edit: #5933 was merged and I rebased this PR.

Comment on lines +38 to +39
max_width: MaxWidth, true, "Maximum width of each line";
hard_tabs: HardTabs, true, "Use tab characters for indentation, spaces for alignment";
tab_spaces: TabSpaces, true, "Number of spaces per tab";
newline_style: NewlineStyleConfig, true, "Unix or Windows line endings";
indent_style: IndentStyleConfig, false, "How do we indent expressions or items";
Copy link
Contributor Author

@ytmimi ytmimi Oct 9, 2023

Choose a reason for hiding this comment

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

Not a change we need to make now (or ever), but as I was working on this I wondered if we could generalize StyleEditionDefault and rename it to something like RustfmtConfigOption (happy to bikeshed on the name). That trait could also be used to define each configuration option's description, stability, and default value for different style editions.

For example:

use crate::config::StyleEdition;
use crate::config::config_type::ConfigType;

pub trait RustfmtConfigOption {
    type ConfigType: ConfigType;

    fn default(style_edition: StyleEdition) -> Self::ConfigType;
    fn description() -> &'static str;
    fn stability() -> bool;
}

That way, create_config! could focus on setting field names and tys that impl RustfmtConfigOption.

For example if we implemented RustfmtConfigOption for MaxWidth it might look something like this:

impl RustfmtConfigOption for MaxWidth {
    type ConfigType = usize;

    fn default(_: StyleEdition) -> Self::ConfigType { 100 }
    fn description() -> &'static str { "Maximum width of each line" }
    fn stability() -> bool { true }
}

And then the call to create_config! would be simplified to:

create_config! {
    max_width: MaxWidth,
}

This would also eliminate some of the duplication for the test config we create in mod test

Copy link
Member

Choose a reason for hiding this comment

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

I like the sound of this, but agree it's something worth deferring

Comment on lines 519 to 525
config_option_with_style_edition_default!(
// Fundamental stuff
MaxWidth, usize, 100;
HardTabs, bool, false;
TabSpaces, usize, 4;
NewlineStyleConfig, NewlineStyle, NewlineStyle::Auto;
IndentStyleConfig, IndentStyle, IndentStyle::Block;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Defines a unit struct for each config option that implements StyleEditionDefault

@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch from 09eb6ff to 37b0505 Compare December 16, 2023 18:10
pub use crate::config::lists::*;
#[allow(unreachable_pub)]
pub use crate::config::macro_names::{MacroSelector, MacroSelectors};
pub use crate::config::macro_names::MacroSelector;
Copy link
Contributor Author

@ytmimi ytmimi Dec 16, 2023

Choose a reason for hiding this comment

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

The unused import lint was triggering for crate::config::lists::*; and MacroSelectors so I removed the import for crate::config::lists::*;, and I moved the MacroSelectors import down to the test module.

Comment on lines -421 to +428
use crate::config::macro_names::MacroName;
use crate::config::macro_names::{MacroName, MacroSelectors};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@calebcartwright
Copy link
Member

@ytmimi do you think this would be better to merge before or after the next sync? (no promises but I'm going to try to do one today)

@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 7, 2024

@calebcartwright If we're going to do a sync we should update the changelog. I've started that on #6004, but there are a few other fixes that I merged that don't have entries yet.

@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 7, 2024

That said, we can probably wait to merge this until after the sync

@calebcartwright calebcartwright self-assigned this Apr 7, 2024
@ytmimi ytmimi added the a-2024-edition Style Edition 2024 label Jun 27, 2024
@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch 2 times, most recently from d1d1f41 to 3e03c8d Compare June 27, 2024 05:05
@calebcartwright calebcartwright force-pushed the impl_style_edition_default_trait_for_all_configs branch from 3e03c8d to 2adf879 Compare July 1, 2024 04:39
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

Still have a bit more to get through but leaving some initial thoughts now

@@ -317,14 +340,17 @@ macro_rules! create_config {
}
name_out.push_str(name_raw);
name_out.push(' ');
let mut default_str = format!("{}", $def);
let default = <$ty as StyleEditionDefault>::style_edition_default(
Copy link
Member

Choose a reason for hiding this comment

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

probably best to name this as something other than default given keyword overlap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was renamed as default_value

$(
let default = <$ty as StyleEditionDefault>::style_edition_default(
Copy link
Member

Choose a reason for hiding this comment

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

same here

self.$i.0.set(true);
self.$i.2.clone()
}
)+

#[allow(unreachable_pub)]
pub fn deafult_with_style_edition(style_edition: StyleEdition) -> Config {
Copy link
Member

Choose a reason for hiding this comment

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

typo, would need to be updated at call sites too (though I won't try to leave inline comments for those)

Suggested change
pub fn deafult_with_style_edition(style_edition: StyleEdition) -> Config {
pub fn default_with_style_edition(style_edition: StyleEdition) -> Config {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this. Luckily this was a pretty easy fix with find and replace.

Comment on lines +38 to +39
max_width: MaxWidth, true, "Maximum width of each line";
hard_tabs: HardTabs, true, "Use tab characters for indentation, spaces for alignment";
tab_spaces: TabSpaces, true, "Number of spaces per tab";
newline_style: NewlineStyleConfig, true, "Unix or Windows line endings";
indent_style: IndentStyleConfig, false, "How do we indent expressions or items";
Copy link
Member

Choose a reason for hiding this comment

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

I like the sound of this, but agree it's something worth deferring

verbose: Verbosity, Verbosity::Normal, false,
"How much to information to emit to the user";
file_lines: FileLines, FileLines::all(), false,
max_width: MaxWidth, true, "Maximum width of each line";
Copy link
Member

Choose a reason for hiding this comment

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

just leaving this as a thought, not requesting/suggesting a pivot, but I found myself having to dart back and forth between the two sites to get a "full picture" of a given option.

that's fine for moving ahead with 2024 edition support, but it's definitely something I want to address sooner rather than later, as it's going to be prone for errors IMO (perhaps you're earlier suggestion on future refactoring would help as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed that there's plenty of room for improvement, and I'm open to suggestions on how to make this better

Makes internal changes to define each configuration in terms of the
`StyleEditionDefault` trait, and hard codes `StyleEdition::Edition2015`
anywhere the new `style_edition` is needed.

**Note** users are still unable to configure `style edition`.
@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch from 2adf879 to 0e612d5 Compare July 6, 2024 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants