Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

markdown_preview: Fix relative links in remote Markdown subfolders (#61438)

Smit Barmase9d0ef37a2571
diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rs
index bf9bfcf7a63..98088370f01 100644
--- a/crates/markdown_preview/src/markdown_preview_view.rs
+++ b/crates/markdown_preview/src/markdown_preview_view.rs
@@ -643,15 +643,20 @@ impl MarkdownPreviewView {
/// The absolute path of the file that is currently being previewed.
fn get_folder_for_active_editor(editor: &Editor, cx: &App) -> Option<PathBuf> {
- if let Some(file) = editor.file_at(MultiBufferOffset(0), cx) {
- if let Some(file) = file.as_local() {
- file.abs_path(cx).parent().map(|p| p.to_path_buf())
- } else {
- None
- }
- } else {
- None
- }
+ let file = editor.file_at(MultiBufferOffset(0), cx)?;
+ let absolute_path = editor
+ .project()
+ .and_then(|project| {
+ project.read(cx).absolute_path(
+ &ProjectPath {
+ worktree_id: file.worktree_id(cx),
+ path: file.path().clone(),
+ },
+ cx,
+ )
+ })
+ .or_else(|| file.as_local().map(|file| file.abs_path(cx)))?;
+ absolute_path.parent().map(Path::to_path_buf)
}
fn line_scroll_amount(&self, cx: &App) -> Pixels {
@@ -1802,9 +1807,10 @@ mod tests {
use crate::markdown_preview_view::resolve_preview_image;
use buffer_diff::BufferDiff;
use editor::Editor;
+ use fs::FakeFs;
use gpui::{AppContext as _, Entity, Focusable as _, TestAppContext, WindowHandle};
- use language::Point;
- use project::{FakeFs, Project};
+ use language::{Buffer, DiskState, Point};
+ use project::Project;
use serde_json::json;
use std::path::PathBuf;
use std::sync::Arc;
@@ -2769,6 +2775,35 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn derives_remote_preview_source_directory(cx: &mut TestAppContext) {
+ init_test(cx);
+ let project = Project::test(FakeFs::new(cx.executor()), [], cx).await;
+ let worktree = project.update(cx, |project, cx| {
+ project.add_test_remote_worktree("/remote/project", cx)
+ });
+ let file: Arc<dyn language::File> = Arc::new(project::File {
+ worktree,
+ path: rel_path("docs/readme.md").into(),
+ disk_state: DiskState::New,
+ entry_id: None,
+ is_local: false,
+ is_private: false,
+ });
+ let buffer = cx.new(|cx| {
+ let mut buffer = Buffer::local("# readme\n", cx);
+ buffer.file_updated(file, cx);
+ buffer
+ });
+ let (editor, cx) =
+ cx.add_window_view(|window, cx| Editor::for_buffer(buffer, Some(project), window, cx));
+
+ let folder = editor.read_with(cx, |editor, cx| {
+ MarkdownPreviewView::get_folder_for_active_editor(editor, cx)
+ });
+ assert_eq!(folder, Some(PathBuf::from("/remote/project/docs")));
+ }
+
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
cx.update(|cx| {
let state = AppState::test(cx);
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index eb454d9c64c..879d0201fc9 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -4749,7 +4749,7 @@ impl Workspace {
);
(**cx)
.spawn(async move |cx| {
- if let Err(_) = task.await {
+ if task.await.is_err() {
cx.update(|cx| cx.open_url(&url_or_path));
}
})
@@ -4768,6 +4768,7 @@ impl Workspace {
// Not a valid URL - treat as a file path
let project = self.project();
let path_style = project.read(cx).path_style(cx);
+ let project_is_local = project.read(cx).is_local();
// If it's an absolute path, open it directly
if path_style.is_absolute(url_or_path) {
@@ -4777,29 +4778,36 @@ impl Workspace {
let path = Path::new(url_or_path);
// Try to resolve relative path against base_path first
- if let Some(base) = base_path
- // TODO: remotes, the exists check below hits the local FS, unsure
- // if this runs on the remote or not
- && project.read(cx).is_local()
- {
- let resolved = path_style.join(base, path).map(PathBuf::from);
- if let Some(resolved) = resolved
- && resolved.exists()
- {
- open_abs_path(self, resolved, cx);
- return;
+ let path_from_base = if let Some(base) = base_path {
+ let resolved_path = path_style.join(base, path).map(PathBuf::from);
+ if project_is_local {
+ if let Some(resolved_path) = resolved_path
+ && resolved_path.exists()
+ {
+ open_abs_path(self, resolved_path, cx);
+ return;
+ }
+ None
+ } else {
+ resolved_path
}
- }
+ } else {
+ None
+ };
// Try to resolve against project worktrees
- if let Some(project_path) =
- project.update(cx, |project, cx| project.find_project_path(url_or_path, cx))
- {
+ let project_path = project.update(cx, |project, cx| {
+ path_from_base
+ .as_deref()
+ .and_then(|base_path| project.find_project_path(base_path, cx))
+ .or_else(|| project.find_project_path(path, cx))
+ });
+ if let Some(project_path) = project_path {
let url_or_path = url_or_path.to_owned();
let task = self.open_path(project_path, None, true, window, cx);
(**cx)
.spawn(async move |cx| {
- if let Err(_) = task.await {
+ if task.await.is_err() {
cx.update(|cx| cx.open_url(&url_or_path));
}
})
@@ -16269,9 +16277,12 @@ mod tests {
// View
struct TestPngItemView {
focus_handle: FocusHandle,
+ project_path: ProjectPath,
}
// Model
- struct TestPngItem {}
+ struct TestPngItem {
+ project_path: ProjectPath,
+ }
impl project::ProjectItem for TestPngItem {
fn try_open(
@@ -16280,7 +16291,8 @@ mod tests {
cx: &mut App,
) -> Option<Task<anyhow::Result<Entity<Self>>>> {
if path.path.extension().unwrap() == "png" {
- Some(cx.spawn(async move |cx| Ok(cx.new(|_| TestPngItem {}))))
+ let project_path = path.clone();
+ Some(cx.spawn(async move |cx| Ok(cx.new(|_| TestPngItem { project_path }))))
} else {
None
}
@@ -16291,7 +16303,7 @@ mod tests {
}
fn project_path(&self, _: &App) -> Option<ProjectPath> {
- None
+ Some(self.project_path.clone())
}
fn is_dirty(&self) -> bool {
@@ -16328,7 +16340,7 @@ mod tests {
fn for_project_item(
_project: Entity<Project>,
_pane: Option<&Pane>,
- _item: Entity<Self::Item>,
+ item: Entity<Self::Item>,
_: &mut Window,
cx: &mut Context<Self>,
) -> Self
@@ -16337,6 +16349,7 @@ mod tests {
{
Self {
focus_handle: cx.focus_handle(),
+ project_path: item.read(cx).project_path.clone(),
}
}
}
@@ -16527,6 +16540,43 @@ mod tests {
assert!(handle.is_err());
}
+ #[gpui::test]
+ async fn test_open_url_or_file_resolves_remote_base_path(cx: &mut TestAppContext) {
+ init_test(cx);
+ cx.update(register_project_item::<TestPngItemView>);
+
+ let project = Project::test(FakeFs::new(cx.executor()), [], cx).await;
+ let worktree = project.update(cx, |project, cx| {
+ let worktree = project.add_test_remote_worktree("/remote/project", cx);
+ project.mark_as_collab_for_testing();
+ worktree
+ });
+ let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id());
+ let (workspace, cx) =
+ cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx));
+
+ workspace.update_in(cx, |workspace, window, cx| {
+ workspace.open_url_or_file(
+ "./sibling.png",
+ Some(Path::new("/remote/project/docs")),
+ window,
+ cx,
+ );
+ });
+ cx.run_until_parked();
+
+ let opened_item = workspace
+ .read_with(cx, |workspace, cx| {
+ workspace
+ .active_item(cx)
+ .and_then(|item| item.downcast::<TestPngItemView>())
+ })
+ .expect("resolved remote project item should be opened");
+ let project_path = opened_item.read_with(cx, |item, _| item.project_path.clone());
+ assert_eq!(project_path.worktree_id, worktree_id);
+ assert_eq!(project_path.path.as_ref(), rel_path("docs/sibling.png"));
+ }
+
#[gpui::test]
async fn test_register_project_item_two_enter_one_leaves(cx: &mut TestAppContext) {
init_test(cx);