-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
sway-fmt-v2 formatting should use tokens directly from sway-parse #2097
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e831f74
sway-fmt-v2 uses tokens directly from parser & comments cleaned-up.
kayagokalp 5187eb8
Merge branch 'master' into kayagokalp/2072
eureka-cpu aa6a142
merge master & use tokens from parser for struct
kayagokalp 09cf7aa
fix forgetten brace
kayagokalp ba8fab8
struct angle brace fix
kayagokalp 6772a75
Merge master
kayagokalp c077a33
doc updated
kayagokalp dbd76c9
Merge branch 'master' into kayagokalp/2072
eureka-cpu 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 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 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 |
---|---|---|
|
@@ -7,7 +7,8 @@ use crate::{ | |
}, | ||
FormatterError, | ||
}; | ||
use sway_parse::ItemStruct; | ||
use std::fmt::Write; | ||
use sway_parse::{token::Delimiter, token::PunctKind, ItemStruct}; | ||
use sway_types::Spanned; | ||
|
||
impl Format for ItemStruct { | ||
|
@@ -77,7 +78,7 @@ fn format_struct( | |
|
||
// Check if there is generic provided | ||
if let Some(generics) = &item_struct.generics { | ||
// Push angle brace | ||
// Push open angle brace | ||
ItemStruct::open_angle_bracket(formatted_code, formatter)?; | ||
// Get generics fields | ||
let generics = generics.parameters.inner.value_separator_pairs.clone(); | ||
|
@@ -89,6 +90,8 @@ fn format_struct( | |
formatted_code.push_str(", "); | ||
} | ||
} | ||
// Push closing angle brace | ||
ItemStruct::close_angle_bracket(formatted_code, formatter)?; | ||
} | ||
|
||
// Handle openning brace | ||
|
@@ -99,7 +102,8 @@ fn format_struct( | |
// Push a single whitespace before `{` | ||
formatted_code.push(' '); | ||
// Push open brace | ||
formatted_code.push('{'); | ||
let open_brace = Delimiter::Brace.as_open_char(); | ||
formatted_code.push(open_brace); | ||
// Push a single whitespace after `{` | ||
formatted_code.push(' '); | ||
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. Similarly here ^ |
||
} | ||
|
@@ -177,16 +181,17 @@ impl CurlyBrace for ItemStruct { | |
) -> Result<(), FormatterError> { | ||
let brace_style = formatter.config.items.item_brace_style; | ||
let extra_width = formatter.config.whitespace.tab_spaces; | ||
let open_brace = Delimiter::Brace.as_open_char(); | ||
let mut shape = formatter.shape; | ||
match brace_style { | ||
ItemBraceStyle::AlwaysNextLine => { | ||
// Add openning brace to the next line. | ||
line.push_str("\n{"); | ||
write!(line, "\n{}", open_brace)?; | ||
shape = shape.block_indent(extra_width); | ||
} | ||
_ => { | ||
// Add opening brace to the same line | ||
line.push_str(" {"); | ||
write!(line, " {}", open_brace)?; | ||
shape = shape.block_indent(extra_width); | ||
} | ||
} | ||
|
@@ -199,7 +204,8 @@ impl CurlyBrace for ItemStruct { | |
line: &mut String, | ||
formatter: &mut Formatter, | ||
) -> Result<(), FormatterError> { | ||
line.push('}'); | ||
let close_brace = Delimiter::Brace.as_close_char(); | ||
write!(line, "{}", close_brace)?; | ||
// If shape is becoming left-most alligned or - indent just have the defualt shape | ||
formatter.shape = formatter | ||
.shape | ||
|
@@ -214,15 +220,17 @@ impl AngleBracket for ItemStruct { | |
line: &mut String, | ||
_formatter: &mut Formatter, | ||
) -> Result<(), FormatterError> { | ||
line.push('<'); | ||
let open_angle = PunctKind::LessThan.as_char(); | ||
write!(line, "{}", open_angle)?; | ||
Ok(()) | ||
} | ||
|
||
fn close_angle_bracket( | ||
line: &mut String, | ||
_formatter: &mut Formatter, | ||
) -> Result<(), FormatterError> { | ||
line.push('>'); | ||
let close_angle = PunctKind::GreaterThan.as_char(); | ||
write!(line, "{}", close_angle)?; | ||
Ok(()) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use sway_parse::ModuleKind; | ||
use sway_parse::{token::PunctKind, ModuleKind}; | ||
use sway_types::Spanned; | ||
|
||
/// Insert the program type without applying a formatting to it. | ||
|
@@ -24,7 +24,6 @@ pub(crate) fn insert_program_type(push_to: &mut String, module_kind: ModuleKind) | |
push_to.push_str(name.as_str()); | ||
} | ||
}; | ||
push_to.push(';'); | ||
push_to.push('\n'); | ||
push_to.push('\n'); | ||
push_to.push(PunctKind::Semicolon.as_char()); | ||
push_to.push_str("\n\n"); | ||
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. Also here ^ |
||
} |
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.
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.
Could also be written as: