mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-25 10:56:19 +04:00
feat(ui): add nbsp (non-breaking space) to rendered whitespace (#2322)
This commit is contained in:
parent
e4c2618099
commit
2c60798b00
@ -145,7 +145,7 @@ ### `[editor.whitespace]` Section
|
||||
| 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
|
||||
|
||||
@ -160,6 +160,7 @@ # or control each character
|
||||
|
||||
[editor.whitespace.characters]
|
||||
space = "·"
|
||||
nbsp = "⍽"
|
||||
tab = "→"
|
||||
newline = "⏎"
|
||||
```
|
||||
|
@ -370,6 +370,7 @@ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
|
||||
" ".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 {
|
||||
@ -406,6 +407,14 @@ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
|
||||
" "
|
||||
};
|
||||
|
||||
let nbsp = if whitespace.render.nbsp() == WhitespaceRenderValue::All
|
||||
&& text.len_chars() < end
|
||||
{
|
||||
 
|
||||
} else {
|
||||
" "
|
||||
};
|
||||
|
||||
use helix_core::graphemes::{grapheme_width, RopeGraphemes};
|
||||
|
||||
for grapheme in RopeGraphemes::new(text) {
|
||||
@ -445,6 +454,9 @@ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
|
||||
} 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
|
||||
|
@ -288,6 +288,7 @@ pub enum WhitespaceRender {
|
||||
Specific {
|
||||
default: Option<WhitespaceRenderValue>,
|
||||
space: Option<WhitespaceRenderValue>,
|
||||
nbsp: Option<WhitespaceRenderValue>,
|
||||
tab: Option<WhitespaceRenderValue>,
|
||||
newline: Option<WhitespaceRenderValue>,
|
||||
},
|
||||
@ -311,6 +312,14 @@ pub fn space(&self) -> WhitespaceRenderValue {
|
||||
}
|
||||
}
|
||||
}
|
||||
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,
|
||||
@ -333,6 +342,7 @@ pub fn newline(&self) -> WhitespaceRenderValue {
|
||||
#[serde(default)]
|
||||
pub struct WhitespaceCharacters {
|
||||
pub space: char,
|
||||
pub nbsp: char,
|
||||
pub tab: char,
|
||||
pub newline: char,
|
||||
}
|
||||
@ -341,6 +351,7 @@ impl Default for WhitespaceCharacters {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
space: '·', // U+00B7
|
||||
nbsp: '⍽', // U+237D
|
||||
tab: '→', // U+2192
|
||||
newline: '⏎', // U+23CE
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user