Show lines selected & selection quantity in status bar (#10327)
Fixes #4204     Release Notes: - Line number indicator now shows number of lines selected and a number of selections.
This commit is contained in:
parent
6b320b9efe
commit
fff197b227
1 changed files with 53 additions and 7 deletions
|
@ -9,9 +9,16 @@ use ui::{
|
||||||
use util::paths::FILE_ROW_COLUMN_DELIMITER;
|
use util::paths::FILE_ROW_COLUMN_DELIMITER;
|
||||||
use workspace::{item::ItemHandle, StatusItemView, Workspace};
|
use workspace::{item::ItemHandle, StatusItemView, Workspace};
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Default, PartialOrd, PartialEq)]
|
||||||
|
struct SelectionStats {
|
||||||
|
lines: usize,
|
||||||
|
characters: usize,
|
||||||
|
selections: usize,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CursorPosition {
|
pub struct CursorPosition {
|
||||||
position: Option<Point>,
|
position: Option<Point>,
|
||||||
selected_count: usize,
|
selected_count: SelectionStats,
|
||||||
workspace: WeakView<Workspace>,
|
workspace: WeakView<Workspace>,
|
||||||
_observe_active_editor: Option<Subscription>,
|
_observe_active_editor: Option<Subscription>,
|
||||||
}
|
}
|
||||||
|
@ -20,7 +27,7 @@ impl CursorPosition {
|
||||||
pub fn new(workspace: &Workspace) -> Self {
|
pub fn new(workspace: &Workspace) -> Self {
|
||||||
Self {
|
Self {
|
||||||
position: None,
|
position: None,
|
||||||
selected_count: 0,
|
selected_count: Default::default(),
|
||||||
workspace: workspace.weak_handle(),
|
workspace: workspace.weak_handle(),
|
||||||
_observe_active_editor: None,
|
_observe_active_editor: None,
|
||||||
}
|
}
|
||||||
|
@ -30,10 +37,11 @@ impl CursorPosition {
|
||||||
let editor = editor.read(cx);
|
let editor = editor.read(cx);
|
||||||
let buffer = editor.buffer().read(cx).snapshot(cx);
|
let buffer = editor.buffer().read(cx).snapshot(cx);
|
||||||
|
|
||||||
self.selected_count = 0;
|
self.selected_count = Default::default();
|
||||||
|
self.selected_count.selections = editor.selections.count();
|
||||||
let mut last_selection: Option<Selection<usize>> = None;
|
let mut last_selection: Option<Selection<usize>> = None;
|
||||||
for selection in editor.selections.all::<usize>(cx) {
|
for selection in editor.selections.all::<usize>(cx) {
|
||||||
self.selected_count += selection.end - selection.start;
|
self.selected_count.characters += selection.end - selection.start;
|
||||||
if last_selection
|
if last_selection
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(true, |last_selection| selection.id > last_selection.id)
|
.map_or(true, |last_selection| selection.id > last_selection.id)
|
||||||
|
@ -41,10 +49,50 @@ impl CursorPosition {
|
||||||
last_selection = Some(selection);
|
last_selection = Some(selection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for selection in editor.selections.all::<Point>(cx) {
|
||||||
|
if selection.end != selection.start {
|
||||||
|
self.selected_count.lines += (selection.end.row - selection.start.row + 1) as usize;
|
||||||
|
}
|
||||||
|
}
|
||||||
self.position = last_selection.map(|s| s.head().to_point(&buffer));
|
self.position = last_selection.map(|s| s.head().to_point(&buffer));
|
||||||
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_position(&self, text: &mut String) {
|
||||||
|
if self.selected_count
|
||||||
|
<= (SelectionStats {
|
||||||
|
selections: 1,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
{
|
||||||
|
// Do not write out anything if we have just one empty selection.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let SelectionStats {
|
||||||
|
lines,
|
||||||
|
characters,
|
||||||
|
selections,
|
||||||
|
} = self.selected_count;
|
||||||
|
let lines = (lines > 1).then_some((lines, "line"));
|
||||||
|
let selections = (selections > 1).then_some((selections, "selection"));
|
||||||
|
let characters = (characters > 0).then_some((characters, "character"));
|
||||||
|
if (None, None, None) == (characters, selections, lines) {
|
||||||
|
// Nothing to display.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
write!(text, " (").unwrap();
|
||||||
|
let mut wrote_once = false;
|
||||||
|
for (count, name) in [selections, lines, characters].into_iter().flatten() {
|
||||||
|
if wrote_once {
|
||||||
|
write!(text, ", ").unwrap();
|
||||||
|
}
|
||||||
|
let plural_suffix = if count > 1 { "s" } else { "" };
|
||||||
|
write!(text, "{count} {name}{plural_suffix}").unwrap();
|
||||||
|
wrote_once = true;
|
||||||
|
}
|
||||||
|
text.push(')');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for CursorPosition {
|
impl Render for CursorPosition {
|
||||||
|
@ -55,9 +103,7 @@ impl Render for CursorPosition {
|
||||||
position.row + 1,
|
position.row + 1,
|
||||||
position.column + 1
|
position.column + 1
|
||||||
);
|
);
|
||||||
if self.selected_count > 0 {
|
self.write_position(&mut text);
|
||||||
write!(text, " ({} selected)", self.selected_count).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
el.child(
|
el.child(
|
||||||
Button::new("go-to-line-column", text)
|
Button::new("go-to-line-column", text)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue