on master help sign in
feat(ui): add nbsp (non-breaking space) to rendered whitespace (#2322)
commit2c60798b00cc9b8efc9fdbec304a7ea04d11de4c authorunrelentingtech <greg@unrelenting.technology> dateSat 30 Apr 2022, 03:48 (5 years ago) parente4c2618
3 files changed+25−1
book/src/configuration.md+2−1 helix-term/src/ui/editor.rs+12 helix-view/src/editor.rs+11
book/src/configuration.md+2−1
@@ -145,7 +145,7 @@ Options for rendering whitespace with visible characters. Use `:set whitespace.r
145145| Key | Description | Default |
146146|-----|-------------|---------|
147147| `render` | Whether to render whitespace. May either be `"all"` or `"none"`, or a table with sub-keys `space`, `tab`, and `newline`. | `"none"` |
148−| `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space` or `newline` | See example below |
148+| `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space`, `nbsp` or `newline` | See example below |
149149
150150Example
151151
@@ -160,6 +160,7 @@ newline = "none"
160160
161161[editor.whitespace.characters]
162162space = "·"
163+nbsp = "⍽"
163164tab = "→"
164165newline = "⏎"
165166```
helix-term/src/ui/editor.rs+12
@@ -370,6 +370,7 @@ impl EditorView {
370370 " ".repeat(tab_width)
371371 };
372372 let space = whitespace.characters.space.to_string();
373+ let nbsp = whitespace.characters.nbsp.to_string();
373374 let newline = if whitespace.render.newline() == WhitespaceRenderValue::All {
374375 whitespace.characters.newline.to_string()
375376 } else {
@@ -406,6 +407,14 @@ impl EditorView {
406407 " "
407408 };
408409
410+ let nbsp = if whitespace.render.nbsp() == WhitespaceRenderValue::All
411+ && text.len_chars() < end
412+ {
413+ &nbsp
414+ } else {
415+ " "
416+ };
417+
409418 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
410419
411420 for grapheme in RopeGraphemes::new(text) {
@@ -445,6 +454,9 @@ impl EditorView {
445454 } else if grapheme == " " {
446455 is_whitespace = true;
447456 (space, 1)
457+ } else if grapheme == "\u{00A0}" {
458+ is_whitespace = true;
459+ (nbsp, 1)
448460 } else {
449461 is_whitespace = false;
450462 // Cow will prevent allocations if span contained in a single slice
helix-view/src/editor.rs+11
@@ -288,6 +288,7 @@ pub enum WhitespaceRender {
288288 Specific {
289289 default: Option<WhitespaceRenderValue>,
290290 space: Option<WhitespaceRenderValue>,
291+ nbsp: Option<WhitespaceRenderValue>,
291292 tab: Option<WhitespaceRenderValue>,
292293 newline: Option<WhitespaceRenderValue>,
293294 },
@@ -311,6 +312,14 @@ impl WhitespaceRender {
311312 }
312313 }
313314 }
315+ pub fn nbsp(&self) -> WhitespaceRenderValue {
316+ match *self {
317+ Self::Basic(val) => val,
318+ Self::Specific { default, nbsp, .. } => {
319+ nbsp.or(default).unwrap_or(WhitespaceRenderValue::None)
320+ }
321+ }
322+ }
314323 pub fn tab(&self) -> WhitespaceRenderValue {
315324 match *self {
316325 Self::Basic(val) => val,
@@ -333,6 +342,7 @@ impl WhitespaceRender {
333342#[serde(default)]
334343pub struct WhitespaceCharacters {
335344 pub space: char,
345+ pub nbsp: char,
336346 pub tab: char,
337347 pub newline: char,
338348}
@@ -341,6 +351,7 @@ impl Default for WhitespaceCharacters {
341351 fn default() -> Self {
342352 Self {
343353 space: '·', // U+00B7
354+ nbsp: '⍽', // U+237D
344355 tab: '→', // U+2192
345356 newline: '⏎', // U+23CE
346357 }