Fix rand import and tweak callbacks

Co-Authored-By: Julia <30666851+ForLoveOfCats@users.noreply.github.com>
This commit is contained in:
Joseph T. Lyons 2023-07-25 11:44:13 -04:00
parent 1a84382881
commit 299818cde0
2 changed files with 15 additions and 41 deletions

View File

@ -10,7 +10,6 @@ doctest = false
[features] [features]
test-support = [ test-support = [
"rand",
"copilot/test-support", "copilot/test-support",
"text/test-support", "text/test-support",
"language/test-support", "language/test-support",
@ -62,8 +61,8 @@ serde.workspace = true
serde_derive.workspace = true serde_derive.workspace = true
smallvec.workspace = true smallvec.workspace = true
smol.workspace = true smol.workspace = true
rand.workspace = true
rand = { workspace = true, optional = true }
tree-sitter-rust = { workspace = true, optional = true } tree-sitter-rust = { workspace = true, optional = true }
tree-sitter-html = { workspace = true, optional = true } tree-sitter-html = { workspace = true, optional = true }
tree-sitter-typescript = { workspace = true, optional = true } tree-sitter-typescript = { workspace = true, optional = true }

View File

@ -74,8 +74,9 @@ pub use multi_buffer::{
}; };
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use project::{FormatTrigger, Location, LocationLink, Project, ProjectPath, ProjectTransaction}; use project::{FormatTrigger, Location, LocationLink, Project, ProjectPath, ProjectTransaction};
use rand::seq::SliceRandom; use rand::{seq::SliceRandom, thread_rng};
use rand::thread_rng; // use rand::seq::SliceRandom;
// use rand::thread_rng;
use scroll::{ use scroll::{
autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide, autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
}; };
@ -353,7 +354,7 @@ pub fn init(cx: &mut AppContext) {
cx.add_action(Editor::sort_lines_case_sensitive); cx.add_action(Editor::sort_lines_case_sensitive);
cx.add_action(Editor::sort_lines_case_insensitive); cx.add_action(Editor::sort_lines_case_insensitive);
cx.add_action(Editor::reverse_lines); cx.add_action(Editor::reverse_lines);
// cx.add_action(Editor::shuffle_lines); cx.add_action(Editor::shuffle_lines);
cx.add_action(Editor::delete_to_previous_word_start); cx.add_action(Editor::delete_to_previous_word_start);
cx.add_action(Editor::delete_to_previous_subword_start); cx.add_action(Editor::delete_to_previous_subword_start);
cx.add_action(Editor::delete_to_next_word_end); cx.add_action(Editor::delete_to_next_word_end);
@ -4220,12 +4221,7 @@ impl Editor {
_: &SortLinesCaseSensitive, _: &SortLinesCaseSensitive,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
self.manipulate_lines(cx, { self.manipulate_lines(cx, |lines| lines.sort())
|mut lines| {
lines.sort();
lines
}
})
} }
pub fn sort_lines_case_insensitive( pub fn sort_lines_case_insensitive(
@ -4233,35 +4229,20 @@ impl Editor {
_: &SortLinesCaseInsensitive, _: &SortLinesCaseInsensitive,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
self.manipulate_lines(cx, { self.manipulate_lines(cx, |lines| lines.sort_by_key(|line| line.to_lowercase()))
|mut lines| {
lines.sort_by_key(|line| line.to_lowercase());
lines
}
})
} }
pub fn reverse_lines(&mut self, _: &ReverseLines, cx: &mut ViewContext<Self>) { pub fn reverse_lines(&mut self, _: &ReverseLines, cx: &mut ViewContext<Self>) {
self.manipulate_lines(cx, { self.manipulate_lines(cx, |lines| lines.reverse())
|mut lines| {
lines.reverse();
lines
}
})
} }
// pub fn shuffle_lines(&mut self, _: &ShuffleLines, cx: &mut ViewContext<Self>) { pub fn shuffle_lines(&mut self, _: &ShuffleLines, cx: &mut ViewContext<Self>) {
// self.manipulate_lines(cx, { self.manipulate_lines(cx, |lines| lines.shuffle(&mut thread_rng()))
// |mut lines| { }
// lines.shuffle(&mut thread_rng());
// lines
// }
// })
// }
fn manipulate_lines<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn) fn manipulate_lines<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn)
where where
Fn: FnMut(Vec<&str>) -> Vec<&str>, Fn: FnMut(&mut Vec<&str>),
{ {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let buffer = self.buffer.read(cx).snapshot(cx); let buffer = self.buffer.read(cx).snapshot(cx);
@ -4280,12 +4261,12 @@ impl Editor {
&display_map, &display_map,
&mut selections, &mut selections,
); );
let start = Point::new(start_row, 0); let start = Point::new(start_row, 0);
let end = Point::new(end_row - 1, buffer.line_len(end_row - 1)); let end = Point::new(end_row - 1, buffer.line_len(end_row - 1));
let text = buffer.text_for_range(start..end).collect::<String>(); let text = buffer.text_for_range(start..end).collect::<String>();
// TODO SORT LINES: Is there a smarter / more effificent way to obtain lines? let mut lines = text.split("\n").collect::<Vec<_>>();
let lines = text.split("\n").collect::<Vec<_>>(); callback(&mut lines);
let lines = callback(lines);
edits.push((start..end, lines.join("\n"))); edits.push((start..end, lines.join("\n")));
} }
@ -4300,12 +4281,6 @@ impl Editor {
this.request_autoscroll(Autoscroll::fit(), cx); this.request_autoscroll(Autoscroll::fit(), cx);
}); });
// TODO:
// Write tests
// - Use cx.set_state("«one✅ ˇ»two «three ˇ»four «five ˇ»six "); in tests
// Mikayla check for perf stuff
// Shuffle
} }
pub fn duplicate_line(&mut self, _: &DuplicateLine, cx: &mut ViewContext<Self>) { pub fn duplicate_line(&mut self, _: &DuplicateLine, cx: &mut ViewContext<Self>) {