mirror of https://github.com/lapce/lapce.git
Clean up insert_new_line
This commit is contained in:
parent
05da4b7c80
commit
75289540ab
|
@ -261,8 +261,7 @@ fn insert_new_line(
|
||||||
cursor: &mut Cursor,
|
cursor: &mut Cursor,
|
||||||
selection: Selection,
|
selection: Selection,
|
||||||
) -> Vec<(RopeDelta, InvalLines)> {
|
) -> Vec<(RopeDelta, InvalLines)> {
|
||||||
let mut deltas = Vec::new();
|
let mut edits = Vec::with_capacity(selection.regions().len());
|
||||||
let mut edits = Vec::new();
|
|
||||||
let mut extra_edits = Vec::new();
|
let mut extra_edits = Vec::new();
|
||||||
let mut shift = 0i32;
|
let mut shift = 0i32;
|
||||||
for region in selection.regions() {
|
for region in selection.regions() {
|
||||||
|
@ -271,46 +270,47 @@ fn insert_new_line(
|
||||||
let line_start = buffer.offset_of_line(line);
|
let line_start = buffer.offset_of_line(line);
|
||||||
let line_end = buffer.line_end_offset(line, true);
|
let line_end = buffer.line_end_offset(line, true);
|
||||||
let line_indent = buffer.indent_on_line(line);
|
let line_indent = buffer.indent_on_line(line);
|
||||||
let first_half = buffer.slice_to_cow(line_start..offset).to_string();
|
let first_half = buffer.slice_to_cow(line_start..offset);
|
||||||
let second_half = buffer.slice_to_cow(offset..line_end).to_string();
|
let second_half = buffer.slice_to_cow(offset..line_end);
|
||||||
|
let second_half = second_half.trim();
|
||||||
|
|
||||||
let indent = if has_unmatched_pair(&first_half) {
|
let new_line_content = {
|
||||||
format!("{}{}", line_indent, buffer.indent_unit())
|
let indent_storage;
|
||||||
} else if second_half.trim().is_empty() {
|
let indent = if has_unmatched_pair(&first_half) {
|
||||||
let next_line_indent = buffer.indent_on_line(line + 1);
|
indent_storage =
|
||||||
if next_line_indent.len() > line_indent.len() {
|
format!("{}{}", line_indent, buffer.indent_unit());
|
||||||
next_line_indent
|
&indent_storage
|
||||||
|
} else if second_half.is_empty() {
|
||||||
|
indent_storage = buffer.indent_on_line(line + 1);
|
||||||
|
if indent_storage.len() > line_indent.len() {
|
||||||
|
&indent_storage
|
||||||
|
} else {
|
||||||
|
&line_indent
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
line_indent.clone()
|
&line_indent
|
||||||
}
|
};
|
||||||
} else {
|
format!("\n{indent}")
|
||||||
line_indent.clone()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let selection = Selection::region(region.min(), region.max());
|
let selection = Selection::region(region.min(), region.max());
|
||||||
let content = format!("{}{}", "\n", indent);
|
|
||||||
|
|
||||||
shift -= (region.max() - region.min()) as i32;
|
shift -= (region.max() - region.min()) as i32;
|
||||||
shift += content.len() as i32;
|
shift += new_line_content.len() as i32;
|
||||||
|
|
||||||
edits.push((selection, content));
|
edits.push((selection, new_line_content));
|
||||||
|
|
||||||
for c in first_half.chars().rev() {
|
if let Some(c) = first_half.chars().rev().find(|&c| c != ' ') {
|
||||||
if c != ' ' {
|
if let Some(true) = matching_pair_direction(c) {
|
||||||
if let Some(pair_start) = matching_pair_direction(c) {
|
if let Some(c) = matching_char(c) {
|
||||||
if pair_start {
|
if second_half.starts_with(c) {
|
||||||
if let Some(c) = matching_char(c) {
|
let selection = Selection::caret(
|
||||||
if second_half.trim().starts_with(&c.to_string()) {
|
(region.max() as i32 + shift) as usize,
|
||||||
let selection = Selection::caret(
|
);
|
||||||
(region.max() as i32 + shift) as usize,
|
let content = format!("\n{line_indent}");
|
||||||
);
|
extra_edits.push((selection, content));
|
||||||
let content = format!("{}{}", "\n", line_indent);
|
|
||||||
extra_edits.push((selection.clone(), content));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,17 +318,18 @@ fn insert_new_line(
|
||||||
let edits = edits
|
let edits = edits
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(selection, s)| (selection, s.as_str()))
|
.map(|(selection, s)| (selection, s.as_str()))
|
||||||
.collect::<Vec<(&Selection, &str)>>();
|
.collect::<Vec<_>>();
|
||||||
let (delta, inval_lines) = buffer.edit(&edits, EditType::InsertNewline);
|
let (delta, inval_lines) = buffer.edit(&edits, EditType::InsertNewline);
|
||||||
let mut selection =
|
let mut selection =
|
||||||
selection.apply_delta(&delta, true, InsertDrift::Default);
|
selection.apply_delta(&delta, true, InsertDrift::Default);
|
||||||
deltas.push((delta, inval_lines));
|
|
||||||
|
let mut deltas = vec![(delta, inval_lines)];
|
||||||
|
|
||||||
if !extra_edits.is_empty() {
|
if !extra_edits.is_empty() {
|
||||||
let edits = extra_edits
|
let edits = extra_edits
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(selection, s)| (selection, s.as_str()))
|
.map(|(selection, s)| (selection, s.as_str()))
|
||||||
.collect::<Vec<(&Selection, &str)>>();
|
.collect::<Vec<_>>();
|
||||||
let (delta, inval_lines) = buffer.edit(&edits, EditType::InsertNewline);
|
let (delta, inval_lines) = buffer.edit(&edits, EditType::InsertNewline);
|
||||||
selection = selection.apply_delta(&delta, false, InsertDrift::Default);
|
selection = selection.apply_delta(&delta, false, InsertDrift::Default);
|
||||||
deltas.push((delta, inval_lines));
|
deltas.push((delta, inval_lines));
|
||||||
|
|
Loading…
Reference in New Issue