diff options
Diffstat (limited to 'support/texlab/crates/test-utils/src/fixture.rs')
-rw-r--r-- | support/texlab/crates/test-utils/src/fixture.rs | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/support/texlab/crates/test-utils/src/fixture.rs b/support/texlab/crates/test-utils/src/fixture.rs index 55ab162d28..9a768035c8 100644 --- a/support/texlab/crates/test-utils/src/fixture.rs +++ b/support/texlab/crates/test-utils/src/fixture.rs @@ -1,9 +1,7 @@ use std::path::PathBuf; -use base_db::{ - util::{LineCol, LineIndex}, - Owner, Workspace, -}; +use base_db::{DocumentLocation, FeatureParams, Owner, Workspace}; +use line_index::{LineCol, LineIndex}; use rowan::{TextRange, TextSize}; use url::Url; @@ -47,6 +45,28 @@ impl Fixture { documents, } } + + pub fn make_params(&self) -> Option<(FeatureParams, TextSize)> { + let spec = self + .documents + .iter() + .find(|spec| spec.cursor.is_some()) + .or_else(|| self.documents.first())?; + + let document = self.workspace.lookup(&spec.uri)?; + let params = FeatureParams::new(&self.workspace, document); + let cursor = spec.cursor.unwrap_or_default(); + Some((params, cursor)) + } + + pub fn locations(&self) -> impl Iterator<Item = DocumentLocation> { + self.documents.iter().flat_map(|spec| { + let document = self.workspace.lookup(&spec.uri).unwrap(); + spec.ranges + .iter() + .map(|range| DocumentLocation::new(document, *range)) + }) + } } #[derive(Debug)] @@ -99,10 +119,10 @@ impl DocumentSpec { let line_index = LineIndex::new(&text); - let cursor = cursor.map(|cursor| cursor.to_offset(&text, &line_index)); + let cursor = cursor.and_then(|cursor| cursor.to_offset(&text, &line_index)); let ranges = ranges .into_iter() - .map(|range| range.to_offset(&text, &line_index)) + .filter_map(|range| range.to_offset(&text, &line_index)) .collect(); Self { @@ -125,11 +145,11 @@ impl CharacterPosition { Self { line, col } } - fn to_offset(self, text: &str, line_index: &LineIndex) -> TextSize { + fn to_offset(self, text: &str, line_index: &LineIndex) -> Option<TextSize> { let start = line_index.offset(LineCol { line: (self.line - 1) as u32, col: 0, - }); + })?; let slice = &text[start.into()..]; let len = slice @@ -137,7 +157,7 @@ impl CharacterPosition { .nth(self.col) .map_or_else(|| slice.len(), |(i, _)| i); - start + TextSize::try_from(len).unwrap() + Some(start + TextSize::try_from(len).ok()?) } } @@ -152,9 +172,9 @@ impl CharacterRange { Self { start, end } } - fn to_offset(self, text: &str, line_index: &LineIndex) -> TextRange { - let start = self.start.to_offset(text, line_index); - let end = self.end.to_offset(text, line_index); - TextRange::new(start, end) + fn to_offset(self, text: &str, line_index: &LineIndex) -> Option<TextRange> { + let start = self.start.to_offset(text, line_index)?; + let end = self.end.to_offset(text, line_index)?; + Some(TextRange::new(start, end)) } } |