jmacdonald / amp

A complete text editor for your terminal.
https://amp.rs
Other
3.67k stars 105 forks source link

simplify paste command logic #245

Open oldaccountdeadname opened 2 years ago

oldaccountdeadname commented 2 years ago

Just shortening a match statement!

Commit message below:


The previous big match statement did three separate things in the same deeply indented code block:

  1. Prepare the buffer
  2. Identify the content to paste
  3. Paste the proper content

Preparing the buffer was simplified by just calling ensure_trailing_newline beforehand, as that's essentially what the special cases deep inside the match statement were doing anyway. This is done indiscriminately due to the borrow checker, which wouldn't like us manipulating the buffer to add a newline while we held an immutable reference to it in buffer. This has the unfortunate-ish side effect of requiring modifications to the tests to account for the fact that a trailing newline is added.

Separating 2 and 3 allows us to refocus the match statement on identification, and, if there is content, we can paste later down the function.

1 and 2 are still homogeneous in the match statement, which isn't super pretty, but, as they both depend on the same data (and conditionals thereof), it's not too big a deal.