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

goto_file: Try multiple relative paths (including cwd) to find existing files #11859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
29 changes: 23 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,10 +1286,6 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
let text = doc.text();
let selections = doc.selection(view.id);
let primary = selections.primary();
let rel_path = doc
.relative_path()
.map(|path| path.parent().unwrap().to_path_buf())
.unwrap_or_default();

let paths: Vec<_> = if selections.len() == 1 && primary.len() == 1 {
let mut pos = primary.cursor(text.slice(..));
Expand Down Expand Up @@ -1320,14 +1316,35 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
.collect()
};

let doc_path = doc
.relative_path()
.map(|path| path.parent().unwrap().to_path_buf())
.unwrap_or_default();

let mut search_paths = vec![doc_path];

let cwd = helix_stdx::env::current_working_dir();
if cwd.exists() {
search_paths.push(cwd);
}
// FIXME: Also include project root? Or just use project root, not cwd?
Copy link
Author

Choose a reason for hiding this comment

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

I could not quickly figure out how to find the LSP root for the document, but if available, this may be a better (or at least a valid additional) choice.


for sel in paths {
if let Ok(url) = Url::parse(&sel) {
open_url(cx, url, action);
continue;
}

let path = path::expand(&sel);
let path = &rel_path.join(path);
let selpath = path::expand(&sel);
let paths: Vec<_> = search_paths.iter().map(|sp| sp.join(&selpath)).collect();
let existing: Vec<_> = paths.iter().filter(|p| p.exists()).collect();

let path = if !existing.is_empty() {
existing.first().unwrap()
} else {
paths.first().unwrap()
};

if path.is_dir() {
let picker = ui::file_picker(path.into(), &cx.editor.config());
cx.push_layer(Box::new(overlaid(picker)));
Expand Down
Loading