Merge branch 'master' into copy-paste

This commit is contained in:
Antonio Scandurra
2021-04-14 12:05:52 +02:00
10 changed files with 102 additions and 84 deletions
+56 -45
View File
@@ -311,7 +311,7 @@ impl BufferView {
let display_map = self.display_map.read(ctx);
let cursor = display_map
.anchor_before(position, Bias::Left, ctx.app())
.anchor_before(position, Bias::Left, ctx.as_ref())
.unwrap();
let selection = Selection {
start: cursor.clone(),
@@ -336,7 +336,9 @@ impl BufferView {
) {
let buffer = self.buffer.read(ctx);
let map = self.display_map.read(ctx);
let cursor = map.anchor_before(position, Bias::Left, ctx.app()).unwrap();
let cursor = map
.anchor_before(position, Bias::Left, ctx.as_ref())
.unwrap();
if let Some(selection) = self.pending_selection.as_mut() {
selection.set_head(buffer, cursor);
} else {
@@ -351,8 +353,8 @@ impl BufferView {
fn end_selection(&mut self, ctx: &mut ViewContext<Self>) {
if let Some(selection) = self.pending_selection.take() {
let ix = self.selection_insertion_index(&selection.start, ctx.app());
let mut selections = self.selections(ctx.app()).to_vec();
let ix = self.selection_insertion_index(&selection.start, ctx.as_ref());
let mut selections = self.selections(ctx.as_ref()).to_vec();
selections.insert(ix, selection);
self.update_selections(selections, ctx);
} else {
@@ -392,8 +394,8 @@ impl BufferView {
let mut selections = Vec::new();
for range in ranges {
selections.push(Selection {
start: map.anchor_after(range.start, Bias::Left, ctx.app())?,
end: map.anchor_before(range.end, Bias::Left, ctx.app())?,
start: map.anchor_after(range.start, Bias::Left, ctx.as_ref())?,
end: map.anchor_before(range.end, Bias::Left, ctx.as_ref())?,
reversed: false,
goal_column: None,
});
@@ -406,7 +408,7 @@ impl BufferView {
let mut offset_ranges = SmallVec::<[Range<usize>; 32]>::new();
{
let buffer = self.buffer.read(ctx);
for selection in self.selections(ctx.app()) {
for selection in self.selections(ctx.as_ref()) {
let start = selection.start.to_offset(buffer).unwrap();
let end = selection.end.to_offset(buffer).unwrap();
offset_ranges.push(start..end);
@@ -456,18 +458,21 @@ impl BufferView {
pub fn backspace(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
self.start_transaction(ctx);
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let buffer = self.buffer.read(ctx);
let map = self.display_map.read(ctx);
for selection in &mut selections {
if selection.range(buffer).is_empty() {
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
let head = selection
.head()
.to_display_point(map, ctx.as_ref())
.unwrap();
let cursor = map
.anchor_before(
movement::left(map, head, ctx.app()).unwrap(),
movement::left(map, head, ctx.as_ref()).unwrap(),
Bias::Left,
ctx.app(),
ctx.as_ref(),
)
.unwrap();
selection.set_head(&buffer, cursor);
@@ -485,7 +490,7 @@ impl BufferView {
pub fn cut(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
self.start_transaction(ctx);
let mut text = String::new();
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
let mut clipboard_selections = Vec::with_capacity(selections.len());
{
let buffer = self.buffer.read(ctx);
@@ -516,7 +521,7 @@ impl BufferView {
self.insert(&String::new(), ctx);
self.end_transaction(ctx);
ctx.app_mut()
ctx.as_mut()
.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
}
@@ -524,7 +529,7 @@ impl BufferView {
let buffer = self.buffer.read(ctx);
let max_point = buffer.max_point();
let mut text = String::new();
let selections = self.selections(ctx.app());
let selections = self.selections(ctx.as_ref());
let mut clipboard_selections = Vec::with_capacity(selections.len());
for selection in selections {
let mut start = selection.start.to_point(buffer).expect("invalid start");
@@ -545,15 +550,15 @@ impl BufferView {
});
}
ctx.app_mut()
ctx.as_mut()
.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
}
pub fn paste(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
if let Some(item) = ctx.app_mut().read_from_clipboard() {
if let Some(item) = ctx.as_mut().read_from_clipboard() {
let clipboard_text = item.text();
if let Some(clipboard_selections) = item.metadata::<Vec<ClipboardSelection>>() {
let selections_len = self.selections(ctx.app()).len();
let selections_len = self.selections(ctx.as_ref()).len();
if clipboard_selections.len() == selections_len {
// If there are the same number of selections as there were at the time that
// this clipboard data was written, then paste one slice of the clipboard text
@@ -585,7 +590,7 @@ impl BufferView {
ctx: &mut ViewContext<Self>,
) {
self.start_transaction(ctx);
let selections = self.selections(ctx.app()).to_vec();
let selections = self.selections(ctx.as_ref()).to_vec();
let mut new_selections = Vec::with_capacity(selections.len());
let mut clipboard_offset = 0;
for (selection, clipboard_selection) in selections.iter().zip(clipboard_selections) {
@@ -648,7 +653,7 @@ impl BufferView {
}
pub fn move_left(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
let app = ctx.app();
let app = ctx.as_ref();
let mut selections = self.selections(app).to_vec();
{
let map = self.display_map.read(app);
@@ -674,17 +679,20 @@ impl BufferView {
}
pub fn select_left(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let buffer = self.buffer.read(ctx);
let map = self.display_map.read(ctx);
for selection in &mut selections {
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
let head = selection
.head()
.to_display_point(map, ctx.as_ref())
.unwrap();
let cursor = map
.anchor_before(
movement::left(map, head, ctx.app()).unwrap(),
movement::left(map, head, ctx.as_ref()).unwrap(),
Bias::Left,
ctx.app(),
ctx.as_ref(),
)
.unwrap();
selection.set_head(&buffer, cursor);
@@ -696,9 +704,9 @@ impl BufferView {
}
pub fn move_right(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let map = self.display_map.read(app);
for selection in &mut selections {
let start = selection.start.to_display_point(map, app).unwrap();
@@ -722,13 +730,16 @@ impl BufferView {
}
pub fn select_right(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let buffer = self.buffer.read(app);
let map = self.display_map.read(app);
for selection in &mut selections {
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
let head = selection
.head()
.to_display_point(map, ctx.as_ref())
.unwrap();
let cursor = map
.anchor_before(movement::right(map, head, app).unwrap(), Bias::Right, app)
.unwrap();
@@ -744,9 +755,9 @@ impl BufferView {
if self.single_line {
ctx.propagate_action();
} else {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let map = self.display_map.read(app);
for selection in &mut selections {
let start = selection.start.to_display_point(map, app).unwrap();
@@ -773,9 +784,9 @@ impl BufferView {
if self.single_line {
ctx.propagate_action();
} else {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let buffer = self.buffer.read(app);
let map = self.display_map.read(app);
for selection in &mut selections {
@@ -795,9 +806,9 @@ impl BufferView {
if self.single_line {
ctx.propagate_action();
} else {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let map = self.display_map.read(app);
for selection in &mut selections {
let start = selection.start.to_display_point(map, app).unwrap();
@@ -824,9 +835,9 @@ impl BufferView {
if self.single_line {
ctx.propagate_action();
} else {
let mut selections = self.selections(ctx.app()).to_vec();
let mut selections = self.selections(ctx.as_ref()).to_vec();
{
let app = ctx.app();
let app = ctx.as_ref();
let buffer = self.buffer.read(app);
let map = self.display_map.read(app);
for selection in &mut selections {
@@ -969,7 +980,7 @@ impl BufferView {
let mut fold_ranges = Vec::new();
let app = ctx.app();
let app = ctx.as_ref();
let map = self.display_map.read(app);
for selection in self.selections(app) {
let (start, end) = selection.display_range(map, app).sorted();
@@ -999,7 +1010,7 @@ impl BufferView {
pub fn unfold(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
use super::RangeExt;
let app = ctx.app();
let app = ctx.as_ref();
let map = self.display_map.read(app);
let buffer = self.buffer.read(app);
let ranges = self
@@ -1083,7 +1094,7 @@ impl BufferView {
self.display_map.update(ctx, |map, ctx| {
let buffer = self.buffer.read(ctx);
let ranges = self
.selections(ctx.app())
.selections(ctx.as_ref())
.iter()
.map(|s| s.range(buffer))
.collect::<Vec<_>>();
@@ -1601,7 +1612,7 @@ mod tests {
.unwrap();
view.fold(&(), ctx);
assert_eq!(
view.text(ctx.app()),
view.text(ctx.as_ref()),
"
impl Foo {
// Hello!
@@ -1622,7 +1633,7 @@ mod tests {
view.fold(&(), ctx);
assert_eq!(
view.text(ctx.app()),
view.text(ctx.as_ref()),
"
impl Foo {…
}
@@ -1632,7 +1643,7 @@ mod tests {
view.unfold(&(), ctx);
assert_eq!(
view.text(ctx.app()),
view.text(ctx.as_ref()),
"
impl Foo {
// Hello!
@@ -1652,7 +1663,7 @@ mod tests {
);
view.unfold(&(), ctx);
assert_eq!(view.text(ctx.app()), buffer.read(ctx).text());
assert_eq!(view.text(ctx.as_ref()), buffer.read(ctx).text());
});
});
}
@@ -1679,12 +1690,12 @@ mod tests {
view.update(app, |view, ctx| {
view.move_down(&(), ctx);
assert_eq!(
view.selection_ranges(ctx.app()),
view.selection_ranges(ctx.as_ref()),
&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)]
);
view.move_right(&(), ctx);
assert_eq!(
view.selection_ranges(ctx.app()),
view.selection_ranges(ctx.as_ref()),
&[DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4)]
);
Ok::<(), Error>(())