Skip to content

Commit

Permalink
use 'char' type for whitespace substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Mar 15, 2022
1 parent 1010ef2 commit 954d423
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
13 changes: 10 additions & 3 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl EditorView {
let mut line = 0u16;
let tab_width = doc.tab_width();
let tab = if whitespace.render.tab() == WhitespaceRenderValue::All {
(1..tab_width).fold(whitespace.substitutions.tab.clone(), |s, _| s + " ")
(1..tab_width).fold(whitespace.substitutions.tab.to_string(), |s, _| s + " ")
} else {
" ".repeat(tab_width)
};
Expand Down Expand Up @@ -376,12 +376,16 @@ impl EditorView {
acc.patch(theme.highlight(span.0))
});

let char_buf = &mut [0; 4];

// 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,
if whitespace.render.newline() == WhitespaceRenderValue::All {
whitespace.substitutions.newline.as_ref()
let visible_newline: &str =
whitespace.substitutions.newline.encode_utf8(char_buf);
visible_newline
} else {
" "
},
Expand All @@ -398,6 +402,7 @@ impl EditorView {
}
} else {
let grapheme = Cow::from(grapheme);
let char_buf = &mut [0; 4];
let is_whitespace;

let (grapheme, width) = if grapheme == "\t" {
Expand All @@ -414,7 +419,9 @@ impl EditorView {
} else if grapheme == " " && should_render_space && !is_trailing_cursor
{
is_whitespace = true;
(whitespace.substitutions.space.as_ref(), 1)
let visible_space: &str =
whitespace.substitutions.space.encode_utf8(char_buf);
(visible_space, 1)
} else {
is_whitespace = false;
// Cow will prevent allocations if span contained in a single slice
Expand Down
30 changes: 6 additions & 24 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,35 +296,17 @@ impl WhitespaceRender {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WhitespaceSubstitutions {
#[serde(deserialize_with = "deserialize_single_grapheme")]
pub space: String,
#[serde(deserialize_with = "deserialize_single_grapheme")]
pub tab: String,
#[serde(deserialize_with = "deserialize_single_grapheme")]
pub newline: String,
}

fn deserialize_single_grapheme<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let buf = String::deserialize(deserializer)?;

if helix_core::graphemes::grapheme_width(&buf) == 1 {
Ok(buf)
} else {
Err(serde::de::Error::custom(
"whitespace substitions must be single-width graphemes",
))
}
pub space: char,
pub tab: char,
pub newline: char,
}

impl Default for WhitespaceSubstitutions {
fn default() -> Self {
Self {
space: "·".to_string(),
tab: "→".to_string(),
newline: "⏎".to_string(),
space: '·',
tab: '→',
newline: '⏎',
}
}
}
Expand Down

0 comments on commit 954d423

Please sign in to comment.