Skip to content

Commit 6f5ccc8

Browse files
committed
fix bug in ScopeRegionIterator skipping the last operation on the line
1 parent ec47d71 commit 6f5ccc8

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/easy.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ static NOOP_OP: ScopeStackOp = ScopeStackOp::Noop;
148148
impl<'a> Iterator for ScopeRegionIterator<'a> {
149149
type Item = (&'a str, &'a ScopeStackOp);
150150
fn next(&mut self) -> Option<Self::Item> {
151-
let next_str_i = if self.index >= self.ops.len() {
152-
if self.last_str_index >= self.line.len() {
153-
return None;
154-
}
151+
let next_str_i = if self.index > self.ops.len() {
152+
return None;
153+
} else if self.index == self.ops.len() {
155154
self.line.len()
156155
} else {
157156
self.ops[self.index].0
@@ -219,4 +218,27 @@ mod tests {
219218
}
220219
assert_eq!(token_count, 5);
221220
}
221+
222+
#[test]
223+
fn can_find_regions_with_trailing_newline() {
224+
let ss = SyntaxSet::load_defaults_newlines();
225+
let mut state = ParseState::new(ss.find_syntax_by_extension("rb").unwrap());
226+
let lines = ["# hello world\n", "lol=5+2\n"];
227+
let mut stack = ScopeStack::new();
228+
229+
for line in lines.iter() {
230+
let ops = state.parse_line(&line);
231+
println!("{:?}", ops);
232+
233+
let mut iterated_ops: Vec<&ScopeStackOp> = Vec::new();
234+
for (_, op) in ScopeRegionIterator::new(&ops, &line) {
235+
stack.apply(op);
236+
iterated_ops.push(&op);
237+
println!("{:?}", op);
238+
}
239+
240+
let all_ops: Vec<&ScopeStackOp> = ops.iter().map(|t|&t.1).collect();
241+
assert_eq!(all_ops.len(), iterated_ops.len() - 1); // -1 because we want to ignore the NOOP
242+
}
243+
}
222244
}

0 commit comments

Comments
 (0)