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

Toggle boolean values using increment/decrement shortcuts #11746

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions helix-core/src/increment/boolean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// Toggle a boolean value
pub fn increment(selected_text: &str, _amount: i64) -> Option<String> {
match selected_text.trim() {
// Common boolean values
"true" => Some(String::from("false")),
"false" => Some(String::from("true")),

// Python, Haskell
"True" => Some(String::from("False")),
"False" => Some(String::from("True")),

// R, COBOL
"TRUE" => Some(String::from("FALSE")),
"FALSE" => Some(String::from("TRUE")),

// Scheme
"#t" => Some(String::from("#f")),
"#f" => Some(String::from("#t")),

_ => None,
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_boolean_toggles() {
let tests = [
// true/false
("true", 1, "false"),
("true", -1, "false"),
("false", 1, "true"),
("false", -1, "true"),
// True/False
("True", 1, "False"),
("True", -1, "False"),
("False", 1, "True"),
("False", -1, "True"),
// TRUE/FALSE
("TRUE", 1, "FALSE"),
("TRUE", -1, "FALSE"),
("FALSE", 1, "TRUE"),
("FALSE", -1, "TRUE"),
// #t/#f
("#t", 1, "#f"),
("#t", -1, "#f"),
("#f", 1, "#t"),
("#f", -1, "#t"),
];
for (original, amount, expected) in tests {
assert_eq!(increment(original, amount).unwrap(), expected);
}
}
}
5 changes: 5 additions & 0 deletions helix-core/src/increment/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod date_time;
mod integer;
mod boolean;

pub fn integer(selected_text: &str, amount: i64) -> Option<String> {
integer::increment(selected_text, amount)
Expand All @@ -8,3 +9,7 @@ pub fn integer(selected_text: &str, amount: i64) -> Option<String> {
pub fn date_time(selected_text: &str, amount: i64) -> Option<String> {
date_time::increment(selected_text, amount)
}

pub fn boolean(selected_text: &str, amount: i64) -> Option<String> {
boolean::increment(selected_text, amount)
}
2 changes: 1 addition & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6005,7 +6005,7 @@ fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
for range in selection {
let selected_text: Cow<str> = range.fragment(text);
let new_from = ((range.from() as i128) + cumulative_length_diff) as usize;
let incremented = [increment::integer, increment::date_time]
let incremented = [increment::integer, increment::date_time, increment::boolean]
.iter()
.find_map(|incrementor| incrementor(selected_text.as_ref(), amount));

Expand Down