on master help sign in
Replace `in_bounds` with calculation of end_indent

Instead of repeatedly checking if it is in_bounds, calculate the max_indent beforehand and just loop. I added a debug_assert to "prove" that it never tries drawing out of bounds.

commit8bbddf90ffda7c37f8a87f71227a185007ddfae0 authorA-Walrus <ofek.r.guy@gmail.com> dateMon 17 Oct 2022, 14:23 (4 years ago) parent1de02a1
1 file changed+11−5
helix-term/src/ui/editor.rs+11−5
helix-term/src/ui/editor.rs+11−5
@@ -24,7 +24,7 @@ use helix_view::{
2424 keyboard::{KeyCode, KeyModifiers},
2525 Document, Editor, Theme, View,
2626};
27−use std::{borrow::Cow, path::PathBuf};
27+use std::{borrow::Cow, cmp::min, path::PathBuf};
2828
2929use tui::buffer::Buffer as Surface;
3030
@@ -468,12 +468,18 @@ impl EditorView {
468468 let starting_indent =
469469 (offset.col / tab_width) + config.indent_guides.skip_levels as usize;
470470
471− for i in starting_indent..(indent_level / tab_width) {
471+ // Don't draw indent guides outside of view
472+ let end_indent = min(
473+ indent_level,
474+ // Add tab_width - 1 to round up, since the first visible
475+ // indent might be a bit after offset.col
476+ offset.col + viewport.width as usize + (tab_width - 1),
477+ ) / tab_width;
478+
479+ for i in starting_indent..end_indent {
472480 let x = (viewport.x as usize + (i * tab_width) - offset.col) as u16;
473481 let y = viewport.y + line;
474− if !surface.in_bounds(x, y) {
475− break;
476− }
482+ debug_assert!(surface.in_bounds(x, y));
477483 surface.set_string(x, y, &indent_guide_char, indent_guide_style);
478484 }
479485 };