on master help sign in
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>

commit722cfedb382abf2a4e48ac45bdfd9b3c5de50a58 authorCor Peters <luctius@users.noreply.github.com> dateFri 16 Jul 2021, 18:12 (5 years ago) parente2bcef7
3 files changed+81−20
book/src/keymap.md+23−20 helix-term/src/commands.rs+54 helix-term/src/keymap.rs+4
book/src/keymap.md+23−20
@@ -41,26 +41,29 @@
4141
4242### Changes
4343
44−| Key | Description |
45−| ----- | ----------- |
46−| `r` | Replace with a character |
47−| `R` | Replace with yanked text |
48−| `i` | Insert before selection |
49−| `a` | Insert after selection (append) |
50−| `I` | Insert at the start of the line |
51−| `A` | Insert at the end of the line |
52−| `o` | Open new line below selection |
53−| `o` | Open new line above selection |
54−| `u` | Undo change |
55−| `U` | Redo change |
56−| `y` | Yank selection |
57−| `p` | Paste after selection |
58−| `P` | Paste before selection |
59−| `>` | Indent selection |
60−| `<` | Unindent selection |
61−| `=` | Format selection |
62−| `d` | Delete selection |
63−| `c` | Change selection (delete and enter insert mode) |
44+| Key | Description |
45+| ----- | ----------- |
46+| `r` | Replace with a character |
47+| `R` | Replace with yanked text |
48+| `~` | Switch case of the selected text |
49+| `\`` | Set the selected text to upper case |
50+| `Alt-\`` | Set the selected text to lower case |
51+| `i` | Insert before selection |
52+| `a` | Insert after selection (append) |
53+| `I` | Insert at the start of the line |
54+| `A` | Insert at the end of the line |
55+| `o` | Open new line below selection |
56+| `o` | Open new line above selection |
57+| `u` | Undo change |
58+| `U` | Redo change |
59+| `y` | Yank selection |
60+| `p` | Paste after selection |
61+| `P` | Paste before selection |
62+| `>` | Indent selection |
63+| `<` | Unindent selection |
64+| `=` | Format selection |
65+| `d` | Delete selection |
66+| `c` | Change selection (delete and enter insert mode) |
6467
6568### Selection manipulation
6669
helix-term/src/commands.rs+54
@@ -186,6 +186,9 @@ impl Command {
186186 extend_till_prev_char,
187187 extend_prev_char,
188188 replace,
189+ switch_case,
190+ switch_to_uppercase,
191+ switch_to_lowercase,
189192 page_up,
190193 page_down,
191194 half_page_up,
@@ -780,6 +783,57 @@ fn replace(cx: &mut Context) {
780783 })
781784}
782785
786+fn switch_case(cx: &mut Context) {
787+ let (view, doc) = current!(cx.editor);
788+ let transaction =
789+ Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
790+ let text: Tendril = range
791+ .fragment(doc.text().slice(..))
792+ .chars()
793+ .flat_map(|ch| {
794+ if ch.is_lowercase() {
795+ ch.to_uppercase().collect()
796+ } else if ch.is_uppercase() {
797+ ch.to_lowercase().collect()
798+ } else {
799+ vec![ch]
800+ }
801+ })
802+ .collect();
803+
804+ (range.from(), range.to() + 1, Some(text))
805+ });
806+
807+ doc.apply(&transaction, view.id);
808+ doc.append_changes_to_history(view.id);
809+}
810+
811+fn switch_to_uppercase(cx: &mut Context) {
812+ let (view, doc) = current!(cx.editor);
813+ let transaction =
814+ Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
815+ let text: Tendril = range.fragment(doc.text().slice(..)).to_uppercase().into();
816+
817+ (range.from(), range.to() + 1, Some(text))
818+ });
819+
820+ doc.apply(&transaction, view.id);
821+ doc.append_changes_to_history(view.id);
822+}
823+
824+fn switch_to_lowercase(cx: &mut Context) {
825+ let (view, doc) = current!(cx.editor);
826+ let transaction =
827+ Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
828+ let text: Tendril = range.fragment(doc.text().slice(..)).to_lowercase().into();
829+
830+ (range.from(), range.to() + 1, Some(text))
831+ });
832+
833+ doc.apply(&transaction, view.id);
834+ doc.append_changes_to_history(view.id);
835+}
836+
783837fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
784838 use Direction::*;
785839 let (view, doc) = current!(cx.editor);
helix-term/src/keymap.rs+4
@@ -82,6 +82,10 @@ impl Default for Keymaps {
8282 key!('r') => Command::replace,
8383 key!('R') => Command::replace_with_yanked,
8484
85+ key!('~') => Command::switch_case,
86+ alt!('`') => Command::switch_to_uppercase,
87+ key!('`') => Command::switch_to_lowercase,
88+
8589 key!(Home) => Command::goto_line_start,
8690 key!(End) => Command::goto_line_end,
8791