Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions app/src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,8 @@ enum OpenFileAction {

/// Pure routing decision for `open_file`. Extracted so it can be unit-tested without
/// standing up a full `AppContext`.
fn classify_open_file_action(path: &Path) -> OpenFileAction {
if is_markdown_file(path) {
fn classify_open_file_action(path: &Path, prefer_markdown_viewer: bool) -> OpenFileAction {
if is_markdown_file(path) && prefer_markdown_viewer {
OpenFileAction::Notebook
} else if is_runnable_shell_script(path) {
OpenFileAction::ExecuteInSession
Expand All @@ -1243,7 +1243,7 @@ fn can_open_file_editor_path(path: &Path) -> bool {
}

/// Handle an incoming `file://` URL.
/// * Markdown files are opened as notebook panes.
/// * Markdown files are opened as notebook panes when the viewer preference is enabled.
/// * For directories, open a new session at the directory path.
/// * For other files, open a new session at the parent directory path, then possibly execute the
/// file.
Expand All @@ -1253,7 +1253,15 @@ fn open_file(window_id: Option<WindowId>, path: PathBuf, ctx: &mut AppContext) {
.map(|view_id| (window_id, view_id))
});

let action = classify_open_file_action(&path);
#[cfg(feature = "local_fs")]
let prefer_markdown_viewer = {
use crate::util::file::external_editor::EditorSettings;
*EditorSettings::as_ref(ctx).prefer_markdown_viewer
};
#[cfg(not(feature = "local_fs"))]
let prefer_markdown_viewer = true;

let action = classify_open_file_action(&path, prefer_markdown_viewer);

if action == OpenFileAction::Notebook {
if let Some((primary_window_id, root_view_id)) = primary_window_and_view {
Expand Down
27 changes: 19 additions & 8 deletions app/src/uri/uri_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ fn test_open_file_executable_sh_routes_to_execute() {
let p = dir.path().join("run.sh");
std::fs::write(&p, b"#!/bin/sh\n:\n").unwrap();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
let action = classify_open_file_action(&p);
let action = classify_open_file_action(&p, true);
assert_eq!(action, OpenFileAction::ExecuteInSession);
}

Expand All @@ -755,7 +755,7 @@ fn test_open_file_non_executable_sh_routes_to_editor() {
let p = dir.path().join("view.sh");
std::fs::write(&p, b"#!/bin/sh\n:\n").unwrap();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o644)).unwrap();
assert_eq!(classify_open_file_action(&p), OpenFileAction::Editor);
assert_eq!(classify_open_file_action(&p, true), OpenFileAction::Editor);
}

#[test]
Expand All @@ -768,19 +768,30 @@ fn test_open_file_executable_bash_zsh_fish_route_to_execute() {
std::fs::write(&p, b"#!/bin/sh\n:\n").unwrap();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(
classify_open_file_action(&p),
classify_open_file_action(&p, true),
OpenFileAction::ExecuteInSession,
"{name} should route to ExecuteInSession",
);
}
}

#[test]
fn test_open_file_markdown_unchanged() {
fn test_open_file_markdown_routes_to_notebook_when_viewer_enabled() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("README.md");
std::fs::write(&p, b"# hi\n").unwrap();
assert_eq!(classify_open_file_action(&p), OpenFileAction::Notebook);
assert_eq!(
classify_open_file_action(&p, true),
OpenFileAction::Notebook
);
}

#[test]
fn test_open_file_markdown_routes_to_editor_when_viewer_disabled() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("README.md");
std::fs::write(&p, b"# hi\n").unwrap();
assert_eq!(classify_open_file_action(&p, false), OpenFileAction::Editor);
}

#[test]
Expand All @@ -789,7 +800,7 @@ fn test_open_file_rust_source_still_opens_in_editor() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("main.rs");
std::fs::write(&p, b"fn main() {}\n").unwrap();
assert_eq!(classify_open_file_action(&p), OpenFileAction::Editor);
assert_eq!(classify_open_file_action(&p, true), OpenFileAction::Editor);
}

#[test]
Expand Down Expand Up @@ -825,7 +836,7 @@ fn test_open_file_editor_binary_file_is_rejected() {
fn test_open_file_directory_routes_to_session() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(
classify_open_file_action(dir.path()),
classify_open_file_action(dir.path(), true),
OpenFileAction::ExecuteInSession
);
}
Expand All @@ -841,7 +852,7 @@ fn test_open_file_non_runnable_shebang_routes_to_editor() {
let p = dir.path().join("noext");
std::fs::write(&p, b"#!/bin/sh\necho hi\n").unwrap();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o644)).unwrap();
assert_eq!(classify_open_file_action(&p), OpenFileAction::Editor);
assert_eq!(classify_open_file_action(&p, true), OpenFileAction::Editor);
}

#[test]
Expand Down