Skip to content

Add config option for clear whitespace line on <ret> #5159

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

Closed
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
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Options for rendering whitespace with visible characters. Use `:set whitespace.r
|-----|-------------|---------|
| `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`, `nbsp`, `newline` or `tabpad` | See example below |
| `clear-whitespace-line-on-ret` | If current line is only whitespace, whether to clear it when `<ret>` is pressed | `true`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor.whitespace is about rendering whitespace with characters like ·. This should live at the top-level of editor instead


Example

Expand Down
15 changes: 11 additions & 4 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3131,17 +3131,24 @@ pub mod insert {
let curr = contents.get_char(pos).unwrap_or(' ');

let current_line = text.char_to_line(pos);

let line_is_only_whitespace = text
.line(current_line)
.chars()
.all(|char| char.is_ascii_whitespace());

let ws_cfg = &cx.editor.config().whitespace;

let clear_line = ws_cfg.clear_whitespace_line_on_ret &&
line_is_only_whitespace;

let mut new_text = String::new();

// If the current line is all whitespace, insert a line ending at the beginning of
// the current line. This makes the current line empty and the new line contain the
// indentation of the old line.
let (from, to, local_offs) = if line_is_only_whitespace {
let (from, to, local_offs) = if clear_line {
// If the current line is all whitespace, insert a line ending at
// the beginning of the current line. This makes the current
// line empty and the new line contain the indentation of the
// old line.
let line_start = text.line_to_char(current_line);
new_text.push_str(doc.line_ending.as_str());

Expand Down
4 changes: 3 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,17 @@ impl std::str::FromStr for GutterType {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
#[serde(default, rename_all = "kebab-case")]
pub struct WhitespaceConfig {
pub clear_whitespace_line_on_ret: bool,
pub render: WhitespaceRender,
pub characters: WhitespaceCharacters,
}

impl Default for WhitespaceConfig {
fn default() -> Self {
Self {
clear_whitespace_line_on_ret: true,
render: WhitespaceRender::Basic(WhitespaceRenderValue::None),
characters: WhitespaceCharacters::default(),
}
Expand Down