Add newline above and improve newline below
Add a new action for inserting a new line above the current line. @ForLoveOfCats also helped fix a bug among other things. When two collaborators had their cursors at the end of a line, and one collaborator performed a newline below action, the second collaborator's cursor would be dragged to the new line. This is also fixing that. Co-Authored-By: Julia <30666851+ForLoveOfCats@users.noreply.github.com>
This commit is contained in:
parent
c5e56a5e45
commit
f9c60b98c0
7 changed files with 264 additions and 17 deletions
|
@ -184,6 +184,7 @@ actions!(
|
|||
Backspace,
|
||||
Delete,
|
||||
Newline,
|
||||
NewlineAbove,
|
||||
NewlineBelow,
|
||||
GoToDiagnostic,
|
||||
GoToPrevDiagnostic,
|
||||
|
@ -301,6 +302,7 @@ pub fn init(cx: &mut AppContext) {
|
|||
cx.add_action(Editor::select);
|
||||
cx.add_action(Editor::cancel);
|
||||
cx.add_action(Editor::newline);
|
||||
cx.add_action(Editor::newline_above);
|
||||
cx.add_action(Editor::newline_below);
|
||||
cx.add_action(Editor::backspace);
|
||||
cx.add_action(Editor::delete);
|
||||
|
@ -2118,7 +2120,7 @@ impl Editor {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn newline_below(&mut self, _: &NewlineBelow, cx: &mut ViewContext<Self>) {
|
||||
pub fn newline_above(&mut self, _: &NewlineAbove, cx: &mut ViewContext<Self>) {
|
||||
let buffer = self.buffer.read(cx);
|
||||
let snapshot = buffer.snapshot(cx);
|
||||
|
||||
|
@ -2130,19 +2132,17 @@ impl Editor {
|
|||
let cursor = selection.head();
|
||||
let row = cursor.row;
|
||||
|
||||
let end_of_line = snapshot
|
||||
.clip_point(Point::new(row, snapshot.line_len(row)), Bias::Left)
|
||||
.to_point(&snapshot);
|
||||
let start_of_line = snapshot.clip_point(Point::new(row, 0), Bias::Left);
|
||||
|
||||
let newline = "\n".to_string();
|
||||
edits.push((end_of_line..end_of_line, newline));
|
||||
edits.push((start_of_line..start_of_line, newline));
|
||||
|
||||
rows_inserted += 1;
|
||||
rows.push(row + rows_inserted);
|
||||
rows_inserted += 1;
|
||||
}
|
||||
|
||||
self.transact(cx, |editor, cx| {
|
||||
editor.edit_with_autoindent(edits, cx);
|
||||
editor.edit(edits, cx);
|
||||
|
||||
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||
let mut index = 0;
|
||||
|
@ -2157,6 +2157,85 @@ impl Editor {
|
|||
(clipped, SelectionGoal::None)
|
||||
});
|
||||
});
|
||||
|
||||
let mut indent_edits = Vec::new();
|
||||
let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
|
||||
for row in rows {
|
||||
let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
|
||||
for (row, indent) in indents {
|
||||
if indent.len == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let text = match indent.kind {
|
||||
IndentKind::Space => " ".repeat(indent.len as usize),
|
||||
IndentKind::Tab => "\t".repeat(indent.len as usize),
|
||||
};
|
||||
let point = Point::new(row, 0);
|
||||
indent_edits.push((point..point, text));
|
||||
}
|
||||
}
|
||||
editor.edit(indent_edits, cx);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn newline_below(&mut self, _: &NewlineBelow, cx: &mut ViewContext<Self>) {
|
||||
let buffer = self.buffer.read(cx);
|
||||
let snapshot = buffer.snapshot(cx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
let mut rows = Vec::new();
|
||||
let mut rows_inserted = 0;
|
||||
|
||||
for selection in self.selections.all_adjusted(cx) {
|
||||
let cursor = selection.head();
|
||||
let row = cursor.row;
|
||||
|
||||
let point = Point::new(row + 1, 0);
|
||||
let start_of_line = snapshot.clip_point(point, Bias::Left);
|
||||
|
||||
let newline = "\n".to_string();
|
||||
edits.push((start_of_line..start_of_line, newline));
|
||||
|
||||
rows_inserted += 1;
|
||||
rows.push(row + rows_inserted);
|
||||
}
|
||||
|
||||
self.transact(cx, |editor, cx| {
|
||||
editor.edit(edits, cx);
|
||||
|
||||
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||
let mut index = 0;
|
||||
s.move_cursors_with(|map, _, _| {
|
||||
let row = rows[index];
|
||||
index += 1;
|
||||
|
||||
let point = Point::new(row, 0);
|
||||
let boundary = map.next_line_boundary(point).1;
|
||||
let clipped = map.clip_point(boundary, Bias::Left);
|
||||
|
||||
(clipped, SelectionGoal::None)
|
||||
});
|
||||
});
|
||||
|
||||
let mut indent_edits = Vec::new();
|
||||
let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
|
||||
for row in rows {
|
||||
let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
|
||||
for (row, indent) in indents {
|
||||
if indent.len == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let text = match indent.kind {
|
||||
IndentKind::Space => " ".repeat(indent.len as usize),
|
||||
IndentKind::Tab => "\t".repeat(indent.len as usize),
|
||||
};
|
||||
let point = Point::new(row, 0);
|
||||
indent_edits.push((point..point, text));
|
||||
}
|
||||
}
|
||||
editor.edit(indent_edits, cx);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1467,6 +1467,55 @@ fn test_newline_with_old_selections(cx: &mut gpui::AppContext) {
|
|||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_newline_above(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = EditorTestContext::new(cx);
|
||||
cx.update(|cx| {
|
||||
cx.update_global::<Settings, _, _>(|settings, _| {
|
||||
settings.editor_overrides.tab_size = Some(NonZeroU32::new(4).unwrap());
|
||||
});
|
||||
});
|
||||
|
||||
let language = Arc::new(
|
||||
Language::new(
|
||||
LanguageConfig::default(),
|
||||
Some(tree_sitter_rust::language()),
|
||||
)
|
||||
.with_indents_query(r#"(_ "(" ")" @end) @indent"#)
|
||||
.unwrap(),
|
||||
);
|
||||
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
|
||||
|
||||
cx.set_state(indoc! {"
|
||||
const a: ˇA = (
|
||||
(ˇ
|
||||
«const_functionˇ»(ˇ),
|
||||
so«mˇ»et«hˇ»ing_ˇelse,ˇ
|
||||
)ˇ
|
||||
ˇ);ˇ
|
||||
"});
|
||||
cx.update_editor(|e, cx| e.newline_above(&NewlineAbove, cx));
|
||||
cx.assert_editor_state(indoc! {"
|
||||
ˇ
|
||||
const a: A = (
|
||||
ˇ
|
||||
(
|
||||
ˇ
|
||||
ˇ
|
||||
const_function(),
|
||||
ˇ
|
||||
ˇ
|
||||
ˇ
|
||||
ˇ
|
||||
something_else,
|
||||
ˇ
|
||||
)
|
||||
ˇ
|
||||
ˇ
|
||||
);
|
||||
"});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_newline_below(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx = EditorTestContext::new(cx);
|
||||
|
|
|
@ -167,7 +167,7 @@ impl<'a> EditorTestContext<'a> {
|
|||
///
|
||||
/// See the `util::test::marked_text_ranges` function for more information.
|
||||
pub fn set_state(&mut self, marked_text: &str) -> ContextHandle {
|
||||
let _state_context = self.add_assertion_context(format!(
|
||||
let state_context = self.add_assertion_context(format!(
|
||||
"Initial Editor State: \"{}\"",
|
||||
marked_text.escape_debug().to_string()
|
||||
));
|
||||
|
@ -178,7 +178,22 @@ impl<'a> EditorTestContext<'a> {
|
|||
s.select_ranges(selection_ranges)
|
||||
})
|
||||
});
|
||||
_state_context
|
||||
state_context
|
||||
}
|
||||
|
||||
/// Only change the editor's selections
|
||||
pub fn set_selections_state(&mut self, marked_text: &str) -> ContextHandle {
|
||||
let state_context = self.add_assertion_context(format!(
|
||||
"Initial Editor State: \"{}\"",
|
||||
marked_text.escape_debug().to_string()
|
||||
));
|
||||
let (_, selection_ranges) = marked_text_ranges(marked_text, true);
|
||||
self.editor.update(self.cx, |editor, cx| {
|
||||
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||
s.select_ranges(selection_ranges)
|
||||
})
|
||||
});
|
||||
state_context
|
||||
}
|
||||
|
||||
/// Make an assertion about the editor's text and the ranges and directions
|
||||
|
@ -189,10 +204,11 @@ impl<'a> EditorTestContext<'a> {
|
|||
pub fn assert_editor_state(&mut self, marked_text: &str) {
|
||||
let (unmarked_text, expected_selections) = marked_text_ranges(marked_text, true);
|
||||
let buffer_text = self.buffer_text();
|
||||
assert_eq!(
|
||||
buffer_text, unmarked_text,
|
||||
"Unmarked text doesn't match buffer text"
|
||||
);
|
||||
|
||||
if buffer_text != unmarked_text {
|
||||
panic!("Unmarked text doesn't match buffer text\nBuffer text: {buffer_text:?}\nUnmarked text: {unmarked_text:?}\nRaw buffer text\n{buffer_text}Raw unmarked text\n{unmarked_text}");
|
||||
}
|
||||
|
||||
self.assert_selections(expected_selections, marked_text.to_string())
|
||||
}
|
||||
|
||||
|
@ -254,10 +270,10 @@ impl<'a> EditorTestContext<'a> {
|
|||
panic!(
|
||||
indoc! {"
|
||||
{}Editor has unexpected selections.
|
||||
|
||||
|
||||
Expected selections:
|
||||
{}
|
||||
|
||||
|
||||
Actual selections:
|
||||
{}
|
||||
"},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue