Added change_case command (#441)

* Added change_case command

* Added switch_to_uppercase and switch_to_lowercase

Renamed change_case to switch_case.

* Updated the Keymap section of the Book

* Use flat_map instead of map + flatten

* Fix switch_to_uppercase using to_lowercase

* Switched 'Alt-`' to uppercase and '`' to lowercase

Co-authored-by: Cor <prive@corpeters.nl>
This commit is contained in:
Cor Peters 2021-07-16 18:12:59 +02:00 committed by GitHub
parent e2bcef718a
commit 722cfedb38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 20 deletions

View file

@ -41,26 +41,29 @@
### Changes ### Changes
| Key | Description | | Key | Description |
| ----- | ----------- | | ----- | ----------- |
| `r` | Replace with a character | | `r` | Replace with a character |
| `R` | Replace with yanked text | | `R` | Replace with yanked text |
| `i` | Insert before selection | | `~` | Switch case of the selected text |
| `a` | Insert after selection (append) | | `\`` | Set the selected text to upper case |
| `I` | Insert at the start of the line | | `Alt-\`` | Set the selected text to lower case |
| `A` | Insert at the end of the line | | `i` | Insert before selection |
| `o` | Open new line below selection | | `a` | Insert after selection (append) |
| `o` | Open new line above selection | | `I` | Insert at the start of the line |
| `u` | Undo change | | `A` | Insert at the end of the line |
| `U` | Redo change | | `o` | Open new line below selection |
| `y` | Yank selection | | `o` | Open new line above selection |
| `p` | Paste after selection | | `u` | Undo change |
| `P` | Paste before selection | | `U` | Redo change |
| `>` | Indent selection | | `y` | Yank selection |
| `<` | Unindent selection | | `p` | Paste after selection |
| `=` | Format selection | | `P` | Paste before selection |
| `d` | Delete selection | | `>` | Indent selection |
| `c` | Change selection (delete and enter insert mode) | | `<` | Unindent selection |
| `=` | Format selection |
| `d` | Delete selection |
| `c` | Change selection (delete and enter insert mode) |
### Selection manipulation ### Selection manipulation

View file

@ -186,6 +186,9 @@ impl Command {
extend_till_prev_char, extend_till_prev_char,
extend_prev_char, extend_prev_char,
replace, replace,
switch_case,
switch_to_uppercase,
switch_to_lowercase,
page_up, page_up,
page_down, page_down,
half_page_up, half_page_up,
@ -780,6 +783,57 @@ fn replace(cx: &mut Context) {
}) })
} }
fn switch_case(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range
.fragment(doc.text().slice(..))
.chars()
.flat_map(|ch| {
if ch.is_lowercase() {
ch.to_uppercase().collect()
} else if ch.is_uppercase() {
ch.to_lowercase().collect()
} else {
vec![ch]
}
})
.collect();
(range.from(), range.to() + 1, Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
fn switch_to_uppercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_uppercase().into();
(range.from(), range.to() + 1, Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
fn switch_to_lowercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_lowercase().into();
(range.from(), range.to() + 1, Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
fn scroll(cx: &mut Context, offset: usize, direction: Direction) { fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
use Direction::*; use Direction::*;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);

View file

@ -82,6 +82,10 @@ impl Default for Keymaps {
key!('r') => Command::replace, key!('r') => Command::replace,
key!('R') => Command::replace_with_yanked, key!('R') => Command::replace_with_yanked,
key!('~') => Command::switch_case,
alt!('`') => Command::switch_to_uppercase,
key!('`') => Command::switch_to_lowercase,
key!(Home) => Command::goto_line_start, key!(Home) => Command::goto_line_start,
key!(End) => Command::goto_line_end, key!(End) => Command::goto_line_end,