Skip to main content

wonopui/components/code_editor/
diff.rs

1use yew::prelude::*;
2
3/// Represents a type of diff change
4#[derive(Clone, Debug, PartialEq)]
5pub enum DiffType {
6    /// Line was added (green highlight)
7    Added,
8    /// Line was removed (red highlight)
9    Removed,
10    /// Line was modified (yellow highlight)
11    Modified
12}
13
14/// Represents a diff in the code editor
15#[derive(Clone, Debug, PartialEq, Properties)]
16pub struct Diff {
17    /// Line number where the diff appears (1-indexed)
18    pub line_number: usize,
19    
20    /// Type of change this diff represents
21    pub diff_type: DiffType,
22    
23    /// Optional column range for partial line diffs
24    /// Format is (start_column, end_column)
25    #[prop_or_default]
26    pub column_range: Option<(usize, usize)>,
27    
28    /// Optional message to show when hovering over the diff
29    #[prop_or_default]
30    pub message: Option<String>,
31}
32
33impl Diff {
34    /// Create a new diff for an added line
35    pub fn added(line_number: usize) -> Self {
36        Self {
37            line_number,
38            diff_type: DiffType::Added,
39            column_range: None,
40            message: None,
41        }
42    }
43    
44    /// Create a new diff for a removed line
45    pub fn removed(line_number: usize) -> Self {
46        Self {
47            line_number,
48            diff_type: DiffType::Removed,
49            column_range: None,
50            message: None,
51        }
52    }
53    
54    /// Create a new diff for a modified line
55    pub fn modified(line_number: usize) -> Self {
56        Self {
57            line_number,
58            diff_type: DiffType::Modified,
59            column_range: None,
60            message: None,
61        }
62    }
63    
64    /// Add a column range to this diff
65    pub fn with_column_range(mut self, start: usize, end: usize) -> Self {
66        self.column_range = Some((start, end));
67        self
68    }
69    
70    /// Add a message to this diff
71    pub fn with_message(mut self, message: impl Into<String>) -> Self {
72        self.message = Some(message.into());
73        self
74    }
75}