Skip to content

Commit d174112

Browse files
committed
goto_file: try multiple relative paths to find existing files
1 parent 761f70d commit d174112

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

helix-term/src/commands.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,6 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
12661266
let text = doc.text();
12671267
let selections = doc.selection(view.id);
12681268
let primary = selections.primary();
1269-
let rel_path = doc
1270-
.relative_path()
1271-
.map(|path| path.parent().unwrap().to_path_buf())
1272-
.unwrap_or_default();
12731269

12741270
let paths: Vec<_> = if selections.len() == 1 && primary.len() == 1 {
12751271
// Secial case: if there is only one one-width selection, try to detect the
@@ -1323,14 +1319,35 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
13231319
.collect()
13241320
};
13251321

1322+
let doc_path = doc
1323+
.relative_path()
1324+
.map(|path| path.parent().unwrap().to_path_buf())
1325+
.unwrap_or_default();
1326+
1327+
let mut search_paths = vec![doc_path];
1328+
1329+
let cwd = helix_stdx::env::current_working_dir();
1330+
if cwd.exists() {
1331+
search_paths.push(cwd);
1332+
}
1333+
// FIXME: Also include project root? Or just use project root, not cwd?
1334+
13261335
for sel in paths {
13271336
if let Ok(url) = Url::parse(&sel) {
13281337
open_url(cx, url, action);
13291338
continue;
13301339
}
13311340

1332-
let path = expand_tilde(Cow::from(PathBuf::from(sel)));
1333-
let path = &rel_path.join(path);
1341+
let selpath = expand_tilde(Cow::from(PathBuf::from(sel)));
1342+
let paths: Vec<_> = search_paths.iter().map(|sp| sp.join(&selpath)).collect();
1343+
let existing: Vec<_> = paths.iter().filter(|p| p.exists()).collect();
1344+
1345+
let path = if !existing.is_empty() {
1346+
existing.first().unwrap()
1347+
} else {
1348+
paths.first().unwrap()
1349+
};
1350+
13341351
if path.is_dir() {
13351352
let picker = ui::file_picker(path.into(), &cx.editor.config());
13361353
cx.push_layer(Box::new(overlaid(picker)));

0 commit comments

Comments
 (0)