microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
language_service/src/test_utils.rs
57lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use crate::qsc_utils::Compilation; |
| 5 | use qsc::hir::PackageId; |
| 6 | use qsc::{compile, PackageStore, SourceMap}; |
| 7 | |
| 8 | pub(crate) fn get_source_and_marker_offsets( |
| 9 | source_with_markers: &str, |
| 10 | ) -> (String, Vec<u32>, Vec<u32>) { |
| 11 | let mut cursor_offsets: Vec<u32> = Vec::new(); |
| 12 | let mut target_offsets: Vec<u32> = Vec::new(); |
| 13 | let mut source = source_with_markers.to_string(); |
| 14 | let markers = &['↘', '◉']; |
| 15 | |
| 16 | loop { |
| 17 | let next_offset = source.find(markers); |
| 18 | match next_offset { |
| 19 | #[allow(clippy::cast_possible_truncation)] |
| 20 | Some(offset) => match source.chars().nth(offset) { |
| 21 | Some('↘') => cursor_offsets.push(offset as u32), |
| 22 | Some('◉') => target_offsets.push(offset as u32), |
| 23 | _ => panic!("Expected to find marker"), |
| 24 | }, |
| 25 | None => break, |
| 26 | }; |
| 27 | source = source.replacen(markers, "", 1); |
| 28 | } |
| 29 | (source, cursor_offsets, target_offsets) |
| 30 | } |
| 31 | |
| 32 | pub(crate) fn compile_with_fake_stdlib(source_name: &str, source_contents: &str) -> Compilation { |
| 33 | let mut package_store = PackageStore::new(compile::core()); |
| 34 | let std_source_map = SourceMap::new( |
| 35 | [( |
| 36 | "<std>".into(), |
| 37 | "namespace FakeStdLib { |
| 38 | operation Fake() : Unit {} |
| 39 | operation FakeCtlAdj() : Unit is Ctl + Adj {} |
| 40 | }" |
| 41 | .into(), |
| 42 | )], |
| 43 | None, |
| 44 | ); |
| 45 | let (std_compile_unit, std_errors) = |
| 46 | compile::compile(&package_store, &[PackageId::CORE], std_source_map); |
| 47 | assert!(std_errors.is_empty()); |
| 48 | let std_package_id = package_store.insert(std_compile_unit); |
| 49 | let source_map = SourceMap::new([(source_name.into(), source_contents.into())], None); |
| 50 | let (unit, errors) = compile::compile(&package_store, &[std_package_id], source_map); |
| 51 | Compilation { |
| 52 | package_store, |
| 53 | std_package_id, |
| 54 | unit, |
| 55 | errors, |
| 56 | } |
| 57 | } |
| 58 | |