Skip to content

Commit

Permalink
insert double whitespace inside pair
Browse files Browse the repository at this point in the history
  • Loading branch information
dead10ck committed Jun 7, 2023
1 parent ace01e7 commit 8afdd40
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 3 deletions.
49 changes: 48 additions & 1 deletion helix-core/src/auto_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub fn hook_insert(
// && char_at pos == close
return handle_insert_close(doc, range, pair);
}
} else if ch.is_whitespace() {
return handle_insert_whitespace(doc, range, ch, pairs);
}

None
Expand All @@ -139,12 +141,33 @@ pub fn hook_delete(doc: &Rope, range: &Range, pairs: &AutoPairs) -> Option<(Dele

let cur = doc.get_char(cursor)?;
let prev = prev_char(doc, cursor)?;

// check for whitespace surrounding a pair
if doc.len_chars() >= 4 && prev.is_whitespace() && cur.is_whitespace() {
let second_prev = doc.get_char(graphemes::nth_prev_grapheme_boundary(text, cursor, 2))?;
let second_next = doc.get_char(graphemes::next_grapheme_boundary(text, cursor))?;
log::debug!("second_prev: {}, second_next: {}", second_prev, second_next);

if let Some(pair) = pairs.get(second_prev) {
if pair.open == second_prev && pair.close == second_next {
return handle_delete(doc, range);
}
}
}

let pair = pairs.get(cur)?;

if pair.open != prev {
if pair.open != prev || pair.close != cur {
return None;
}

handle_delete(doc, range)
}

pub fn handle_delete(doc: &Rope, range: &Range) -> Option<(Deletion, Range)> {
let text = doc.slice(..);
let cursor = range.cursor(text);

let end_next = graphemes::next_grapheme_boundary(text, cursor);
let end_prev = graphemes::prev_grapheme_boundary(text, cursor);

Expand All @@ -162,6 +185,30 @@ pub fn hook_delete(doc: &Rope, range: &Range, pairs: &AutoPairs) -> Option<(Dele
Some((delete, next_range))
}

fn handle_insert_whitespace(
doc: &Rope,
range: &Range,
ch: char,
pairs: &AutoPairs,
) -> Option<(Change, Range)> {
let text = doc.slice(..);
let cursor = range.cursor(text);
let cur = doc.get_char(cursor)?;
let prev = prev_char(doc, cursor)?;
let pair = pairs.get(cur)?;

if pair.open != prev || pair.close != cur {
return None;
}

let whitespace_pair = Pair {
open: ch,
close: ch,
};

handle_insert_same(doc, range, &whitespace_pair)
}

fn prev_char(doc: &Rope, pos: usize) -> Option<char> {
if pos == 0 {
return None;
Expand Down
220 changes: 218 additions & 2 deletions helix-term/tests/test/auto_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,119 @@ fn matching_pairs() -> impl Iterator<Item = &'static (char, char)> {
async fn insert_basic() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
format!("#[{}|]#", LINE_END),
helpers::platform_line("#[\n|]#"),
format!("i{}", pair.0),
format!("{}#[|{}]#{}", pair.0, pair.1, LINE_END),
helpers::platform_line(&format!("{}#[|{}]#", pair.0, pair.1)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn insert_whitespace() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
helpers::platform_line(&format!("{}#[|{}]#", pair.0, pair.1)),
"i ",
helpers::platform_line(&format!("{} #[| ]#{}", pair.0, pair.1)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn insert_whitespace_multi() -> anyhow::Result<()> {
for pair in differing_pairs() {
test((
helpers::platform_line(&format!(
indoc! {"\
{open}#[|{close}]#
{open}#(|{open})#{close}{close}
{open}{open}#(|{close}{close})#
foo#(|\n)#
"},
open = pair.0,
close = pair.1,
)),
"i ",
helpers::platform_line(&format!(
indoc! {"\
{open} #[| ]#{close}
{open} #(|{open})#{close}{close}
{open}{open} #(| {close}{close})#
foo #(|\n)#
"},
open = pair.0,
close = pair.1,
)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn append_whitespace_multi() -> anyhow::Result<()> {
for pair in differing_pairs() {
test((
helpers::platform_line(&format!(
indoc! {"\
#[|{open}]#{close}
#(|{open})#{open}{close}{close}
#(|{open}{open})#{close}{close}
#(|foo)#
"},
open = pair.0,
close = pair.1,
)),
"a ",
helpers::platform_line(&format!(
indoc! {"\
#[{open} |]#{close}
#({open} {open}|)#{close}{close}
#({open}{open} |)#{close}{close}
#(foo \n|)#
"},
open = pair.0,
close = pair.1,
)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn insert_whitespace_no_pair() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
// sanity check - do not insert extra whitespace unless immediately
// surrounded by a pair
test((
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.1)),
"i ",
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.1)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn insert_whitespace_no_matching_pair() -> anyhow::Result<()> {
for pair in differing_pairs() {
// sanity check - verify whitespace does not insert unless both pairs
// are matches, i.e. no two different openers
test((
helpers::platform_line(&format!("{}#[|{}]#", pair.0, pair.0)),
"i ",
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.0)),
))
.await?;
}
Expand Down Expand Up @@ -568,6 +678,112 @@ async fn delete_multi() -> anyhow::Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_whitespace() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
helpers::platform_line(&format!("{} #[| ]#{}", pair.0, pair.1)),
"i<backspace>",
helpers::platform_line(&format!("{}#[{}|]#", pair.0, pair.1)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_whitespace_multi() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
helpers::platform_line(&format!(
indoc! {"\
{open} #[| ]#{close}
{open} #(|{open})#{close}{close}
{open}{open} #(| {close}{close})#
foo #(|\n)#
"},
open = pair.0,
close = pair.1,
)),
"i<backspace>",
helpers::platform_line(&format!(
indoc! {"\
{open}#[{close}|]#
{open}#(|{open})#{close}{close}
{open}{open}#(|{close}{close})#
foo#(|\n)#
"},
open = pair.0,
close = pair.1,
)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_append_whitespace_multi() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
helpers::platform_line(&format!(
indoc! {"\
#[{open} |]# {close}
#({open} |)#{open}{close}{close}
#({open}{open} |)# {close}{close}
#(foo |)#
"},
open = pair.0,
close = pair.1,
)),
"a<backspace>",
helpers::platform_line(&format!(
indoc! {"\
#[{open}{close}|]#
#({open}{open}|)#{close}{close}
#({open}{open}{close}|)#{close}
#(foo\n|)#
"},
open = pair.0,
close = pair.1,
)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_whitespace_no_pair() -> anyhow::Result<()> {
for pair in DEFAULT_PAIRS {
test((
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.1)),
"i<backspace>",
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.1)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_whitespace_no_matching_pair() -> anyhow::Result<()> {
for pair in differing_pairs() {
test((
helpers::platform_line(&format!("{} #[|{}]#", pair.0, pair.0)),
"i<backspace>",
helpers::platform_line(&format!("{}#[|{}]#", pair.0, pair.0)),
))
.await?;
}

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_configured_multi_byte_chars() -> anyhow::Result<()> {
// NOTE: these are multi-byte Unicode characters
Expand Down

0 comments on commit 8afdd40

Please sign in to comment.