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

feat(ui): add nbsp (non-breaking space) to rendered whitespace #2322

Merged
merged 1 commit into from
Apr 30, 2022
Merged
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
3 changes: 2 additions & 1 deletion book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Options for rendering whitespace with visible characters. Use `:set whitespace.r
| Key | Description | Default |
|-----|-------------|---------|
| `render` | Whether to render whitespace. May either be `"all"` or `"none"`, or a table with sub-keys `space`, `tab`, and `newline`. | `"none"` |
| `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space` or `newline` | See example below |
| `characters` | Literal characters to use when rendering whitespace. Sub-keys may be any of `tab`, `space`, `nbsp` or `newline` | See example below |

Example

Expand All @@ -160,6 +160,7 @@ newline = "none"

[editor.whitespace.characters]
space = "·"
nbsp = "⍽"
tab = "→"
newline = "⏎"
```
12 changes: 12 additions & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ impl EditorView {
" ".repeat(tab_width)
};
let space = whitespace.characters.space.to_string();
let nbsp = whitespace.characters.nbsp.to_string();
let newline = if whitespace.render.newline() == WhitespaceRenderValue::All {
whitespace.characters.newline.to_string()
} else {
Expand Down Expand Up @@ -404,6 +405,14 @@ impl EditorView {
" "
};

let nbsp = if whitespace.render.nbsp() == WhitespaceRenderValue::All
&& text.len_chars() < end
{
&nbsp
} else {
" "
};

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

for grapheme in RopeGraphemes::new(text) {
Expand Down Expand Up @@ -443,6 +452,9 @@ impl EditorView {
} 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
Expand Down
11 changes: 11 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ pub enum WhitespaceRender {
Specific {
default: Option<WhitespaceRenderValue>,
space: Option<WhitespaceRenderValue>,
nbsp: Option<WhitespaceRenderValue>,
tab: Option<WhitespaceRenderValue>,
newline: Option<WhitespaceRenderValue>,
},
Expand All @@ -311,6 +312,14 @@ impl WhitespaceRender {
}
}
}
pub fn nbsp(&self) -> WhitespaceRenderValue {
match *self {
Self::Basic(val) => val,
Self::Specific { default, nbsp, .. } => {
nbsp.or(default).unwrap_or(WhitespaceRenderValue::None)
}
}
}
pub fn tab(&self) -> WhitespaceRenderValue {
match *self {
Self::Basic(val) => val,
Expand All @@ -333,6 +342,7 @@ impl WhitespaceRender {
#[serde(default)]
pub struct WhitespaceCharacters {
pub space: char,
pub nbsp: char,
pub tab: char,
pub newline: char,
}
Expand All @@ -341,6 +351,7 @@ impl Default for WhitespaceCharacters {
fn default() -> Self {
Self {
space: '·', // U+00B7
nbsp: '⍽', // U+237D
tab: '→', // U+2192
newline: '⏎', // U+23CE
}
Expand Down