diff --git a/src/ast.rs b/src/ast.rs index be9378d..47dbbc2 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -80,6 +80,51 @@ impl Function { } } +/// Source line of a statement (every variant carries a span). +pub fn stmt_line(st: &Stmt) -> u32 { + match st { + Stmt::Assign { span, .. } + | Stmt::Return { span, .. } + | Stmt::Call { span, .. } + | Stmt::Expr { span, .. } + | Stmt::Branch { span, .. } + | Stmt::While { span, .. } + | Stmt::ForEach { span, .. } => span.line, + } +} + +/// Depth-first walk over every statement of a function: its body, nested block +/// bodies (branch arms / loops) and child functions. +/// +/// `` slimming: three view modules each re-implemented this recursion; the +/// traversal lives here so collectors only supply the per-statement logic. +pub fn walk_stmts(fun: &Function, f: &mut impl FnMut(&Stmt)) { + fn go(st: &Stmt, f: &mut impl FnMut(&Stmt)) { + f(st); + match st { + Stmt::Branch { arms, .. } => { + for arm in arms { + for b in &arm.body { + go(b, f); + } + } + } + Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { + for b in body { + go(b, f); + } + } + _ => {} + } + } + for st in &fun.body { + go(st, f); + } + for c in &fun.children { + walk_stmts(c, f); + } +} + #[derive(Debug, Clone)] pub enum Stmt { Assign { diff --git a/src/view/annot.rs b/src/view/annot.rs index 8cc15cb..2f0d2be 100644 --- a/src/view/annot.rs +++ b/src/view/annot.rs @@ -566,39 +566,9 @@ fn find_fun_by_line(module: &Module, line: u32) -> Option<&Function> { fn fun_stmt_lines(fun: &Function) -> std::collections::HashSet { let mut s = std::collections::HashSet::new(); - fn walk_stmt(st: &Stmt, s: &mut std::collections::HashSet) { - let line = match st { - Stmt::Assign { span, .. } - | Stmt::Return { span, .. } - | Stmt::Call { span, .. } - | Stmt::Expr { span, .. } - | Stmt::Branch { span, .. } - | Stmt::While { span, .. } - | Stmt::ForEach { span, .. } => span.line, - }; - s.insert(line); - match st { - Stmt::Branch { arms, .. } => { - for arm in arms { - for b in &arm.body { - walk_stmt(b, s); - } - } - } - Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { - for b in body { - walk_stmt(b, s); - } - } - _ => {} - } - } - for st in &fun.body { - walk_stmt(st, &mut s); - } - for c in &fun.children { - s.extend(fun_stmt_lines(c)); - } + crate::ast::walk_stmts(fun, &mut |st| { + s.insert(crate::ast::stmt_line(st)); + }); s } diff --git a/src/view/document.rs b/src/view/document.rs index 96c225d..f65da86 100644 --- a/src/view/document.rs +++ b/src/view/document.rs @@ -983,51 +983,14 @@ fn index_functions(module: &Module) -> HashMap { fn index_stmt_lines(module: &Module) -> HashSet { let mut s = HashSet::new(); - fn walk(fun: &Function, s: &mut HashSet) { - for st in &fun.body { - s.insert(stmt_line(st)); - walk_stmt(st, s); - } - for c in &fun.children { - walk(c, s); - } - } - fn walk_stmt(st: &Stmt, s: &mut HashSet) { - match st { - Stmt::Branch { arms, .. } => { - for arm in arms { - for b in &arm.body { - s.insert(stmt_line(b)); - walk_stmt(b, s); - } - } - } - Stmt::While { body, .. } | Stmt::ForEach { body, .. } => { - for b in body { - s.insert(stmt_line(b)); - walk_stmt(b, s); - } - } - _ => {} - } - } for f in &module.functions { - walk(f, &mut s); + crate::ast::walk_stmts(f, &mut |st| { + s.insert(crate::ast::stmt_line(st)); + }); } s } -fn stmt_line(st: &Stmt) -> u32 { - match st { - Stmt::Assign { span, .. } - | Stmt::Return { span, .. } - | Stmt::Call { span, .. } - | Stmt::Expr { span, .. } - | Stmt::Branch { span, .. } - | Stmt::While { span, .. } - | Stmt::ForEach { span, .. } => span.line, - } -} fn index_special_assigns(module: &Module, source: &str) -> HashMap { let src_lines: Vec<&str> = source.lines().collect();