-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit-id:69f01fb6
- Loading branch information
Showing
3 changed files
with
72 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use cairo_lang_defs::db::DefsGroup; | ||
use cairo_lang_filesystem::db::FilesGroup; | ||
use cairo_lang_filesystem::ids::FileId; | ||
use cairo_lang_filesystem::span::TextSpan; | ||
|
||
use crate::lang::db::AnalysisDatabase; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct SearchScope { | ||
/// A collection of all files constituting this search scope, with optional text spans to | ||
/// narrow down searching ranges. | ||
entries: HashMap<FileId, Option<TextSpan>>, | ||
} | ||
|
||
impl SearchScope { | ||
/// Builds a new empty search scope. | ||
pub fn empty() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Builds a search scope spanning an entire set of analysed crates. | ||
#[tracing::instrument(skip_all)] | ||
pub fn everything(db: &AnalysisDatabase) -> Self { | ||
let mut this = Self::empty(); | ||
for crate_id in db.crates() { | ||
for &module_id in db.crate_modules(crate_id).iter() { | ||
if let Ok(file_id) = db.module_main_file(module_id) { | ||
this.entries.insert(file_id, None); | ||
} | ||
} | ||
} | ||
this | ||
} | ||
|
||
/// Creates an iterator over all files and the optional search scope text spans. | ||
pub fn files_and_spans(&self) -> impl Iterator<Item = (FileId, Option<TextSpan>)> + use<'_> { | ||
self.entries.iter().map(|(&file, &span)| (file, span)) | ||
} | ||
|
||
/// Creates an iterator over all files, their contents and the optional search scope text spans. | ||
pub fn file_contents<'a, 'b>( | ||
&'a self, | ||
db: &'b AnalysisDatabase, | ||
) -> impl Iterator<Item = (FileId, Arc<str>, Option<TextSpan>)> + use<'a, 'b> { | ||
self.files_and_spans().filter_map(move |(file, span)| { | ||
let text = db.file_content(file)?; | ||
Some((file, text, span)) | ||
}) | ||
} | ||
} |