Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only strip query parameters and fragment for flake ref URLs #141

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
33 changes: 14 additions & 19 deletions crates/ide/src/ide/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,15 @@ fn try_resolve_link_uri(uri: &str) -> Option<Url> {
// 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;
}

Expand Down Expand Up @@ -132,8 +125,10 @@ fn try_resolve_link_uri(uri: &str) -> Option<Url> {

// 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)
}
Expand Down Expand Up @@ -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
"#]],
);
}
Expand Down