From d9203097d942d572bec0dd6bd138cca4257bb948 Mon Sep 17 00:00:00 2001 From: Luv-Ray Date: Thu, 4 Apr 2024 22:03:38 +0800 Subject: [PATCH] Time pairs when `gf` --- helix-term/src/commands.rs | 26 +++++++++++++++++++------- helix-term/tests/test/commands.rs | 11 +++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b3614793ff18..1aa74040137c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1207,14 +1207,26 @@ fn goto_file_impl(cx: &mut Context, action: Action) { true, ); // Trims some surrounding chars so that the actual file is opened. - let surrounding_chars: &[_] = &['\'', '"', '(', ')']; + let surrounding_pairs: &[_] = &[ + ('\'', '\''), + ('"', '"'), + ('(', ')'), + ('[', ']'), + ('{', '}'), + ('<', '>'), + ]; + // Check pairs to prevent unnecessary trimming. + let current_word = current_word.fragment(text_slice); + let mut current_word = current_word.as_ref(); + for (l, r) in surrounding_pairs { + if current_word.starts_with(*l) && current_word.ends_with(*r) { + current_word = current_word.trim_start_matches(*l); + current_word = current_word.trim_end_matches(*r); + } + } + paths.clear(); - paths.push( - current_word - .fragment(text_slice) - .trim_matches(surrounding_chars) - .to_string(), - ); + paths.push(current_word.to_string()); } for sel in paths { diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index aaa442f36e18..77fc137608e1 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -153,6 +153,17 @@ async fn test_goto_file_impl() -> anyhow::Result<()> { ) .await?; + // Don't trim unmatched pairs + test_key_sequence( + &mut AppBuilder::new().with_file(file.path(), None).build()?, + Some("i)one.js)%gf"), + Some(&|app| { + assert_eq!(1, match_paths(app, vec![")one.js)"])); + }), + false, + ) + .await?; + Ok(()) }