diff --git a/crates/edit/src/buffer/mod.rs b/crates/edit/src/buffer/mod.rs index 46350b77f01..5dd84f4f171 100644 --- a/crates/edit/src/buffer/mod.rs +++ b/crates/edit/src/buffer/mod.rs @@ -1707,16 +1707,14 @@ impl TextBuffer { } fn set_cursor_internal(&mut self, cursor: Cursor) { - debug_assert!( - cursor.offset <= self.text_length() - && cursor.logical_pos.x >= 0 - && cursor.logical_pos.y >= 0 - && cursor.logical_pos.y <= self.stats.logical_lines - && cursor.visual_pos.x >= 0 - && (self.word_wrap_column <= 0 || cursor.visual_pos.x <= self.word_wrap_column) - && cursor.visual_pos.y >= 0 - && cursor.visual_pos.y <= self.stats.visual_lines - ); + debug_assert!(cursor.offset <= self.text_length()); + debug_assert!(cursor.logical_pos.x >= 0); + debug_assert!(cursor.logical_pos.y >= 0); + debug_assert!(cursor.logical_pos.y <= self.stats.logical_lines); + debug_assert!(cursor.visual_pos.x >= 0); + debug_assert!(self.word_wrap_column <= 0 || cursor.visual_pos.x <= self.word_wrap_column); + debug_assert!(cursor.visual_pos.y >= 0); + debug_assert!(cursor.visual_pos.y <= self.stats.visual_lines); self.cursor = cursor; } @@ -2258,7 +2256,8 @@ impl TextBuffer { { let cursor = self.cursor; self.edit_write(if self.newlines_are_crlf { b"\r\n" } else { b"\n" }); - self.set_cursor_internal(cursor); + // Can't use `set_cursor_internal` here, because we haven't updated the line stats yet. + self.cursor = cursor; } self.edit_end(); diff --git a/crates/edit/src/tui.rs b/crates/edit/src/tui.rs index a8b139200d6..314cdc30cc6 100644 --- a/crates/edit/src/tui.rs +++ b/crates/edit/src/tui.rs @@ -973,15 +973,13 @@ impl Tui { } match &mut node.content { - NodeContent::Modal(title) => { - if !title.is_empty() { - self.framebuffer.replace_text( - node.outer.top, - node.outer.left + 2, - node.outer.right - 1, - title, - ); - } + NodeContent::Modal(title) if !title.is_empty() => { + self.framebuffer.replace_text( + node.outer.top, + node.outer.left + 2, + node.outer.right - 1, + title, + ); } NodeContent::Text(content) => self.render_styled_text( inner, @@ -2864,39 +2862,36 @@ impl<'a> Context<'a, '_> { self.set_input_consumed(); } else if self.tui.mouse_state != InputMouseState::None { match self.tui.mouse_state { - InputMouseState::Left => { - if self.tui.mouse_is_drag { - // We don't need to look up the previous track node, - // since it has a fixed size based on the container size. - let track_rect = Rect { - left: container_rect.right, - top: container_rect.top, - right: container_rect.right + 1, - bottom: container_rect.bottom, - }; - if track_rect.contains(self.tui.mouse_down_position) { - if sc.scroll_offset_y_drag_start == CoordType::MIN { - sc.scroll_offset_y_drag_start = sc.scroll_offset.y; - } - - let content = prev_container.children.first.unwrap().borrow(); - let content_rect = content.inner; - let content_height = content_rect.height(); - let track_height = track_rect.height(); - let scrollable_height = content_height - track_height; - - if scrollable_height > 0 { - let trackable = track_height - sc.thumb_height; - let delta_y = - self.tui.mouse_position.y - self.tui.mouse_down_position.y; - sc.scroll_offset.y = sc.scroll_offset_y_drag_start - + (delta_y as i64 * scrollable_height as i64 - / trackable as i64) - as CoordType; - } + InputMouseState::Left if self.tui.mouse_is_drag => { + // We don't need to look up the previous track node, + // since it has a fixed size based on the container size. + let track_rect = Rect { + left: container_rect.right, + top: container_rect.top, + right: container_rect.right + 1, + bottom: container_rect.bottom, + }; + if track_rect.contains(self.tui.mouse_down_position) { + if sc.scroll_offset_y_drag_start == CoordType::MIN { + sc.scroll_offset_y_drag_start = sc.scroll_offset.y; + } - self.set_input_consumed(); + let content = prev_container.children.first.unwrap().borrow(); + let content_rect = content.inner; + let content_height = content_rect.height(); + let track_height = track_rect.height(); + let scrollable_height = content_height - track_height; + + if scrollable_height > 0 { + let trackable = track_height - sc.thumb_height; + let delta_y = + self.tui.mouse_position.y - self.tui.mouse_down_position.y; + sc.scroll_offset.y = sc.scroll_offset_y_drag_start + + (delta_y as i64 * scrollable_height as i64 / trackable as i64) + as CoordType; } + + self.set_input_consumed(); } } InputMouseState::Release => {