Skip to content

Commit

Permalink
Strip version prefix when doing completions
Browse files Browse the repository at this point in the history
  • Loading branch information
osiewicz committed Nov 11, 2024
1 parent 58dae03 commit 1cde69b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "package-version-server"
version = "0.0.5"
version = "0.0.6"
edition = "2021"
authors = ["Zed Team <[email protected]>"]
license = "MIT"
Expand Down
15 changes: 12 additions & 3 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String>,
pub date: DateTime<FixedOffset>,
Expand Down Expand Up @@ -130,10 +131,18 @@ async fn fetch(
}

fn parse_version_info(response: &Value, version_info: &Value) -> Option<PackageVersion> {
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,
Expand Down
26 changes: 17 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1cde69b

Please sign in to comment.