Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify paste command logic #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 29 additions & 47 deletions src/commands/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ pub fn redo(app: &mut Application) -> Result {
}

pub fn paste(app: &mut Application) -> Result {
ensure_trailing_newline(app)?;

let insert_below = match app.mode {
Mode::Select(_) | Mode::SelectLine(_) | Mode::Search(_) => {
commands::selection::delete(app).chain_err(|| {
Expand All @@ -640,49 +642,29 @@ pub fn paste(app: &mut Application) -> Result {
_ => true,
};

// TODO: Clean up duplicate buffer.insert(content.clone()) calls.
if let Some(buffer) = app.workspace.current_buffer() {
match *app.clipboard.get_content() {
ClipboardContent::Inline(ref content) => buffer.insert(content.clone()),
ClipboardContent::Block(ref content) => {
let original_cursor_position = *buffer.cursor.clone();
let line = original_cursor_position.line;

if insert_below {
buffer.cursor.move_to(Position {
line: line + 1,
offset: 0,
});

if *buffer.cursor == original_cursor_position {
// That didn't work because we're at the last line.
// Move to the end of the line to insert the data.
if let Some(line_content) = buffer.data().lines().nth(line) {
buffer.cursor.move_to(Position {
line,
offset: line_content.len(),
});
buffer.insert(format!("\n{}", content));
buffer.cursor.move_to(original_cursor_position);
} else {
// We're on a trailing newline, which doesn't
// have any data; just insert the content here.
buffer.insert(content.clone());
}
} else {
buffer.insert(content.clone());
}
} else {
buffer.insert(content.clone());
}
let buffer = app.workspace.current_buffer().ok_or(BUFFER_MISSING)?;

if let Some(content) = match app.clipboard.get_content() {
ClipboardContent::Inline(ref content) => Some(content.clone()),
ClipboardContent::Block(ref content) => {
let original_cursor_position = *buffer.cursor.clone();
let line = original_cursor_position.line;

if insert_below {
buffer.cursor.move_to(Position {
line: line + 1,
offset: 0,
});
}
ClipboardContent::None => (),

Some(content.clone())
}
} else {
bail!(BUFFER_MISSING);
ClipboardContent::None => None,
} {
buffer.insert(content);
}
commands::view::scroll_to_cursor(app)?;

commands::view::scroll_to_cursor(app)?;
Ok(())
}

Expand Down Expand Up @@ -1328,7 +1310,7 @@ mod tests {
fn paste_inserts_at_cursor_when_pasting_inline_data() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
buffer.insert("amp\neditor");
buffer.insert("amp\neditor\n");

// Now that we've set up the buffer, add it
// to the application, copy the first line to
Expand All @@ -1341,14 +1323,14 @@ mod tests {

// Ensure that the clipboard contents are pasted to the line below.
assert_eq!(app.workspace.current_buffer().unwrap().data(),
"aamp\neditor");
"aamp\neditor\n");
}

#[test]
fn paste_inserts_on_line_below_when_pasting_block_data() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
buffer.insert("amp\neditor");
buffer.insert("amp\neditor\n");
buffer.cursor.move_to(Position {
line: 0,
offset: 2,
Expand All @@ -1364,7 +1346,7 @@ mod tests {

// Ensure that the clipboard contents are pasted to the line below.
assert_eq!(app.workspace.current_buffer().unwrap().data(),
"amp\namp\neditor");
"amp\namp\neditor\n");
}

#[test]
Expand Down Expand Up @@ -1567,7 +1549,7 @@ mod tests {
fn paste_with_inline_content_replaces_selection() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
buffer.insert("amp");
buffer.insert("amp\n");
app.clipboard.set_content(ClipboardContent::Inline("editor".to_string())).unwrap();

// Now that we've set up the buffer, add it to
Expand All @@ -1578,7 +1560,7 @@ mod tests {
commands::buffer::paste(&mut app).unwrap();

// Ensure that the content is replaced
assert_eq!(app.workspace.current_buffer().unwrap().data(), "editor");
assert_eq!(app.workspace.current_buffer().unwrap().data(), "editor\n");

// TODO: Ensure that the operation is treated atomically.
// commands::buffer::undo(&mut app);
Expand All @@ -1589,7 +1571,7 @@ mod tests {
fn paste_with_block_content_replaces_selection() {
let mut app = Application::new(&Vec::new()).unwrap();
let mut buffer = Buffer::new();
buffer.insert("amp\neditor");
buffer.insert("amp\neditor\n");
app.clipboard.set_content(ClipboardContent::Block("paste amp\n".to_string())).unwrap();

// Now that we've set up the buffer, add it to
Expand All @@ -1600,7 +1582,7 @@ mod tests {

// Ensure that the content is replaced
assert_eq!(app.workspace.current_buffer().unwrap().data(),
"paste amp\neditor");
"paste amp\neditor\n");

// TODO: Ensure that the operation is treated atomically.
// commands::buffer::undo(&mut app);
Expand Down