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.

editor: Decode escaped characters in hover popover links (#55973)

Matt Good950ec7943f3f
diff --git a/Cargo.lock b/Cargo.lock
index 46c2fc46e2f..1d5fe8fd9bd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5810,6 +5810,7 @@ dependencies = [
"unicode-width",
"unindent",
"url",
+ "urlencoding",
"util",
"uuid",
"vim_mode_setting",
diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml
index 1ca500832e2..b6df4a370fc 100644
--- a/crates/editor/Cargo.toml
+++ b/crates/editor/Cargo.toml
@@ -98,6 +98,7 @@ unindent = { workspace = true, optional = true }
ui.workspace = true
ui_input.workspace = true
url.workspace = true
+urlencoding.workspace = true
util.workspace = true
uuid.workspace = true
vim_mode_setting.workspace = true
diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs
index 96aeae19933..ff4b8c4b8d0 100644
--- a/crates/editor/src/hover_popover.rs
+++ b/crates/editor/src/hover_popover.rs
@@ -798,19 +798,33 @@ pub fn diagnostics_markdown_style(window: &Window, cx: &App) -> MarkdownStyle {
}
}
+fn parse_file_link(link: &str) -> Option<(PathBuf, Option<String>)> {
+ let uri = Url::parse(link).ok().filter(|uri| uri.scheme() == "file")?;
+ let fragment = uri.fragment().map(ToOwned::to_owned);
+ let path = uri.to_file_path().unwrap_or_else(|_| {
+ let encoded = uri.path();
+
+ urlencoding::decode(encoded)
+ .map(Cow::into_owned)
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| PathBuf::from(encoded))
+ });
+
+ Some((path, fragment))
+}
+
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"
+ if let Some((path, fragment)) = parse_file_link(&link)
&& let Some(workspace) = workspace
{
workspace.update(cx, |workspace, cx| {
let task = workspace.open_abs_path(
- PathBuf::from(uri.path()),
+ path,
OpenOptions {
visible: Some(OpenVisible::None),
..Default::default()
@@ -823,7 +837,7 @@ pub fn open_markdown_url(
let item = task.await?;
// Ruby LSP uses URLs with #L1,1-4,4
// we'll just take the first number and assume it's a line number
- let Some(fragment) = uri.fragment() else {
+ let Some(fragment) = fragment else {
return anyhow::Ok(());
};
let mut accum = 0u32;
@@ -2747,4 +2761,21 @@ mod tests {
);
});
}
+
+ #[test]
+ fn test_parse_file_links() {
+ assert_eq!(
+ parse_file_link("file:///path/to/file"),
+ Some((PathBuf::from("/path/to/file"), None))
+ );
+ assert_eq!(
+ parse_file_link("file:///path/to/file%20with%20spaces"),
+ Some((PathBuf::from("/path/to/file with spaces"), None))
+ );
+ assert_eq!(
+ parse_file_link("file:///path/to/file#123"),
+ Some((PathBuf::from("/path/to/file"), Some("123".to_string())))
+ );
+ assert_eq!(parse_file_link("http://example.com/"), None,);
+ }
}