input: Simplify newline logic for multi-line text and fix auto-grow lines count. (#948)
- Fix to only keep indent in CodeEditor mode. https://github.com/user-attachments/assets/4b926a18-b0b5-4419-97dd-21778cd20fb3
This commit is contained in:
parent
6ea427549a
commit
ed6adc2dae
3 changed files with 12 additions and 27 deletions
|
|
@ -61,6 +61,10 @@ pub enum InputMode {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputMode {
|
impl InputMode {
|
||||||
|
pub(super) fn is_code_editor(&self) -> bool {
|
||||||
|
matches!(self, InputMode::CodeEditor { .. })
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn set_rows(&mut self, new_rows: usize) {
|
pub(super) fn set_rows(&mut self, new_rows: usize) {
|
||||||
match self {
|
match self {
|
||||||
InputMode::MultiLine { rows, .. } => {
|
InputMode::MultiLine { rows, .. } => {
|
||||||
|
|
|
||||||
|
|
@ -1243,26 +1243,16 @@ impl InputState {
|
||||||
|
|
||||||
pub(super) fn enter(&mut self, action: &Enter, window: &mut Window, cx: &mut Context<Self>) {
|
pub(super) fn enter(&mut self, action: &Enter, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
if self.is_multi_line() {
|
if self.is_multi_line() {
|
||||||
let is_eof = self.selected_range.end == self.text.len();
|
|
||||||
|
|
||||||
// Get current line indent
|
// Get current line indent
|
||||||
let indent = self.indent_of_next_line(window, cx);
|
let indent = if self.mode.is_code_editor() {
|
||||||
self.replace_text_in_range(None, "\n", window, cx);
|
self.indent_of_next_line(window, cx)
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
// Move cursor to the start of the next line
|
// Add newline and indent
|
||||||
let mut new_offset = self.cursor_offset() - 1;
|
let new_line_text = format!("\n{}", indent);
|
||||||
if is_eof {
|
self.replace_text_in_range(None, &new_line_text, window, cx);
|
||||||
new_offset += 1;
|
|
||||||
}
|
|
||||||
self.move_to(self.next_boundary(new_offset), window, cx);
|
|
||||||
|
|
||||||
// Add indent
|
|
||||||
self.replace_text_in_range(
|
|
||||||
Some(self.range_to_utf16(&(self.cursor_offset()..self.cursor_offset()))),
|
|
||||||
&indent,
|
|
||||||
window,
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.emit(InputEvent::PressEnter {
|
cx.emit(InputEvent::PressEnter {
|
||||||
|
|
|
||||||
|
|
@ -95,15 +95,6 @@ impl TextWrapper {
|
||||||
prev_line_ix += line.len() + 1;
|
prev_line_ix += line.len() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add last empty line.
|
|
||||||
if text.chars().last().unwrap_or('\n') == '\n' {
|
|
||||||
wrapped_lines.push(text.len()..text.len());
|
|
||||||
lines.push(LineWrap {
|
|
||||||
wrap_lines: 0,
|
|
||||||
range: text.len()..text.len(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.text = text;
|
self.text = text;
|
||||||
self.wrapped_lines = wrapped_lines;
|
self.wrapped_lines = wrapped_lines;
|
||||||
self.lines = lines;
|
self.lines = lines;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue