Skip to content

Commit

Permalink
Trim quotes and braces from paths in goto_file_impl (helix-editor#4370)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
  • Loading branch information
2 people authored and andreytkachenko committed Oct 28, 2022
1 parent 55fe542 commit a2cf09e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
23 changes: 17 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ fn goto_file_vsplit(cx: &mut Context) {
goto_file_impl(cx, Action::VerticalSplit);
}

/// Goto files in selection.
fn goto_file_impl(cx: &mut Context, action: Action) {
let (view, doc) = current_ref!(cx.editor);
let text = doc.text();
Expand All @@ -1032,15 +1033,25 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
.map(|r| text.slice(r.from()..r.to()).to_string())
.collect();
let primary = selections.primary();
if selections.len() == 1 && primary.to() - primary.from() == 1 {
let current_word = movement::move_next_long_word_start(
text.slice(..),
movement::move_prev_long_word_start(text.slice(..), primary, 1),
1,
// Checks whether there is only one selection with a width of 1
if selections.len() == 1 && primary.len() == 1 {
let count = cx.count();
let text_slice = text.slice(..);
// In this case it selects the WORD under the cursor
let current_word = textobject::textobject_word(
text_slice,
primary,
textobject::TextObject::Inside,
count,
true,
);
// Trims some surrounding chars so that the actual file is opened.
let surrounding_chars: &[_] = &['\'', '"', '(', ')'];
paths.clear();
paths.push(
text.slice(current_word.from()..current_word.to())
current_word
.fragment(text_slice)
.trim_matches(surrounding_chars)
.to_string(),
);
}
Expand Down
60 changes: 60 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ops::RangeInclusive;

use helix_core::diagnostic::Severity;
use helix_term::application::Application;

use super::*;

Expand Down Expand Up @@ -133,3 +134,62 @@ async fn test_selection_duplication() -> anyhow::Result<()> {
.await?;
Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_goto_file_impl() -> anyhow::Result<()> {
let file = tempfile::NamedTempFile::new()?;

fn match_paths(app: &Application, matches: Vec<&str>) -> usize {
app.editor
.documents()
.filter_map(|d| d.path()?.file_name())
.filter(|n| matches.iter().any(|m| *m == n.to_string_lossy()))
.count()
}

// Single selection
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("ione.js<esc>%gf"),
Some(&|app| {
assert_eq!(1, match_paths(app, vec!["one.js"]));
}),
false,
)
.await?;

// Multiple selection
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("ione.js<ret>two.js<esc>%<A-s>gf"),
Some(&|app| {
assert_eq!(2, match_paths(app, vec!["one.js", "two.js"]));
}),
false,
)
.await?;

// Cursor on first quote
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("iimport 'one.js'<esc>B;gf"),
Some(&|app| {
assert_eq!(1, match_paths(app, vec!["one.js"]));
}),
false,
)
.await?;

// Cursor on last quote
test_key_sequence(
&mut AppBuilder::new().with_file(file.path(), None).build()?,
Some("iimport 'one.js'<esc>bgf"),
Some(&|app| {
assert_eq!(1, match_paths(app, vec!["one.js"]));
}),
false,
)
.await?;

Ok(())
}

0 comments on commit a2cf09e

Please sign in to comment.