Skip to content

Commit

Permalink
fix bug in ScopeRegionIterator skipping the last operation on the line
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-hall committed Mar 11, 2017
1 parent ec47d71 commit 6f5ccc8
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ 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.last_str_index >= self.line.len() {
return None;
}
let next_str_i = if self.index > self.ops.len() {
return None;
} else if self.index == self.ops.len() {
self.line.len()
} else {
self.ops[self.index].0
Expand Down Expand Up @@ -219,4 +218,27 @@ 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);

let mut iterated_ops: Vec<&ScopeStackOp> = Vec::new();
for (_, op) in ScopeRegionIterator::new(&ops, &line) {
stack.apply(op);
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
}
}
}

0 comments on commit 6f5ccc8

Please sign in to comment.