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 11 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
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ serde = { version = "1.0", features = ["derive"] }
grep-regex = "0.1.12"
grep-searcher = "0.1.13"

[target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
# expand env-variables in paths
shellexpand = "3.1"

[target.'cfg(not(windows))'.dependencies] # https://github.com/vorner/signal-hook/issues/100
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
libc = "0.2.150"

Expand Down
66 changes: 49 additions & 17 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,25 +1176,57 @@ 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| {
let valid_chars: &[char] = if cfg!(target_os = "windows") {
&[
'@', '/', '\\', '.', '-', '_', '+', '#', '$', '%', '{', '}', '[', ']', ':',
'!', '~', '=',
]
} else {
&['@', '/', '.', '-', '_', '+', '#', '$', '%', '~', '=']
};
valid_chars.contains(c) || c.is_alphabetic()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked that up from neovim. See :h isf in neovim for the list of chars.

};

let start_pos = {
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
let cursor_pos = primary.cursor(text.slice(..));
let pre_cursor_pos = cursor_pos.saturating_sub(1);
let post_cursor_pos = cursor_pos.saturating_add(1);

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)
.clone()
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
.reversed()
.take_while(is_valid_path_char)
.count();

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

let path = Cow::Borrowed(
text.slice((start_pos - prefix_len)..(start_pos + postfix_len))
.as_str()
.unwrap(),
);
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
log::debug!("Goto file path: {}", path);

match shellexpand::full(&path) {
TornaxO7 marked this conversation as resolved.
Show resolved Hide resolved
Ok(path) => paths.push(path.to_string()),
Err(e) => cx.editor.set_error(format!("{}", e)),
};
}

for sel in paths {
Expand Down
11 changes: 11 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ 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?;

Ok(())
}

Expand Down
Loading