egui-charts 0.2.0

High-performance financial charting engine for egui — candlesticks, 95 drawing tools, 130+ indicators, and a full design-token theme system
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
610
611
612
613
//! Pine Script editor dialog implementation.
//!
//! Provides a code text area, compile/run button, output panel, and error display
//! with basic syntax highlighting for the Pine-like scripting language.

use egui::{Context, RichText, ScrollArea, TextEdit, Vec2};

use crate::ext::UiExt;
use crate::scripting::{Lexer, ParseError, Parser, Runtime};
use crate::styles::typography;
use crate::tokens::DESIGN_TOKENS;
use crate::ui_kit::dialog::{DialogFrame, dialog_header};

/// Action returned by the Pine editor dialog
#[derive(Debug, Clone, PartialEq)]
pub enum PineEditorAction {
    /// No action
    None,
    /// Dialog was closed
    Close,
    /// Script was compiled and executed successfully
    Executed,
}

/// Compilation/execution result for display
#[derive(Debug, Clone)]
enum EditorResult {
    /// No result yet
    Empty,
    /// Parse error with line/column information
    ParseError {
        message: String,
        line: usize,
        column: usize,
    },
    /// Runtime error during execution
    RuntimeError(String),
    /// Successful execution with output lines
    Success(Vec<String>),
}

/// Pine Script editor dialog
pub struct PineEditor {
    /// Whether the dialog is currently open
    pub is_open: bool,
    /// Source code in the editor
    code: String,
    /// Last compilation/execution result
    result: EditorResult,
    /// Whether the output panel is expanded
    output_expanded: bool,
}

impl Default for PineEditor {
    fn default() -> Self {
        Self::new()
    }
}

impl PineEditor {
    pub fn new() -> Self {
        Self {
            is_open: false,
            code: Self::default_script().to_string(),
            result: EditorResult::Empty,
            output_expanded: true,
        }
    }

    /// Default example script shown when the editor opens
    fn default_script() -> &'static str {
        r#"//@version=5
indicator("My Script", overlay=true)

length = 20
sma_val = ta.sma(close, length)
plot(sma_val)"#
    }

    /// Open the dialog
    pub fn open(&mut self) {
        self.is_open = true;
    }

    /// Close the dialog
    pub fn close(&mut self) {
        self.is_open = false;
    }

    /// Show the dialog. Returns the action taken.
    pub fn show(&mut self, ctx: &Context) -> PineEditorAction {
        if !self.is_open {
            return PineEditorAction::None;
        }

        let mut action = PineEditorAction::None;

        DialogFrame::new(
            "Pine Script Editor",
            Vec2::new(
                DESIGN_TOKENS.sizing.dialog.default_width.max(560.0),
                DESIGN_TOKENS.sizing.dialog.default_height.max(480.0),
            ),
        )
        .show(ctx, |ui| {
            action = self.render_contents(ui);
        });

        // Close on Escape
        if ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
            self.is_open = false;
            action = PineEditorAction::Close;
        }

        action
    }

    fn render_contents(&mut self, ui: &mut egui::Ui) -> PineEditorAction {
        let mut action = PineEditorAction::None;

        // Title bar
        if dialog_header(ui, "Pine Script Editor") {
            action = PineEditorAction::Close;
            self.is_open = false;
        }
        ui.separator();

        // Toolbar with compile/run button
        ui.space_sm();
        ui.horizontal(|ui| {
            ui.space_lg();
            if ui
                .button(RichText::new("Compile & Run").size(typography::MD))
                .clicked()
            {
                self.compile_and_run();
                if matches!(self.result, EditorResult::Success(_)) {
                    action = PineEditorAction::Executed;
                }
            }
            ui.space_md();
            if ui
                .button(RichText::new("Clear").size(typography::MD))
                .clicked()
            {
                self.result = EditorResult::Empty;
            }
            ui.space_md();
            if ui
                .button(RichText::new("Reset").size(typography::MD))
                .clicked()
            {
                self.code = Self::default_script().to_string();
                self.result = EditorResult::Empty;
            }
        });
        ui.space_sm();
        ui.separator();

        // Code editor area
        ui.space_sm();

        let editor_height = if self.output_expanded {
            DESIGN_TOKENS.sizing.dialog.default_height.max(480.0) * 0.5
        } else {
            DESIGN_TOKENS.sizing.dialog.default_height.max(480.0) * 0.75
        };

        ScrollArea::vertical()
            .id_salt("pine_code_area")
            .max_height(editor_height)
            .show(ui, |ui| {
                self.render_code_editor(ui);
            });

        ui.separator();

        // Output/error panel
        self.render_output_panel(ui);

        action
    }

    /// Render the code editor with basic syntax highlighting via layouter
    fn render_code_editor(&mut self, ui: &mut egui::Ui) {
        let text_color = ui.style().visuals.text_color();
        let keyword_color = ui.style().visuals.hyperlink_color;
        let string_color = ui.style().visuals.warn_fg_color;
        let number_color = ui.style().visuals.widgets.active.fg_stroke.color;
        let comment_color = ui.style().visuals.weak_text_color();
        let builtin_color = keyword_color;

        let font_size = typography::SM_MD;

        let mut layouter = move |ui: &egui::Ui, text: &dyn egui::TextBuffer, wrap_width: f32| {
            let text_str = text.as_str();
            let mut job = egui::text::LayoutJob::default();
            job.wrap.max_width = wrap_width;

            // Tokenize for syntax highlighting
            highlight_pine_source(
                text_str,
                &mut job,
                ui,
                font_size,
                text_color,
                keyword_color,
                string_color,
                number_color,
                comment_color,
                builtin_color,
            );

            ui.painter().layout_job(job)
        };

        let editor = TextEdit::multiline(&mut self.code)
            .font(egui::FontId::monospace(font_size))
            .desired_width(f32::INFINITY)
            .min_size(Vec2::new(
                DESIGN_TOKENS.sizing.dialog.default_width.max(560.0)
                    - DESIGN_TOKENS.spacing.xl * 2.0,
                200.0,
            ))
            .layouter(&mut layouter);

        ui.add(editor);
    }

    /// Render the output/error panel
    fn render_output_panel(&mut self, ui: &mut egui::Ui) {
        ui.horizontal(|ui| {
            ui.space_lg();
            let toggle_label = if self.output_expanded {
                "Output [-]"
            } else {
                "Output [+]"
            };
            if ui
                .add(
                    egui::Label::new(
                        RichText::new(toggle_label)
                            .size(typography::MD)
                            .strong()
                            .color(ui.style().visuals.text_color()),
                    )
                    .sense(egui::Sense::click()),
                )
                .clicked()
            {
                self.output_expanded = !self.output_expanded;
            }
        });

        if !self.output_expanded {
            return;
        }

        ui.space_xs();

        let output_height = DESIGN_TOKENS.sizing.dialog.default_height.max(480.0) * 0.25;

        ScrollArea::vertical()
            .id_salt("pine_output_area")
            .max_height(output_height)
            .show(ui, |ui| {
                ui.space_xs();
                match &self.result {
                    EditorResult::Empty => {
                        ui.horizontal(|ui| {
                            ui.space_lg();
                            ui.label(
                                RichText::new("Press 'Compile & Run' to execute your script.")
                                    .size(typography::SM)
                                    .color(ui.style().visuals.weak_text_color()),
                            );
                        });
                    }
                    EditorResult::ParseError {
                        message,
                        line,
                        column,
                    } => {
                        ui.horizontal(|ui| {
                            ui.space_lg();
                            ui.label(
                                RichText::new(format!("Parse Error at line {line}, col {column}:"))
                                    .size(typography::SM)
                                    .strong()
                                    .color(ui.style().visuals.error_fg_color),
                            );
                        });
                        ui.horizontal(|ui| {
                            ui.space_xl();
                            ui.label(
                                RichText::new(message)
                                    .size(typography::SM)
                                    .color(ui.style().visuals.error_fg_color),
                            );
                        });
                    }
                    EditorResult::RuntimeError(msg) => {
                        ui.horizontal(|ui| {
                            ui.space_lg();
                            ui.label(
                                RichText::new("Runtime Error:")
                                    .size(typography::SM)
                                    .strong()
                                    .color(ui.style().visuals.error_fg_color),
                            );
                        });
                        ui.horizontal(|ui| {
                            ui.space_xl();
                            ui.label(
                                RichText::new(msg)
                                    .size(typography::SM)
                                    .color(ui.style().visuals.error_fg_color),
                            );
                        });
                    }
                    EditorResult::Success(lines) => {
                        for line in lines {
                            ui.horizontal(|ui| {
                                ui.space_lg();
                                ui.label(
                                    RichText::new(line)
                                        .size(typography::SM)
                                        .color(ui.style().visuals.text_color()),
                                );
                            });
                        }
                    }
                }
                ui.space_sm();
            });
    }

    /// Compile and run the current script
    fn compile_and_run(&mut self) {
        // Step 1: Parse
        let lexer = Lexer::new(&self.code);
        let mut parser = Parser::new(lexer);
        let program = match parser.parse() {
            Ok(p) => p,
            Err(e) => {
                let (line, col) = extract_parse_error_location(&e);
                self.result = EditorResult::ParseError {
                    message: format!("{e}"),
                    line,
                    column: col,
                };
                return;
            }
        };

        // Step 2: Execute with an empty bar set (validation mode)
        let mut runtime = Runtime::new();
        crate::scripting::register_builtins(&mut runtime);

        // Execute the script to verify it runs
        match runtime.execute(&self.code) {
            Ok(()) => {
                let mut output = Vec::new();
                output.push("Script compiled and executed successfully.".to_string());

                // Report number of statements
                output.push(format!("Statements: {}", program.statements.len()));

                // Report plots
                let plots = runtime.get_plots();
                if !plots.is_empty() {
                    output.push(format!("Plots: {}", plots.len()));
                    for plot in plots {
                        output.push(format!("  - {} ({} values)", plot.name, plot.values.len()));
                    }
                }

                // Report strategy results
                let strategy = runtime.get_strategy_state();
                if strategy.metrics.total_trades > 0 {
                    output.push("Strategy Results:".to_string());
                    output.push(format!("  Total trades: {}", strategy.metrics.total_trades));
                    output.push(format!(
                        "  Win rate: {:.1}%",
                        strategy.metrics.win_rate * 100.0
                    ));
                    output.push(format!("  Net profit: {:.2}", strategy.metrics.net_profit));
                    output.push(format!(
                        "  Max drawdown: {:.2}",
                        strategy.metrics.max_drawdown
                    ));
                }

                self.result = EditorResult::Success(output);
            }
            Err(e) => {
                self.result = EditorResult::RuntimeError(format!("{e}"));
            }
        }
    }
}

/// Extract line/column from a ParseError (best-effort)
fn extract_parse_error_location(err: &ParseError) -> (usize, usize) {
    match err {
        ParseError::UnexpectedToken { line, column, .. } => (*line, *column),
        ParseError::UnexpectedEof => (0, 0),
        ParseError::InvalidSyntax(_) => (0, 0),
    }
}

/// Basic Pine Script syntax highlighting applied to a LayoutJob
fn highlight_pine_source(
    text: &str,
    job: &mut egui::text::LayoutJob,
    _ui: &egui::Ui,
    font_size: f32,
    text_color: egui::Color32,
    keyword_color: egui::Color32,
    string_color: egui::Color32,
    number_color: egui::Color32,
    comment_color: egui::Color32,
    builtin_color: egui::Color32,
) {
    let font_id = egui::FontId::monospace(font_size);

    let mut chars = text.char_indices().peekable();

    while let Some(&(start_byte, ch)) = chars.peek() {
        // Comments: // to end of line
        if ch == '/' {
            let mut lookahead = chars.clone();
            lookahead.next();
            if let Some(&(_, '/')) = lookahead.peek() {
                // Consume to end of line
                let mut end_byte = start_byte;
                while let Some(&(idx, c)) = chars.peek() {
                    end_byte = idx + c.len_utf8();
                    chars.next();
                    if c == '\n' {
                        break;
                    }
                }
                job.append(
                    &text[start_byte..end_byte],
                    0.0,
                    egui::text::TextFormat {
                        font_id: font_id.clone(),
                        color: comment_color,
                        ..Default::default()
                    },
                );
                continue;
            }
        }

        // String literals
        if ch == '"' || ch == '\'' {
            let quote = ch;
            chars.next(); // skip opening quote
            let mut end_byte = start_byte + ch.len_utf8();
            let mut escaped = false;
            while let Some(&(idx, c)) = chars.peek() {
                end_byte = idx + c.len_utf8();
                chars.next();
                if escaped {
                    escaped = false;
                    continue;
                }
                if c == '\\' {
                    escaped = true;
                    continue;
                }
                if c == quote {
                    break;
                }
            }
            job.append(
                &text[start_byte..end_byte],
                0.0,
                egui::text::TextFormat {
                    font_id: font_id.clone(),
                    color: string_color,
                    ..Default::default()
                },
            );
            continue;
        }

        // Numbers
        if ch.is_ascii_digit()
            || (ch == '.'
                && chars
                    .clone()
                    .nth(1)
                    .is_some_and(|(_, c)| c.is_ascii_digit()))
        {
            let mut end_byte = start_byte;
            while let Some(&(idx, c)) = chars.peek() {
                if c.is_ascii_digit() || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-' {
                    // Allow +/- only after e/E
                    if (c == '+' || c == '-') && end_byte > start_byte {
                        let prev = text.as_bytes()[end_byte - 1];
                        if prev != b'e' && prev != b'E' {
                            break;
                        }
                    }
                    end_byte = idx + c.len_utf8();
                    chars.next();
                } else {
                    break;
                }
            }
            job.append(
                &text[start_byte..end_byte],
                0.0,
                egui::text::TextFormat {
                    font_id: font_id.clone(),
                    color: number_color,
                    ..Default::default()
                },
            );
            continue;
        }

        // Identifiers and keywords
        if ch.is_alphabetic() || ch == '_' {
            let mut end_byte = start_byte;
            while let Some(&(idx, c)) = chars.peek() {
                if c.is_alphanumeric() || c == '_' || c == '.' {
                    end_byte = idx + c.len_utf8();
                    chars.next();
                } else {
                    break;
                }
            }
            let word = &text[start_byte..end_byte];
            let color = classify_identifier(word, keyword_color, builtin_color, text_color);
            job.append(
                word,
                0.0,
                egui::text::TextFormat {
                    font_id: font_id.clone(),
                    color,
                    ..Default::default()
                },
            );
            continue;
        }

        // Everything else (operators, whitespace, punctuation)
        chars.next();
        let end_byte = start_byte + ch.len_utf8();
        job.append(
            &text[start_byte..end_byte],
            0.0,
            egui::text::TextFormat {
                font_id: font_id.clone(),
                color: text_color,
                ..Default::default()
            },
        );
    }
}

/// Classify an identifier token for syntax highlighting
fn classify_identifier(
    word: &str,
    keyword_color: egui::Color32,
    builtin_color: egui::Color32,
    default_color: egui::Color32,
) -> egui::Color32 {
    // Keywords
    match word {
        "if" | "else" | "for" | "while" | "var" | "varip" | "true" | "false" | "and" | "or"
        | "not" | "import" | "export" | "return" | "switch" | "break" | "continue" => {
            return keyword_color;
        }
        _ => {}
    }

    // Built-in variables
    match word {
        "open" | "high" | "low" | "close" | "volume" | "time" | "hl2" | "hlc3" | "ohlc4"
        | "bar_index" | "na" | "nz" => {
            return builtin_color;
        }
        _ => {}
    }

    // Namespaced builtins (ta.*, math.*, str.*, strategy.*, input.*, array.*, color.*)
    if word.starts_with("ta.")
        || word.starts_with("math.")
        || word.starts_with("str.")
        || word.starts_with("strategy.")
        || word.starts_with("input.")
        || word.starts_with("array.")
        || word.starts_with("color.")
    {
        return builtin_color;
    }

    // Top-level functions
    match word {
        "indicator" | "strategy" | "plot" | "plotshape" | "plotchar" | "plotarrow" | "bgcolor"
        | "fill" | "hline" | "alertcondition" | "fixnan" => {
            return builtin_color;
        }
        _ => {}
    }

    default_color
}