Take selections by either value or ref

This commit is contained in:
Dániel Buga 2022-04-11 18:55:45 +02:00
parent 90dcfa886d
commit 976de7a5cc
3 changed files with 15 additions and 17 deletions

View File

@ -659,14 +659,14 @@ pub fn first_non_blank_character_on_line(&self, line: usize) -> usize {
pub fn edit_multiple(
&mut self,
edits: &[(&Selection, &str)],
edits: &[(impl AsRef<Selection>, &str)],
edit_type: EditType,
) -> RopeDelta {
let mut builder = DeltaBuilder::new(self.len());
let mut interval_rope = Vec::new();
for (selection, content) in edits {
let rope = Rope::from(content);
for region in selection.regions() {
for region in selection.as_ref().regions() {
interval_rope.push((region.min(), region.max(), rope.clone()));
}
}

View File

@ -12,8 +12,8 @@
use crossbeam_channel::{unbounded, Receiver, Sender};
use druid::{
piet::{PietText, PietTextLayout, Text, TextLayout, TextLayoutBuilder},
theme, Command, Data, Env, EventCtx, ExtEventSink, FontFamily, Lens,
Point, Rect, Size, Target, Vec2, WidgetId, WindowId,
theme, Command, Data, Env, EventCtx, ExtEventSink, FontFamily, Lens, Point,
Rect, Size, Target, Vec2, WidgetId, WindowId,
};
use lapce_rpc::{
@ -1916,7 +1916,7 @@ pub fn document_format(
if !edits.is_empty() {
let buffer = self.open_files.get_mut(path).unwrap();
let edits: Vec<(Selection, String)> = edits
let edits: Vec<(Selection, &str)> = edits
.iter()
.map(|edit| {
let selection = Selection::region(
@ -1929,20 +1929,11 @@ pub fn document_format(
config.editor.tab_width,
),
);
(selection, edit.new_text.clone())
(selection, edit.new_text.as_str())
})
.collect();
self.edit(
path,
&edits.iter().map(|(s, c)| (s, c.as_str())).collect::<Vec<(
&Selection,
&str,
)>>(
),
EditType::Other,
config,
);
self.edit(path, &edits, EditType::Other, config);
}
}
}
@ -2042,7 +2033,7 @@ fn cursor_apply_delta(&mut self, path: &Path, delta: &RopeDelta) {
pub fn edit(
&mut self,
path: &Path,
edits: &[(&Selection, &str)],
edits: &[(impl AsRef<Selection>, &str)],
edit_type: EditType,
config: &Config,
) -> Option<RopeDelta> {
@ -2053,6 +2044,7 @@ pub fn edit(
let buffer_len = buffer.len();
let mut move_cursor = true;
for (selection, _) in edits.iter() {
let selection = selection.as_ref();
if selection.min_offset() == 0
&& selection.max_offset() >= buffer_len - 1
{

View File

@ -502,6 +502,12 @@ pub struct Selection {
last_inserted: usize,
}
impl AsRef<Selection> for Selection {
fn as_ref(&self) -> &Selection {
self
}
}
impl Selection {
pub fn new() -> Selection {
Selection {