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

Improve goto_file_impl #9065

Merged
merged 23 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
eab5cbd
adding `shellexpand` and extend `surround_chars`
TornaxO7 Dec 12, 2023
0fdbc66
goto-file-path: adding test with ending character
TornaxO7 Dec 12, 2023
5f83d91
reformat code
TornaxO7 Dec 12, 2023
df34a6a
improving char collection for path
TornaxO7 Dec 13, 2023
264cd2b
add heuristic if cursor is on invalid char
TornaxO7 Dec 13, 2023
a88c86d
fix test
TornaxO7 Dec 13, 2023
1753c69
make clippy happy
TornaxO7 Dec 13, 2023
0da3262
removing `,` from the accepted file paths
TornaxO7 Dec 13, 2023
014a152
goto-file: improve head and tail detection
TornaxO7 Jan 11, 2024
63218c3
goto-file: remove one nesting
TornaxO7 Jan 11, 2024
1b9f5c4
goto-file: fix ci
TornaxO7 Jan 11, 2024
583d9aa
improve code format
TornaxO7 Feb 24, 2024
9bbccc5
Merge branch 'master' into feat/extend-goto-file
TornaxO7 Feb 24, 2024
f8063bb
removing `shellexpand` dependency
TornaxO7 Feb 24, 2024
7fbcbc0
reducing nested blocks
TornaxO7 Feb 24, 2024
b7bd427
removing unecessary clone
TornaxO7 Feb 24, 2024
a5cd316
allow numeric-values in path for goto-file
TornaxO7 Feb 24, 2024
c5c4c3e
add test to allow numeric values in filename for goto-file
TornaxO7 Feb 24, 2024
893c590
Merge branch 'master' of github.com:helix-editor/helix into feat/exte…
TornaxO7 Feb 27, 2024
6cce366
use `cfg` attribute instead of macro
TornaxO7 Feb 27, 2024
51dfd0a
removing saturating add
TornaxO7 Feb 27, 2024
de4fdbe
Merge branch 'master' of github.com:helix-editor/helix into feat/exte…
TornaxO7 Apr 6, 2024
366d792
goto-file: adjust valid chars
TornaxO7 Apr 6, 2024
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
67 changes: 48 additions & 19 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pub(crate) mod typed;

pub use dap::*;
use helix_event::status;
use helix_stdx::rope::{self, RopeSliceExt};
use helix_stdx::{
path::expand_tilde,
rope::{self, RopeSliceExt},
};
use helix_vcs::{FileChange, Hunk};
pub use lsp::*;
use tui::{
Expand Down Expand Up @@ -1196,25 +1199,51 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
let primary = selections.primary();
// 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(
current_word
.fragment(text_slice)
.trim_matches(surrounding_chars)
.to_string(),
);

let is_valid_path_char = |c: &char| {
#[cfg(target_os = "windows")]
let valid_chars = &[
'@', '/', '\\', '.', '-', '_', '+', '#', '$', '%', '{', '}', '[', ']', ':', '!',
'~', '=',
];
#[cfg(not(target_os = "windows"))]
let valid_chars = &['@', '/', '.', '-', '_', '+', '#', '$', '%', '~', '=', ':'];

valid_chars.contains(c) || c.is_alphabetic() || c.is_numeric()
};

let cursor_pos = primary.cursor(text.slice(..));
let pre_cursor_pos = cursor_pos.saturating_sub(1);
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
let post_cursor_pos = cursor_pos + 1;
let start_pos = if is_valid_path_char(&text.char(cursor_pos)) {
cursor_pos
} else if is_valid_path_char(&text.char(pre_cursor_pos)) {
pre_cursor_pos
} else {
post_cursor_pos
};

let prefix_len = text
.chars_at(start_pos)
.reversed()
.take_while(is_valid_path_char)
.count();

TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
let postfix_len = text
.chars_at(start_pos)
.take_while(is_valid_path_char)
.count();

let path: Cow<str> = text
.slice((start_pos - prefix_len)..(start_pos + postfix_len))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readers' context: rope slicing is by character index rather than byte index (like &str) which is why this uses chars_at(..).count() rather than a len of a substring

.into();
log::debug!("Goto file path: {}", path);

match expand_tilde(PathBuf::from(path.as_ref())).to_str() {
Some(path) => paths.push(path.to_string()),
None => cx.editor.set_error("Couldn't get string out of path."),
};
}

for sel in paths {
Expand Down
22 changes: 22 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ async fn test_goto_file_impl() -> anyhow::Result<()> {
)
.await?;

// ';' is behind the path
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?;

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

Ok(())
}

Expand Down
Loading