|
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::*; |
0 commit comments