-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add means of comment formatting to sway-fmt-v2
#2229
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 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ee9abdb
CommentedTokenTree::Comment case for span comment map implemented
kayagokalp 6b6681a
CommentSpan ordering fix and test added
kayagokalp be4511c
clippy
kayagokalp 9dc606a
Handle in-block and next-to item cases for comment map generation
kayagokalp ddfb007
Comments in between items are added correctly.
kayagokalp fe5c851
remove premature comment addition
kayagokalp 9d48927
Merge branch 'master' into kayagokalp/2224
kayagokalp 06b5968
removed FormatComment trait
kayagokalp d9cb274
rename: get_comment_from_token_stream to collect_comments_from_token_…
kayagokalp d2b61ce
Merge branch 'master' into kayagokalp/2224
kayagokalp 2793a16
Merge branch 'master' into kayagokalp/2224
eureka-cpu bf9dcce
comment ordering corrected
kayagokalp f56674d
Merge branch 'master' into kayagokalp/2224
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use anyhow::Result; | ||
| use std::{cmp::Ordering, collections::BTreeMap, sync::Arc}; | ||
| use sway_parse::token::{lex_commented, Comment, CommentedTokenTree}; | ||
|
|
||
| /// Represents a span for the comments in a spesific file | ||
| /// A stripped down version of sway-types::src::Span | ||
| #[derive(PartialEq, Eq, Debug)] | ||
| pub struct CommentSpan { | ||
| // The byte position in the string of the start of the span. | ||
| start: usize, | ||
| // The byte position in the string of the end of the span. | ||
| end: usize, | ||
| } | ||
|
|
||
| impl Ord for CommentSpan { | ||
| fn cmp(&self, other: &Self) -> Ordering { | ||
| self.start.cmp(&other.start) | ||
| } | ||
| } | ||
|
|
||
| impl PartialOrd for CommentSpan { | ||
| fn partial_cmp(&self, other: &CommentSpan) -> Option<Ordering> { | ||
| Some(self.cmp(other)) | ||
| } | ||
| } | ||
|
|
||
| pub type CommentMap = BTreeMap<CommentSpan, Comment>; | ||
|
|
||
| /// Get the CommentedTokenStream and collect the spans -> Comment mapping for the input source | ||
| /// code. | ||
| pub fn construct_comment_map(input: Arc<str>) -> Result<CommentMap> { | ||
| let mut comment_map = BTreeMap::new(); | ||
|
|
||
| // pass the input through lexer | ||
| let commented_token_stream = lex_commented(&input, 0, input.len(), None)?; | ||
| let tts = commented_token_stream.token_trees().iter(); | ||
|
|
||
| for comment in tts { | ||
| if let CommentedTokenTree::Comment(comment) = comment { | ||
| let comment_span = CommentSpan { | ||
| start: comment.span.start(), | ||
| end: comment.span.end(), | ||
| }; | ||
| comment_map.insert(comment_span, comment.clone()); | ||
| } | ||
| // TODO: implement CommentedTokenTree::Tree case for getting the comments inside code | ||
| // blocks | ||
| } | ||
| Ok(comment_map) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{construct_comment_map, CommentSpan}; | ||
| use std::{ops::Bound::Included, sync::Arc}; | ||
|
|
||
| #[test] | ||
| fn test_comment_span_map() { | ||
| let input = r#" | ||
| // Single-line comment. | ||
| struct Foo { | ||
| /* multi- | ||
| * line- | ||
| * comment */ | ||
| bar: i32, | ||
| } | ||
| "#; | ||
| let map = construct_comment_map(Arc::from(input)).unwrap(); | ||
| assert!(map.len() != 0); | ||
| let range_start_span = CommentSpan { start: 0, end: 32 }; | ||
| let range_end_span = CommentSpan { start: 33, end: 34 }; | ||
| let found_comment = map | ||
| .range((Included(range_start_span), Included(range_end_span))) | ||
| .last() | ||
| .unwrap(); | ||
| assert_eq!(found_comment.1.span.as_str(), "// Single-line comment."); | ||
| } | ||
| } | ||
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.
When it comes to ordering spans, I think the correct approach might be to reflect not just the order in which the
startoccurs, but also the order in which the spans would be visited during traversal (e.g. larger enclosing spans preceding inner spans.)E.g. when comparing the spans
2..6and2..4, the2..6span should come first as it encloses the2..4span. I think this would look something like:Anyway, this is more relevant to the ordering of AST spans generally, and shouldn't be relevant to our case as I suppose comments cannot overlap 😂
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.
I was actually thinking about this while writing that part but later on couldn't think of an example where comments would be overlapping as you pointed out 😄. But I will fix the ordering like this. Although it seems like overlapping won't be happening, if somehow it happens I feel like this will be really hard to debug.