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
use std::sync::Arc;

use re_entity_db::InstancePath;
use re_log_types::{
    external::re_types_core::ComponentName, ComponentPath, EntityPath, EntityPathPart, Instance,
};

use egui::{text::LayoutJob, Color32, Style, TextFormat};

// ----------------------------------------------------------------------------
pub trait SyntaxHighlighting {
    fn syntax_highlighted(&self, style: &Style) -> LayoutJob {
        let mut job = LayoutJob::default();
        self.syntax_highlight_into(style, &mut job);
        job
    }

    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob);
}

// ----------------------------------------------------------------------------

/// Easily build syntax-highlighted text.
pub struct SyntaxHighlightedBuilder {
    pub style: Arc<Style>,
    pub job: LayoutJob,
}

/// Easilut build
impl SyntaxHighlightedBuilder {
    pub fn new(style: Arc<Style>) -> Self {
        Self {
            style,
            job: LayoutJob::default(),
        }
    }

    #[inline]
    pub fn append(mut self, portion: &dyn SyntaxHighlighting) -> Self {
        portion.syntax_highlight_into(&self.style, &mut self.job);
        self
    }

    #[inline]
    pub fn into_job(self) -> LayoutJob {
        self.job
    }

    #[inline]
    pub fn into_widget_text(self) -> egui::WidgetText {
        self.into_job().into()
    }
}

impl From<SyntaxHighlightedBuilder> for LayoutJob {
    fn from(builder: SyntaxHighlightedBuilder) -> Self {
        builder.into_job()
    }
}

impl From<SyntaxHighlightedBuilder> for egui::WidgetText {
    fn from(builder: SyntaxHighlightedBuilder) -> Self {
        builder.into_widget_text()
    }
}

// ----------------------------------------------------------------------------

fn text_format(style: &Style) -> TextFormat {
    TextFormat {
        font_id: egui::TextStyle::Body.resolve(style),

        // This color be replaced with appropriate color based on widget,
        // and whether the widget is hovered, etc
        color: Color32::PLACEHOLDER,

        ..Default::default()
    }
}

fn faint_text_format(style: &Style) -> TextFormat {
    TextFormat {
        color: Color32::WHITE,

        ..text_format(style)
    }
}

impl SyntaxHighlighting for &'_ str {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        job.append(self.to_owned(), 0.0, text_format(style));
    }
}

impl SyntaxHighlighting for String {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        job.append(self, 0.0, text_format(style));
    }
}

impl SyntaxHighlighting for EntityPathPart {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        job.append(&self.ui_string(), 0.0, text_format(style));
    }
}

impl SyntaxHighlighting for Instance {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        if self.is_all() {
            job.append("all", 0.0, text_format(style));
        } else {
            job.append(&re_format::format_uint(self.get()), 0.0, text_format(style));
        }
    }
}

impl SyntaxHighlighting for EntityPath {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        job.append("/", 0.0, faint_text_format(style));

        for (i, part) in self.iter().enumerate() {
            if i != 0 {
                job.append("/", 0.0, faint_text_format(style));
            }
            part.syntax_highlight_into(style, job);
        }
    }
}

impl SyntaxHighlighting for InstancePath {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        self.entity_path.syntax_highlight_into(style, job);
        if self.instance.is_specific() {
            InstanceInBrackets(self.instance).syntax_highlight_into(style, job);
        }
    }
}

impl SyntaxHighlighting for ComponentName {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        self.short_name().syntax_highlight_into(style, job);
    }
}

impl SyntaxHighlighting for ComponentPath {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        let Self {
            entity_path,
            component_name,
        } = self;
        entity_path.syntax_highlight_into(style, job);
        job.append(":", 0.0, faint_text_format(style));
        component_name.syntax_highlight_into(style, job);
    }
}

/// Formats an instance number enclosed in square brackets: `[123]`
pub struct InstanceInBrackets(pub Instance);

impl SyntaxHighlighting for InstanceInBrackets {
    fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
        job.append("[", 0.0, faint_text_format(style));
        self.0.syntax_highlight_into(style, job);
        job.append("]", 0.0, faint_text_format(style));
    }
}