Skip to content

Commit

Permalink
Do not show cursor position for empty files (#21295)
Browse files Browse the repository at this point in the history
Closes #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
  • Loading branch information
SomeoneToIgnore authored Nov 28, 2024
1 parent 4a96db0 commit 0acd98a
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions crates/go_to_line/src/cursor_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
})
Expand Down

0 comments on commit 0acd98a

Please sign in to comment.