This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Comments on nodes #1744
Merged
Merged
Comments on nodes #1744
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
21cfb37
[wip]
mwu-tow 04f2c5b
[wip]
mwu-tow 135e897
[wip]
mwu-tow 63fbcdb
[wip]
mwu-tow 2cc8760
[wip]
mwu-tow 5518337
[wip]
mwu-tow 95752b2
[wip]
mwu-tow 3e1b864
Merge remote-tracking branch 'origin/develop' into wip/mwu/comments-o…
mwu-tow 1cfc887
post merge fixes, warnings
mwu-tow 679c18e
cleanups, CR feedback, minor improvements
mwu-tow 94cef3d
fixing multi line comments
mwu-tow de20ceb
dealing with partial information about comments
mwu-tow c6cceed
cleanups
mwu-tow 391143c
damn
mwu-tow 14d11c0
cleanups
mwu-tow c9f413f
cleanups, fixes
mwu-tow 1beb91e
clippy, minor
mwu-tow 99c1cf9
prettier
mwu-tow ebf472a
cleanups
mwu-tow 9dd201c
CR feedback
mwu-tow 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
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 |
---|---|---|
|
@@ -618,6 +618,16 @@ pub enum Escape { | |
// === Block === | ||
// ============= | ||
|
||
/// Iterate over non-empty lines, while maintaining their indices. | ||
pub fn enumerate_non_empty_lines<'a,T:'a>(iter:impl IntoIterator<Item = &'a BlockLine<Option<T>>> + 'a) | ||
-> impl Iterator<Item=(usize,BlockLine<&'a T>)> + 'a { | ||
iter.into_iter().enumerate().filter_map(|(index,line):(usize,&BlockLine<Option<T>>)| { | ||
let non_empty_line = line.transpose_ref()?; | ||
Some((index, non_empty_line)) | ||
}) | ||
} | ||
|
||
|
||
#[ast_node] pub enum BlockType {Continuous {} , Discontinuous {}} | ||
|
||
/// Holder for line in `Block` or `Module`. Lines store value of `T` and trailing whitespace info. | ||
|
@@ -630,7 +640,6 @@ pub struct BlockLine <T> { | |
} | ||
|
||
|
||
|
||
// ============= | ||
// === Macro === | ||
// ============= | ||
|
@@ -1201,29 +1210,68 @@ impl<T> BlockLine<T> { | |
pub fn new(elem:T) -> BlockLine<T> { | ||
BlockLine {elem,off:0} | ||
} | ||
|
||
pub fn as_ref(&self) -> BlockLine<&T> { | ||
BlockLine { | ||
elem : &self.elem, | ||
off : self.off, | ||
} | ||
} | ||
|
||
pub fn map<U>(self, f:impl FnOnce(T) -> U) -> BlockLine<U> { | ||
BlockLine { | ||
elem : f(self.elem), | ||
off : self.off | ||
} | ||
} | ||
} | ||
|
||
impl <T> BlockLine<Option<T>> { | ||
pub fn transpose(self) -> Option<BlockLine<T>> { | ||
let off = self.off; | ||
self.elem.map(|elem| BlockLine {elem,off}) | ||
} | ||
|
||
pub fn transpose_ref(&self) -> Option<BlockLine<&T>> { | ||
self.as_ref().map(Option::as_ref).transpose() | ||
} | ||
|
||
pub fn map_opt<U>(self, f:impl FnOnce(T) -> U) -> BlockLine<Option<U>> { | ||
self.map(|elem| elem.map(f)) | ||
} | ||
} | ||
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. Do you have warnings about missing documentation? |
||
|
||
|
||
impl <T> Block<T> { | ||
/// Concatenate `Block`'s `first_line` with `lines` and returns a collection with all the lines. | ||
pub fn all_lines(&self) -> Vec<BlockLine<Option<T>>> where T:Clone { | ||
let mut lines = Vec::new(); | ||
for off in &self.empty_lines { | ||
pub fn iter_all_lines(&self) -> impl Iterator<Item=BlockLine<Option<&T>>> + '_ { | ||
let indent = self.indent; | ||
let leading_empty_lines = self.empty_lines.iter().map(move |off| { | ||
let elem = None; | ||
// TODO [mwu] | ||
// Empty lines use absolute indent, while BlockLines are relative to Block. | ||
// We might lose some data here, as empty lines shorter than block will get filled | ||
// with spaces. This is something that should be improved in the future but also | ||
// requires changes in the AST. | ||
let off = off.checked_sub(self.indent).unwrap_or(0); | ||
lines.push(BlockLine{elem,off}) | ||
} | ||
let off = off.saturating_sub(indent); | ||
BlockLine {elem,off} | ||
}); | ||
|
||
let first_line = std::iter::once(self.first_line.as_ref().map(Some)); | ||
let lines = self.lines.iter().map(|line| line.as_ref().map(|elem| elem.as_ref())); | ||
leading_empty_lines.chain(first_line).chain(lines) | ||
} | ||
|
||
/// Calculate absolute indentation of lines in this block. | ||
pub fn indent(&self, parent_indent:usize) -> usize { | ||
parent_indent + self.indent | ||
} | ||
|
||
let first_line = self.first_line.clone(); | ||
let elem = Some(first_line.elem); | ||
let off = first_line.off; | ||
lines.push(BlockLine{elem,off}); | ||
lines.extend(self.lines.iter().cloned()); | ||
lines | ||
/// Iterate over non-empty lines, while keeping their absolute indices. | ||
pub fn enumerate_non_empty_lines(&self) -> impl Iterator<Item=(usize,BlockLine<&T>)> + '_ { | ||
self.iter_all_lines().enumerate().filter_map(|(index,line):(usize,BlockLine<Option<&T>>)| { | ||
let non_empty_line = line.transpose()?; | ||
Some((index, non_empty_line)) | ||
}) | ||
Comment on lines
+1273
to
+1278
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. Why we cannot use non-associated enumerate_non_empty_lines here? |
||
} | ||
} | ||
|
||
|
@@ -1668,7 +1716,7 @@ mod tests { | |
let expected_repr = "\n \n head \n tail0 \n \n tail2 "; | ||
assert_eq!(block.repr(), expected_repr); | ||
|
||
let all_lines = block.all_lines(); | ||
let all_lines = block.iter_all_lines().collect_vec(); | ||
let (empty_line,head_line,tail0,tail1,tail2) = all_lines.iter().expect_tuple(); | ||
assert!(empty_line.elem.is_none()); | ||
assert_eq!(empty_line.off,1); // other 4 indents are provided by Block | ||
|
Oops, something went wrong.
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.
restore line.