nativeview_cli/view_diff/mod.rs
1//! Additive Tier-1 hot-patch differ for `view! { ... }` Rust source files powered by
2//! [`nativeview_view_syntax`].
3//!
4//! Compares old vs new `.rs` source files containing `view!` blocks:
5//! - Single-sources lexing, `view!` scanning/parsing, deterministic `XXH64` [`AnchorKey`]
6//! computation, string interpolation analysis, and the Tier-1 direct-root allowlist
7//! (`Node`, `LeafNode`, `Text`, `Pressable`, `ScrollView`, `Column`, `Row`, bare string literals)
8//! directly from `nativeview-view-syntax`.
9//! - Reuses `hot_diff`'s typed attribute specs, const evaluator, `TypedStyle`/`Style` chain
10//! scanner, and direct binary `RowPatch` word lowering (zero CSS string emission).
11//! - Emits [`HotPatchMsg::TemplatePatch`] with `template_key = anchor_key.0` and
12//! `node_path = Vec::new()`, matching `nativeview_hot_reload::HotPatchIndex`'s O(1) contract.
13//! - Falls back cleanly to [`ViewDiffOutcome::NeedsRebuild`] for non-`view!` Rust code edits,
14//! structural tree edits, control-flow changes, dynamic expressions, interpolated strings, or
15//! composite component (`Button`, `Card`, `Dialog`, path-qualified tags) edits.
16
17mod diff;
18pub mod patch;
19mod prop_specs;
20mod props;
21mod tokens;
22
23#[cfg(test)]
24mod tests;
25
26use nativeview_engine::HotPatchMsg;
27use nativeview_view_syntax::{Lex, scan_lex_views, workspace_rel_file};
28
29use crate::hot_diff::cache::FileDiff;
30
31pub use diff::diff_view_source;
32pub use patch::{TypedViewPatch, ViewAnchorTarget, ViewAttrChange, ViewTextChange};
33
34use diff::diff_scanned_views;
35use tokens::{lex_has_view_macro, lex_streams_equal, may_contain_view_macro};
36
37/// Classification of a watched `.rs` file save containing `view! { ... }` blocks.
38#[derive(Debug, Clone, PartialEq)]
39pub enum ViewDiffOutcome {
40 /// Comment-only, whitespace-only, or semantically identical save (no patch or rebuild needed).
41 NoOp,
42 /// Static text/style/prop edits on direct-root patchable `view!` nodes, lowered directly to
43 /// `AnchorKey`-addressed binary `HotPatchMsg::TemplatePatch` messages (`node_path: vec![]`).
44 Tier1Patch {
45 patch: TypedViewPatch,
46 messages: Vec<HotPatchMsg>,
47 },
48 /// Non-`view!` Rust edit, structural/control-flow/dynamic change, composite component edit,
49 /// or syntax error requiring a Tier-2 rebuild.
50 NeedsRebuild { reason: String },
51}
52
53/// Fast, zero-allocation pre-check followed by token confirmation returning `true` iff `src`
54/// contains at least one `view! { ... }` / `view!(...)` / `view![...]` macro invocation outside
55/// comments, string literals, `macro_rules!` definitions, and `.view!` method positions.
56pub fn has_view_macro(src: &str) -> bool {
57 if !may_contain_view_macro(src) {
58 return false;
59 }
60 let Ok(lex) = Lex::tokenize(src) else {
61 // Unbalanced/unterminated syntax while `view!` pattern is present: let `diff_view_source`
62 // surface the syntax error cleanly as Tier-2.
63 return true;
64 };
65 lex_has_view_macro(&lex)
66}
67
68/// Watch-loop classifier behind [`crate::hot_diff::WatchCache::classify_batch`].
69///
70/// Comment/whitespace-only saves are [`FileDiff::NoOp`]; edits to files without any `view!`
71/// invocation are [`FileDiff::Tier2`] with zero AST parsing; `view!` files tokenize and scan
72/// `old_src` / `new_src` at most once.
73pub fn classify_for_watch(rel_file: &str, old_src: &str, new_src: &str, generation: u64) -> FileDiff {
74 if old_src == new_src {
75 return FileDiff::NoOp;
76 }
77 let old_lex = match Lex::tokenize(old_src) {
78 Ok(lex) => lex,
79 Err(err) => return FileDiff::Tier2(format!("baseline lex error: {err:?}")),
80 };
81 let new_lex = match Lex::tokenize(new_src) {
82 Ok(lex) => lex,
83 Err(err) => return FileDiff::Tier2(format!("syntax error: {err:?}")),
84 };
85 if lex_streams_equal(&old_lex, &new_lex) {
86 return FileDiff::NoOp;
87 }
88 if !lex_has_view_macro(&old_lex) && !lex_has_view_macro(&new_lex) {
89 return FileDiff::Tier2(format!("Rust code changed in {rel_file} (no view! invocation)"));
90 }
91
92 let norm_rel = workspace_rel_file(rel_file, "");
93 let old_calls = scan_lex_views(&old_lex, &norm_rel);
94 let new_calls = scan_lex_views(&new_lex, &norm_rel);
95 match diff_scanned_views(&old_lex, &new_lex, &old_calls, &new_calls, generation) {
96 ViewDiffOutcome::NoOp => FileDiff::NoOp,
97 ViewDiffOutcome::Tier1Patch { patch, messages } => FileDiff::Tier1 {
98 logs: patch.log_lines(),
99 messages,
100 },
101 ViewDiffOutcome::NeedsRebuild { reason } => FileDiff::Tier2(reason),
102 }
103}