fixed highlight logic

This commit is contained in:
filipriec
2025-04-16 09:48:39 +02:00
parent afc8e1a1e5
commit 26b899df16

View File

@@ -23,7 +23,7 @@ pub fn render_canvas(
is_edit_mode: bool, is_edit_mode: bool,
highlight_state: &HighlightState, // Using the enum state highlight_state: &HighlightState, // Using the enum state
) -> Option<Rect> { ) -> Option<Rect> {
// ... (column split, container styling, block dimensions - unchanged) ... // ... (setup code remains the same) ...
let columns = Layout::default() let columns = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)]) .constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
@@ -93,9 +93,11 @@ pub fn render_canvas(
} }
HighlightState::Characterwise { anchor } => { HighlightState::Characterwise { anchor } => {
// --- Character-wise Highlight Logic --- // --- Character-wise Highlight Logic ---
let (anchor_field, anchor_char) = *anchor; // Dereference the tuple from the enum let (anchor_field, anchor_char) = *anchor;
let start_field = min(anchor_field, *current_field_idx); let start_field = min(anchor_field, *current_field_idx);
let end_field = max(anchor_field, *current_field_idx); let end_field = max(anchor_field, *current_field_idx);
// Use start_char and end_char consistently
let (start_char, end_char) = if anchor_field == *current_field_idx { let (start_char, end_char) = if anchor_field == *current_field_idx {
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos)) (min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
} else if anchor_field < *current_field_idx { } else if anchor_field < *current_field_idx {
@@ -111,17 +113,22 @@ pub fn render_canvas(
if i >= start_field && i <= end_field { if i >= start_field && i <= end_field {
// This line is within the character-wise highlight range // This line is within the character-wise highlight range
if start_field == end_field { // Case 1: Single Line Highlight if start_field == end_field { // Case 1: Single Line Highlight
let safe_start = start_char.min(text_len); // Use start_char and end_char here
let safe_end = end_char.min(text_len); let clamped_start = start_char.min(text_len);
let before: String = text.chars().take(safe_start).collect(); let clamped_end = end_char.min(text_len); // Use text_len for slicing logic
let highlighted: String = text.chars().skip(safe_start).take(safe_end - safe_start).collect();
let after: String = text.chars().skip(safe_end).collect(); let before: String = text.chars().take(clamped_start).collect();
let highlighted: String = text.chars().skip(clamped_start).take(clamped_end.saturating_sub(clamped_start) + 1).collect();
// Define 'after' here
let after: String = text.chars().skip(clamped_end + 1).collect();
line = Line::from(vec![ line = Line::from(vec![
Span::styled(before, normal_style_in_highlight), Span::styled(before, normal_style_in_highlight),
Span::styled(highlighted, highlight_style), Span::styled(highlighted, highlight_style),
Span::styled(after, normal_style_in_highlight), Span::styled(after, normal_style_in_highlight), // Use defined 'after'
]); ]);
} else if i == start_field { // Case 2: Multi-Line Highlight - Start Line } else if i == start_field { // Case 2: Multi-Line Highlight - Start Line
// Use start_char here
let safe_start = start_char.min(text_len); let safe_start = start_char.min(text_len);
let before: String = text.chars().take(safe_start).collect(); let before: String = text.chars().take(safe_start).collect();
let highlighted: String = text.chars().skip(safe_start).collect(); let highlighted: String = text.chars().skip(safe_start).collect();
@@ -129,28 +136,30 @@ pub fn render_canvas(
Span::styled(before, normal_style_in_highlight), Span::styled(before, normal_style_in_highlight),
Span::styled(highlighted, highlight_style), Span::styled(highlighted, highlight_style),
]); ]);
} else if i == end_field { // Case 4: Multi-Line Highlight - End Line } else if i == end_field { // Case 3: Multi-Line Highlight - End Line (Corrected index)
let safe_end = end_char.min(text_len); // Use end_char here
let highlighted: String = text.chars().take(safe_end).collect(); let safe_end_inclusive = if text_len > 0 { end_char.min(text_len - 1) } else { 0 };
let after: String = text.chars().skip(safe_end).collect(); let highlighted: String = text.chars().take(safe_end_inclusive + 1).collect();
let after: String = text.chars().skip(safe_end_inclusive + 1).collect();
line = Line::from(vec![ line = Line::from(vec![
Span::styled(highlighted, highlight_style), Span::styled(highlighted, highlight_style),
Span::styled(after, normal_style_in_highlight), Span::styled(after, normal_style_in_highlight),
]); ]);
} else { // Case 3: Multi-Line Highlight - Middle Line } else { // Case 4: Multi-Line Highlight - Middle Line (Corrected index)
line = Line::from(Span::styled(text, highlight_style)); // Highlight whole line line = Line::from(Span::styled(text, highlight_style)); // Highlight whole line
} }
} else { // Case 5: Line Outside Character-wise Highlight Range } else { // Case 5: Line Outside Character-wise Highlight Range
line = Line::from(Span::styled( line = Line::from(Span::styled(
text, text,
// Use normal styling (active or inactive)
if is_active { normal_style_in_highlight } else { normal_style_outside } if is_active { normal_style_in_highlight } else { normal_style_outside }
)); ));
} }
} }
HighlightState::Linewise { anchor_line } => { HighlightState::Linewise { anchor_line } => {
// --- Linewise Highlight Logic --- // --- Linewise Highlight Logic ---
let start_field = min(*anchor_line, *current_field_idx); // Dereference anchor_line let start_field = min(*anchor_line, *current_field_idx);
let end_field = max(*anchor_line, *current_field_idx); // Dereference anchor_line let end_field = max(*anchor_line, *current_field_idx);
let highlight_style = Style::default().fg(theme.highlight).bg(theme.highlight_bg).add_modifier(Modifier::BOLD); let highlight_style = Style::default().fg(theme.highlight).bg(theme.highlight_bg).add_modifier(Modifier::BOLD);
let normal_style_in_highlight = Style::default().fg(theme.highlight); let normal_style_in_highlight = Style::default().fg(theme.highlight);
let normal_style_outside = Style::default().fg(theme.fg); let normal_style_outside = Style::default().fg(theme.fg);