fn advance_caret(&self, mut k: Caret, dir: Dir) -> Caret {
        let line = &self.lines[k.row];

        match dir {
            Dir::Right => {
                if let Some(c) = line.data[k.offset..].chars().next() {
                    k.advance(c, true);
                } else if k.row + 1 < self.lines.len() {
                    k.row += 1;
                    k.col = 0;
                    k.offset = 0;
                }
            }
            Dir::Left => {
                if let Some(c) = line.data[..k.offset].chars().next_back() {
                    k.advance(c, false);
                } else if k.row > 0 {
                    k.row -= 1;
                    k.col = self.lines[k.row].columns;
                    k.offset = self.lines[k.row].data.len();
                }
            }
            Dir::Down => {
                if k.row + 1 < self.lines.len() {
                    k.row += 1;
                } else {
                    k.col = usize::MAX;
                }
            }
            Dir::Up => {
                if k.row > 0 {
                    k.row -= 1;
                } else {
                    k.col = 0;
                }
            }
        }

        match dir {
            Dir::Down | Dir::Up => {
                let line = &self.lines[k.row];
                if k.col > line.columns {
                    // Move to the end of the line.
                    k.col = line.columns;
                    k.offset = line.data.len();
                } else {
                    // Find the caret position at the same column.
                    let target = k.col;
                    k.col = 0;
                    k.offset = 0;
                    for c in line.data.chars() {
                        if k.col >= target {
                            break;
                        }
                        k.advance(c, true);
                    }
                }
            }
            _ => {}
        }

        k
    }
/// Move a caret forwards or backwards, wrapping at line ends.
Move a caret forwards or backwards, wrapping at line ends.
Move a caret forwards or backwards wrapping at line ends .
fn advance_caret(&self, mut k: Caret, dir: Dir) -> Caret {
        let line = &self.lines[k.row];
        match dir {
            Dir::Right => {
                if let Some(c) = line.data[k.offset..].chars().next() {
                    k.advance(c, true);
                } else if k.row + 1 < self.lines.len() {
                    k.row += 1;
                    k.col = 0;
                    k.offset = 0;
                }
            }
            Dir::Left => {
                if let Some(c) = line.data[..k.offset].chars().next_back() {
                    k.advance(c, false);
                } else if k.row > 0 {
                    k.row -= 1;
                    k.col = self.lines[k.row].columns;
                    k.offset = self.lines[k.row].data.len();
                }
            }
            Dir::Down => {
                if k.row + 1 < self.lines.len() {
                    k.row += 1;
                } else {
                    k.col = usize::MAX;
                }
            }
            Dir::Up => {
                if k.row > 0 {
                    k.row -= 1;
                } else {
                    k.col = 0;
                }
            }
        }
        match dir {
            Dir::Down | Dir::Up => {
                let line = &self.lines[k.row];
                if k.col > line.columns {
                    // Move to the end of the line.
                    k.col = line.columns;
                    k.offset = line.data.len();
                } else {
                    // Find the caret position at the same column.
                    let target = k.col;
                    k.col = 0;
                    k.offset = 0;
                    for c in line.data.chars() {
                        if k.col >= target {
                            break;
                        }
                        k.advance(c, true);
                    }
                }
            }
            _ => {}
        }
        k
    }
fn advance_caret ( & self , mut k : Caret , dir : Dir ) -> Caret { let line = & self . lines [ k . row ] ; match dir { Dir :: Right => { if let Some ( c ) = line . data [ k . offset .. ] . chars ( ) . next ( ) { k . advance ( c , true ) ; } else if k . row + 1 < self . lines . len ( ) { k . row += 1 ; k . col = 0 ; k . offset = 0 ; } } Dir :: Left => { if let Some ( c ) = line . data [ .. k . offset ] . chars ( ) . next_back ( ) { k . advance ( c , false ) ; } else if k . row > 0 { k . row -= 1 ; k . col = self . lines [ k . row ] . columns ; k . offset = self . lines [ k . row ] . data . len ( ) ; } } Dir :: Down => { if k . row + 1 < self . lines . len ( ) { k . row += 1 ; } else { k . col = usize :: MAX ; } } Dir :: Up => { if k . row > 0 { k . row -= 1 ; } else { k . col = 0 ; } } } match dir { Dir :: Down | Dir :: Up => { let line = & self . lines [ k . row ] ; if k . col > line . columns { // Move to the end of the line. k . col = line . columns ; k . offset = line . data . len ( ) ; } else { // Find the caret position at the same column. let target = k . col ; k . col = 0 ; k . offset = 0 ; for c in line . data . chars ( ) { if k . col >= target { break ; } k . advance ( c , true ) ; } } } _ => { } } k }
Move a caret forwards or backwards, wrapping at line ends.
Move a caret forwards or backwards wrapping at line ends .
&self
k
Caret
dir
Dir
&self
k
Caret
dir
Dir