-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Adds attribute handling to sway-fmt-v2
#2061
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
Merged
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
64a33f2
wip
eureka-cpu 0c116be
add unit test
eureka-cpu 733f644
update tests
eureka-cpu fb9ffbe
updated unimplemented cases to return span
eureka-cpu cb8ce15
test now passes incorrectly
eureka-cpu feaf356
update AttributeDecl::format()
21c2aa9
update test comment
9985938
Merge branch 'master' into eureka-cpu/2012
eureka-cpu 5e0d7ee
add close paren
a0cab9f
Merge branch 'master' into eureka-cpu/2012
53b06ea
Merge branch 'eureka-cpu/2012' of https://github.com/FuelLabs/sway in…
a68ce01
update Annotated for consistency
ca81d7a
chng return type for Annotated
ce3d73a
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
0b448e7
remove test and add todos
0591fb9
chng return value back to FormattedCode
50b6987
.
4a65e91
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
488d5a4
experimenting with using token instead of char
4926670
update Delimiter fns
7242449
Merge branch 'master' into eureka-cpu/2012
eureka-cpu 2ba891e
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
92596b1
change file name to reflect parser
f19492d
.
4848b78
Merge branch 'eureka-cpu/2012' of https://github.com/FuelLabs/sway in…
55a1c2e
fix files from merge
64a1d7c
add Format to Annotated, create item.format()
954e3a3
Merge branch 'master' into eureka-cpu/2012
eureka-cpu 17ecb86
Merge branch 'master' into eureka-cpu/2012
eureka-cpu 11c1be1
Merge branch 'master' into eureka-cpu/2012
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 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 |
|---|---|---|
| @@ -1,11 +1,82 @@ | ||
| use crate::Formatter; | ||
| use sway_parse::AttributeDecl; | ||
| use crate::fmt::{Format, FormattedCode, Formatter}; | ||
| use sway_parse::{ | ||
| attribute::{Annotated, AttributeDecl}, | ||
| token::Delimiter, | ||
| Parse, | ||
| }; | ||
| use sway_types::Spanned; | ||
| pub fn format_attributes(attributes: Vec<AttributeDecl>, _formatter: &mut Formatter) -> String { | ||
| // TODO format attributes | ||
| attributes | ||
| .into_iter() | ||
| .map(|x| x.span().as_str().to_string()) | ||
| .collect::<Vec<String>>() | ||
| .join("\n") | ||
|
|
||
| use super::bracket::{Parenthesis, SquareBracket}; | ||
|
|
||
| impl<T: Parse> Format for Annotated<T> { | ||
| fn format(&self, formatter: &mut Formatter) -> FormattedCode { | ||
| let attributes = &self.attribute_list; | ||
| let mut formatted_code = String::new(); | ||
|
|
||
| for attr in attributes { | ||
| AttributeDecl::format(attr, &mut formatted_code, formatter); | ||
| } | ||
|
|
||
| formatted_code | ||
| } | ||
| } | ||
|
|
||
| trait FormatDecl { | ||
| fn format(&self, line: &mut String, formatter: &mut Formatter); | ||
| } | ||
|
|
||
| impl FormatDecl for AttributeDecl { | ||
| fn format(&self, line: &mut String, formatter: &mut Formatter) { | ||
| // At some point there will be enough attributes to warrant the need | ||
| // of formatting the list according to `config::lists::ListTactic`. | ||
| // For now the default implementation will be `Horizontal`. | ||
| // | ||
| // `#` | ||
| line.push_str(self.hash_token.span().as_str()); | ||
| // `[` | ||
| Self::open_square_bracket(line, formatter); | ||
| let attr = self.attribute.clone().into_inner(); | ||
| // name e.g. `storage` | ||
| line.push_str(attr.name.span().as_str()); | ||
| // `(` | ||
| Self::open_parenthesis(line, formatter); | ||
| // format and add args `read, write` | ||
| if let Some(args) = attr.args { | ||
| let mut args = args | ||
| .into_inner() | ||
| .value_separator_pairs | ||
| .iter() | ||
| .map(|arg| format!("{}{}", arg.0.as_str(), arg.1.span().as_str())) | ||
| .collect::<Vec<String>>() | ||
| .join(" "); | ||
| args.pop(); // pop the ending space | ||
| args.pop(); // pop the ending comma | ||
| line.push_str(&args); | ||
| } | ||
eureka-cpu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // ')' | ||
| Self::close_parenthesis(line, formatter); | ||
| // `]\n` | ||
| Self::close_square_bracket(line, formatter); | ||
| } | ||
| } | ||
|
|
||
| impl SquareBracket for AttributeDecl { | ||
| fn open_square_bracket(line: &mut String, _formatter: &mut Formatter) { | ||
| line.push(Delimiter::as_open_char(Delimiter::Bracket)); | ||
eureka-cpu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| fn close_square_bracket(line: &mut String, _formatter: &mut Formatter) { | ||
| line.push_str(&format!( | ||
| "{}\n", | ||
| Delimiter::as_close_char(Delimiter::Bracket) | ||
eureka-cpu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| )); | ||
| } | ||
| } | ||
|
|
||
| impl Parenthesis for AttributeDecl { | ||
| fn open_parenthesis(line: &mut String, _formatter: &mut Formatter) { | ||
| line.push(Delimiter::as_open_char(Delimiter::Parenthesis)) | ||
eureka-cpu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| fn close_parenthesis(line: &mut String, _formatter: &mut Formatter) { | ||
| line.push(Delimiter::as_close_char(Delimiter::Parenthesis)) | ||
eureka-cpu 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
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.