Skip to content

Commit df209f9

Browse files
the-mikedavisFrederik Vestre
authored andcommitted
Clear line on <ret> when line is all whitespace (helix-editor#4854)
This matches the insert-mode behavior for Vim and Kakoune: if the current line is empty except for whitespace, `<ret>` should insert a line ending at the beginning of the line, moving any indentation to the next line.
1 parent e4d7bd4 commit df209f9

File tree

1 file changed

+54
-35
lines changed

1 file changed

+54
-35
lines changed

helix-term/src/commands.rs

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,40 +3085,59 @@ pub mod insert {
30853085
let curr = contents.get_char(pos).unwrap_or(' ');
30863086

30873087
let current_line = text.char_to_line(pos);
3088-
let indent = indent::indent_for_newline(
3089-
doc.language_config(),
3090-
doc.syntax(),
3091-
&doc.indent_style,
3092-
doc.tab_width(),
3093-
text,
3094-
current_line,
3095-
pos,
3096-
current_line,
3097-
);
3098-
let mut text = String::new();
3099-
// If we are between pairs (such as brackets), we want to
3100-
// insert an additional line which is indented one level
3101-
// more and place the cursor there
3102-
let on_auto_pair = doc
3103-
.auto_pairs(cx.editor)
3104-
.and_then(|pairs| pairs.get(prev))
3105-
.and_then(|pair| if pair.close == curr { Some(pair) } else { None })
3106-
.is_some();
3107-
3108-
let local_offs = if on_auto_pair {
3109-
let inner_indent = indent.clone() + doc.indent_style.as_str();
3110-
text.reserve_exact(2 + indent.len() + inner_indent.len());
3111-
text.push_str(doc.line_ending.as_str());
3112-
text.push_str(&inner_indent);
3113-
let local_offs = text.chars().count();
3114-
text.push_str(doc.line_ending.as_str());
3115-
text.push_str(&indent);
3116-
local_offs
3088+
let line_is_only_whitespace = text
3089+
.line(current_line)
3090+
.chars()
3091+
.all(|char| char.is_ascii_whitespace());
3092+
3093+
let mut new_text = String::new();
3094+
3095+
// If the current line is all whitespace, insert a line ending at the beginning of
3096+
// the current line. This makes the current line empty and the new line contain the
3097+
// indentation of the old line.
3098+
let (from, to, local_offs) = if line_is_only_whitespace {
3099+
let line_start = text.line_to_char(current_line);
3100+
new_text.push_str(doc.line_ending.as_str());
3101+
3102+
(line_start, line_start, new_text.chars().count())
31173103
} else {
3118-
text.reserve_exact(1 + indent.len());
3119-
text.push_str(doc.line_ending.as_str());
3120-
text.push_str(&indent);
3121-
text.chars().count()
3104+
let indent = indent::indent_for_newline(
3105+
doc.language_config(),
3106+
doc.syntax(),
3107+
&doc.indent_style,
3108+
doc.tab_width(),
3109+
text,
3110+
current_line,
3111+
pos,
3112+
current_line,
3113+
);
3114+
3115+
// If we are between pairs (such as brackets), we want to
3116+
// insert an additional line which is indented one level
3117+
// more and place the cursor there
3118+
let on_auto_pair = doc
3119+
.auto_pairs(cx.editor)
3120+
.and_then(|pairs| pairs.get(prev))
3121+
.and_then(|pair| if pair.close == curr { Some(pair) } else { None })
3122+
.is_some();
3123+
3124+
let local_offs = if on_auto_pair {
3125+
let inner_indent = indent.clone() + doc.indent_style.as_str();
3126+
new_text.reserve_exact(2 + indent.len() + inner_indent.len());
3127+
new_text.push_str(doc.line_ending.as_str());
3128+
new_text.push_str(&inner_indent);
3129+
let local_offs = new_text.chars().count();
3130+
new_text.push_str(doc.line_ending.as_str());
3131+
new_text.push_str(&indent);
3132+
local_offs
3133+
} else {
3134+
new_text.reserve_exact(1 + indent.len());
3135+
new_text.push_str(doc.line_ending.as_str());
3136+
new_text.push_str(&indent);
3137+
new_text.chars().count()
3138+
};
3139+
3140+
(pos, pos, local_offs)
31223141
};
31233142

31243143
let new_range = if doc.restore_cursor {
@@ -3139,9 +3158,9 @@ pub mod insert {
31393158
// range.replace(|range| range.is_empty(), head); -> fn extend if cond true, new head pos
31403159
// can be used with cx.mode to do replace or extend on most changes
31413160
ranges.push(new_range);
3142-
global_offs += text.chars().count();
3161+
global_offs += new_text.chars().count();
31433162

3144-
(pos, pos, Some(text.into()))
3163+
(from, to, Some(new_text.into()))
31453164
});
31463165

31473166
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

0 commit comments

Comments
 (0)