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::<Editor>(cx)` with
`item.downcast::<Editor>()` 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
This commit is contained in:
Kirill Bulatov 2024-11-28 20:42:57 +02:00 committed by GitHub
parent 4a96db026c
commit 0acd98a07e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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::<Selection<usize>>;
for selection in editor.selections.all::<usize>(cx) {
cursor_position.selected_count.characters += buffer
.text_for_range(selection.start..selection.end)
.map(|t| t.chars().count())
.sum::<usize>();
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::<Point>(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::<Selection<usize>>;
let buffer = editor.buffer().read(cx).snapshot(cx);
if buffer.excerpts().count() > 0 {
for selection in editor.selections.all::<usize>(cx) {
cursor_position.selected_count.characters += buffer
.text_for_range(selection.start..selection.end)
.map(|t| t.chars().count())
.sum::<usize>();
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::<Point>(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();
})
})