Skip to content

Commit fcd615e

Browse files
committed
Extract project model to separate crate
1 parent 34398a8 commit fcd615e

File tree

8 files changed

+109
-53
lines changed

8 files changed

+109
-53
lines changed

Cargo.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_lsp_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ url_serde = "0.2.0"
1919
lsp-types = "0.55.0"
2020
walkdir = "2.2.7"
2121
im = "12.0.0"
22-
cargo_metadata = "0.7.0"
2322
rustc-hash = "1.0"
2423
parking_lot = "0.7.0"
2524

@@ -30,6 +29,7 @@ ra_ide_api = { path = "../ra_ide_api" }
3029
ra_arena = { path = "../ra_arena" }
3130
gen_lsp_server = { path = "../gen_lsp_server" }
3231
ra_vfs = { path = "../ra_vfs" }
32+
ra_project_model = { path = "../ra_project_model" }
3333

3434
[dev-dependencies]
3535
tempfile = "3"
Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
mod cargo_workspace;
2-
mod sysroot;
1+
use std::path::PathBuf;
32

4-
use std::path::{Path, PathBuf};
5-
6-
use failure::bail;
73
use thread_worker::{WorkerHandle, Worker};
84

95
use crate::Result;
106

11-
pub use crate::project_model::{
12-
cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
13-
sysroot::Sysroot,
7+
pub use ra_project_model::{
8+
ProjectWorkspace, CargoWorkspace, Package, Target, TargetKind, Sysroot,
149
};
1510

16-
#[derive(Debug, Clone)]
17-
pub struct ProjectWorkspace {
18-
pub(crate) cargo: CargoWorkspace,
19-
pub(crate) sysroot: Sysroot,
20-
}
21-
22-
impl ProjectWorkspace {
23-
pub fn discover(path: &Path) -> Result<ProjectWorkspace> {
24-
let cargo_toml = find_cargo_toml(path)?;
25-
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml)?;
26-
let sysroot = Sysroot::discover(&cargo_toml)?;
27-
let res = ProjectWorkspace { cargo, sysroot };
28-
Ok(res)
29-
}
30-
}
31-
3211
pub fn workspace_loader() -> (Worker<PathBuf, Result<ProjectWorkspace>>, WorkerHandle) {
3312
thread_worker::spawn::<PathBuf, Result<ProjectWorkspace>, _>(
3413
"workspace loader",
@@ -42,18 +21,3 @@ pub fn workspace_loader() -> (Worker<PathBuf, Result<ProjectWorkspace>>, WorkerH
4221
},
4322
)
4423
}
45-
46-
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
47-
if path.ends_with("Cargo.toml") {
48-
return Ok(path.to_path_buf());
49-
}
50-
let mut curr = Some(path);
51-
while let Some(path) = curr {
52-
let candidate = path.join("Cargo.toml");
53-
if candidate.exists() {
54-
return Ok(candidate);
55-
}
56-
curr = path.parent();
57-
}
58-
bail!("can't find Cargo.toml at {}", path.display())
59-
}

crates/ra_project_model/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
edition = "2018"
3+
name = "ra_project_model"
4+
version = "0.1.0"
5+
authors = ["Aleksey Kladov <[email protected]>"]
6+
7+
[dependencies]
8+
# itertools = "0.8.0"
9+
# join_to_string = "0.1.3"
10+
# log = "0.4.5"
11+
# relative-path = "0.4.0"
12+
# rayon = "1.0.2"
13+
# fst = "0.3.1"
14+
rustc-hash = "1.0"
15+
# parking_lot = "0.7.0"
16+
# unicase = "2.2.0"
17+
18+
# TODO get rid of these?
19+
failure = "0.1.4"
20+
failure_derive = "0.1.4"
21+
22+
smol_str = { version = "0.1.9", features = ["serde"] }
23+
walkdir = "2.2.7"
24+
25+
cargo_metadata = "0.7.0"
26+
27+
ra_arena = { path = "../ra_arena" }
28+
29+
[dev-dependencies]
30+
test_utils = { path = "../test_utils" }

crates/ra_lsp_server/src/project_model/cargo_workspace.rs renamed to crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use std::path::{Path, PathBuf};
22

33
use cargo_metadata::{MetadataCommand, CargoOpt};
4-
use ra_syntax::SmolStr;
4+
use smol_str::SmolStr;
55
use ra_arena::{Arena, RawId, impl_arena_id};
66
use rustc_hash::FxHashMap;
77
use failure::format_err;
88

99
use crate::Result;
1010

11-
/// `CargoWorksapce` represents the logical structure of, well, a Cargo
11+
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
1212
/// workspace. It pretty closely mirrors `cargo metadata` output.
1313
///
14-
/// Note that internally, rust analyzer uses a differnet structure:
14+
/// Note that internally, rust analyzer uses a different structure:
1515
/// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
1616
/// while this knows about `Pacakges` & `Targets`: purely cargo-related
1717
/// concepts.
@@ -162,9 +162,11 @@ impl CargoWorkspace {
162162

163163
Ok(CargoWorkspace { packages, targets })
164164
}
165+
165166
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a {
166167
self.packages.iter().map(|(id, _pkg)| id)
167168
}
169+
168170
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
169171
self.packages().filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)).next()
170172
}

crates/ra_project_model/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
mod cargo_workspace;
2+
mod sysroot;
3+
4+
use std::path::{Path, PathBuf};
5+
6+
use failure::bail;
7+
8+
pub use crate::{
9+
cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
10+
sysroot::Sysroot,
11+
};
12+
13+
// TODO use own error enum?
14+
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
15+
16+
#[derive(Debug, Clone)]
17+
pub struct ProjectWorkspace {
18+
pub cargo: CargoWorkspace,
19+
pub sysroot: Sysroot,
20+
}
21+
22+
impl ProjectWorkspace {
23+
pub fn discover(path: &Path) -> Result<ProjectWorkspace> {
24+
let cargo_toml = find_cargo_toml(path)?;
25+
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml)?;
26+
let sysroot = Sysroot::discover(&cargo_toml)?;
27+
let res = ProjectWorkspace { cargo, sysroot };
28+
Ok(res)
29+
}
30+
}
31+
32+
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
33+
if path.ends_with("Cargo.toml") {
34+
return Ok(path.to_path_buf());
35+
}
36+
let mut curr = Some(path);
37+
while let Some(path) = curr {
38+
let candidate = path.join("Cargo.toml");
39+
if candidate.exists() {
40+
return Ok(candidate);
41+
}
42+
curr = path.parent();
43+
}
44+
bail!("can't find Cargo.toml at {}", path.display())
45+
}

crates/ra_lsp_server/src/project_model/sysroot.rs renamed to crates/ra_project_model/src/sysroot.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::{
33
process::Command,
44
};
55

6-
use ra_syntax::SmolStr;
6+
use smol_str::SmolStr;
7+
78
use ra_arena::{Arena, RawId, impl_arena_id};
89

910
use crate::Result;
@@ -25,15 +26,15 @@ struct SysrootCrateData {
2526
}
2627

2728
impl Sysroot {
28-
pub(crate) fn std(&self) -> Option<SysrootCrate> {
29+
pub fn std(&self) -> Option<SysrootCrate> {
2930
self.by_name("std")
3031
}
3132

32-
pub(crate) fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + 'a {
33+
pub fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + 'a {
3334
self.crates.iter().map(|(id, _data)| id)
3435
}
3536

36-
pub(super) fn discover(cargo_toml: &Path) -> Result<Sysroot> {
37+
pub fn discover(cargo_toml: &Path) -> Result<Sysroot> {
3738
let rustc_output = Command::new("rustc")
3839
.current_dir(cargo_toml.parent().unwrap())
3940
.args(&["--print", "sysroot"])
@@ -80,16 +81,16 @@ impl Sysroot {
8081
}
8182

8283
impl SysrootCrate {
83-
pub(crate) fn name(self, sysroot: &Sysroot) -> &SmolStr {
84+
pub fn name(self, sysroot: &Sysroot) -> &SmolStr {
8485
&sysroot.crates[self].name
8586
}
86-
pub(crate) fn root(self, sysroot: &Sysroot) -> &Path {
87+
pub fn root(self, sysroot: &Sysroot) -> &Path {
8788
sysroot.crates[self].root.as_path()
8889
}
89-
pub(crate) fn root_dir(self, sysroot: &Sysroot) -> &Path {
90+
pub fn root_dir(self, sysroot: &Sysroot) -> &Path {
9091
self.root(sysroot).parent().unwrap()
9192
}
92-
pub(crate) fn deps<'a>(self, sysroot: &'a Sysroot) -> impl Iterator<Item = SysrootCrate> + 'a {
93+
pub fn deps<'a>(self, sysroot: &'a Sysroot) -> impl Iterator<Item = SysrootCrate> + 'a {
9394
sysroot.crates[self].deps.iter().map(|&it| it)
9495
}
9596
}

crates/ra_syntax/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ drop_bomb = "0.1.4"
1515
parking_lot = "0.7.0"
1616
rowan = "0.3.3"
1717

18-
# ideally, `serde` should be enabled by `ra_lsp_serder`, but we enable it here
18+
# ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here
1919
# to reduce number of compilations
2020
text_unit = { version = "0.1.6", features = ["serde"] }
2121
smol_str = { version = "0.1.9", features = ["serde"] }

0 commit comments

Comments
 (0)