Skip to content

Commit 9ec160d

Browse files
authored
refactor(workspace): extract model types (#297)
2 parents 90dbf9b + 642881e commit 9ec160d

13 files changed

Lines changed: 522 additions & 460 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ project-model = { path = "./crates/project-model/", version = "0.0.0" }
5959
syntax = { path = "./crates/syntax/", version = "0.0.0" }
6060
utils = { path = "./crates/utils", version = "0.0.0" }
6161
vfs = { path = "./crates/vfs", version = "0.0.0" }
62+
workspace-model = { path = "./crates/workspace-model/", version = "0.0.0" }
6263
slang = { path = "./crates/slang", version = "0.0.1" }
6364
parking_lot = "0.12.1"
6465

crates/hir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ tracing.workspace = true
1919
triomphe.workspace = true
2020
utils.workspace = true
2121
vfs.workspace = true
22+
workspace-model.workspace = true

crates/hir/src/base_db/project.rs

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1 @@
1-
use triomphe::Arc;
2-
use utils::{line_index::TextRange, paths::AbsPathBuf};
3-
4-
use crate::base_db::source_root::SourceRootId;
5-
6-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7-
pub struct CompilationProfileId(pub u32);
8-
9-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
10-
pub struct PreprocessConfig {
11-
pub predefines: Vec<Predefine>,
12-
pub include_dirs: Vec<AbsPathBuf>,
13-
}
14-
15-
impl PreprocessConfig {
16-
pub fn with_predefine_strings(
17-
predefines: impl IntoIterator<Item = impl Into<String>>,
18-
include_dirs: Vec<AbsPathBuf>,
19-
) -> Self {
20-
Self {
21-
predefines: predefines.into_iter().map(|predefine| Predefine::new(predefine)).collect(),
22-
include_dirs,
23-
}
24-
}
25-
26-
pub fn include_dir_strings(&self) -> Vec<String> {
27-
self.include_dirs.iter().map(ToString::to_string).collect()
28-
}
29-
30-
pub fn predefine_strings(&self) -> Vec<String> {
31-
self.predefines.iter().map(|predefine| predefine.definition.clone()).collect()
32-
}
33-
}
34-
35-
#[derive(Debug, Clone, PartialEq, Eq)]
36-
pub struct Predefine {
37-
pub definition: String,
38-
pub source: Option<PredefineSource>,
39-
}
40-
41-
impl Predefine {
42-
pub fn new(definition: impl Into<String>) -> Self {
43-
Self { definition: definition.into(), source: None }
44-
}
45-
46-
pub fn with_source(definition: impl Into<String>, source: PredefineSource) -> Self {
47-
Self { definition: definition.into(), source: Some(source) }
48-
}
49-
50-
pub fn as_str(&self) -> &str {
51-
self.definition.as_str()
52-
}
53-
}
54-
55-
impl From<String> for Predefine {
56-
fn from(value: String) -> Self {
57-
Predefine::new(value)
58-
}
59-
}
60-
61-
impl From<&str> for Predefine {
62-
fn from(value: &str) -> Self {
63-
Predefine::new(value)
64-
}
65-
}
66-
67-
#[derive(Debug, Clone, PartialEq, Eq)]
68-
pub struct PredefineSource {
69-
pub path: AbsPathBuf,
70-
pub range: TextRange,
71-
}
72-
73-
#[derive(Debug, Clone, PartialEq, Eq)]
74-
pub struct CompilationProfile {
75-
pub source_roots: Vec<SourceRootId>,
76-
pub top_modules: Vec<String>,
77-
pub preprocess: PreprocessConfig,
78-
}
79-
80-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
81-
pub struct ProjectConfig {
82-
root_profiles: Vec<Option<CompilationProfileId>>,
83-
profiles: Vec<CompilationProfile>,
84-
}
85-
86-
impl ProjectConfig {
87-
pub fn new(
88-
root_profiles: Vec<Option<CompilationProfileId>>,
89-
profiles: Vec<CompilationProfile>,
90-
) -> Self {
91-
Self { root_profiles, profiles }
92-
}
93-
94-
pub fn profile_for_root(&self, root_id: SourceRootId) -> Option<CompilationProfileId> {
95-
self.root_profiles.get(root_id.0 as usize).copied().flatten()
96-
}
97-
98-
pub fn profile(&self, profile_id: CompilationProfileId) -> Option<&CompilationProfile> {
99-
self.profiles.get(profile_id.0 as usize)
100-
}
101-
102-
pub fn root_profile_count(&self) -> usize {
103-
self.root_profiles.len()
104-
}
105-
106-
pub fn has_compilation_profiles(&self) -> bool {
107-
!self.profiles.is_empty()
108-
}
109-
110-
pub fn profile_ids(&self) -> Vec<CompilationProfileId> {
111-
(0..self.profiles.len())
112-
.map(|idx| CompilationProfileId(u32::try_from(idx).unwrap_or(u32::MAX)))
113-
.collect()
114-
}
115-
116-
pub fn preprocess_for_profile(
117-
&self,
118-
profile_id: Option<CompilationProfileId>,
119-
) -> PreprocessConfig {
120-
profile_id
121-
.and_then(|profile_id| self.profile(profile_id))
122-
.map(|profile| profile.preprocess.clone())
123-
.unwrap_or_default()
124-
}
125-
}
126-
127-
pub type SharedProjectConfig = Arc<ProjectConfig>;
1+
pub use workspace_model::project::*;

crates/hir/src/base_db/source_db.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::{
55
};
66
use triomphe::Arc;
77
use utils::{line_index::TextSize, path_identity::PathIdentityIndex};
8-
use vfs::{FileId, VfsPath, anchored_path::AnchoredPath};
8+
use vfs::{FileId, anchored_path::AnchoredPath};
99

1010
use crate::base_db::{
1111
compilation_plan::{self, CompilationPlan},
@@ -16,6 +16,8 @@ use crate::base_db::{
1616

1717
mod preproc;
1818

19+
pub use workspace_model::source_db::SourceFileKind;
20+
1921
pub(crate) use self::preproc::workspace_preproc_model_file_ids;
2022
pub use self::preproc::{
2123
MappedSourcePreprocModel, PreprocManifestSource, PreprocSourceMap, PreprocSourceMapError,
@@ -63,40 +65,6 @@ pub trait SourceDb: FileLoader + std::fmt::Debug {
6365
fn project_config(&self) -> Arc<ProjectConfig>;
6466
}
6567

66-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
67-
pub enum SourceFileKind {
68-
#[default]
69-
SystemVerilog,
70-
IncludeHeader,
71-
LibraryMap,
72-
ProjectManifest,
73-
}
74-
75-
impl SourceFileKind {
76-
pub fn from_path(path: &VfsPath) -> Self {
77-
match path.name_and_extension() {
78-
Some((name, Some(ext))) if name == "vide" && ext.eq_ignore_ascii_case("toml") => {
79-
Self::ProjectManifest
80-
}
81-
Some((_, Some(ext))) if ext.eq_ignore_ascii_case("map") => Self::LibraryMap,
82-
Some((_, Some(ext)))
83-
if ["vh", "svh", "svi"].iter().any(|header| ext.eq_ignore_ascii_case(header)) =>
84-
{
85-
Self::IncludeHeader
86-
}
87-
_ => Self::SystemVerilog,
88-
}
89-
}
90-
91-
pub(crate) fn is_semantic_compilation_unit(self) -> bool {
92-
matches!(self, Self::SystemVerilog | Self::LibraryMap)
93-
}
94-
95-
fn is_slang_parse_unit(self) -> bool {
96-
matches!(self, Self::SystemVerilog | Self::LibraryMap)
97-
}
98-
}
99-
10068
fn parse_src(db: &dyn SourceDb, file_id: FileId) -> SyntaxTree {
10169
let _span = tracing::info_span!("slang.parse_src", ?file_id).entered();
10270
let text = db.file_text(file_id);

0 commit comments

Comments
 (0)