Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Allow project model to download crates #1020

Merged
merged 8 commits into from
Aug 28, 2018
Merged
Changes from 1 commit
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: 17 additions & 16 deletions src/project_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// This module represents the RLS view of the Cargo project model:
/// a graph of interdependent packages.
//! This module represents the RLS view of the Cargo project model:
//! a graph of interdependent packages.
use std::{
collections::HashMap,
sync::Arc,
Expand All @@ -20,6 +20,7 @@ use racer;

#[derive(Debug)]
pub struct ProjectModel {
manifest_to_id: HashMap<PathBuf, Package>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

packages: Vec<PackageData>,
}

Expand All @@ -28,7 +29,6 @@ pub struct Package(usize);

#[derive(Debug)]
struct PackageData {
manifest: PathBuf,
lib: Option<PathBuf>,
deps: Vec<Dep>,
}
Expand All @@ -43,8 +43,8 @@ impl ProjectModel {
pub fn load(manifest: &Path, vfs: &Vfs) -> Result<ProjectModel, failure::Error> {
assert!(manifest.ends_with("Cargo.toml"));
let mut config = Config::default()?;
// frozen=true, locked=true
config.configure(0, Some(true), &None, true, true, &None, &[])?;
// frozen = false, locked = false
Copy link
Member

@Xanewok Xanewok Aug 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: It's definitely not making things any more worse/more complicated, since we currently https://github.com/rust-lang-nursery/rls/blob/master/src/build/cargo.rs#L572 a Cargo config with defaults (frozen, locked = false) as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

config.configure(0, Some(true), &None, false, false, &None, &[])?;
let ws = Workspace::new(&manifest, &config)?;
// get resolve from lock file
let prev = {
Expand All @@ -55,26 +55,28 @@ impl ProjectModel {
let v: EncodableResolve = resolve.try_into()?;
Some(v.into_resolve(&ws)?)
}
_ => None
_ => None,
}
};
// then resolve precisely and add overrides
let mut registry = PackageRegistry::new(ws.config())?;
let resolve = resolve_with_prev(&mut registry, &ws, prev.as_ref())?;
let cargo_packages = {
let ids: Vec<PackageId> = resolve.iter().cloned().collect();
registry.get(&ids)
};

let mut pkg_id_to_pkg = HashMap::new();
let mut manifest_to_id = HashMap::new();
let mut packages = Vec::new();
for (idx, pkg_id) in resolve.iter().enumerate() {
let pkg = Package(idx);
pkg_id_to_pkg.insert(pkg_id.clone(), pkg);
let cargo_pkg = cargo_packages.get(pkg_id)?;
let manifest = cargo_pkg.manifest_path().to_owned();
manifest_to_id.insert(manifest, pkg);
packages.push(PackageData {
manifest: cargo_pkg.manifest_path().to_owned(),
lib: cargo_pkg.targets().iter()
lib: cargo_pkg
.targets()
.iter()
.find(|t| t.is_lib())
.map(|t| t.src_path().to_owned()),
deps: Vec::new(),
Expand All @@ -99,20 +101,19 @@ impl ProjectModel {
}
}
}
Ok(ProjectModel { packages })
Ok(ProjectModel {
manifest_to_id,
packages,
})
}

pub fn package_for_manifest(&self, manifest_path: &Path) -> Option<Package> {
self.packages.iter()
.enumerate()
.find(|(_idx, p)| p.manifest == manifest_path)
.map(|(idx, _p)| Package(idx))
self.manifest_to_id.get(manifest_path).map(|&x| x)
}

fn get(&self, pkg: Package) -> &PackageData {
&self.packages[pkg.0]
}

}

impl Package {
Expand Down