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

sway-fmt-v2 formatting should use tokens directly from sway-parse #2097

Merged
merged 8 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sway-fmt-v2/src/items/item_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
FormatterError,
};
use std::fmt::Write;
use sway_parse::{token::Delimiter, ItemEnum};
use sway_parse::{token::Delimiter, token::PunctKind, ItemEnum};
use sway_types::Spanned;

impl Format for ItemEnum {
Expand Down Expand Up @@ -65,7 +65,9 @@ impl Format for ItemEnum {
// TODO: Improve handling this
formatted_code.push_str(&(0..required_alignment).map(|_| ' ').collect::<String>());
}
formatted_code.push_str(" : ");
formatted_code.push(' ');
formatted_code.push(PunctKind::Colon.as_char());
formatted_code.push(' ');
Copy link
Contributor

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:

write!(formatted_code, " {} ", PunctKind::Colon.as_char())?;

// TODO: We are currently converting ty to string directly but we will probably need to format ty before adding.
formatted_code.push_str(type_field.ty.span().as_str());
formatted_code.push(',');
Expand Down
24 changes: 16 additions & 8 deletions sway-fmt-v2/src/items/item_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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(' ');
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly here ^

}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
Expand All @@ -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(())
}
}
7 changes: 3 additions & 4 deletions sway-fmt-v2/src/utils/program_type.rs
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.
Expand All @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here ^

}