logisheets_controller 1.0.0

the core of LogiSheets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use super::FormulaExecutor;
use logisheets_base::errors::BasicError;
use logisheets_base::{
    BlockId, BlockRange, CellId, EphemeralId, NormalRange, Range, RangeId, RefAbs, SheetId,
};
use logisheets_parser::ast::{self, RangeDisplay};
use logisheets_parser::Parser;
use std::collections::HashSet;

use crate::formula_manager::{ctx::FormulaExecCtx, FormulaManager, Vertex};

pub fn input_formula<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    row: usize,
    col: usize,
    formula: String,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    let cell_id = ctx.fetch_cell_id(&sheet, row, col)?;
    input(executor, sheet, cell_id, formula, None, ctx)
}

/// Register `formula` as the formula for a block cell addressed by
/// (block_id, block_row, block_col). Mirrors {@link input_formula} but
/// avoids translating through sheet-absolute coords — callers like the
/// BlockInput handler already have the block triple.
pub fn input_block_formula<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    block_id: BlockId,
    block_row: usize,
    block_col: usize,
    formula: String,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    let bcid = ctx.fetch_block_cell_id(&sheet, &block_id, block_row, block_col)?;
    input(executor, sheet, CellId::BlockCell(bcid), formula, None, ctx)
}

pub fn input_ephemeral_formula<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    id: EphemeralId,
    formula: String,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    if let Some((sheet_id, cell_id)) = ctx.get_cell_id_by_shadow_id(&id) {
        let range = match cell_id {
            CellId::NormalCell(normal_cell_id) => {
                Range::Normal(NormalRange::Single(normal_cell_id))
            }
            CellId::BlockCell(block_cell_id) => Range::Block(BlockRange::Single(block_cell_id)),
            CellId::EphemeralCell(_) => unreachable!(),
        };
        let range_id = ctx.fetch_range_id(&sheet_id, &range);
        let placeholder_node = ast::Node {
            pure: ast::PureNode::Reference(ast::CellReference::Mut(RangeDisplay {
                range_id,
                ref_abs: RefAbs::default(),
                sheet_id,
            })),
            bracket: true,
        };

        // If the shadow target is a block cell, build per-row `#FIELD`
        // and `#KEY` substitutes too — so validation formulas can reach
        // sibling fields (e.g. resolve the row's key column to feed a
        // BLOCKREF lookup against another block). Non-block targets get
        // only `#PLACEHOLDER`, matching the prior behavior.
        if let CellId::BlockCell(block_cell_id) = cell_id {
            if let Some(row_ctx) = ctx.block_cell_row_substitutes(sheet_id, &block_cell_id) {
                use std::collections::HashMap;
                let mut sib_subs: HashMap<String, ast::Node> = HashMap::new();
                for (name, sib_cell) in row_ctx.siblings {
                    let sib_range = Range::Block(BlockRange::Single(sib_cell));
                    let sib_range_id = ctx.fetch_range_id(&sheet_id, &sib_range);
                    sib_subs.insert(
                        name,
                        ast::Node {
                            pure: ast::PureNode::Reference(ast::CellReference::Mut(RangeDisplay {
                                range_id: sib_range_id,
                                ref_abs: RefAbs::default(),
                                sheet_id,
                            })),
                            bracket: true,
                        },
                    );
                }
                let key_node = ast::Node {
                    pure: ast::PureNode::Value(ast::Value::Text(row_ctx.key_value)),
                    bracket: false,
                };
                return input_with_resolver(
                    executor,
                    sheet,
                    CellId::EphemeralCell(id),
                    formula,
                    placeholder_node,
                    sib_subs,
                    key_node,
                    ctx,
                );
            }
        }

        input(
            executor,
            sheet,
            CellId::EphemeralCell(id),
            formula,
            Some(placeholder_node),
            ctx,
        )
    } else {
        let cell_id = CellId::EphemeralCell(id);
        input(executor, sheet, cell_id, formula, None, ctx)
    }
}

/// Ephemeral input variant that resolves all three placeholder kinds:
/// `#PLACEHOLDER` → the shadow target cell, `#FIELD("X")` → the
/// target's same-row sibling cell, `#KEY` → the target's row key.
/// Used for validation formulas on block cells.
fn input_with_resolver<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet: SheetId,
    cell_id: CellId,
    formula: String,
    placeholder: ast::Node,
    sib_subs: std::collections::HashMap<String, ast::Node>,
    key_node: ast::Node,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let parser = Parser {};
    let ast = parser.parse_with_substitutes(&formula, sheet, ctx, &|kind| match kind {
        logisheets_parser::PlaceholderKind::Placeholder => Some(placeholder.clone()),
        logisheets_parser::PlaceholderKind::FieldRef(name) => sib_subs.get(name).cloned(),
        logisheets_parser::PlaceholderKind::Key => Some(key_node.clone()),
    });
    let Some(ast) = ast else { return Ok(executor) };
    register_parsed_ast(executor, sheet, cell_id, ast, ctx)
}

pub fn remove_formula<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    row: usize,
    col: usize,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    let cell_id = ctx.fetch_cell_id(&sheet, row, col)?;
    remove(executor, sheet, cell_id, ctx)
}

pub fn remove_ephemeral_formula<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    id: EphemeralId,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    remove(executor, sheet, CellId::EphemeralCell(id), ctx)
}

fn remove<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet: SheetId,
    cell_id: CellId,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let range = match cell_id {
        CellId::NormalCell(normal) => Range::Normal(NormalRange::Single(normal)),
        CellId::BlockCell(block) => Range::Block(BlockRange::Single(block)),
        CellId::EphemeralCell(v) => Range::Ephemeral(v),
    };
    let range_id = ctx.fetch_range_id(&sheet, &range);
    let this_vertex = Vertex::Range(sheet, range_id);

    let FormulaManager {
        mut graph,
        mut formulas,
        names,
    } = executor.manager;
    if let Some(old_formula_deps) = graph.clone().get_deps(&this_vertex) {
        old_formula_deps.iter().for_each(|old_dep| {
            graph.remove_dep(&this_vertex, old_dep);
        })
    };
    if let CellId::BlockCell(bcid) = cell_id {
        graph.remove_dep(&Vertex::BlockAll(sheet, bcid.block_id), &this_vertex);
    }

    formulas.remove(&(sheet, cell_id));

    Ok(FormulaExecutor {
        manager: FormulaManager {
            graph,
            formulas,
            names,
        },
        dirty_vertices: executor.dirty_vertices,
        dirty_ranges: executor.dirty_ranges,
        dirty_cubes: executor.dirty_cubes,
        dirty_blocks: executor.dirty_blocks,
        trigger: executor.trigger,
    })
}

fn input<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet: SheetId,
    cell_id: CellId,
    formula: String,
    substitute: Option<ast::Node>,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let parser = Parser {};
    let ast = if let Some(node) = substitute {
        parser.parse_with_substitude(&formula, &node, sheet, ctx)
    } else {
        parser.parse(&formula, sheet, ctx)
    };
    let Some(ast) = ast else { return Ok(executor) };
    register_parsed_ast(executor, sheet, cell_id, ast, ctx)
}

/// Register a pre-parsed AST as the formula for `cell_id`. Shared
/// between the user-facing `input` (text-formula path) and the
/// template-driven block-cell input path.
fn register_parsed_ast<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet: SheetId,
    cell_id: CellId,
    ast: ast::Node,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    let range = match cell_id {
        CellId::NormalCell(normal) => Range::Normal(NormalRange::Single(normal)),
        CellId::BlockCell(block) => Range::Block(BlockRange::Single(block)),
        CellId::EphemeralCell(v) => Range::Ephemeral(v),
    };
    let range_id = ctx.fetch_range_id(&sheet, &range);
    let this_vertex = Vertex::Range(sheet, range_id);

    // Hard-reject any BLOCKREF / BLOCKREFS whose target block is
    // this formula cell's own block. Such a self-ref would create a
    // cycle with the topo-barrier edge `BlockAll(B) → cell`
    // registered below. Same-row sibling refs inside a templated
    // formula should use `#FIELD("name")` — it expands at template
    // instantiation time to a direct cell reference, no virtual
    // block vertex involved.
    if let CellId::BlockCell(bcid) = cell_id {
        if let Some(kind) = find_self_block_ref(&ast, sheet, bcid.block_id) {
            return Err(BasicError::InvalidFormula(format!(
                "{} targeting the cell's own block (sheet={}, block={}) \
                 is not allowed; use `#FIELD(\"...\")` for same-row \
                 sibling refs.",
                kind, sheet, bcid.block_id
            )));
        }
    }

    let mut new_formula_deps = HashSet::<Vertex>::new();
    get_all_vertices_from_ast(&ast, &mut new_formula_deps);

    let FormulaManager {
        mut graph,
        mut formulas,
        names,
    } = executor.manager;

    if let Some(old_formula_deps) = graph.clone().get_deps(&this_vertex) {
        old_formula_deps.iter().for_each(|old_dep| {
            graph.remove_dep(&this_vertex, old_dep);
        })
    };

    new_formula_deps.into_iter().for_each(|new_dep| {
        graph.add_dep(this_vertex.clone(), new_dep.clone());

        let range_deps = ctx.get_range_deps(&new_dep);
        range_deps.into_iter().for_each(|range_dep| {
            graph.add_dep(new_dep.clone(), range_dep);
        });
    });

    // Topo-barrier edge: every formula cell that lives inside a
    // block is registered as a dep of `BlockAll(block)`. Aggregators
    // like `=SUM(BLOCKREFS("X","*","f"))` depend on `BlockAll(X)`,
    // so this makes Tarjan compute every per-row formula in X BEFORE
    // the aggregator runs. It is also what bridges cross-block
    // propagation: after a block cell is recomputed, walking rdeps
    // reaches BlockAll(B), which reaches every external BLOCKREF
    // Single user.
    //
    // Cycle safety relies on `reject_self_block_ref` rejecting any
    // BLOCKREF whose target block equals the formula cell's own
    // block (`#FIELD("...")` is the supported way to express a
    // same-row sibling ref).
    if let CellId::BlockCell(bcid) = cell_id {
        graph.add_dep(Vertex::BlockAll(sheet, bcid.block_id), this_vertex.clone());
    }

    formulas.insert((sheet, cell_id), ast.clone());

    // Mark this formula's own vertex dirty so the end-of-tx calc pass
    // computes it on first registration. Without this, `register_parsed_ast`
    // only wires up the dep graph — the formula has no signal to
    // initially evaluate unless something upstream is also dirty in this
    // tx. That matters for templated block cells materialized by
    // `InsertRowsInBlock`: a newly-grown row's formulas (e.g. one whose
    // only deps are in *another* block, like BLOCKREF("Constants",...))
    // would otherwise register and immediately sit at empty forever.
    let mut dirty_vertices = executor.dirty_vertices;
    dirty_vertices.insert(this_vertex);

    Ok(FormulaExecutor {
        manager: FormulaManager {
            graph,
            formulas,
            names,
        },
        dirty_vertices,
        dirty_ranges: executor.dirty_ranges,
        dirty_cubes: executor.dirty_cubes,
        dirty_blocks: executor.dirty_blocks,
        trigger: executor.trigger,
    })
}

/// Materialize a block cell's value-formula template from the schema:
/// pull the template + the per-row sibling cell ids + the row's key
/// value, build a parse-time substitution closure for `#FIELD("name")`
/// and `#KEY`, parse, and register the resulting AST as the cell's
/// formula. No-op if the target cell isn't templated.
pub fn input_block_cell_template<C: FormulaExecCtx>(
    executor: FormulaExecutor,
    sheet_idx: usize,
    block_id: BlockId,
    block_row: usize,
    block_col: usize,
    ctx: &mut C,
) -> Result<FormulaExecutor, BasicError> {
    use std::collections::HashMap;
    let sheet = ctx
        .fetch_sheet_id_by_index(sheet_idx)
        .map_err(|l| BasicError::SheetIdxExceed(l))?;
    let bcid = ctx.fetch_block_cell_id(&sheet, &block_id, block_row, block_col)?;
    let Some(tpl) = ctx.block_cell_template(sheet, &bcid) else {
        return Ok(executor);
    };

    // Build per-sibling Reference nodes once, indexed by field name, so
    // the resolver closure stays pure (only reads, no ctx mutation).
    let mut sib_subs: HashMap<String, ast::Node> = HashMap::new();
    for (name, sib_cell) in tpl.siblings {
        let range = Range::Block(BlockRange::Single(sib_cell));
        let range_id = ctx.fetch_range_id(&sheet, &range);
        sib_subs.insert(
            name,
            ast::Node {
                pure: ast::PureNode::Reference(ast::CellReference::Mut(RangeDisplay {
                    range_id,
                    ref_abs: RefAbs::default(),
                    sheet_id: sheet,
                })),
                bracket: true,
            },
        );
    }
    let key_node = ast::Node {
        pure: ast::PureNode::Value(ast::Value::Text(tpl.key_value)),
        bracket: false,
    };

    // Strip a leading `=` if the schema author included it. The parser
    // takes formula bodies, not `=`-prefixed forms.
    let body = tpl
        .template
        .strip_prefix('=')
        .map(|s| s.to_string())
        .unwrap_or(tpl.template);

    let parser = Parser {};
    let ast = parser.parse_with_substitutes(&body, sheet, ctx, &|kind| match kind {
        logisheets_parser::PlaceholderKind::FieldRef(name) => sib_subs.get(name).cloned(),
        logisheets_parser::PlaceholderKind::Key => Some(key_node.clone()),
        // `#PLACEHOLDER` belongs to validation, not field templates;
        // leaving it untouched is harmless — surfaces as #NAME?.
        logisheets_parser::PlaceholderKind::Placeholder => None,
    });
    let Some(ast) = ast else { return Ok(executor) };
    register_parsed_ast(executor, sheet, CellId::BlockCell(bcid), ast, ctx)
}

// This method is only used in loading a file (especially for shared formula).
// So there are somethings different from inputting a formula.
pub fn add_ast_node(
    manager: &mut FormulaManager,
    sheet_id: SheetId,
    cell_id: CellId,
    range_id: RangeId,
    ast: ast::Node,
) {
    let this_vertex = Vertex::Range(sheet_id, range_id);
    let mut new_formula_deps = HashSet::<Vertex>::new();
    get_all_vertices_from_ast(&ast, &mut new_formula_deps);
    manager.formulas.insert((sheet_id, cell_id), ast);

    new_formula_deps
        .into_iter()
        .for_each(|new_dep| manager.graph.add_dep(this_vertex.clone(), new_dep));
}

/// Walk the AST looking for any `BLOCKREF` / `BLOCKREFS` whose target
/// `(sheet, block)` matches `(self_sheet, self_block)`. Returns the
/// human-readable kind ("BLOCKREF" / "BLOCKREFS") of the first match.
///
/// Used to enforce the "no self-block ref" invariant inside templated
/// formulas — see `register_parsed_ast`.
fn find_self_block_ref(
    ast: &ast::Node,
    self_sheet: SheetId,
    self_block: BlockId,
) -> Option<&'static str> {
    match &ast.pure {
        ast::PureNode::Func(func) => func
            .args
            .iter()
            .find_map(|n| find_self_block_ref(n, self_sheet, self_block)),
        ast::PureNode::Value(_) => None,
        ast::PureNode::Reference(_) => None,
        ast::PureNode::BlockRef(node) => match node {
            ast::BlockRefNode::Single {
                sheet_id,
                block_id,
                key,
                ..
            } => {
                if *sheet_id == self_sheet && *block_id == self_block {
                    Some("BLOCKREF")
                } else {
                    find_self_block_ref(key, self_sheet, self_block)
                }
            }
            ast::BlockRefNode::Multi {
                sheet_id,
                block_id,
                key_condition,
                field_condition,
                ..
            } => {
                if *sheet_id == self_sheet && *block_id == self_block {
                    Some("BLOCKREFS")
                } else {
                    find_self_block_ref(key_condition, self_sheet, self_block)
                        .or_else(|| find_self_block_ref(field_condition, self_sheet, self_block))
                }
            }
        },
    }
}

fn get_all_vertices_from_ast(ast: &ast::Node, vertices: &mut HashSet<Vertex>) {
    match &ast.pure {
        ast::PureNode::Func(func) => {
            func.args
                .iter()
                .for_each(|n| get_all_vertices_from_ast(n, vertices));
        }
        ast::PureNode::Value(_) => {}
        ast::PureNode::Reference(reference) => match reference {
            ast::CellReference::Mut(r) => {
                let sheet_id = r.sheet_id;
                let range_id = r.range_id;
                let vertex = Vertex::Range(sheet_id, range_id);
                vertices.insert(vertex);
            }
            ast::CellReference::UnMut(c) => {
                let cube_id = c.cube_id;
                let vertex = Vertex::Cube(cube_id);
                vertices.insert(vertex);
            }
            ast::CellReference::Ext(ext_ref) => {
                let ext_ref_id = ext_ref.ext_ref_id;
                let vertex = Vertex::Ext(ext_ref_id);
                vertices.insert(vertex);
            }
            ast::CellReference::Name(name) => {
                let vertex = Vertex::Name(*name);
                vertices.insert(vertex);
            }
            ast::CellReference::RefErr => {}
        },
        ast::PureNode::BlockRef(node) => match node {
            ast::BlockRefNode::Single {
                sheet_id,
                block_id,
                field_id,
                key,
                ..
            } => {
                // Single-cell ref reacts to: that field column, the
                // key column (because the matching row depends on key
                // values), and any structural change to the block.
                //
                // `BlockAll(B)` is load-bearing for cross-block
                // propagation: when a cell inside B is recomputed via
                // cascade, the topo-barrier edge `BlockAll(B) → cell`
                // (registered in `register_parsed_ast` for every
                // formula cell inside B) means BlockAll(B) gets walked
                // in rdeps, which then reaches any external Single
                // user. The same-block self-ref that would otherwise
                // cycle is rejected by `reject_self_block_ref` below.
                vertices.insert(Vertex::Block(*sheet_id, *block_id, *field_id));
                vertices.insert(Vertex::BlockKey(*sheet_id, *block_id));
                vertices.insert(Vertex::BlockAll(*sheet_id, *block_id));
                get_all_vertices_from_ast(key, vertices);
            }
            ast::BlockRefNode::Multi {
                sheet_id,
                block_id,
                key_condition,
                field_condition,
                ..
            } => {
                // Multi scans an arbitrary subset of fields under runtime
                // filters; tracking exactly which fields survive the filter
                // is a future optimization. For correctness we depend on the
                // whole block (any field column, any key change, structural
                // change).
                vertices.insert(Vertex::BlockAll(*sheet_id, *block_id));
                vertices.insert(Vertex::BlockKey(*sheet_id, *block_id));
                get_all_vertices_from_ast(key_condition, vertices);
                get_all_vertices_from_ast(field_condition, vertices);
            }
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_all_vertices_from_ast() {
        // let f = "=B2*(1+A2)";
        let sum = ast::Node {
            pure: ast::PureNode::Func(ast::Func {
                op: ast::Operator::Infix(ast::InfixOperator::Plus),
                args: vec![
                    ast::Node {
                        pure: ast::PureNode::Value(ast::Value::Number(1.0)),
                        bracket: false,
                    },
                    ast::Node {
                        pure: ast::PureNode::Reference(ast::CellReference::Mut(
                            ast::RangeDisplay {
                                sheet_id: 0,
                                range_id: 0,
                                ref_abs: RefAbs::from_addr(false, false),
                            },
                        )),
                        bracket: false,
                    },
                ],
            }),
            bracket: false,
        };
        let ast = ast::Node {
            pure: ast::PureNode::Func(ast::Func {
                op: ast::Operator::Infix(ast::InfixOperator::Multiply),
                args: vec![
                    sum,
                    ast::Node {
                        pure: ast::PureNode::Reference(ast::CellReference::Mut(
                            ast::RangeDisplay {
                                sheet_id: 0,
                                range_id: 1,
                                ref_abs: RefAbs::from_addr(false, false),
                            },
                        )),
                        bracket: false,
                    },
                ],
            }),
            bracket: false,
        };

        let mut vertices = HashSet::<Vertex>::new();
        get_all_vertices_from_ast(&ast, &mut vertices);
        assert_eq!(vertices.len(), 2);
    }
}