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

Added change_case command #441

Merged
merged 6 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl Command {
extend_till_prev_char,
extend_prev_char,
replace,
change_case,
page_up,
page_down,
half_page_up,
Expand Down Expand Up @@ -780,6 +781,35 @@ fn replace(cx: &mut Context) {
})
}

fn change_case(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let max_to = rope_end_without_line_ending(&doc.text().slice(..));
let to = std::cmp::min(max_to, range.to() + 1);
let text: String = RopeGraphemes::new(doc.text().slice(range.from()..to))
.map(|g| {
let mut buffer: String = String::with_capacity(g.len_bytes());
g.chars().for_each(|ch| {
if ch.is_lowercase() {
buffer.extend(ch.to_uppercase());
} else if ch.is_uppercase() {
buffer.extend(ch.to_lowercase());
} else {
buffer.push(ch);
}
});
buffer
})
.collect();

(range.from(), to, Some(text.into()))
luctius marked this conversation as resolved.
Show resolved Hide resolved
});

doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}

fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
use Direction::*;
let (view, doc) = current!(cx.editor);
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Default for Keymaps {
// and matching set for select mode (extend)
//
key!('r') => Command::replace,
key!('~') => Command::change_case,
luctius marked this conversation as resolved.
Show resolved Hide resolved
key!('R') => Command::replace_with_yanked,

key!(Home) => Command::goto_line_start,
Expand Down