1 //! FIXME: write short doc here
6 use ra_cfg::CfgOptions;
7 use ra_db::{CrateName, Env, RelativePathBuf};
8 use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
11 Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
17 Plain { path: String, content: String },
18 Fixture(FixtureEntry),
22 fn new(path: String, content: String) -> Self {
23 // `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` '
24 // see https://github.com/rust-lang/rust/issues/69018
25 MockFileData::Plain { path, content }
28 fn path(&self) -> &str {
30 MockFileData::Plain { path, .. } => path.as_str(),
31 MockFileData::Fixture(f) => f.meta.path().as_str(),
35 fn content(&self) -> &str {
37 MockFileData::Plain { content, .. } => content,
38 MockFileData::Fixture(f) => f.text.as_str(),
42 fn cfg_options(&self) -> CfgOptions {
44 MockFileData::Fixture(f) => {
45 f.meta.cfg_options().map_or_else(Default::default, |o| o.clone())
47 _ => CfgOptions::default(),
51 fn edition(&self) -> Edition {
53 MockFileData::Fixture(f) => {
54 f.meta.edition().map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap())
56 _ => Edition::Edition2018,
60 fn env(&self) -> Env {
62 MockFileData::Fixture(f) => Env::from(f.meta.env()),
68 impl From<FixtureEntry> for MockFileData {
69 fn from(fixture: FixtureEntry) -> Self {
70 Self::Fixture(fixture)
74 /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
75 /// from a set of in-memory files.
76 #[derive(Debug, Default)]
77 pub struct MockAnalysis {
78 files: Vec<MockFileData>,
82 pub fn new() -> MockAnalysis {
83 MockAnalysis::default()
85 /// Creates `MockAnalysis` using a fixture data in the following format:
95 pub fn with_files(fixture: &str) -> MockAnalysis {
96 let mut res = MockAnalysis::new();
97 for entry in parse_fixture(fixture) {
98 res.add_file_fixture(entry);
103 /// Same as `with_files`, but requires that a single file contains a `<|>` marker,
104 /// whose position is also returned.
105 pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
106 let mut position = None;
107 let mut res = MockAnalysis::new();
108 for entry in parse_fixture(fixture) {
109 if entry.text.contains(CURSOR_MARKER) {
110 assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
111 position = Some(res.add_file_fixture_with_position(entry));
113 res.add_file_fixture(entry);
116 let position = position.expect("expected a marker (<|>)");
120 pub fn add_file_fixture(&mut self, fixture: FixtureEntry) -> FileId {
121 let file_id = self.next_id();
122 self.files.push(MockFileData::from(fixture));
126 pub fn add_file_fixture_with_position(&mut self, mut fixture: FixtureEntry) -> FilePosition {
127 let (offset, text) = extract_offset(&fixture.text);
129 let file_id = self.next_id();
130 self.files.push(MockFileData::from(fixture));
131 FilePosition { file_id, offset }
134 pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
135 let file_id = self.next_id();
136 self.files.push(MockFileData::new(path.to_string(), text.to_string()));
139 pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
140 let (offset, text) = extract_offset(text);
141 let file_id = self.next_id();
142 self.files.push(MockFileData::new(path.to_string(), text));
143 FilePosition { file_id, offset }
145 pub fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
146 let (range, text) = extract_range(text);
147 let file_id = self.next_id();
148 self.files.push(MockFileData::new(path.to_string(), text));
149 FileRange { file_id, range }
151 pub fn id_of(&self, path: &str) -> FileId {
156 .find(|(_, data)| path == data.path())
157 .expect("no file in this mock");
158 FileId(idx as u32 + 1)
160 pub fn analysis_host(self) -> AnalysisHost {
161 let mut host = AnalysisHost::default();
162 let source_root = SourceRootId(0);
163 let mut change = AnalysisChange::new();
164 change.add_root(source_root, true);
165 let mut crate_graph = CrateGraph::default();
166 let mut root_crate = None;
167 for (i, data) in self.files.into_iter().enumerate() {
168 let path = data.path();
169 assert!(path.starts_with('/'));
170 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
171 let cfg_options = data.cfg_options();
172 let file_id = FileId(i as u32 + 1);
173 let edition = data.edition();
174 let env = data.env();
175 if path == "/lib.rs" || path == "/main.rs" {
176 root_crate = Some(crate_graph.add_crate_root(
185 } else if path.ends_with("/lib.rs") {
186 let crate_name = path.parent().unwrap().file_name().unwrap();
187 let other_crate = crate_graph.add_crate_root(
190 Some(CrateName::new(crate_name).unwrap()),
196 if let Some(root_crate) = root_crate {
198 .add_dep(root_crate, CrateName::new(crate_name).unwrap(), other_crate)
202 change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned()));
204 change.set_crate_graph(crate_graph);
205 host.apply_change(change);
208 pub fn analysis(self) -> Analysis {
209 self.analysis_host().analysis()
212 fn next_id(&self) -> FileId {
213 FileId((self.files.len() + 1) as u32)
217 /// Creates analysis from a multi-file fixture, returns positions marked with <|>.
218 pub fn analysis_and_position(ra_fixture: &str) -> (Analysis, FilePosition) {
219 let (mock, position) = MockAnalysis::with_files_and_position(ra_fixture);
220 (mock.analysis(), position)
223 /// Creates analysis for a single file.
224 pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
225 let mut mock = MockAnalysis::new();
226 let file_id = mock.add_file("/main.rs", ra_fixture);
227 (mock.analysis(), file_id)
230 /// Creates analysis for a single file, returns position marked with <|>.
231 pub fn single_file_with_position(ra_fixture: &str) -> (Analysis, FilePosition) {
232 let mut mock = MockAnalysis::new();
233 let pos = mock.add_file_with_position("/main.rs", ra_fixture);
234 (mock.analysis(), pos)
237 /// Creates analysis for a single file, returns range marked with a pair of <|>.
238 pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) {
239 let mut mock = MockAnalysis::new();
240 let pos = mock.add_file_with_range("/main.rs", ra_fixture);
241 (mock.analysis(), pos)