mirror of https://github.com/lapce/lapce.git
Take selections by either value or ref
This commit is contained in:
parent
90dcfa886d
commit
976de7a5cc
|
@ -659,14 +659,14 @@ pub fn first_non_blank_character_on_line(&self, line: usize) -> usize {
|
||||||
|
|
||||||
pub fn edit_multiple(
|
pub fn edit_multiple(
|
||||||
&mut self,
|
&mut self,
|
||||||
edits: &[(&Selection, &str)],
|
edits: &[(impl AsRef<Selection>, &str)],
|
||||||
edit_type: EditType,
|
edit_type: EditType,
|
||||||
) -> RopeDelta {
|
) -> RopeDelta {
|
||||||
let mut builder = DeltaBuilder::new(self.len());
|
let mut builder = DeltaBuilder::new(self.len());
|
||||||
let mut interval_rope = Vec::new();
|
let mut interval_rope = Vec::new();
|
||||||
for (selection, content) in edits {
|
for (selection, content) in edits {
|
||||||
let rope = Rope::from(content);
|
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()));
|
interval_rope.push((region.min(), region.max(), rope.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,8 @@
|
||||||
use crossbeam_channel::{unbounded, Receiver, Sender};
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
||||||
use druid::{
|
use druid::{
|
||||||
piet::{PietText, PietTextLayout, Text, TextLayout, TextLayoutBuilder},
|
piet::{PietText, PietTextLayout, Text, TextLayout, TextLayoutBuilder},
|
||||||
theme, Command, Data, Env, EventCtx, ExtEventSink, FontFamily, Lens,
|
theme, Command, Data, Env, EventCtx, ExtEventSink, FontFamily, Lens, Point,
|
||||||
Point, Rect, Size, Target, Vec2, WidgetId, WindowId,
|
Rect, Size, Target, Vec2, WidgetId, WindowId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use lapce_rpc::{
|
use lapce_rpc::{
|
||||||
|
@ -1916,7 +1916,7 @@ pub fn document_format(
|
||||||
if !edits.is_empty() {
|
if !edits.is_empty() {
|
||||||
let buffer = self.open_files.get_mut(path).unwrap();
|
let buffer = self.open_files.get_mut(path).unwrap();
|
||||||
|
|
||||||
let edits: Vec<(Selection, String)> = edits
|
let edits: Vec<(Selection, &str)> = edits
|
||||||
.iter()
|
.iter()
|
||||||
.map(|edit| {
|
.map(|edit| {
|
||||||
let selection = Selection::region(
|
let selection = Selection::region(
|
||||||
|
@ -1929,20 +1929,11 @@ pub fn document_format(
|
||||||
config.editor.tab_width,
|
config.editor.tab_width,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
(selection, edit.new_text.clone())
|
(selection, edit.new_text.as_str())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
self.edit(
|
self.edit(path, &edits, EditType::Other, config);
|
||||||
path,
|
|
||||||
&edits.iter().map(|(s, c)| (s, c.as_str())).collect::<Vec<(
|
|
||||||
&Selection,
|
|
||||||
&str,
|
|
||||||
)>>(
|
|
||||||
),
|
|
||||||
EditType::Other,
|
|
||||||
config,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2042,7 +2033,7 @@ fn cursor_apply_delta(&mut self, path: &Path, delta: &RopeDelta) {
|
||||||
pub fn edit(
|
pub fn edit(
|
||||||
&mut self,
|
&mut self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
edits: &[(&Selection, &str)],
|
edits: &[(impl AsRef<Selection>, &str)],
|
||||||
edit_type: EditType,
|
edit_type: EditType,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
) -> Option<RopeDelta> {
|
) -> Option<RopeDelta> {
|
||||||
|
@ -2053,6 +2044,7 @@ pub fn edit(
|
||||||
let buffer_len = buffer.len();
|
let buffer_len = buffer.len();
|
||||||
let mut move_cursor = true;
|
let mut move_cursor = true;
|
||||||
for (selection, _) in edits.iter() {
|
for (selection, _) in edits.iter() {
|
||||||
|
let selection = selection.as_ref();
|
||||||
if selection.min_offset() == 0
|
if selection.min_offset() == 0
|
||||||
&& selection.max_offset() >= buffer_len - 1
|
&& selection.max_offset() >= buffer_len - 1
|
||||||
{
|
{
|
||||||
|
|
|
@ -502,6 +502,12 @@ pub struct Selection {
|
||||||
last_inserted: usize,
|
last_inserted: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<Selection> for Selection {
|
||||||
|
fn as_ref(&self) -> &Selection {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Selection {
|
impl Selection {
|
||||||
pub fn new() -> Selection {
|
pub fn new() -> Selection {
|
||||||
Selection {
|
Selection {
|
||||||
|
|
Loading…
Reference in New Issue