improve flip-bool

This commit is contained in:
Schrottkatze 2025-03-21 14:15:12 +01:00
parent 2da9d27e54
commit 07a58afaff
Signed by: schrottkatze
SSH key fingerprint: SHA256:FPOYVeBy3QP20FEM42uWF1Wa/Qhlk+L3S2+Wuau/Auo
2 changed files with 13 additions and 16 deletions

View file

@ -102,7 +102,7 @@
d = "@<C-w>vgd"; d = "@<C-w>vgd";
f = "@<C-w>vgy"; f = "@<C-w>vgy";
h = ":toggle-option lsp.display-inlay-hints"; h = ":toggle-option lsp.display-inlay-hints";
t = "@|flip-bool<ret>"; t = ":pipe flip-bool";
}; };
}; };
insert = { insert = {

View file

@ -4,7 +4,6 @@
#![feature(pattern)] #![feature(pattern)]
use std::{ use std::{
hint::black_box,
io::{Read, Write}, io::{Read, Write},
str::pattern::Pattern, str::pattern::Pattern,
}; };
@ -86,29 +85,27 @@ fn find_bools(input: &str) -> [[Vec<usize>; 2]; BOOL_COUNT] {
input input
.match_indices(it) .match_indices(it)
.filter_map(|it| { .filter_map(|it| {
let char_guard = |c: char| !(c.is_alphanumeric() || c.is_contained_in("-_")); fn char_guard(c: char) -> bool {
let mut allow = true; !(c.is_alphanumeric() || c.is_contained_in("-_"))
}
if it.0 > 0 { let last_idx = it.0 + it.1.len();
allow &= char_guard(
(it.0 > 0
&& last_idx < input.len()
&& char_guard(
input[it.1.floor_char_boundary(it.0 - 1)..it.0] input[it.1.floor_char_boundary(it.0 - 1)..it.0]
.chars() .chars()
.last() .last()
.unwrap(), .unwrap(),
); )
} && char_guard(
let last_idx = it.0 + it.1.len();
if last_idx < input.len() {
allow &= char_guard(
input[(last_idx)..(input.ceil_char_boundary(last_idx + 1))] input[(last_idx)..(input.ceil_char_boundary(last_idx + 1))]
.chars() .chars()
.last() .last()
.unwrap(), .unwrap(),
); ))
} .then_some(it.0)
allow.then_some(it.0)
}) })
.collect() .collect()
}) })