Skip to content

Commit

Permalink
Clean and comment ScopeRegionIterator
Browse files Browse the repository at this point in the history
I did this to make it clearer what was going on, so that I could understand it
again and review that it was doing the right thing, since I clearly got it
wrong the first time.
  • Loading branch information
trishume committed Mar 12, 2017
1 parent 5d2c55b commit f9da736
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,27 @@ static NOOP_OP: ScopeStackOp = ScopeStackOp::Noop;
impl<'a> Iterator for ScopeRegionIterator<'a> {
type Item = (&'a str, &'a ScopeStackOp);
fn next(&mut self) -> Option<Self::Item> {
let next_str_i = if self.index > self.ops.len() {
if self.index > self.ops.len() {
return None;
} else if self.index == self.ops.len() {
}

// region extends up to next operation (ops[index]) or string end if there is none
// note the next operation may be at, last_str_index, in which case the region is empty
let next_str_i = if self.index == self.ops.len() {
self.line.len()
} else {
self.ops[self.index].0
};
let substr = &self.line[self.last_str_index..next_str_i];
self.last_str_index = next_str_i;

// the first region covers everything before the first op, which may be empty
let op = if self.index == 0 {
&NOOP_OP
} else {
&self.ops[self.index-1].1
};

self.index += 1;
Some((substr, op))
}
Expand All @@ -174,7 +181,7 @@ mod tests {
use parsing::{SyntaxSet, ParseState, ScopeStack};
use highlighting::ThemeSet;
use std::str::FromStr;

#[test]
fn can_highlight_lines() {
let ps = SyntaxSet::load_defaults_nonewlines();
Expand Down Expand Up @@ -218,14 +225,14 @@ mod tests {
}
assert_eq!(token_count, 5);
}

#[test]
fn can_find_regions_with_trailing_newline() {
let ss = SyntaxSet::load_defaults_newlines();
let mut state = ParseState::new(ss.find_syntax_by_extension("rb").unwrap());
let lines = ["# hello world\n", "lol=5+2\n"];
let mut stack = ScopeStack::new();

for line in lines.iter() {
let ops = state.parse_line(&line);
println!("{:?}", ops);
Expand All @@ -236,7 +243,7 @@ mod tests {
iterated_ops.push(&op);
println!("{:?}", op);
}

let all_ops: Vec<&ScopeStackOp> = ops.iter().map(|t|&t.1).collect();
assert_eq!(all_ops.len(), iterated_ops.len() - 1); // -1 because we want to ignore the NOOP
}
Expand Down

0 comments on commit f9da736

Please sign in to comment.