mirror of https://github.com/helix-editor/helix
Clear line on `<ret>` when line is all whitespace (#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.pull/4887/head
parent
e6dad960cf
commit
2271c3aed9
|
@ -3085,40 +3085,59 @@ pub mod insert {
|
||||||
let curr = contents.get_char(pos).unwrap_or(' ');
|
let curr = contents.get_char(pos).unwrap_or(' ');
|
||||||
|
|
||||||
let current_line = text.char_to_line(pos);
|
let current_line = text.char_to_line(pos);
|
||||||
let indent = indent::indent_for_newline(
|
let line_is_only_whitespace = text
|
||||||
doc.language_config(),
|
.line(current_line)
|
||||||
doc.syntax(),
|
.chars()
|
||||||
&doc.indent_style,
|
.all(|char| char.is_ascii_whitespace());
|
||||||
doc.tab_width(),
|
|
||||||
text,
|
|
||||||
current_line,
|
|
||||||
pos,
|
|
||||||
current_line,
|
|
||||||
);
|
|
||||||
let mut text = String::new();
|
|
||||||
// If we are between pairs (such as brackets), we want to
|
|
||||||
// insert an additional line which is indented one level
|
|
||||||
// more and place the cursor there
|
|
||||||
let on_auto_pair = doc
|
|
||||||
.auto_pairs(cx.editor)
|
|
||||||
.and_then(|pairs| pairs.get(prev))
|
|
||||||
.and_then(|pair| if pair.close == curr { Some(pair) } else { None })
|
|
||||||
.is_some();
|
|
||||||
|
|
||||||
let local_offs = if on_auto_pair {
|
let mut new_text = String::new();
|
||||||
let inner_indent = indent.clone() + doc.indent_style.as_str();
|
|
||||||
text.reserve_exact(2 + indent.len() + inner_indent.len());
|
// If the current line is all whitespace, insert a line ending at the beginning of
|
||||||
text.push_str(doc.line_ending.as_str());
|
// the current line. This makes the current line empty and the new line contain the
|
||||||
text.push_str(&inner_indent);
|
// indentation of the old line.
|
||||||
let local_offs = text.chars().count();
|
let (from, to, local_offs) = if line_is_only_whitespace {
|
||||||
text.push_str(doc.line_ending.as_str());
|
let line_start = text.line_to_char(current_line);
|
||||||
text.push_str(&indent);
|
new_text.push_str(doc.line_ending.as_str());
|
||||||
local_offs
|
|
||||||
|
(line_start, line_start, new_text.chars().count())
|
||||||
} else {
|
} else {
|
||||||
text.reserve_exact(1 + indent.len());
|
let indent = indent::indent_for_newline(
|
||||||
text.push_str(doc.line_ending.as_str());
|
doc.language_config(),
|
||||||
text.push_str(&indent);
|
doc.syntax(),
|
||||||
text.chars().count()
|
&doc.indent_style,
|
||||||
|
doc.tab_width(),
|
||||||
|
text,
|
||||||
|
current_line,
|
||||||
|
pos,
|
||||||
|
current_line,
|
||||||
|
);
|
||||||
|
|
||||||
|
// If we are between pairs (such as brackets), we want to
|
||||||
|
// insert an additional line which is indented one level
|
||||||
|
// more and place the cursor there
|
||||||
|
let on_auto_pair = doc
|
||||||
|
.auto_pairs(cx.editor)
|
||||||
|
.and_then(|pairs| pairs.get(prev))
|
||||||
|
.and_then(|pair| if pair.close == curr { Some(pair) } else { None })
|
||||||
|
.is_some();
|
||||||
|
|
||||||
|
let local_offs = if on_auto_pair {
|
||||||
|
let inner_indent = indent.clone() + doc.indent_style.as_str();
|
||||||
|
new_text.reserve_exact(2 + indent.len() + inner_indent.len());
|
||||||
|
new_text.push_str(doc.line_ending.as_str());
|
||||||
|
new_text.push_str(&inner_indent);
|
||||||
|
let local_offs = new_text.chars().count();
|
||||||
|
new_text.push_str(doc.line_ending.as_str());
|
||||||
|
new_text.push_str(&indent);
|
||||||
|
local_offs
|
||||||
|
} else {
|
||||||
|
new_text.reserve_exact(1 + indent.len());
|
||||||
|
new_text.push_str(doc.line_ending.as_str());
|
||||||
|
new_text.push_str(&indent);
|
||||||
|
new_text.chars().count()
|
||||||
|
};
|
||||||
|
|
||||||
|
(pos, pos, local_offs)
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_range = if doc.restore_cursor {
|
let new_range = if doc.restore_cursor {
|
||||||
|
@ -3139,9 +3158,9 @@ pub mod insert {
|
||||||
// range.replace(|range| range.is_empty(), head); -> fn extend if cond true, new head pos
|
// range.replace(|range| range.is_empty(), head); -> fn extend if cond true, new head pos
|
||||||
// can be used with cx.mode to do replace or extend on most changes
|
// can be used with cx.mode to do replace or extend on most changes
|
||||||
ranges.push(new_range);
|
ranges.push(new_range);
|
||||||
global_offs += text.chars().count();
|
global_offs += new_text.chars().count();
|
||||||
|
|
||||||
(pos, pos, Some(text.into()))
|
(from, to, Some(new_text.into()))
|
||||||
});
|
});
|
||||||
|
|
||||||
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
|
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
|
||||||
|
|
Loading…
Reference in New Issue