diff --git a/src/easy.rs b/src/easy.rs index 3abc9e85..633b94b4 100644 --- a/src/easy.rs +++ b/src/easy.rs @@ -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 { - 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 @@ -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 + } + } }