From 0acd98a07e949cdd0e6de09cd0061f7fb7bd48db Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 28 Nov 2024 20:42:57 +0200 Subject: [PATCH] Do not show cursor position for empty files (#21295) Closes https://github.com/zed-industries/zed/issues/21289 Fixes most of the issues: does not display cursor position in empty multi buffers and on non-full editors. Does not fix the startup issue, as it's caused by the AssistantPanel's `ContextEditor` acting as an `Editor`, so whenever default prompts are added, those are registered as added editors, and Zed shows some line numbers for them. We cannot replace `item.act_as::(cx)` with `item.downcast::()` as then multi bufers' navigation will fall off (arguably, those line numbers do not make that much sense too, but still seem useful). This will will fix itself in the future, when assistant panel gets reworked into readonly view by default, as `assistant2` crate already shows (there's no `act_as` impl there and nothing cause issue). Since the remaining issue is minor and will go away on any focus change, and future changes will alter this, closing the original issue. Release Notes: - Improved cursor position display --- crates/go_to_line/src/cursor_position.rs | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/crates/go_to_line/src/cursor_position.rs b/crates/go_to_line/src/cursor_position.rs index 3931cac2845af7e17691f24c957ef8c9c0829632..4f27c64256692a5a02b1a9574ded147e1b02150c 100644 --- a/crates/go_to_line/src/cursor_position.rs +++ b/crates/go_to_line/src/cursor_position.rs @@ -52,34 +52,44 @@ impl CursorPosition { editor .update(&mut cx, |editor, cx| { - let buffer = editor.buffer().read(cx).snapshot(cx); cursor_position.update(cx, |cursor_position, cx| { cursor_position.selected_count = SelectionStats::default(); cursor_position.selected_count.selections = editor.selections.count(); - let mut last_selection = None::>; - for selection in editor.selections.all::(cx) { - cursor_position.selected_count.characters += buffer - .text_for_range(selection.start..selection.end) - .map(|t| t.chars().count()) - .sum::(); - if last_selection - .as_ref() - .map_or(true, |last_selection| selection.id > last_selection.id) - { - last_selection = Some(selection); + match editor.mode() { + editor::EditorMode::AutoHeight { .. } + | editor::EditorMode::SingleLine { .. } => { + cursor_position.position = None } - } - for selection in editor.selections.all::(cx) { - if selection.end != selection.start { - cursor_position.selected_count.lines += - (selection.end.row - selection.start.row) as usize; - if selection.end.column != 0 { - cursor_position.selected_count.lines += 1; + editor::EditorMode::Full => { + let mut last_selection = None::>; + let buffer = editor.buffer().read(cx).snapshot(cx); + if buffer.excerpts().count() > 0 { + for selection in editor.selections.all::(cx) { + cursor_position.selected_count.characters += buffer + .text_for_range(selection.start..selection.end) + .map(|t| t.chars().count()) + .sum::(); + if last_selection.as_ref().map_or(true, |last_selection| { + selection.id > last_selection.id + }) { + last_selection = Some(selection); + } + } + for selection in editor.selections.all::(cx) { + if selection.end != selection.start { + cursor_position.selected_count.lines += + (selection.end.row - selection.start.row) as usize; + if selection.end.column != 0 { + cursor_position.selected_count.lines += 1; + } + } + } } + cursor_position.position = + last_selection.map(|s| s.head().to_point(&buffer)); } } - cursor_position.position = - last_selection.map(|s| s.head().to_point(&buffer)); + cx.notify(); }) })