Skip to content

Commit

Permalink
Merge pull request #42 from alexggh/fix/vscode_indexing
Browse files Browse the repository at this point in the history
Speedup indexing with vscode + rustanalyzer
  • Loading branch information
bkchr committed Dec 6, 2023
2 parents cc100ed + 44ced0a commit 57618a7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type Cache = BTreeMap<String, CacheEntry>;
struct CacheEntry {
manifest_ts: SystemTime,
workspace_manifest_ts: SystemTime,
workspace_manifest_path: PathBuf,
crate_names: CrateNames,
}

Expand All @@ -186,17 +187,16 @@ pub fn crate_name(orig_name: &str) -> Result<FoundCrate, Error> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| Error::CargoManifestDirNotSet)?;
let manifest_path = Path::new(&manifest_dir).join("Cargo.toml");

let workspace_manifest_path = workspace_manifest_path(&manifest_path)?;

let manifest_ts = cargo_toml_timestamp(&manifest_path)?;
let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?;

static CACHE: Mutex<Cache> = Mutex::new(BTreeMap::new());
let mut cache = CACHE.lock().unwrap();

let crate_names = match cache.entry(manifest_dir) {
btree_map::Entry::Occupied(entry) => {
let cache_entry = entry.into_mut();
let workspace_manifest_path = cache_entry.workspace_manifest_path.as_path();
let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?;

// Timestamp changed, rebuild this cache entry.
if manifest_ts != cache_entry.manifest_ts ||
Expand All @@ -213,6 +213,9 @@ pub fn crate_name(orig_name: &str) -> Result<FoundCrate, Error> {
&cache_entry.crate_names
},
btree_map::Entry::Vacant(entry) => {
let workspace_manifest_path = workspace_manifest_path(&manifest_path)?;
let workspace_manifest_ts = cargo_toml_timestamp(&workspace_manifest_path)?;

let cache_entry = entry.insert(read_cargo_toml(
&manifest_path,
&workspace_manifest_path,
Expand Down Expand Up @@ -273,7 +276,12 @@ fn read_cargo_toml(

let crate_names = extract_crate_names(&manifest, workspace_dependencies)?;

Ok(CacheEntry { manifest_ts, workspace_manifest_ts, crate_names })
Ok(CacheEntry {
manifest_ts,
workspace_manifest_ts,
crate_names,
workspace_manifest_path: workspace_manifest_path.to_path_buf(),
})
}

/// Extract all `[workspace.dependencies]`.
Expand Down

0 comments on commit 57618a7

Please sign in to comment.