-
Notifications
You must be signed in to change notification settings - Fork 982
Formatting pre and post cast comments enhancements #4406
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
Merged
calebcartwright
merged 4 commits into
rust-lang:master
from
davidBar-On:issueCastComments
Sep 6, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed05b54
Fix issues in handling cast pre/post comments and adding test cases
davidBar-On c223d41
Add target for cast pre/post comments test cases
davidBar-On fe163d2
Add test cases related to #2896 and #3528
davidBar-On dfb3e8c
Changes per comments to the original PR
davidBar-On File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,24 +12,36 @@ use crate::formatting::{ | |||||||||
| #[derive(Clone, Copy)] | ||||||||||
| pub(crate) struct PairParts<'a> { | ||||||||||
| prefix: &'a str, | ||||||||||
| infix_prefix: &'a str, /* mainly for pre-infix comments */ | ||||||||||
| infix: &'a str, | ||||||||||
| infix_suffix: &'a str, /* mainly for post-infix comments */ | ||||||||||
| suffix: &'a str, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl<'a> PairParts<'a> { | ||||||||||
| /// Constructs a new `PairParts`. | ||||||||||
| pub(crate) fn new(prefix: &'a str, infix: &'a str, suffix: &'a str) -> Self { | ||||||||||
| Self { | ||||||||||
| pub(crate) fn new( | ||||||||||
| prefix: &'a str, | ||||||||||
| infix_prefix: &'a str, | ||||||||||
| infix: &'a str, | ||||||||||
| infix_suffix: &'a str, | ||||||||||
| suffix: &'a str, | ||||||||||
| ) -> Self { | ||||||||||
| PairParts { | ||||||||||
| prefix, | ||||||||||
| infix_prefix, | ||||||||||
| infix, | ||||||||||
| infix_suffix, | ||||||||||
| suffix, | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| pub(crate) fn infix(infix: &'a str) -> PairParts<'a> { | ||||||||||
| PairParts { | ||||||||||
| prefix: "", | ||||||||||
| infix_prefix: "", | ||||||||||
| infix, | ||||||||||
| infix_suffix: "", | ||||||||||
| suffix: "", | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -172,8 +184,11 @@ where | |||||||||
| RHS: Rewrite, | ||||||||||
| { | ||||||||||
calebcartwright marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| let tab_spaces = context.config.tab_spaces(); | ||||||||||
| let infix_result = format!("{}{}", pp.infix, pp.infix_suffix); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| let lhs_overhead = match separator_place { | ||||||||||
| SeparatorPlace::Back => shape.used_width() + pp.prefix.len() + pp.infix.trim_end().len(), | ||||||||||
| SeparatorPlace::Back => { | ||||||||||
| shape.used_width() + pp.prefix.len() + pp.infix.trim_end().len() + pp.infix_prefix.len() | ||||||||||
| } | ||||||||||
| SeparatorPlace::Front => shape.used_width(), | ||||||||||
| }; | ||||||||||
| let lhs_shape = Shape { | ||||||||||
|
|
@@ -182,12 +197,12 @@ where | |||||||||
| }; | ||||||||||
| let lhs_result = lhs | ||||||||||
| .rewrite(context, lhs_shape) | ||||||||||
| .map(|lhs_str| format!("{}{}", pp.prefix, lhs_str))?; | ||||||||||
| .map(|lhs_str| format!("{}{}{}", pp.prefix, lhs_str, pp.infix_prefix))?; | ||||||||||
|
||||||||||
| .map(|lhs_str| format!("{}{}{}", pp.prefix, lhs_str, pp.infix_prefix))?; | |
| .map(|lhs_str| format!("{}{}{}{}", pp.prefix, lhs_str, infix_prefix_separator, pp.infix_prefix))?; |
Outdated
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change
| .and_then(|s| s.sub_width(pp.suffix.len() + pp.infix_suffix.len())) | |
| .and_then(|s| s.sub_width(pp.suffix.len() + pp.infix_suffix.len() + infix_suffix_separator.len())) |
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /***** | ||
calebcartwright marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Tests for proper formatting of pre and post cast ("as") comments | ||
| ******/ | ||
|
|
||
| // Test 1 | ||
| fn main() { | ||
| let x = 0f64 /* x as */ as i32; | ||
| } | ||
|
|
||
| // Test 2 | ||
| fn main() { | ||
| let x = 1 /* foo as */ as i32; | ||
| } | ||
|
|
||
| // Test 3 | ||
| fn main() { | ||
| let x = 1 as /* bar as */ i32; | ||
| } | ||
|
|
||
| // Test 4 | ||
| fn main() { | ||
| let x = 1 /* as foo */ as /* as bar */ i32; | ||
| } | ||
|
|
||
| // Test 5 | ||
| fn main() { | ||
| let x = 1 /* as foo */as/* as bar */ i32; | ||
| } | ||
|
|
||
| // Test 6 | ||
| fn main() { | ||
| let x = 1 /* as foo */ | ||
| as/* as bar */ | ||
| i32; | ||
| } | ||
|
|
||
| // Test 7 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyy */as/* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ i32; | ||
| } | ||
|
|
||
| // Test 8 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyy */as/* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ i32; | ||
| } | ||
|
|
||
| // Test 9 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy */ | ||
| as/* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ | ||
| i32; | ||
| } | ||
|
|
||
|
|
||
| /***** | ||
| * Tests for not leaving trailing spaces related to cast comments (related to #2896?) | ||
| ******/ | ||
| // Test 10 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == 1 | ||
| /* x */ as i32 {} } | ||
|
|
||
| // Test 11 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == ' ' | ||
| as i32 {} } | ||
|
|
||
| // Test 10 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == ' ' /* x */ | ||
| as i32 {} } | ||
calebcartwright marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| /***** | ||
| * Tests for not moving "as" to new line unnecessarily - from #3528 | ||
| ******/ | ||
| fn get_old_backends(old_toml_config: &toml::Value) -> Option<Vec<Box<dyn Backend>>> { | ||
| old_toml_config.as_table().and_then(|table| { | ||
| table | ||
| .get("backends") | ||
| .and_then(|backends| backends.as_table()) | ||
| .map(|backends| { | ||
| backends | ||
| .into_iter() | ||
| .filter_map(|(key, value)| match AvailableBackend::from(key.as_str()) { | ||
| AvailableBackend::Git => Some(Box::new(Git { | ||
| config: value.clone().try_into::<GitConfig>().unwrap(), | ||
| }) | ||
| as Box<dyn Backend>), | ||
| AvailableBackend::Github => Some(Box::new(Github { | ||
| config: value.clone().try_into::<GithubConfig>().unwrap(), | ||
| }) | ||
| as Box<dyn Backend>), | ||
| }) | ||
| .collect() | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /***** | ||
| * Tests for proper formatting of pre and post cast ("as") comments | ||
| ******/ | ||
|
|
||
| // Test 1 | ||
| fn main() { | ||
| let x = 0f64 /* x as */ as i32; | ||
| } | ||
|
|
||
| // Test 2 | ||
| fn main() { | ||
| let x = 1 /* foo as */ as i32; | ||
| } | ||
|
|
||
| // Test 3 | ||
| fn main() { | ||
| let x = 1 as /* bar as */ i32; | ||
| } | ||
|
|
||
| // Test 4 | ||
| fn main() { | ||
| let x = 1 /* as foo */ as /* as bar */ i32; | ||
| } | ||
|
|
||
| // Test 5 | ||
| fn main() { | ||
| let x = 1 /* as foo */ as /* as bar */ i32; | ||
| } | ||
|
|
||
| // Test 6 | ||
| fn main() { | ||
| let x = 1 /* as foo */ as /* as bar */ i32; | ||
| } | ||
|
|
||
| // Test 7 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyy */ | ||
| as /* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ i32; | ||
| } | ||
|
|
||
| // Test 8 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyy */ | ||
| as /* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ | ||
| i32; | ||
| } | ||
|
|
||
| // Test 9 | ||
| fn main() { | ||
| let x = 1 /* as foo yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy */ | ||
| as /* as bar xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ | ||
| i32; | ||
| } | ||
|
|
||
| /***** | ||
| * Tests for not leaving trailing spaces related to cast comments (related to #2896?) | ||
| ******/ | ||
| // Test 10 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == 1 /* x */ as i32 {} | ||
| } | ||
|
|
||
| // Test 11 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == ' ' as i32 {} | ||
| } | ||
|
|
||
| // Test 10 - note the extra blank at the end of the 2nd line | ||
| fn main() { | ||
| if 0 == ' ' /* x */ as i32 {} | ||
| } | ||
|
|
||
| /***** | ||
| * Tests for not moving "as" to new line unnecessarily - from #3528 | ||
| ******/ | ||
| fn get_old_backends(old_toml_config: &toml::Value) -> Option<Vec<Box<dyn Backend>>> { | ||
| old_toml_config.as_table().and_then(|table| { | ||
| table | ||
| .get("backends") | ||
| .and_then(|backends| backends.as_table()) | ||
| .map(|backends| { | ||
| backends | ||
| .into_iter() | ||
| .filter_map(|(key, value)| match AvailableBackend::from(key.as_str()) { | ||
| AvailableBackend::Git => { | ||
| Some(Box::new(Git { | ||
| config: value.clone().try_into::<GitConfig>().unwrap(), | ||
| }) as Box<dyn Backend>) | ||
| } | ||
| AvailableBackend::Github => { | ||
| Some(Box::new(Github { | ||
| config: value.clone().try_into::<GithubConfig>().unwrap(), | ||
| }) as Box<dyn Backend>) | ||
| } | ||
| }) | ||
| .collect() | ||
| }) | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.