From ff9e2f0296a77104f7ce52ec6371dc2718d9457a Mon Sep 17 00:00:00 2001 From: uncenter <47499684+uncenter@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:30:17 -0400 Subject: [PATCH] Only strip query parameters and fragment for flake ref URLs (#141) --- crates/ide/src/ide/links.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/crates/ide/src/ide/links.rs b/crates/ide/src/ide/links.rs index a9041b7..6a31a63 100644 --- a/crates/ide/src/ide/links.rs +++ b/crates/ide/src/ide/links.rs @@ -76,22 +76,15 @@ fn try_resolve_link_uri(uri: &str) -> Option { // 1. Efficiency. // 2. Less false-positives. // 3. Reject not-really-absolute `mirror:` URIs. - const FILTERS: &[&str] = &[ - // Common URLs. - "https:", - "http:", - "ftp:", - "file:", - // Flake-refs. - // https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#types - "path:", - "git+", - "tarball+", - "github:", - "sourcehut:", - ]; - if !FILTERS.iter().any(|prefix| uri.starts_with(prefix)) { + // https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#types + const FLAKE_REFS: &[&str] = &["path:", "git+", "tarball+", "github:", "sourcehut:"]; + // Common URLs. + const FILTERS: &[&str] = &["https:", "http:", "ftp:", "file:"]; + + let is_flake_ref = FLAKE_REFS.iter().any(|prefix| uri.starts_with(prefix)); + + if !is_flake_ref && !FILTERS.iter().any(|prefix| uri.starts_with(prefix)) { return None; } @@ -132,8 +125,10 @@ fn try_resolve_link_uri(uri: &str) -> Option { // Trim `?` and `#` parts, which are special for flake-ref. // Not a part of original URI. - uri.set_query(None); - uri.set_fragment(None); + if is_flake_ref { + uri.set_query(None); + uri.set_fragment(None); + } Some(uri) } @@ -196,7 +191,7 @@ mod tests { github:NixOS/nixpkgs -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs "github:NixOS/nixpkgs/nixos-22.05" -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs "github:NixOS/nixpkgs/pull/190594/head" -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs - "https://example.com?foo=1#bar" -> https://example.com/: https://example.com/ + "https://example.com?foo=1#bar" -> https://example.com/?foo=1#bar: https://example.com/?foo=1#bar "#]], ); }