Skip to content

Commit

Permalink
invalid version diagnostic & new offline data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Aug 29, 2024
1 parent 893aba0 commit d978ad6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 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 = "cargotom"
version = "0.7.0"
version = "0.7.1"
edition = "2021"

[dependencies]
Expand Down
21 changes: 14 additions & 7 deletions src/crate_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};
type OfflineCratesData = Option<Trie<u8, Vec<(String, Vec<String>)>>>;
type OfflineCratesData = Option<Trie<u8, Vec<(String, Vec<(String, Vec<String>)>)>>>;

use reqwest::Client;
use taplo::HashMap;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl CratesIoStorage {

fn read_data(path: &Path) -> OfflineCratesData {
if let Ok(dir) = read_dir(path.join("index")) {
let mut hm: HashMap<String, Vec<(String, Vec<String>)>> = HashMap::new();
let mut hm: HashMap<String, Vec<(String, Vec<(String, Vec<String>)>)>> = HashMap::new();
for file in dir.into_iter().filter_map(|v| v.ok()) {
let file = file.path();
let name = file
Expand All @@ -60,7 +60,12 @@ fn read_data(path: &Path) -> OfflineCratesData {
for (key, value) in read_to_string(file)
.unwrap_or_default()
.split('\n')
.filter_map(|line| serde_json::from_str::<(String, Vec<String>)>(line).ok())
.filter_map(|line| {
serde_json::from_str::<(String, Vec<(String, Vec<String>)>)>(&format!(
"[{line}]"
))
.ok()
})
.map(|(key, value)| (normalize_key(&key), (key, value)))
{
let item = hm.entry(key).or_default();
Expand Down Expand Up @@ -123,7 +128,9 @@ impl CratesIoStorage {
(
a.to_string(),
None,
b.first().map(|v| v.to_string()).unwrap_or_default(),
b.first()
.map(|(version, features)| version.to_string())
.unwrap_or_default(),
)
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -160,7 +167,7 @@ impl CratesIoStorage {
Some(
versions
.iter()
.map(|v| RustVersion::from(v.as_str()))
.map(|(versiom, features)| RustVersion::from(versiom.as_str()))
.collect::<Vec<_>>(),
)
} else {
Expand Down Expand Up @@ -231,8 +238,8 @@ impl CratesIoStorage {
Some(
versions
.iter()
.filter(|v| v.starts_with(version_filter))
.map(|v| RustVersion::from(v.as_str()))
.filter(|(version, features)| version.starts_with(version_filter))
.map(|(version, features)| RustVersion::from(version.as_str()))
.collect::<Vec<_>>(),
)
} else {
Expand Down
42 changes: 42 additions & 0 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ impl Store {
updates
}

pub async fn invalid_versions(
&self,
crates: &Shared<CratesIoStorage>,
) -> Vec<(String, RangeExclusive)> {
let lock = crates.read().await;
let mut updates = vec![];
for cr in self.crates_info.iter() {
let crate_name = &cr.key.value;
if let Some((version, range)) = cr.get_version() {
let crate_version = RustVersion::from(version.as_str());
if let Some(v) = lock.get_version_local(crate_name).await {
if v.iter().find(|v| (*v) == &crate_version).is_none() {
updates.push((crate_name.clone(), range));
}
}
}
}
updates
}

pub fn new(s: String) -> Self {
let mut s = Self {
tree: parse_toml(&s),
Expand Down Expand Up @@ -611,6 +631,28 @@ impl Backend {
})
.collect::<Vec<_>>(),
);

let invalid_versions = store.invalid_versions(&self.crates).await;
items.append(
&mut invalid_versions
.into_iter()
.map(|(name, range)| {
let mut start = store.byte_offset_to_position(range.start);
start.character += 1;
let mut end = store.byte_offset_to_position(range.end);
end.character -= 1;
Diagnostic::new(
Range::new(start, end),
Some(DiagnosticSeverity::ERROR),
None,
None,
format!("Unknown version for crate `{name}` "),
None,
None,
)
})
.collect::<Vec<_>>(),
);
return items;
}
vec![]
Expand Down

0 comments on commit d978ad6

Please sign in to comment.