4927: Better encapsulate reverse-mapping of files to cargo targets r=matklad a=matklad

We need to find a better way to do it...

CrateGraph by itself is fine, CargoWorkspace as well, but the mapping
between the two seems arbitrary...



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-17 15:53:07 +00:00 committed by GitHub
commit 1ce8c2b5a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 24 deletions

View File

@ -2,7 +2,7 @@
use ra_cfg::CfgExpr; use ra_cfg::CfgExpr;
use ra_ide::{FileId, RunnableKind, TestId}; use ra_ide::{FileId, RunnableKind, TestId};
use ra_project_model::{self, ProjectWorkspace, TargetKind}; use ra_project_model::{self, TargetKind};
use crate::{global_state::GlobalStateSnapshot, Result}; use crate::{global_state::GlobalStateSnapshot, Result};
@ -89,27 +89,23 @@ impl CargoTargetSpec {
} }
pub(crate) fn for_file( pub(crate) fn for_file(
world: &GlobalStateSnapshot, global_state_snapshot: &GlobalStateSnapshot,
file_id: FileId, file_id: FileId,
) -> Result<Option<CargoTargetSpec>> { ) -> Result<Option<CargoTargetSpec>> {
let &crate_id = match world.analysis().crate_for(file_id)?.first() { let crate_id = match global_state_snapshot.analysis().crate_for(file_id)?.first() {
Some(crate_id) => crate_id, Some(crate_id) => *crate_id,
None => return Ok(None), None => return Ok(None),
}; };
let file_id = world.analysis().crate_root(crate_id)?; let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
let path = world.file_id_to_path(file_id); Some(it) => it,
let res = world.workspaces.iter().find_map(|ws| match ws { None => return Ok(None),
ProjectWorkspace::Cargo { cargo, .. } => { };
let tgt = cargo.target_by_root(&path)?; let res = CargoTargetSpec {
Some(CargoTargetSpec { package: cargo_ws.package_flag(&cargo_ws[cargo_ws[target].package]),
package: cargo.package_flag(&cargo[cargo[tgt].package]), target: cargo_ws[target].name.clone(),
target: cargo[tgt].name.clone(), target_kind: cargo_ws[target].kind,
target_kind: cargo[tgt].kind, };
}) Ok(Some(res))
}
ProjectWorkspace::Json { .. } => None,
});
Ok(res)
} }
pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) { pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {

View File

@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
use ra_ide::{ use ra_ide::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
}; };
use ra_project_model::{ProcMacroClient, ProjectWorkspace}; use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target};
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use stdx::format_to; use stdx::format_to;
@ -28,7 +28,7 @@ use crate::{
vfs_glob::{Glob, RustPackageFilterBuilder}, vfs_glob::{Glob, RustPackageFilterBuilder},
LspError, Result, LspError, Result,
}; };
use ra_db::ExternSourceId; use ra_db::{CrateId, ExternSourceId};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> { fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> {
@ -290,10 +290,6 @@ impl GlobalStateSnapshot {
file_id_to_url(&self.vfs.read(), id) file_id_to_url(&self.vfs.read(), id)
} }
pub fn file_id_to_path(&self, id: FileId) -> PathBuf {
self.vfs.read().file2path(VfsFile(id.0))
}
pub fn file_line_endings(&self, id: FileId) -> LineEndings { pub fn file_line_endings(&self, id: FileId) -> LineEndings {
self.vfs.read().file_line_endings(VfsFile(id.0)) self.vfs.read().file_line_endings(VfsFile(id.0))
} }
@ -305,6 +301,20 @@ impl GlobalStateSnapshot {
url_from_abs_path(&path) url_from_abs_path(&path)
} }
pub(crate) fn cargo_target_for_crate_root(
&self,
crate_id: CrateId,
) -> Option<(&CargoWorkspace, Target)> {
let file_id = self.analysis().crate_root(crate_id).ok()?;
let path = self.vfs.read().file2path(VfsFile(file_id.0));
self.workspaces.iter().find_map(|ws| match ws {
ProjectWorkspace::Cargo { cargo, .. } => {
cargo.target_by_root(&path).map(|it| (cargo, it))
}
ProjectWorkspace::Json { .. } => None,
})
}
pub fn status(&self) -> String { pub fn status(&self) -> String {
let mut buf = String::new(); let mut buf = String::new();
if self.workspaces.is_empty() { if self.workspaces.is_empty() {