Skip to content

Visual Studio Code, rebuilt in Rust

Rust
37.1 KB
Raw Blame
//! Word wrap (`editor.wordWrap`): monospaceLineBreaksComputer.ts breaks each model line into view lines, and
//! viewModelLines.ts `ViewModelLinesFromProjectedModel` maps model positions onto the wrapped and folded view.

use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::Range;
use std::rc::Rc;

use super::cursor::{self, Position, Selection};
use super::injected_text::{InjectedText, LineLayout, WrapWidth};
use super::{OpenEditor, folding, minimap::is_full_width_character};

/// The `wordWrapBreakBeforeCharacters` and `wordWrapBreakAfterCharacters` defaults (editorOptions.ts); they have no
/// settings schema, so no setting changes them.
pub const BREAK_BEFORE: &str = "([{\u{2018}\u{201C}\u{3008}\u{300A}\u{300C}\u{300E}\u{3010}\u{3014}\u{FF08}\u{FF3B}\u{FF5B}\u{FF62}\u{A3}\u{A5}\u{FF04}\u{FFE1}\u{FFE5}+\u{FF0B}";
pub const BREAK_AFTER: &str = " \t})]?|/&.,;\u{A2}\u{B0}\u{2032}\u{2033}\u{2030}\u{2103}\u{3001}\u{3002}\u{FF61}\u{FF64}\u{FFE0}\u{FF0C}\u{FF0E}\u{FF1A}\u{FF1B}\u{FF1F}\u{FF01}\u{FF05}\u{30FB}\u{FF65}\u{309D}\u{309E}\u{30FD}\u{30FE}\u{30FC}\u{30A1}\u{30A3}\u{30A5}\u{30A7}\u{30A9}\u{30C3}\u{30E3}\u{30E5}\u{30E7}\u{30EE}\u{30F5}\u{30F6}\u{3041}\u{3043}\u{3045}\u{3047}\u{3049}\u{3063}\u{3083}\u{3085}\u{3087}\u{308E}\u{3095}\u{3096}\u{31F0}\u{31F1}\u{31F2}\u{31F3}\u{31F4}\u{31F5}\u{31F6}\u{31F7}\u{31F8}\u{31F9}\u{31FA}\u{31FB}\u{31FC}\u{31FD}\u{31FE}\u{31FF}\u{3005}\u{303B}\u{FF67}\u{FF68}\u{FF69}\u{FF6A}\u{FF6B}\u{FF6C}\u{FF6D}\u{FF6E}\u{FF6F}\u{FF70}\u{201D}\u{3009}\u{300B}\u{300D}\u{300F}\u{3011}\u{3015}\u{FF09}\u{FF3D}\u{FF5D}\u{FF63}";

/// `WrappingIndent`: how far the continuation lines of a wrapped line are indented.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WrappingIndent {
    None,
    Same,
    Indent,
    DeepIndent,
}

impl WrappingIndent {
    /// `editor.wrappingIndent`, `same` by default.
    pub fn from_setting(value: &str) -> Self {
        match value {
            "none" => WrappingIndent::None,
            "indent" => WrappingIndent::Indent,
            "deepIndent" => WrappingIndent::DeepIndent,
            _ => WrappingIndent::Same,
        }
    }
}

/// What the line breaks depend on (`setWrappingSettings` and the factory's break characters).
#[derive(Clone, Debug, PartialEq)]
pub struct WrapOptions {
    /// `wrappingInfo.wrappingColumn`, at least 1.
    pub column: usize,
    /// `isViewportWrapping`: `on` or `bounded`, where the content width follows the viewport.
    pub viewport_wrapping: bool,
    pub tab_size: usize,
    pub indent: WrappingIndent,
    /// `editor.wordBreak` `keepAll`.
    pub keep_all: bool,
    pub wrap_on_escaped_line_feeds: bool,
    /// `typicalFullwidthCharacterWidth / typicalHalfwidthCharacterWidth` of the font.
    pub columns_for_full_width_char: f64,
    /// `effectiveFullwidthCharacterWidth` `twoCells`.
    pub two_cell_full_width: bool,
    pub break_before: String,
    pub break_after: String,
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum CharClass {
    None,
    BreakBefore,
    BreakAfter,
    BreakIdeographic,
}

/// `WrappingCharacterClassifier`.
#[derive(Debug)]
pub struct Classifier {
    classes: HashMap<char, CharClass>,
}

impl Classifier {
    pub fn new(break_before: &str, break_after: &str) -> Self {
        let mut classes = HashMap::new();
        for c in break_before.chars() {
            classes.insert(c, CharClass::BreakBefore);
        }
        for c in break_after.chars() {
            classes.insert(c, CharClass::BreakAfter);
        }
        Classifier { classes }
    }

    fn get(&self, c: char) -> CharClass {
        let code = c as u32;
        // Hiragana and Katakana, CJK Unified Ideographs Extension A and CJK Unified Ideographs.
        if matches!(code, 0x3040..=0x30FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF) {
            return CharClass::BreakIdeographic;
        }
        self.classes.get(&c).copied().unwrap_or(CharClass::None)
    }
}

/// `ModelLineProjectionData`: where each view line ends (the last one at the line's length) as a char column and the
/// drawn width of the injected text at that column still on the view line, their visible columns with the injected
/// text before them, and the indent of every view line after the first. `hint_splits` are the breaks inside a padded
/// inlay hint label part, as (the injection's index on the line, the label char after the break): each piece draws
/// the part's padding, so the hint is wider by `WrapWidth::hint_split_padding` per split.
#[derive(Clone, Debug, PartialEq)]
pub struct LineBreaks {
    pub offsets: Vec<usize>,
    pub injected: Vec<f32>,
    pub visible_columns: Vec<f64>,
    pub wrapped_indent: usize,
    pub hint_splits: Vec<(usize, usize)>,
}

impl LineBreaks {
    /// `translateToOutputPosition` with `PositionAffinity.None`: a column sits before the injected text at it, so a
    /// break before that text starts the next view line with it and a break inside or after it leaves it before.
    pub fn segment_of(&self, column: usize) -> usize {
        let breaks = self.offsets.iter().zip(&self.injected);
        breaks.take_while(|&(&offset, &injected)| offset < column || (offset == column && injected == 0.)).count().min(self.offsets.len() - 1)
    }

    /// Where view line `segment` starts: its char column and the drawn width of the injected text there before it.
    pub fn segment_start(&self, segment: usize) -> (usize, f32) {
        if segment == 0 { (0, 0.) } else { (self.offsets[segment - 1], self.injected[segment - 1]) }
    }
}

/// One char of a line with its injected text applied (`applyInjectedText`), or a width-only piece of injected text
/// (`c` none: inlay hint whitespace, a `widthInEm` box, which `fixed` gives the chars of for the tab stops).
struct Unit<'a> {
    column: usize,
    c: Option<char>,
    fixed: &'a str,
    /// The width wrapping counts when not the char's own column width.
    wrap_width: Option<f64>,
    /// The drawn width of an injected unit: its own, or its share of the injection's text width.
    drawn: Option<f64>,
    /// The drawn width of the injected units at `column` before this one.
    injected_before: f64,
    /// A hint's label char after its first: the injection's index on the line, the char's index, and the padding a
    /// wrap before it adds to the piece before and the piece after (`WrapWidth::hint_split_padding`).
    hint_char: Option<(usize, usize, f64, f64)>,
}

/// `text` with `injections` (by column) applied, as units. An injection's chars share its text width evenly, as a
/// monospace font draws them at any size.
fn line_units<'a>(text: &str, injections: &'a [InjectedText]) -> Vec<Unit<'a>> {
    let mut units = Vec::new();
    let mut pending = injections.iter().enumerate().peekable();
    for (column, c) in text.chars().map(Some).chain([None]).enumerate() {
        let mut injected_before = 0.;
        let mut push = |units: &mut Vec<Unit<'a>>, c, fixed, wrap_width, drawn: f64| {
            units.push(Unit { column, c, fixed, wrap_width, drawn: Some(drawn), injected_before, hint_char: None });
            injected_before += drawn;
        };
        while let Some((injection_index, injection)) = pending.next_if(|(_, injection)| injection.position.column == column) {
            let (columns, chars) = (injection.columns as f64, injection.content.chars().count());
            match injection.wrap {
                WrapWidth::Fixed => push(&mut units, None, injection.content.as_str(), Some(columns), columns),
                WrapWidth::Chars if chars == 0 => push(&mut units, None, "", Some(0.), columns),
                WrapWidth::Chars => injection.content.chars().for_each(|c| push(&mut units, Some(c), "", None, columns / chars as f64)),
                WrapWidth::Hint { left, right, padding, .. } => {
                    let (left, right, padding) = (left as f64, right as f64, padding as f64);
                    if left > 0. {
                        push(&mut units, None, "", Some(left), left);
                    }
                    // The padding box draws with the first label char and the last, the wrapping skips it.
                    let share = (columns - left - right - 2. * padding) / chars.max(1) as f64;
                    for (index, c) in injection.content.chars().enumerate() {
                        let first = if index == 0 { padding } else { 0. };
                        let last = if index + 1 == chars { padding } else { 0. };
                        push(&mut units, Some(c), "", None, first + share + last);
                        let (split_right, split_left) = injection.wrap.hint_split_padding(index);
                        if index > 0 && split_right + split_left > 0. {
                            units.last_mut().expect("pushed above").hint_char = Some((injection_index, index, split_right as f64, split_left as f64));
                        }
                    }
                    if right > 0. {
                        push(&mut units, None, "", Some(right), right);
                    }
                }
            }
        }
        if c.is_some() {
            units.push(Unit { column, c, fixed: "", wrap_width: None, drawn: None, injected_before, hint_char: None });
        }
    }
    units
}

/// `createLineBreaks` of monospaceLineBreaksComputer.ts over the line with its injected text applied. Upstream
/// measures in pixels because injected text can ask for any width; without injected text a char's pixel width is
/// its column width times `typicalHalfwidthCharacterWidth`, so this measures in columns. Offsets count chars, where
/// upstream counts UTF-16 units: a char past U+FFFF is one char here and two units there, and both never split it.
/// `injections` are the line's injected text, by column.
pub fn line_breaks(classifier: &Classifier, text: &str, options: &WrapOptions, injections: &[InjectedText]) -> Option<LineBreaks> {
    let units = line_units(text, injections);
    if units.len() <= 1 {
        return None;
    }
    let tab_size = options.tab_size as f64;
    let full_width = if options.two_cell_full_width { 2. } else { options.columns_for_full_width_char };
    let char_width = |c: char, visible: f64| -> f64 {
        if c == '\t' {
            tab_size - visible % tab_size
        } else if (c as u32) > 0xFFFF {
            2.
        } else if is_full_width_character(c as u32) {
            full_width
        } else if (c as u32) < 32 {
            options.columns_for_full_width_char
        } else {
            1.
        }
    };
    // A width-only injection is `CharCode.Null` of class NONE.
    let class_of = |c: Option<char>| match c {
        Some(c) if (c as u32) <= 0xFFFF => classifier.get(c),
        _ => CharClass::None,
    };
    let chars: Vec<char> = units.iter().map(|unit| unit.c.unwrap_or('\0')).collect();
    let model_chars: Vec<char> = text.chars().collect();
    let first_break_column = options.column as f64;
    let wrapped_indent = wrapped_indent_length(&model_chars, options.tab_size, options.column, full_width, options.indent);
    let wrapped_line_break_width = first_break_column - wrapped_indent as f64;

    let mut breaks = Vec::new();
    let mut visible_columns = Vec::new();
    let mut break_offset = 0;
    let mut break_visible = 0.;
    let mut breaking_width = first_break_column;
    // The tab stops count the injected chars (`applyInjectedText`), the wrapping their drawn width.
    let mut tab_visible = 0.;
    let mut visible = 0.;
    let mut prev_class = CharClass::None;
    for (i, unit) in units.iter().enumerate() {
        let class = class_of(unit.c);
        let char_columns = match unit.c {
            Some(c) => char_width(c, tab_visible),
            None => unit.fixed.chars().fold(0., |columns, c| columns + char_width(c, tab_visible + columns)),
        };
        tab_visible += char_columns;
        let width = unit.wrap_width.unwrap_or(char_columns);
        if i == 0 {
            (visible, prev_class) = (width, class);
            continue;
        }
        let mut escaped_line_feed = false;
        if options.wrap_on_escaped_line_feeds && is_escaped_line_break_at(&chars, i) {
            (break_offset, break_visible) = (i, visible);
            escaped_line_feed = true;
        } else if can_break(chars[i], prev_class, class, options.keep_all) {
            (break_offset, break_visible) = (i, visible);
        }
        visible += width;
        if visible > breaking_width || escaped_line_feed {
            // Cannot break at `break_offset`: break at the current char.
            if break_offset == 0 || visible - break_visible > wrapped_line_break_width {
                (break_offset, break_visible) = (i, visible - width);
            }
            if break_offset > breaks.last().copied().unwrap_or(0) {
                breaks.push(break_offset);
                visible_columns.push(break_visible);
                breaking_width = break_visible + wrapped_line_break_width;
                break_offset = 0;
            }
        }
        prev_class = class;
    }
    if breaks.is_empty() {
        return None;
    }
    let splits: Vec<usize> = breaks.iter().copied().filter(|&i| units[i].hint_char.is_some()).collect();
    // The padding the splits at `column` before unit `end` add.
    let split_padding = |end: usize, column: usize| -> f64 {
        splits.iter().filter(|&&i| i < end && units[i].column == column).map(|&i| units[i].hint_char.map_or(0., |(_, _, right, left)| right + left)).sum()
    };
    let mut offsets = Vec::new();
    let mut injected = Vec::new();
    for &i in &breaks {
        let own_right_side = units[i].hint_char.map_or(0., |(_, _, right, _)| right);
        offsets.push(units[i].column);
        injected.push((units[i].injected_before + split_padding(i, units[i].column) + own_right_side) as f32);
    }
    let trailing: f64 = units.iter().filter(|unit| unit.column == model_chars.len()).filter_map(|unit| unit.drawn).sum();
    offsets.push(model_chars.len());
    injected.push((trailing + split_padding(units.len(), model_chars.len())) as f32);
    visible_columns.push(visible);
    let hint_splits = splits.iter().map(|&i| units[i].hint_char.map(|(injection, char, ..)| (injection, char)).expect("a split")).collect();
    Some(LineBreaks { offsets, injected, visible_columns, wrapped_indent, hint_splits })
}

/// The injected texts on `line`, from all of them by position.
fn line_injections(injected: &[InjectedText], line: usize) -> &[InjectedText] {
    let first = injected.partition_point(|injection| injection.position.line < line);
    let count = injected[first..].partition_point(|injection| injection.position.line == line);
    &injected[first..first + count]
}

/// `isEscapedLineBreakAtPosition`: `\n` written with an odd number of backslashes ends right before `i`.
fn is_escaped_line_break_at(chars: &[char], i: usize) -> bool {
    if i < 2 || chars[i - 1] != 'n' {
        return false;
    }
    let backslashes = chars[..i - 1].iter().rev().take_while(|&&c| c == '\\').count();
    backslashes < i - 1 && backslashes % 2 == 1
}

/// `canBreak`, Kinsoku Shori: no break after a leading character like an open bracket, nor before a trailing one
/// like a period.
fn can_break(c: char, prev: CharClass, class: CharClass, keep_all: bool) -> bool {
    c != ' '
        && ((prev == CharClass::BreakAfter && class != CharClass::BreakAfter)
            || (prev != CharClass::BreakBefore && class == CharClass::BreakBefore)
            || (!keep_all && prev == CharClass::BreakIdeographic && class != CharClass::BreakAfter)
            || (!keep_all && class == CharClass::BreakIdeographic && prev != CharClass::BreakBefore))
}

/// `computeWrappedTextIndentLength`: the leading whitespace's width plus the extra tabs, none when not even one
/// full-width char would fit after it.
fn wrapped_indent_length(chars: &[char], tab_size: usize, first_break_column: usize, full_width: f64, indent: WrappingIndent) -> usize {
    let extra_tabs = match indent {
        WrappingIndent::None => return 0,
        WrappingIndent::Same => 0,
        WrappingIndent::Indent => 1,
        WrappingIndent::DeepIndent => 2,
    };
    let Some(first_non_whitespace) = chars.iter().position(|&c| c != ' ' && c != '\t') else { return 0 };
    let mut length = 0;
    for &c in &chars[..first_non_whitespace] {
        length += if c == '\t' { tab_size - length % tab_size } else { 1 };
    }
    for _ in 0..extra_tabs {
        length += tab_size - length % tab_size;
    }
    if length as f64 + full_width > first_break_column as f64 { 0 } else { length }
}

/// textModel.ts `isDominatedByLongLines`: lines of 10000 UTF-16 units or more hold more of them than the others.
fn is_dominated_by_long_lines(lines: &[String]) -> bool {
    let (mut long, mut short) = (0, 0);
    for line in lines {
        let length = line.encode_utf16().count();
        if length >= 10_000 {
            long += length;
        } else {
            short += length;
        }
    }
    long > short
}

/// One editor's wrapping: the options of its last layout, the line breaks of every model line, and the first view
/// line of each model line for the folds it was computed with.
#[derive(Debug, Default)]
pub struct WordWrap {
    options: Option<WrapOptions>,
    classifier: Option<Classifier>,
    breaks: Vec<Option<LineBreaks>>,
    starts: RefCell<Option<(Vec<Range<usize>>, Rc<Vec<usize>>)>>,
    /// Toggle Word Wrap's state for the model (toggleWordWrap.ts `transientWordWrapState`, `wordWrapOverride2`).
    pub toggled: Option<bool>,
    /// The selections a move left with these cursor heads at the end of the view line before their wrap point
    /// (the cursor's view position, which a model position alone puts at the next view line's start).
    row_ends: Option<(Vec<Selection>, Vec<Position>)>,
    /// textModel.ts `isDominatedByLongLines` when the editor got the model (codeEditorWidget.ts `_attachModel`).
    pub dominated_by_long_lines: bool,
}

/// One view line: model line `line` from char `start` to `end`, drawn after `indent` columns. A view line
/// broken inside or after the injected text at a column starts and ends that much drawn width past it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Segment {
    pub line: usize,
    pub start: usize,
    pub end: usize,
    pub start_injected: f32,
    pub end_injected: f32,
    pub indent: usize,
    pub first: bool,
    pub last: bool,
}

impl Segment {
    /// The x in `layout`, the model line's, where the view line starts.
    pub fn start_x(&self, layout: &LineLayout) -> f32 {
        layout.x(self.start) + self.start_injected
    }

    /// The x in `layout` where the view line ends.
    pub fn end_x(&self, layout: &LineLayout) -> f32 {
        layout.x(self.end) + self.end_injected
    }
}

impl WordWrap {
    pub fn new(lines: &[String], too_large_for_tokenization: bool) -> Self {
        WordWrap { dominated_by_long_lines: !too_large_for_tokenization && is_dominated_by_long_lines(lines), ..Default::default() }
    }

    pub fn options(&self) -> Option<&WrapOptions> {
        self.options.as_ref()
    }

    /// `setWrappingSettings`: breaks every line again when the options changed. Returns whether they did.
    pub fn configure(&mut self, options: Option<WrapOptions>, lines: &[String], injected: &[InjectedText]) -> bool {
        if self.options == options && options.as_ref().is_none_or(|_| self.breaks.len() == lines.len()) {
            return false;
        }
        self.breaks.clear();
        if let Some(options) = &options {
            let classifier = match self.classifier.take() {
                Some(classifier) if self.options.as_ref().is_some_and(|old| (&old.break_before, &old.break_after) == (&options.break_before, &options.break_after)) => classifier,
                _ => Classifier::new(&options.break_before, &options.break_after),
            };
            self.breaks = lines.iter().enumerate().map(|(index, line)| line_breaks(&classifier, line, options, &line_injections(injected, index))).collect();
            self.classifier = Some(classifier);
        }
        self.options = options;
        self.starts.take();
        self.row_ends = None;
        true
    }

    /// `onModelLinesDeleted`/`onModelLinesInserted`/`onModelLineChanged`: lines `start..=end` became `inserted` lines,
    /// already in `lines`.
    pub fn lines_replaced(&mut self, lines: &[String], start: usize, end: usize, inserted: usize, injected: &[InjectedText]) {
        let (Some(options), Some(classifier)) = (&self.options, &self.classifier) else { return };
        let new: Vec<_> = (start..start + inserted).map(|index| line_breaks(classifier, &lines[index], options, &line_injections(injected, index))).collect();
        self.breaks.splice(start..=end, new);
        self.starts.take();
    }

    /// Another editor of the shared model changed its lines (`sync_with_model`): every line breaks again.
    pub fn lines_changed_elsewhere(&mut self, lines: &[String], injected: &[InjectedText]) {
        self.breaks.clear();
        self.configure(self.options.clone(), lines, injected);
    }

    /// `onModelDecorationsChanged` with injected text: line `line` breaks again.
    pub fn line_injections_changed(&mut self, lines: &[String], line: usize, injected: &[InjectedText]) {
        let (Some(options), Some(classifier)) = (&self.options, &self.classifier) else { return };
        self.breaks[line] = line_breaks(classifier, &lines[line], options, &line_injections(injected, line));
        self.starts.take();
    }

    fn view_line_count_of(&self, line: usize) -> usize {
        self.breaks[line].as_ref().map_or(1, |breaks| breaks.offsets.len())
    }
}

impl OpenEditor {
    /// Whether the view wraps lines (`wrappingInfo.wrappingColumn` is not -1).
    pub fn wraps(&self) -> bool {
        self.word_wrap.options.is_some()
    }

    /// The first view line of every model line and the view line count last; a hidden line has none.
    pub fn view_starts(&self) -> Rc<Vec<usize>> {
        let hidden = self.folding.hidden();
        if let Some((cached_hidden, starts)) = &*self.word_wrap.starts.borrow()
            && cached_hidden == hidden
        {
            return starts.clone();
        }
        let mut starts = Vec::with_capacity(self.document().lines.len() + 1);
        let mut total = 0;
        for line in 0..self.document().lines.len() {
            starts.push(total);
            if folding::hidden_range(hidden, line).is_none() {
                total += self.word_wrap.view_line_count_of(line);
            }
        }
        starts.push(total);
        let starts = Rc::new(starts);
        *self.word_wrap.starts.borrow_mut() = Some((hidden.to_vec(), starts.clone()));
        starts
    }

    pub(super) fn wrapped_view_line_count(&self) -> usize {
        *self.view_starts().last().expect("the starts end with the count")
    }

    /// The first view line of model line `line`; a hidden line shows on its fold's line.
    pub(super) fn wrapped_view_line(&self, line: usize) -> usize {
        let shown = self.folding.model_line(self.folding.view_line(line));
        self.view_starts()[shown]
    }

    pub(super) fn wrapped_model_line(&self, view_line: usize) -> usize {
        let starts = self.view_starts();
        let line_count = self.document().lines.len();
        let view_line = view_line.min(starts[line_count].saturating_sub(1));
        starts[..line_count].partition_point(|&start| start <= view_line) - 1
    }

    /// The last view line of model line `line`, the first when it does not wrap or is hidden.
    pub fn last_view_line(&self, line: usize) -> usize {
        let view_line = self.view_line(line);
        match self.wraps() && !self.folding.is_hidden(line) {
            true => view_line + self.word_wrap.view_line_count_of(line) - 1,
            false => view_line,
        }
    }

    /// `LineBreaks::hint_splits` of model line `line` while it wraps.
    pub fn hint_splits(&self, line: usize) -> &[(usize, usize)] {
        if !self.wraps() {
            return &[];
        }
        self.word_wrap.breaks[line].as_ref().map_or(&[], |breaks| &breaks.hint_splits)
    }

    /// The model line and chars that view line `view_line` shows.
    pub fn segment(&self, view_line: usize) -> Segment {
        let line = self.model_line(view_line);
        let end = self.document().lines[line].chars().count();
        let breaks = if self.wraps() { self.word_wrap.breaks[line].as_ref() } else { None };
        let Some(breaks) = breaks else {
            let end_injected = self.injected_text_on(line).iter().filter(|injection| injection.position.column == end).map(|injection| injection.columns).sum();
            return Segment { line, start: 0, end, start_injected: 0., end_injected, indent: 0, first: true, last: true };
        };
        let index = view_line - self.view_line(line);
        let first = index == 0;
        let (start, start_injected) = breaks.segment_start(index);
        let (end, end_injected) = (breaks.offsets[index], breaks.injected[index]);
        let indent = if first { 0 } else { breaks.wrapped_indent };
        Segment { line, start, end, start_injected, end_injected, indent, first, last: index + 1 == breaks.offsets.len() }
    }

    /// The view line `position` shows on; `row_end` keeps a position at a wrap point on the view line it ends.
    pub fn position_view_line(&self, position: Position, row_end: bool) -> usize {
        let view_line = self.view_line(position.line);
        if !self.wraps() || self.folding.is_hidden(position.line) {
            return view_line;
        }
        let Some(breaks) = &self.word_wrap.breaks[position.line] else { return view_line };
        let segment = breaks.segment_of(position.column);
        let at_break = segment > 0 && breaks.segment_start(segment) == (position.column, 0.);
        view_line + segment - usize::from(row_end && at_break)
    }

    /// viewModelLines.ts `getViewLinesIndentGuides`: the indent guide count of view line `view_line`. A wrapped line
    /// repeats its guides on its other view lines only when they start after a wrapping indent.
    pub fn view_indent_guides(&self, view_line: usize) -> usize {
        let segment = self.segment(view_line);
        if !segment.first && segment.indent == 0 {
            return 0;
        }
        self.text_model().guides[segment.line]
    }

    /// viewModelLines.ts `getViewLinesBracketGuides`: of model line guides `guides`, the ones view line `view_line`
    /// shows, a horizontal line cut to its chars. Visible columns stay as they are, as upstream.
    pub fn view_bracket_guides(&self, view_line: usize, guides: &[crate::brackets::BracketGuide]) -> Vec<crate::brackets::BracketGuide> {
        use std::cmp::Ordering;
        let segment = self.segment(view_line);
        let view_line_of = |column| self.position_view_line(Position::new(segment.line, column), false);
        let mut result = Vec::new();
        for guide in guides {
            if guide.wrapped_after.is_some_and(|column| view_line_of(column) >= view_line) || guide.wrapped_before_or_at.is_some_and(|column| view_line_of(column) < view_line) {
                continue;
            }
            let Some((top, end)) = guide.horizontal else {
                result.push(guide.clone());
                continue;
            };
            let column = match guide.visible_column.is_none().then(|| view_line_of(guide.column).cmp(&view_line)) {
                Some(Ordering::Less) => segment.start,
                Some(Ordering::Greater) => continue,
                _ => guide.column,
            };
            let end = match view_line_of(end).cmp(&view_line) {
                Ordering::Equal => end,
                Ordering::Less => continue,
                // Upstream does not repeat a horizontal line at a visible column on the line's other view lines.
                Ordering::Greater if guide.visible_column.is_some() => continue,
                Ordering::Greater => segment.end,
            };
            result.push(crate::brackets::BracketGuide { column, horizontal: Some((top, end)), wrapped_after: None, wrapped_before_or_at: None, ..guide.clone() });
        }
        result
    }

    /// Whether cursor `index`'s head shows at the end of the view line before its wrap point.
    pub fn head_at_row_end(&self, index: usize) -> bool {
        let Some((selections, heads)) = &self.word_wrap.row_ends else { return false };
        *selections == self.selections && heads.contains(&self.selections[index].head)
    }

    /// The view line cursor `index`'s head shows on.
    pub fn head_view_line(&self, index: usize) -> usize {
        self.position_view_line(self.selections[index].head, self.head_at_row_end(index))
    }

    /// The x of column `column` of model line `line` within view line `segment`, in columns from the text's left;
    /// the injected text at the segment's start is on it.
    pub fn segment_visible_column(&self, segment: Segment, column: usize) -> f32 {
        let layout = self.line_layout(segment.line);
        segment.indent as f32 + layout.x(column) - segment.start_x(&layout)
    }

    /// viewLine.ts `_readPixelOffset`: a content widget's anchor sits at `Math.round(spaceWidth * visible column)` on a
    /// basic ASCII line, at the fractional offset the text is drawn at on any other.
    pub fn segment_pixel_left(&self, segment: Segment, column: usize, char_width: f32) -> f32 {
        let left = self.segment_visible_column(segment, column) * char_width;
        match self.document().lines[segment.line].chars().all(super::unicode_highlighter::is_basic_ascii) {
            true => left.round(),
            false => left,
        }
    }

    /// The column at x `visible` of view line `view_line`, kept inside it, and whether it is the view line's end
    /// before a wrap point.
    pub fn segment_column(&self, view_line: usize, visible: f32) -> (Position, bool) {
        let segment = self.segment(view_line);
        let layout = self.line_layout(segment.line);
        let model_x = (visible - segment.indent as f32).max(0.) + segment.start_x(&layout);
        let column = layout.column_at(model_x).clamp(segment.start, segment.end);
        (Position::new(segment.line, column), !segment.last && column == segment.end)
    }

    /// `MoveOperations.vertical` over view lines: keeps the visible column of the view line, plus the leftover,
    /// and goes to the document's edge when there is no view line to go to.
    fn vertical_in_view(&self, position: Position, row_end: bool, leftover: isize, down: bool) -> (Position, isize, bool) {
        let view_line = self.position_view_line(position, row_end);
        let segment = self.segment(view_line);
        let visible = self.segment_visible_column(segment, position.column).round() as isize + leftover;
        let last = self.last_shown_position();
        let was_at_edge = if down { position == last } else { position == Position::default() };
        let (moved, moved_end) = match (down, view_line) {
            (true, line) if line + 1 >= self.view_line_count() => (last, false),
            (false, 0) => (Position::default(), false),
            _ => self.segment_column(if down { view_line + 1 } else { view_line - 1 }, visible.max(0) as f32),
        };
        let moved_view_line = self.position_view_line(moved, moved_end);
        let moved_visible = self.segment_visible_column(self.segment(moved_view_line), moved.column).round() as isize;
        (moved, if was_at_edge { 0 } else { visible - moved_visible }, moved_end)
    }

    /// `cursorUp`/`cursorDown` with wrapped lines.
    pub(super) fn move_vertically_in_view(&mut self, down: bool, select: bool) {
        let mut selections = Vec::new();
        let mut leftovers = Vec::new();
        let mut row_ends = Vec::new();
        for (index, &selection) in self.selections.iter().enumerate() {
            let (anchor_leftover, head_leftover) = self.leftover(index);
            let (from, from_end) = match (select || selection.is_empty(), down) {
                (true, _) => (selection.head, self.head_at_row_end(index)),
                (false, false) => (selection.start(), false),
                (false, true) => (selection.end(), false),
            };
            let (head, leftover, row_end) = self.vertical_in_view(from, from_end, head_leftover, down);
            if row_end {
                row_ends.push(head);
            }
            selections.push(selection.moved(head, select));
            leftovers.push((if select { anchor_leftover } else { leftover }, leftover));
        }
        self.set_cursor_states(selections, leftovers);
        let heads: Vec<_> = self.selections.iter().map(|selection| (selection.head, if down && !row_ends.contains(&selection.head) { usize::MAX } else { 0 })).collect();
        self.set_heads_past_injections(heads);
        self.set_row_ends(row_ends);
    }

    /// `cursorHome`/`cursorEnd` with wrapped lines (`CursorMoveCommands._moveToLineStart`/`_moveToLineEnd`): first
    /// to the view line's start or end, then, already there or on the model line's first or last view line, the
    /// model line's.
    pub(super) fn move_to_line_edge_in_view(&mut self, end: bool, select: bool) {
        let mut selections = Vec::new();
        let mut row_ends = Vec::new();
        for index in 0..self.selections.len() {
            let selection = self.selections[index];
            let head = selection.head;
            let segment = self.segment(self.head_view_line(index));
            let text = &self.document().lines[head.line];
            let moved = if end {
                let at_row_end = self.head_at_row_end(index) || (segment.last && head.column == segment.end);
                match at_row_end || segment.last {
                    true => cursor::line_end(&self.document().lines, head),
                    false => {
                        row_ends.push(Position::new(head.line, segment.end));
                        Position::new(head.line, segment.end)
                    }
                }
            } else {
                let first_non_blank = text.chars().skip(segment.start).take(segment.end - segment.start).position(|c| c != ' ' && c != '\t').map_or(segment.end, |offset| segment.start + offset);
                match segment.first || head.column == first_non_blank {
                    true => cursor::line_start(&self.document().lines, head),
                    false => Position::new(head.line, segment.start),
                }
            };
            selections.push(selection.moved(moved, select));
        }
        self.set_cursor_states(selections, Vec::new());
        // The model line's end is after its injections there, a row end before the ones the next view line starts with.
        let heads: Vec<_> = self.selections.iter().map(|selection| (selection.head, if end && !row_ends.contains(&selection.head) { usize::MAX } else { 0 })).collect();
        self.set_heads_past_injections(heads);
        self.set_row_ends(row_ends);
    }

    /// Keeps `heads` at the end of their view lines while the selections stay as they are now.
    pub fn set_row_ends(&mut self, heads: Vec<Position>) {
        self.word_wrap.row_ends = (!heads.is_empty()).then(|| (self.selections.clone(), heads));
    }

    /// `getViewLineContent`: the wrapped indent's spaces, then the segment's text.
    pub fn view_line_text(&self, view_line: usize) -> Cow<'_, str> {
        let segment = self.segment(view_line);
        let document = self.document();
        let text = &document.lines[segment.line];
        if segment.first && segment.last {
            return Cow::Owned(text.clone());
        }
        Cow::Owned(" ".repeat(segment.indent) + &text.chars().skip(segment.start).take(segment.end - segment.start).collect::<String>())
    }

    /// `view_line_text` with its minimap token runs (byte ends and colors), cut from the model line's `runs`.
    pub fn view_line_minimap_data(&self, view_line: usize, runs: Vec<(usize, [u8; 3])>) -> (Cow<'_, str>, Vec<(usize, [u8; 3])>) {
        let segment = self.segment(view_line);
        let text = self.view_line_text(view_line);
        if segment.first && segment.last {
            return (text, runs);
        }
        let line = &self.document().lines[segment.line];
        let byte = |column: usize| line.char_indices().nth(column).map_or(line.len(), |(byte, _)| byte);
        let (start, end) = (byte(segment.start), byte(segment.end));
        let mut cut = Vec::new();
        if segment.indent > 0 && let Some(&(_, color)) = runs.first() {
            cut.push((segment.indent, color));
        }
        for (run_end, color) in runs.into_iter().filter(|(run_end, _)| *run_end > start) {
            cut.push((run_end.min(end) - start + segment.indent, color));
            if run_end >= end {
                break;
            }
        }
        (text, cut)
    }

    /// `convertModelPositionToViewPosition`: the view line and its column (the wrapped indent counted as chars).
    pub fn view_column(&self, position: Position) -> (usize, usize) {
        let view_line = self.position_view_line(position, false);
        let segment = self.segment(view_line);
        (view_line, segment.indent + position.column.clamp(segment.start, segment.end) - segment.start)
    }

    /// The line breaks' widest view line in columns, the indent included, or `None` without wrapping.
    pub fn wrapped_line_width(&self, view_line: usize) -> Option<f32> {
        if !self.wraps() {
            return None;
        }
        let segment = self.segment(view_line);
        let layout = self.line_layout(segment.line);
        Some(segment.end_x(&layout) - segment.start_x(&layout) + segment.indent as f32)
    }
}

#[cfg(test)]
mod tests;