Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

55 changes: 54 additions & 1 deletion crates/agent_ui/src/conversation_view/thread_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10373,7 +10373,60 @@ pub(crate) fn open_link(
}
})
} else {
cx.open_url(&url);
workspace.update(cx, |workspace, cx| {
workspace.open_url_or_file(&url, None, window, cx);
});
}
}

#[cfg(test)]
mod tests {
use super::*;
use project::{FakeFs, Project};
use serde_json::json;
use util::path;
use workspace::MultiWorkspace;

#[gpui::test]
async fn test_open_link_bare_path(cx: &mut gpui::TestAppContext) {
crate::test_support::init_test(cx);

let fs = FakeFs::new(cx.executor());
fs.insert_tree(path!("/project"), json!({"src": {"main.rs": ""}}))
.await;

let project = Project::test(fs, [path!("/project").as_ref()], cx).await;
let (multi_workspace, cx) =
cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());
let workspace_weak = workspace.downgrade();

// Relative path — call from multi_workspace so the inner workspace entity is not locked
multi_workspace.update_in(cx, |_, window, cx| {
open_link("src/main.rs".into(), &workspace_weak, window, cx);
});
cx.run_until_parked();
workspace.read_with(cx, |workspace, cx| {
let active = workspace
.active_item(cx)
.and_then(|item| item.project_path(cx))
.expect("file should be open");
assert!(*active.path == *"src/main.rs");
});

// Absolute path
let abs_path: SharedString = path!("/project/src/main.rs").to_string().into();
multi_workspace.update_in(cx, |_, window, cx| {
open_link(abs_path, &workspace_weak, window, cx);
});
cx.run_until_parked();
workspace.read_with(cx, |workspace, cx| {
let active = workspace
.active_item(cx)
.and_then(|item| item.project_path(cx))
.expect("file should be open");
assert!(*active.path == *"src/main.rs");
});
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/diagnostics/src/diagnostic_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ impl DiagnosticBlock {
cx: &mut Context<Editor>,
) {
let Some(diagnostic_link) = link.strip_prefix("file://#diagnostic-") else {
editor::hover_popover::open_markdown_url(link, window, cx);
let workspace = editor.workspace();
editor::hover_popover::open_markdown_url(workspace, link, window, cx);
return;
};
let Some((buffer_id, group_id, ix)) = maybe!({
Expand Down
13 changes: 12 additions & 1 deletion crates/editor/src/code_context_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,14 +1256,25 @@ impl CompletionsMenu {
window: &mut Window,
cx: &mut Context<Editor>,
) -> Div {
let editor = cx.weak_entity();
div().child(
MarkdownElement::new(markdown, hover_markdown_style(window, cx))
.code_block_renderer(markdown::CodeBlockRenderer::Default {
copy_button_visibility: CopyButtonVisibility::Hidden,
wrap_button_visibility: markdown::WrapButtonVisibility::Hidden,
border: false,
})
.on_url_click(open_markdown_url),
.on_url_click(move |link, window, cx| {
open_markdown_url(
editor
.read_with(cx, |editor, _| editor.workspace())
.ok()
.flatten(),
link,
window,
cx,
)
}),
)
}

Expand Down
31 changes: 27 additions & 4 deletions crates/editor/src/hover_popover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,15 @@ pub fn diagnostics_markdown_style(window: &Window, cx: &App) -> MarkdownStyle {
}
}

pub fn open_markdown_url(link: SharedString, window: &mut Window, cx: &mut App) {
pub fn open_markdown_url(
workspace: Option<Entity<Workspace>>,
link: SharedString,
window: &mut Window,
cx: &mut App,
) {
if let Ok(uri) = Url::parse(&link)
&& uri.scheme() == "file"
&& let Some(workspace) = Workspace::for_window(window, cx)
&& let Some(workspace) = workspace
{
workspace.update(cx, |workspace, cx| {
let task = workspace.open_abs_path(
Expand Down Expand Up @@ -847,7 +852,14 @@ pub fn open_markdown_url(link: SharedString, window: &mut Window, cx: &mut App)
});
return;
}
cx.open_url(&link);

if let Some(workspace) = workspace {
workspace.update(cx, |workspace, cx| {
workspace.open_url_or_file(&link, None, window, cx);
});
} else {
cx.open_url(&link);
}
}

#[derive(Default)]
Expand Down Expand Up @@ -1033,6 +1045,7 @@ impl InfoPopover {
) -> AnyElement {
let keyboard_grace = Rc::clone(&self.keyboard_grace);
let this = cx.entity().downgrade();
let this2 = this.clone();
let bounds_cell = self.last_bounds.clone();
div()
.id("info_popover")
Expand Down Expand Up @@ -1083,7 +1096,17 @@ impl InfoPopover {
wrap_button_visibility: markdown::WrapButtonVisibility::Hidden,
border: false,
})
.on_url_click(open_markdown_url)
.on_url_click(move |link, window, cx| {
open_markdown_url(
this2
.read_with(cx, |editor, _| editor.workspace())
.ok()
.flatten(),
link,
window,
cx,
)
})
.p_2(),
),
)
Expand Down
28 changes: 26 additions & 2 deletions crates/editor/src/signature_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl SignatureHelpPopover {
return div().into_any_element();
};

let editor = cx.weak_entity();
let main_content = div()
.occlude()
.p_2()
Expand Down Expand Up @@ -413,7 +414,20 @@ impl SignatureHelpPopover {
markdown::WrapButtonVisibility::Hidden,
border: false,
})
.on_url_click(open_markdown_url),
.on_url_click({
let editor = editor.clone();
move |link, window, cx| {
open_markdown_url(
editor
.read_with(cx, |editor, _| editor.workspace())
.ok()
.flatten(),
link,
window,
cx,
)
}
}),
)
},
)
Expand All @@ -427,7 +441,17 @@ impl SignatureHelpPopover {
markdown::WrapButtonVisibility::Hidden,
border: false,
})
.on_url_click(open_markdown_url),
.on_url_click(move |link, window, cx| {
open_markdown_url(
editor
.read_with(cx, |editor, _| editor.workspace())
.ok()
.flatten(),
link,
window,
cx,
)
}),
)
}),
)
Expand Down
108 changes: 11 additions & 97 deletions crates/markdown_preview/src/markdown_preview_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::any::TypeId;
use std::borrow::Cow;
use std::cmp::min;
use std::ops::Range;
use std::path::{Path, PathBuf};
Expand All @@ -25,12 +26,11 @@ use theme::{SystemAppearance, Theme, ThemeRegistry};
use theme_settings::ThemeSettings;
use ui::{ContextMenu, WithScrollbar, prelude::*, right_click_menu};
use util::markdown::split_local_url_fragment;
use util::normalize_path;
use workspace::item::{Item, ItemBufferKind, ItemHandle, SaveOptions};
use workspace::searchable::{
Direction, SearchEvent, SearchOptions, SearchToken, SearchableItem, SearchableItemHandle,
};
use workspace::{OpenOptions, OpenVisible, Pane, Workspace};
use workspace::{Pane, Workspace};

use crate::{
OpenFollowingPreview, OpenPreview, OpenPreviewToTheSide, ScrollDown, ScrollDownByItem,
Expand Down Expand Up @@ -785,26 +785,16 @@ fn open_preview_url(
) {
let (path_text, _) = split_preview_url(url.as_ref());

if let Some(path) = resolve_preview_path(path_text, base_directory.as_deref())
&& let Some(workspace) = workspace.upgrade()
{
let _ = workspace.update(cx, |workspace, cx| {
workspace
.open_abs_path(
normalize_path(path.as_path()),
OpenOptions {
visible: Some(OpenVisible::None),
..Default::default()
},
window,
cx,
)
.detach();
// URL-decode the path for proper handling of encoded characters
let decoded_path = urlencoding::decode(path_text).unwrap_or_else(|_| Cow::Borrowed(path_text));

if let Some(workspace) = workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
workspace.open_url_or_file(&decoded_path, base_directory.as_deref(), window, cx);
});
return;
} else {
cx.open_url(url.as_ref());
}

cx.open_url(url.as_ref());
}

fn split_preview_url(url: &str) -> (&str, Option<&str>) {
Expand All @@ -814,30 +804,6 @@ fn split_preview_url(url: &str) -> (&str, Option<&str>) {
}
}

fn resolve_preview_path(url: &str, base_directory: Option<&Path>) -> Option<PathBuf> {
if url.starts_with("http://") || url.starts_with("https://") {
return None;
}

let (path_text, _) = split_preview_url(url);
let decoded_url = urlencoding::decode(path_text)
.map(|decoded| decoded.into_owned())
.unwrap_or_else(|_| path_text.to_string());
let candidate = PathBuf::from(&decoded_url);

if candidate.is_absolute() && candidate.exists() {
return Some(candidate);
}

let base_directory = base_directory?;
let resolved = base_directory.join(decoded_url);
if resolved.exists() {
Some(resolved)
} else {
None
}
}

fn resolve_preview_image(
dest_url: &str,
base_directory: Option<&Path>,
Expand Down Expand Up @@ -1212,53 +1178,7 @@ mod tests {
use util::test::TempTree;
use workspace::{AppState, MultiWorkspace, SaveIntent, Workspace, open_paths};

use super::{MarkdownPreviewView, resolve_preview_path};

#[test]
fn resolves_relative_preview_path_and_missing_cases() {
let tree = markdown_fixture_tree(json!({
"notes.md": "# Notes"
}));
let base_directory = markdown_fixture_directory(&tree);
let file = base_directory.join("notes.md");

assert_eq!(
resolve_preview_path("notes.md", Some(base_directory.as_path())),
Some(file)
);
assert_eq!(
resolve_preview_path("nonexistent.md", Some(base_directory.as_path())),
None
);
assert_eq!(resolve_preview_path("notes.md", None), None);
}

#[test]
fn resolves_urlencoded_preview_path_and_ignores_fragment_component() {
let tree = markdown_fixture_tree(json!({
"release notes.md": "# Release Notes",
"notes.md": "# Notes"
}));
let base_directory = markdown_fixture_directory(&tree);

assert_eq!(
resolve_preview_path(
"release%20notes.md#overview",
Some(base_directory.as_path())
),
Some(base_directory.join("release notes.md"))
);
assert_eq!(
resolve_preview_path("notes.md#L10", Some(base_directory.as_path())),
Some(base_directory.join("notes.md"))
);
}

#[test]
fn does_not_treat_web_links_as_preview_files() {
assert_eq!(resolve_preview_path("https://zed.dev", None), None);
assert_eq!(resolve_preview_path("http://example.com", None), None);
}
use super::MarkdownPreviewView;

#[test]
fn resolves_workspace_absolute_preview_image_path_and_rejects_missing() {
Expand Down Expand Up @@ -1463,12 +1383,6 @@ mod tests {
})
}

fn markdown_fixture_tree(docs_tree: serde_json::Value) -> TempTree {
TempTree::new(json!({
"docs": docs_tree
}))
}

fn markdown_fixture_directory(tree: &TempTree) -> PathBuf {
tree.path().join("docs")
}
Expand Down
1 change: 1 addition & 0 deletions crates/workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ telemetry.workspace = true
theme.workspace = true
theme_settings.workspace = true
ui.workspace = true
url.workspace = true
util.workspace = true
uuid.workspace = true
zed_actions.workspace = true
Expand Down
10 changes: 9 additions & 1 deletion crates/workspace/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,15 @@ impl Render for LanguageServerPrompt {
wrap_button_visibility: markdown::WrapButtonVisibility::Hidden,
border: false,
})
.on_url_click(|link, _, cx| cx.open_url(&link)),
.on_url_click(|link, window, cx| {
if let Some(workspace) = Workspace::for_window(window, cx) {
workspace.update(cx, |workspace, cx| {
workspace.open_url_or_file(&link, None, window, cx);
});
} else {
cx.open_url(&link);
}
}),
)
.children(request.actions.iter().enumerate().map(|(ix, action)| {
let this_handle = cx.entity();
Expand Down
Loading
Loading