This commit is contained in:
2026-01-23 10:37:48 +01:00
parent 183dd5b516
commit 8af64fc4e2
5 changed files with 165911 additions and 118 deletions

View File

@@ -6,7 +6,7 @@ use ratatui::Frame;
use crate::app::App;
use crate::engine::{LinkState, SequencerSnapshot};
use crate::model::forth::SourceSpan;
use crate::model::SourceSpan;
use crate::page::Page;
use crate::state::{Modal, PatternField};
use crate::views::highlight::{self, highlight_line, highlight_line_with_runtime};
@@ -14,6 +14,18 @@ use crate::widgets::{ConfirmModal, ModalFrame, TextInputModal};
use super::{audio_view, doc_view, main_view, patterns_view, title_view};
fn adjust_spans_for_line(spans: &[SourceSpan], line_start: usize, line_len: usize) -> Vec<SourceSpan> {
spans.iter().filter_map(|s| {
if s.end <= line_start || s.start >= line_start + line_len {
return None;
}
Some(SourceSpan {
start: s.start.max(line_start) - line_start,
end: (s.end.min(line_start + line_len)) - line_start,
})
}).collect()
}
pub fn render(frame: &mut Frame, app: &mut App, link: &LinkState, snapshot: &SequencerSnapshot) {
let term = frame.area();
let blank = " ".repeat(term.width as usize);
@@ -393,41 +405,25 @@ fn render_modal(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, term
};
frame.render_widget(empty, centered_area);
} else {
let runtime_spans = if app.ui.runtime_highlight && app.playback.playing {
snapshot.get_trace(
app.editor_ctx.bank,
app.editor_ctx.pattern,
step_idx,
)
let trace = if app.ui.runtime_highlight && app.playback.playing {
let source = pattern.resolve_source(step_idx);
snapshot.get_trace(app.editor_ctx.bank, app.editor_ctx.pattern, source)
} else {
None
};
let mut offset = 0usize;
let mut line_start = 0usize;
let lines: Vec<Line> = script
.lines()
.map(|line_str| {
let tokens = if let Some(traces) = runtime_spans {
let shifted: Vec<_> = traces
.iter()
.filter_map(|s| {
let start = s.start.saturating_sub(offset);
let end = s.end.saturating_sub(offset);
if end > 0 && start < line_str.len() {
Some(SourceSpan {
start: start.min(line_str.len()),
end: end.min(line_str.len()),
})
} else {
None
}
})
.collect();
highlight_line_with_runtime(line_str, &shifted)
let tokens = if let Some(t) = trace {
let exec = adjust_spans_for_line(&t.executed_spans, line_start, line_str.len());
let sel = adjust_spans_for_line(&t.selected_spans, line_start, line_str.len());
highlight_line_with_runtime(line_str, &exec, &sel)
} else {
highlight_line(line_str)
};
offset += line_str.len() + 1;
line_start += line_str.len() + 1;
let spans: Vec<Span> = tokens
.into_iter()
.map(|(style, text)| Span::styled(text, style))
@@ -459,8 +455,9 @@ fn render_modal(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, term
let (cursor_row, cursor_col) = app.editor_ctx.text.cursor();
let runtime_spans = if app.ui.runtime_highlight && app.playback.playing {
snapshot.get_trace(app.editor_ctx.bank, app.editor_ctx.pattern, app.editor_ctx.step)
let trace = if app.ui.runtime_highlight && app.playback.playing {
let source = app.current_edit_pattern().resolve_source(app.editor_ctx.step);
snapshot.get_trace(app.editor_ctx.bank, app.editor_ctx.pattern, source)
} else {
None
};
@@ -480,25 +477,16 @@ fn render_modal(frame: &mut Frame, app: &App, snapshot: &SequencerSnapshot, term
let mut spans: Vec<Span> = Vec::new();
let line_start = line_offsets[row];
let line_end = line_start + line.len();
let adjusted_spans: Vec<crate::model::SourceSpan> = runtime_spans
.map(|rs| {
rs.iter()
.filter_map(|s| {
if s.start < line_end && s.end > line_start {
Some(crate::model::SourceSpan {
start: s.start.saturating_sub(line_start),
end: s.end.saturating_sub(line_start).min(line.len()),
})
} else {
None
}
})
.collect()
})
.unwrap_or_default();
let (exec_spans, sel_spans) = if let Some(t) = trace {
(
adjust_spans_for_line(&t.executed_spans, line_start, line.len()),
adjust_spans_for_line(&t.selected_spans, line_start, line.len()),
)
} else {
(Vec::new(), Vec::new())
};
let tokens = highlight::highlight_line_with_runtime(line, &adjusted_spans);
let tokens = highlight::highlight_line_with_runtime(line, &exec_spans, &sel_spans);
if row == cursor_row {
let mut col = 0;