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 adds program type to the output #1997

Merged
merged 10 commits into from
Jun 20, 2022
31 changes: 23 additions & 8 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::{
attributes::format_attributes, indent_style::Shape, newline_style::apply_newline_style,
program_type::insert_program_type,
};
use std::{path::Path, sync::Arc};
use sway_core::BuildConfig;
Expand Down Expand Up @@ -38,8 +39,20 @@ impl Formatter {
build_config: Option<&BuildConfig>,
) -> Result<FormattedCode, FormatterError> {
let path = build_config.map(|build_config| build_config.canonical_root_module());
let items = sway_parse::parse_file(src, path)?.items;
let formatted_raw_newline = items
let module = sway_parse::parse_file(src, path)?;
// Get parsed items
let items = module.items;
// Get the program type (script, predicate, contract or library)
let program_type = module.kind;

// Formatted code will be pushed here with raw newline stlye.
// Which means newlines are not converted into system-specific versions by apply_newline_style
let mut raw_formatted_code = String::new();

// Insert program type to the formatted code.
insert_program_type(&mut raw_formatted_code, program_type);
// Insert parsed & formatted items into the formatted code.
raw_formatted_code += &items
.into_iter()
.map(|item| -> Result<String, FormatterError> {
use ItemKind::*;
Expand All @@ -60,12 +73,12 @@ impl Formatter {
})
.collect::<Result<Vec<String>, _>>()?
.join("\n");
let mut formatted_code = String::from(&formatted_raw_newline);
let mut formatted_code = String::from(&raw_formatted_code);
apply_newline_style(
// The user's setting for `NewlineStyle`
self.config.whitespace.newline_style,
&mut formatted_code,
&formatted_raw_newline,
&raw_formatted_code,
);
Ok(formatted_code)
}
Expand Down Expand Up @@ -93,8 +106,9 @@ enum Color {
Grey: (), }
"#;

// Until #1995 is addressed we will not have contract; in the output
let correct_sway_code = r#"enum Color {
let correct_sway_code = r#"contract;
Copy link
Member Author

@kayagokalp kayagokalp Jun 17, 2022

Choose a reason for hiding this comment

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

I removed the separate program type test which was not testing things correctly (even though I remove the changes from this PR, it wouldn't fail as the failure depends on the presence of other items in the source code that we are trying to format). Although we do not have any separate test for program type, it is checked in each test.


enum Color {
Blue : (),
Green : (),
Red : (),
Expand All @@ -117,8 +131,9 @@ enum Color {
Grey: (), }
"#;

// Until #1995 is addressed we will not have contract; in the output
let correct_sway_code = r#"enum Color {
let correct_sway_code = r#"contract;

enum Color {
Blue : (),
Green : (),
Red : (),
Expand Down
1 change: 1 addition & 0 deletions sway-fmt-v2/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod attributes;
pub mod bracket;
pub mod indent_style;
pub mod newline_style;
pub mod program_type;
30 changes: 30 additions & 0 deletions sway-fmt-v2/src/utils/program_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use sway_parse::ModuleKind;
use sway_types::Spanned;

/// Insert the program type without applying a formatting to it.
///
/// Possible list of program types:
/// - Script
/// - Contract
/// - Predicate
/// - Library
pub(crate) fn insert_program_type(push_to: &mut String, module_kind: ModuleKind) {
match module_kind {
ModuleKind::Script { script_token } => push_to.push_str(script_token.span().as_str()),
ModuleKind::Contract { contract_token } => push_to.push_str(contract_token.span().as_str()),
ModuleKind::Predicate { predicate_token } => {
push_to.push_str(predicate_token.span().as_str())
}
ModuleKind::Library {
library_token,
name,
} => {
push_to.push_str(library_token.span().as_str());
push_to.push(' ');
push_to.push_str(name.as_str());
}
};
push_to.push(';');
push_to.push('\n');
push_to.push('\n');
}