Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft wrap #2184

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions helix-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@ use crate::{
pub struct Position {
pub row: usize,
pub col: usize,
pub char_idx: usize,
}

impl Position {
pub const fn new(row: usize, col: usize) -> Self {
Self { row, col }
pub const fn new(row: usize, col: usize, char_idx: usize) -> Self {
Self { row, col, char_idx }
}

pub const fn is_zero(self) -> bool {
self.row == 0 && self.col == 0
self.char_idx == 0
}

// TODO: generalize
pub fn traverse(self, text: &crate::Tendril) -> Self {
let Self { mut row, mut col } = self;
let Self { mut row, mut col, mut char_idx } = self;
// TODO: there should be a better way here
let mut chars = text.chars().peekable();
while let Some(ch) = chars.next() {
char_idx += 1;
if char_is_line_ending(ch) && !(ch == '\r' && chars.peek() == Some(&'\n')) {
row += 1;
col = 0;
} else {
col += 1;
}
}
Self { row, col }
Self { row, col, char_idx }
}
}

impl From<(usize, usize)> for Position {
fn from(tuple: (usize, usize)) -> Self {
impl From<(usize, usize, usize)> for Position {
fn from(tuple: (usize, usize, usize)) -> Self {
Self {
row: tuple.0,
col: tuple.1,
char_idx: tuple.2,
}
}
}
Expand All @@ -65,7 +68,7 @@ pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
let pos = ensure_grapheme_boundary_prev(text, pos);
let col = RopeGraphemes::new(text.slice(line_start..pos)).count();

Position::new(line, col)
Position::new(line, col, pos)
}

/// Convert a character index to (line, column) coordinates visually.
Expand All @@ -90,7 +93,7 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po
}
}

Position::new(line, col)
Position::new(line, col, pos)
}

/// Convert (line, column) coordinates to a character index.
Expand All @@ -113,7 +116,7 @@ pub fn visual_coords_at_pos(text: RopeSlice, pos: usize, tab_width: usize) -> Po
/// TODO: this should be changed to work in terms of visual row/column, not
/// graphemes.
pub fn pos_at_coords(text: RopeSlice, coords: Position, limit_before_line_ending: bool) -> usize {
let Position { mut row, col } = coords;
let Position { mut row, col, char_idx } = coords;
if limit_before_line_ending {
row = row.min(text.len_lines() - 1);
};
Expand Down Expand Up @@ -143,7 +146,7 @@ mod test {
#[test]
fn test_ordering() {
// (0, 5) is less than (1, 0)
assert!(Position::new(0, 5) < Position::new(1, 0));
assert!(Position::new(0, 5, 5) < Position::new(1, 0, 1));
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions helix-core/src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use ropey::RopeSlice;
use smartstring::{LazyCompact, SmartString};

/// Given a slice of text, return the text re-wrapped to fit it
/// within the given width.
pub fn reflow_hard_wrap(text: &str, max_line_len: usize) -> SmartString<LazyCompact> {
textwrap::refill(text, max_line_len).into()
}

pub struct RopeSoftWrap<'a> {
text: RopeSlice<'a>,
}
159 changes: 105 additions & 54 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,69 +436,120 @@ impl EditorView {

use helix_core::graphemes::{grapheme_width, RopeGraphemes};

for grapheme in RopeGraphemes::new(text) {
let out_of_bounds = visual_x < offset.col as u16
|| visual_x >= viewport.width + offset.col as u16;

if LineEnding::from_rope_slice(&grapheme).is_some() {
if !out_of_bounds {
// we still want to render an empty cell with the style
surface.set_string(
viewport.x + visual_x - offset.col as u16,
viewport.y + line,
&newline,
style.patch(whitespace_style),
);
}

visual_x = 0;
let mut iter = RopeGraphemes::new(text);
let mut next_grapheme = iter.next();
while let Some(grapheme) = next_grapheme {
let grapheme = Cow::from(grapheme);
let (grapheme, width) = if grapheme == "\t" {
// make sure we display tab as appropriate amount of spaces
let visual_tab_width = tab_width - (visual_x as usize % tab_width);
(&tab[..visual_tab_width], visual_tab_width)
} else {
// Cow will prevent allocations if span contained in a single slice
// which should really be the majority case
let width = grapheme_width(&grapheme);
(grapheme.as_ref(), width)
};
let old_visual_x = visual_x;
visual_x = visual_x.saturating_add(width as u16);

let out_of_bounds = old_visual_x < offset.col as u16;
let skip = visual_x >= viewport.width + offset.col as u16;

if skip {
visual_x = offset.col as u16;
line += 1;

// TODO: with proper iter this shouldn't be necessary
if line >= viewport.height {
break 'outer;
}
} else {
let grapheme = Cow::from(grapheme);
let is_whitespace;

let (grapheme, width) = if grapheme == "\t" {
is_whitespace = true;
// make sure we display tab as appropriate amount of spaces
let visual_tab_width = tab_width - (visual_x as usize % tab_width);
let grapheme_tab_width =
ropey::str_utils::char_to_byte_idx(&tab, visual_tab_width);

(&tab[..grapheme_tab_width], visual_tab_width)
} else if grapheme == " " {
is_whitespace = true;
(space, 1)
} else if grapheme == "\u{00A0}" {
is_whitespace = true;
(nbsp, 1)
} else {
is_whitespace = false;
// Cow will prevent allocations if span contained in a single slice
// which should really be the majority case
let width = grapheme_width(&grapheme);
(grapheme.as_ref(), width)
};

if !out_of_bounds {
next_grapheme = iter.next();

if LineEnding::from_str(&grapheme).is_some() {
if !out_of_bounds {
// we still want to render an empty cell with the style
surface.set_string(
viewport.x + old_visual_x - offset.col as u16,
viewport.y + line,
" ",
style,
);
}
visual_x = 0;
line += 1;
} else if !out_of_bounds {
// if we're offscreen just keep going until we hit a new line
surface.set_string(
viewport.x + visual_x - offset.col as u16,
viewport.x + old_visual_x - offset.col as u16,
viewport.y + line,
grapheme,
if is_whitespace {
style.patch(whitespace_style)
} else {
style
},
style,
);
}
}

for grapheme in RopeGraphemes::new(text) {
let out_of_bounds = visual_x < offset.col as u16
|| visual_x >= viewport.width + offset.col as u16;

if LineEnding::from_rope_slice(&grapheme).is_some() {
if !out_of_bounds {
// we still want to render an empty cell with the style
surface.set_string(
viewport.x + visual_x - offset.col as u16,
viewport.y + line,
&newline,
style.patch(whitespace_style),
);
}

visual_x = visual_x.saturating_add(width as u16);
visual_x = 0;
line += 1;

// TODO: with proper iter this shouldn't be necessary
if line >= viewport.height {
break 'outer;
}
} else {
let grapheme = Cow::from(grapheme);
let is_whitespace;

let (grapheme, width) = if grapheme == "\t" {
is_whitespace = true;
// make sure we display tab as appropriate amount of spaces
let visual_tab_width =
tab_width - (visual_x as usize % tab_width);
let grapheme_tab_width =
ropey::str_utils::char_to_byte_idx(&tab, visual_tab_width);

(&tab[..grapheme_tab_width], visual_tab_width)
} else if grapheme == " " {
is_whitespace = true;
(space, 1)
} else if grapheme == "\u{00A0}" {
is_whitespace = true;
(nbsp, 1)
} else {
is_whitespace = false;
// Cow will prevent allocations if span contained in a single slice
// which should really be the majority case
let width = grapheme_width(&grapheme);
(grapheme.as_ref(), width)
};

if !out_of_bounds {
// if we're offscreen just keep going until we hit a new line
surface.set_string(
viewport.x + visual_x - offset.col as u16,
viewport.y + line,
grapheme,
if is_whitespace {
style.patch(whitespace_style)
} else {
style
},
);
}

visual_x = visual_x.saturating_add(width as u16);
}
}
}
}
Expand Down