From 1cde69bd01ec825cdecf7b619b62e49a50c5fa28 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Mon, 11 Nov 2024 05:41:27 +0100 Subject: [PATCH] Strip version prefix when doing completions --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/fetcher.rs | 15 ++++++++++++--- src/main.rs | 26 +++++++++++++++++--------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 553b7ba..1c84e0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -700,7 +700,7 @@ dependencies = [ [[package]] name = "package-version-server" -version = "0.0.5" +version = "0.0.6" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 65640fb..c02017c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "package-version-server" -version = "0.0.5" +version = "0.0.6" edition = "2021" authors = ["Zed Team "] license = "MIT" diff --git a/src/fetcher.rs b/src/fetcher.rs index 395a49c..3290378 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -7,6 +7,7 @@ use std::{ use chrono::{DateTime, FixedOffset}; use itertools::{Either, Itertools}; use reqwest::Client; +use semver_rs::Parseable; use serde_json::Value; use tokio::sync::Mutex; @@ -79,7 +80,7 @@ pub(super) struct MetadataFromRegistry { #[derive(Clone)] pub(super) struct PackageVersion { - pub version: String, + pub version: semver_rs::Version, pub description: String, pub homepage: Option, pub date: DateTime, @@ -130,10 +131,18 @@ async fn fetch( } fn parse_version_info(response: &Value, version_info: &Value) -> Option { - let version = version_info["version"].as_str()?.to_string(); + let version_str = version_info["version"].as_str()?; + let version = semver_rs::Version::parse( + version_str, + Some(semver_rs::Options { + loose: true, + include_prerelease: true, + }), + ) + .ok()?; let description = version_info["description"].as_str()?.to_string(); let homepage = version_info["homepage"].as_str().map(ToString::to_string); - let date_str = response["time"][version.as_str()].as_str()?; + let date_str = response["time"][version_str].as_str()?; let date = DateTime::parse_from_rfc3339(date_str).ok()?; Some(PackageVersion { version, diff --git a/src/main.rs b/src/main.rs index c7844e6..cdfc3d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -216,20 +216,28 @@ impl LanguageServer for Backend { .await; } + let mut query = version.to_string(); + for prefix in [">", "<", ">=", "<=", "=", "==", "!=", "===", "!==", "~"] { + if let Some(stripped) = query.strip_prefix(prefix) { + query = stripped.to_string(); + break; + } + } let mut completion_items: Vec<_> = response .package_versions .into_iter() .filter_map(|package_version| { - if package_version.version.starts_with(&version) { - Some(CompletionItem { - label: package_version.version.clone(), - detail: Some(package_version.date.format("%d/%m/%Y %H:%M").to_string()), - insert_text: Some(package_version.version.clone()), - ..Default::default() - }) - } else { - None + let label = package_version.version.to_string(); + if !label.starts_with(&query) { + return None; } + let insert_text = Some(label.clone()); + Some(CompletionItem { + label, + insert_text, + detail: Some(package_version.date.format("%d/%m/%Y %H:%M").to_string()), + ..Default::default() + }) }) .collect(); completion_items