dbtui 0.3.22

Terminal database client with Vim-style navigation
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
mod overlays;
mod tabs;
use overlays::*;
use tabs::*;

use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};
use unicode_width::UnicodeWidthStr;

use crate::core::virtual_fs::SyncState;
use crate::ui::state::{AppState, Focus, Mode, Overlay};
use crate::ui::tabs::{SubView, WorkspaceTab};
use crate::ui::theme::Theme;
use crate::ui::widgets;

const SIDEBAR_MIN_WIDTH: u16 = 22;

/// Write explicit space characters to every cell in the area.
/// Prevents ghosting: ratatui's diff always has real content to compare.
fn fill_bg(frame: &mut Frame, area: Rect, style: Style) {
    let fill = " ".repeat(area.width as usize);
    let lines: Vec<Line> = (0..area.height)
        .map(|_| Line::from(Span::styled(fill.clone(), style)))
        .collect();
    frame.render_widget(Paragraph::new(lines), area);
}

pub fn render(frame: &mut Frame, state: &mut AppState, theme: &Theme) {
    let area = frame.area();

    // Root: top bar (1) + main content (fill) + status bar (1)
    let root = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(5),
            Constraint::Length(1),
        ])
        .split(area);

    render_topbar(frame, state, theme, root[0]);

    // Main: sidebar (optional) + center
    if state.sidebar_visible {
        let sidebar_width = (area.width / 5).max(SIDEBAR_MIN_WIDTH).min(area.width / 3);
        let main = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Length(sidebar_width), Constraint::Min(20)])
            .split(root[1]);
        let sidebar_split = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Percentage(66), Constraint::Percentage(34)])
            .split(main[0]);
        widgets::sidebar::render(frame, &mut *state, theme, sidebar_split[0]);
        render_scripts_panel(frame, state, theme, sidebar_split[1]);
        render_center(frame, state, theme, main[1]);
    } else {
        render_center(frame, state, theme, root[1]);
    };

    widgets::statusbar::render(frame, state, theme, root[2]);

    // Oil floating navigator (above main UI, below overlays)
    if state.oil.is_some() {
        widgets::oil_navigator::render(frame, state, theme);
    }

    // Render overlays on top
    match &state.overlay {
        Some(Overlay::ConnectionDialog) => {
            widgets::connection_dialog::render(
                frame,
                &state.dialogs.connection_form,
                &state.dialogs.saved_connections,
                theme,
            );
        }
        Some(Overlay::Help) => {
            widgets::help::render(frame, state, theme);
        }
        Some(Overlay::ConnectionMenu) => {
            widgets::conn_menu::render(frame, &state.dialogs.conn_menu, theme);
        }
        Some(Overlay::GroupMenu) => {
            widgets::group_menu::render(frame, &state.dialogs.group_menu, theme);
        }
        Some(Overlay::ObjectFilter) => {
            widgets::schema_filter::render(frame, &mut state.sidebar.object_filter, theme);
        }
        Some(Overlay::ConfirmDeleteConnection { name }) => {
            render_confirm_delete_connection(frame, theme, area, name);
        }
        Some(Overlay::ConfirmClose) => {
            render_confirm_close(frame, theme, area);
        }
        Some(Overlay::ConfirmQuit) => {
            render_confirm_quit(frame, state, theme, area);
        }
        Some(Overlay::SaveScriptName) => {
            render_save_script_name(frame, state, theme, area);
        }
        Some(Overlay::ScriptConnection) => {
            render_script_conn_picker(frame, state, theme, area);
        }
        Some(Overlay::ThemePicker) => {
            render_theme_picker(frame, state, theme, area);
        }
        Some(Overlay::BindVariables) => {
            render_bind_variables(frame, state, theme, area);
        }
        Some(Overlay::SaveGridChanges) => {
            render_save_grid_confirm(frame, state, theme, area);
        }
        Some(Overlay::ConfirmDropObject) => {
            render_confirm_drop(frame, state, theme, area);
        }
        Some(Overlay::RenameObject) => {
            render_rename_object(frame, state, theme, area);
        }
        Some(Overlay::ConfirmCompile) => {
            render_confirm_compile(frame, state, theme, area);
        }
        Some(Overlay::ExportDialog) => {
            render_export_dialog(frame, state, theme, area);
        }
        Some(Overlay::ImportDialog) => {
            render_import_dialog(frame, state, theme, area);
        }
        _ => {}
    }

    // Experimental inline connection editor (Proposal D). Rendered as a
    // floating panel on top of everything else when active.
    if state.dialogs.inline_conn_editor.is_some() {
        crate::ui::widgets::inline_conn::render(frame, state, theme);
    }

    // Leader help hint (non-blocking, bottom-right corner)
    if state.leader.help_visible {
        let level = if state.leader.b_pending {
            2
        } else if state.leader.leader_pending {
            3
        } else if state.leader.w_pending {
            4
        } else if state.leader.s_pending {
            5
        } else if state.leader.f_pending {
            6
        } else if state.leader.q_pending {
            7
        } else {
            1
        };
        render_leader_help(frame, state, theme, area, level);
    }
}

/// Render the leader help popup. `level`: 1=root, 2=after b, 3=after <leader>
pub(crate) fn render_scripts_panel(
    frame: &mut Frame,
    state: &mut AppState,
    theme: &Theme,
    area: Rect,
) {
    let is_focused = state.focus == Focus::ScriptsPanel;
    render_scripts_panel_with_focus(frame, state, theme, area, is_focused);
}

pub(crate) fn render_scripts_panel_with_focus(
    frame: &mut Frame,
    state: &mut AppState,
    theme: &Theme,
    area: Rect,
    is_focused: bool,
) {
    use crate::ui::state::{ScriptNode, ScriptsMode};
    let border_style = theme.border_style(is_focused, &state.mode);

    let script_count = state
        .scripts
        .tree
        .iter()
        .filter(|n| matches!(n, ScriptNode::Script { .. }))
        .count();

    // Show mode hint in title
    let mode_hint = match &state.scripts.mode {
        ScriptsMode::PendingD => " [d]",
        ScriptsMode::PendingY => " [y]",
        _ => "",
    };
    let title = format!(" Scripts ({script_count}){mode_hint} ");

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(border_style)
        .style(Style::default().bg(theme.editor_bg));

    let inner = block.inner(area);
    frame.render_widget(block, area);

    if inner.height == 0 {
        return;
    }

    let visible_height = inner.height as usize;

    let visible: Vec<(usize, ScriptNode)> = state
        .scripts
        .visible_scripts()
        .into_iter()
        .map(|(i, n)| (i, n.clone()))
        .collect();

    if state.scripts.cursor < state.scripts.offset {
        state.scripts.offset = state.scripts.cursor;
    }
    if state.scripts.cursor >= state.scripts.offset + visible_height {
        state.scripts.offset = state.scripts.cursor - visible_height + 1;
    }

    if state.scripts.tree.is_empty() && !matches!(state.scripts.mode, ScriptsMode::Insert { .. }) {
        let lines = vec![
            Line::from(Span::styled(
                "  (no scripts)",
                Style::default().fg(theme.dim),
            )),
            Line::from(Span::styled(
                "  press i to create",
                Style::default().fg(theme.dim),
            )),
        ];
        let content = Paragraph::new(lines);
        frame.render_widget(content, inner);
        return;
    }

    let inner_width = inner.width as usize;

    let mut lines: Vec<Line> = visible
        .iter()
        .enumerate()
        .skip(state.scripts.offset)
        .take(visible_height)
        .map(|(vi, (_tree_idx, node))| {
            let is_selected = vi == state.scripts.cursor && is_focused;

            // Check for confirm delete on this item
            if let ScriptsMode::ConfirmDelete { path } = &state.scripts.mode {
                let node_path = match node {
                    ScriptNode::Collection { name, .. } => name.as_str(),
                    ScriptNode::Script { file_path, .. } => file_path.as_str(),
                };
                if path == node_path {
                    let display = match node {
                        ScriptNode::Collection { name, .. } => format!("{name}/"),
                        ScriptNode::Script { name, .. } => name.clone(),
                    };
                    return Line::from(vec![
                        Span::styled(
                            format!("  Delete '{display}'? "),
                            Style::default()
                                .fg(theme.error_fg)
                                .add_modifier(Modifier::BOLD),
                        ),
                        Span::styled(
                            "y",
                            Style::default()
                                .fg(theme.conn_connected)
                                .add_modifier(Modifier::BOLD),
                        ),
                        Span::styled("/", Style::default().fg(theme.dim)),
                        Span::styled(
                            "n",
                            Style::default()
                                .fg(theme.error_fg)
                                .add_modifier(Modifier::BOLD),
                        ),
                    ]);
                }
            }

            // Check for rename on this item
            if let ScriptsMode::Rename { buf, original_path } = &state.scripts.mode {
                let node_path = match node {
                    ScriptNode::Collection { name, .. } => name.as_str(),
                    ScriptNode::Script { file_path, .. } => file_path.as_str(),
                };
                if original_path == node_path {
                    let indent: String = match node {
                        ScriptNode::Collection { name, .. } => {
                            "  ".repeat(name.matches('/').count() + 1)
                        }
                        ScriptNode::Script { collection, .. } => match collection {
                            Some(c) => "  ".repeat(c.matches('/').count() + 2),
                            None => "  ".to_string(),
                        },
                    };
                    return Line::from(Span::styled(
                        format!("{indent}{buf}â–ˆ"),
                        Style::default()
                            .fg(theme.conn_connecting)
                            .add_modifier(Modifier::BOLD),
                    ));
                }
            }

            match node {
                ScriptNode::Collection { name, expanded } => {
                    // Indent by depth (number of `/` in the full path).
                    let depth = name.matches('/').count();
                    let indent: String = "  ".repeat(depth + 1);
                    let icon = if *expanded { "â–¼" } else { "â–¶" };
                    // Show only the last segment so deep trees stay readable.
                    let last = name.rsplit('/').next().unwrap_or(name.as_str());
                    let text = format!("{indent}{icon} {last}/");
                    let style = if is_selected {
                        Style::default()
                            .bg(theme.tree_selected_bg)
                            .fg(theme.accent)
                            .add_modifier(Modifier::BOLD)
                    } else {
                        Style::default()
                            .fg(theme.accent)
                            .add_modifier(Modifier::BOLD)
                    };
                    let display_w = UnicodeWidthStr::width(text.as_str());
                    let padded = if display_w < inner_width {
                        format!("{}{}", text, " ".repeat(inner_width - display_w))
                    } else {
                        text
                    };
                    Line::from(Span::styled(padded, style))
                }
                ScriptNode::Script {
                    name, collection, ..
                } => {
                    // Script indent = (parent depth + 2) levels of 2-space
                    // units, so it nests one step deeper than its parent
                    // collection line. Root scripts keep the original 2-space
                    // indent.
                    let indent: String = match collection {
                        Some(coll) => "  ".repeat(coll.matches('/').count() + 2),
                        None => "  ".to_string(),
                    };
                    let text = format!("{indent}{name}");
                    let style = if is_selected {
                        Style::default()
                            .bg(theme.tree_selected_bg)
                            .fg(theme.tree_selected_fg)
                    } else {
                        Style::default()
                    };
                    let display_w = UnicodeWidthStr::width(text.as_str());
                    let padded = if display_w < inner_width {
                        format!("{}{}", text, " ".repeat(inner_width - display_w))
                    } else {
                        text
                    };
                    Line::from(Span::styled(padded, style))
                }
            }
        })
        .collect();

    // Insert mode: show input line at the cursor position (inside current collection)
    if let ScriptsMode::Insert { buf } = &state.scripts.mode {
        let indent: String = match state.scripts.current_collection() {
            Some(coll) => "  ".repeat(coll.matches('/').count() + 2),
            None => "  ".to_string(),
        };
        let input_line = Line::from(Span::styled(
            format!("{indent}> {buf}â–ˆ"),
            Style::default()
                .fg(theme.conn_connecting)
                .add_modifier(Modifier::BOLD),
        ));
        // Insert after the current cursor position within the visible lines
        let insert_pos = if state.scripts.cursor >= state.scripts.offset {
            (state.scripts.cursor - state.scripts.offset + 1).min(lines.len())
        } else {
            0
        };
        lines.insert(insert_pos, input_line);
    }

    // Show yank indicator
    if state.scripts.yank.is_some() {
        let remaining = visible_height.saturating_sub(lines.len());
        if remaining > 0 {
            lines.push(Line::from(Span::styled(
                "  [yanked — p to paste]",
                Style::default().fg(theme.dim),
            )));
        }
    }

    let content = Paragraph::new(lines);
    frame.render_widget(content, inner);
}

fn render_topbar(frame: &mut Frame, state: &mut AppState, theme: &Theme, area: Rect) {
    // Resolve the displayed connection. Priority:
    //   1. If focus is on the sidebar, use whichever Connection node owns
    //      the cursor — so navigating into another schema's tables changes
    //      the topbar to that connection.
    //   2. Otherwise (focus on tabs / scripts / oil), use the active tab's
    //      connection.
    //   3. Fall back to the global state.conn.
    let sidebar_conn_name = if state.focus == Focus::Sidebar {
        state
            .selected_tree_index()
            .and_then(|idx| state.connection_for_tree_idx(idx).map(|s| s.to_string()))
    } else {
        None
    };
    let tab_conn_name = state
        .active_tab()
        .and_then(|t| t.kind.conn_name().map(|s| s.to_string()));
    let resolved_conn = sidebar_conn_name.or(tab_conn_name);

    let (conn_name_display, db_label, schema, is_connected) = if let Some(cn) = resolved_conn {
        let db_type = state
            .dialogs
            .saved_connections
            .iter()
            .find(|c| c.name == cn)
            .map(|c| c.db_type.to_string())
            .unwrap_or_default();
        let schema = state
            .engine
            .metadata_indexes
            .get(&cn)
            .and_then(|idx| idx.current_schema())
            .unwrap_or("")
            .to_string();
        let connected = state.sidebar.tree.iter().any(|n| {
            matches!(n, crate::ui::state::TreeNode::Connection { name, status, .. }
                if name == &cn && *status == crate::ui::state::ConnStatus::Connected)
        });
        (cn, db_type, schema, connected)
    } else {
        let cn = state
            .conn
            .name
            .clone()
            .unwrap_or_else(|| "not connected".to_string());
        let db_label = state
            .conn
            .db_type
            .as_ref()
            .map(|t| t.to_string())
            .unwrap_or_default();
        let schema = state.conn.current_schema.clone().unwrap_or_default();
        (cn, db_label, schema, state.conn.connected)
    };

    let (conn_icon, conn_style) = theme.connection_indicator(is_connected);
    let conn_name = conn_name_display.as_str();
    let schema = schema.as_str();
    let db_label = db_label.as_str();

    let status_text = if is_connected {
        "CONNECTED"
    } else {
        "DISCONNECTED"
    };

    let sep = Span::styled(" \u{2502} ", Style::default().fg(theme.separator));

    let line = Line::from(vec![
        Span::raw(" "),
        Span::styled(conn_icon, conn_style),
        Span::raw(" "),
        Span::styled(
            conn_name,
            Style::default()
                .fg(theme.topbar_fg)
                .add_modifier(Modifier::BOLD),
        ),
        sep.clone(),
        Span::styled(db_label, Style::default().fg(theme.accent)),
        sep.clone(),
        Span::styled(
            schema,
            Style::default()
                .fg(theme.tree_schema)
                .add_modifier(Modifier::BOLD),
        ),
        sep,
        Span::styled(
            status_text,
            if is_connected {
                Style::default().fg(theme.conn_connected)
            } else {
                Style::default().fg(theme.conn_disconnected)
            },
        ),
    ]);

    let bar = Paragraph::new(line).style(Style::default().bg(theme.topbar_bg));
    frame.render_widget(bar, area);
}

fn render_center(frame: &mut Frame, state: &mut AppState, theme: &Theme, area: Rect) {
    fill_bg(frame, area, Style::default().bg(theme.editor_bg));

    if state.tabs.is_empty() {
        render_empty_workspace(frame, theme, area);
        return;
    }

    // Split rendering: when groups is Some, render two halves side-by-side
    if state.groups.is_some() {
        let halves = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(area);

        let saved_active_tab_idx = state.active_tab_idx;
        let saved_active_group = state.active_group;

        // Render group 0 (left)
        render_group_panel(frame, state, theme, halves[0], 0);
        // Render group 1 (right)
        render_group_panel(frame, state, theme, halves[1], 1);

        // Restore active tab idx for downstream code (diagnostics, etc.)
        state.active_group = saved_active_group;
        state.active_tab_idx = saved_active_tab_idx;
        state.sync_active_tab_idx();

        // Diagnostics render only on the focused group's content area
        let focused_half = halves[saved_active_group];
        let content_area = compute_content_area(state, focused_half);
        render_diagnostics_overlays(frame, state, theme, content_area);
        return;
    }

    let content_area = render_single_tab_panel(frame, state, theme, area);
    render_diagnostics_overlays(frame, state, theme, content_area);
}

/// Render the tab bar + sub-view bar + content for the current active tab.
/// Returns the content area Rect.
fn render_single_tab_panel(
    frame: &mut Frame,
    state: &mut AppState,
    theme: &Theme,
    area: Rect,
) -> Rect {
    let has_sub_views = state
        .active_tab()
        .map(|t| !t.available_sub_views().is_empty())
        .unwrap_or(false);

    let constraints = if has_sub_views {
        vec![
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Min(3),
        ]
    } else {
        vec![Constraint::Length(1), Constraint::Min(3)]
    };

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(area);

    render_tab_bar(frame, state, theme, chunks[0]);

    if has_sub_views {
        render_sub_view_bar(frame, state, theme, chunks[1]);
        render_tab_content(frame, state, theme, chunks[2]);
        chunks[2]
    } else {
        render_tab_content(frame, state, theme, chunks[1]);
        chunks[1]
    }
}

/// Render a single group's tab bar + content. Temporarily swaps `active_tab_idx`
/// to the group's active tab so the existing renderers work unchanged.
fn render_group_panel(
    frame: &mut Frame,
    state: &mut AppState,
    theme: &Theme,
    area: Rect,
    group_idx: usize,
) {
    // Get the tab ID for this group's active tab
    let target_id = state
        .groups
        .as_ref()
        .and_then(|g| g[group_idx].active_tab_id());
    let target_tab_idx = target_id.and_then(|id| state.tabs.iter().position(|t| t.id == id));

    if let Some(idx) = target_tab_idx {
        // Swap state to point at this group's active tab during rendering
        state.active_tab_idx = idx;
        state.rendering_group = Some(group_idx);
        let _ = render_single_tab_panel(frame, state, theme, area);
        state.rendering_group = None;
    }
}

/// Compute the content area Rect for the focused tab (mirrors render_single_tab_panel layout).
fn compute_content_area(state: &AppState, area: Rect) -> Rect {
    let has_sub_views = state
        .active_tab()
        .map(|t| !t.available_sub_views().is_empty())
        .unwrap_or(false);

    let constraints = if has_sub_views {
        vec![
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Min(3),
        ]
    } else {
        vec![Constraint::Length(1), Constraint::Min(3)]
    };

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(area);

    if has_sub_views { chunks[2] } else { chunks[1] }
}

/// Render diagnostic underlines, list, completion popup, hover tooltip on the given content area.
fn render_diagnostics_overlays(
    frame: &mut Frame,
    state: &mut AppState,
    theme: &Theme,
    content_area: Rect,
) {
    // Render diagnostic list panel (Spc-x)
    if state.engine.diagnostic_list_visible && !state.engine.diagnostics.is_empty() {
        let list_height = (content_area.height / 4).clamp(5, 10);
        let list_area = Rect::new(
            content_area.x,
            content_area.bottom().saturating_sub(list_height),
            content_area.width,
            list_height,
        );
        render_diagnostic_list(frame, state, theme, list_area);
    }

    // Render diagnostic underlines on the editor (skip for PL/SQL tabs)
    if !state.engine.diagnostics.is_empty() {
        let is_plsql = state.active_tab().is_some_and(|t| {
            matches!(
                t.kind,
                crate::ui::tabs::TabKind::Package { .. }
                    | crate::ui::tabs::TabKind::Function { .. }
                    | crate::ui::tabs::TabKind::Procedure { .. }
                    | crate::ui::tabs::TabKind::DbType { .. }
                    | crate::ui::tabs::TabKind::Trigger { .. }
            )
        });
        if !is_plsql {
            render_diagnostic_underlines(frame, state, theme, content_area);
        }
    }

    // Render completion popup on top of everything
    if state.engine.completion.is_some() {
        render_completion_popup(frame, state, theme, content_area);
    }

    // Render diagnostic hover tooltip (K key)
    if let Some((row, ref msg)) = state.engine.diagnostic_hover {
        render_diagnostic_hover(frame, state, theme, content_area, row, msg);
    }
}

fn render_empty_workspace(frame: &mut Frame, theme: &Theme, area: Rect) {
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.border_unfocused))
        .style(Style::default().bg(theme.editor_bg));
    let text = Paragraph::new(vec![
        Line::from(""),
        Line::from(Span::styled(
            "  No tabs open",
            Style::default().fg(theme.dim),
        )),
        Line::from(""),
        Line::from(Span::styled(
            "  i  - New script (in scripts panel)",
            Style::default().fg(theme.dim),
        )),
        Line::from(Span::styled(
            "  l  - Open selected object",
            Style::default().fg(theme.dim),
        )),
        Line::from(Span::styled(
            "  a  - Add connection",
            Style::default().fg(theme.dim),
        )),
        Line::from(Span::styled("  ?  - Help", Style::default().fg(theme.dim))),
    ])
    .block(block);
    frame.render_widget(text, area);
}

// ---------------------------------------------------------------------------
// Export / Import dialog rendering
// ---------------------------------------------------------------------------