feat: add depended_{paths,{source_,}files} methods (#1150)

This commit is contained in:
Myriad-Dreamin 2025-01-11 17:44:23 +08:00 committed by GitHub
parent ec121070d8
commit f43a8e116c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 17 deletions

View file

@ -355,6 +355,39 @@ impl LocalContext {
}
}
/// Get all depended files in the workspace, inclusively.
pub fn depended_source_files(&self) -> Vec<TypstFileId> {
let mut ids = self.depended_files();
let preference = PathPreference::Source {
allow_package: false,
};
ids.retain(|id| preference.is_match(id.vpath().as_rooted_path()));
ids
}
/// Get all depended file ids of a compilation, inclusively.
/// Note: must be called after compilation.
pub fn depended_files(&self) -> Vec<TypstFileId> {
let mut ids = vec![];
for dep in self.depended_paths() {
if let Ok(ref_fid) = self.file_id_by_path(&dep) {
ids.push(ref_fid);
}
}
ids
}
/// Get depended paths of a compilation.
/// Note: must be called after compilation.
pub(crate) fn depended_paths(&self) -> EcoVec<reflexo::ImmutPath> {
let mut deps = EcoVec::new();
self.world.iter_dependencies(&mut |path| {
deps.push(path);
});
deps
}
/// Get the world surface for Typst compiler.
pub fn world(&self) -> &LspWorld {
&self.shared.world
@ -601,17 +634,6 @@ impl SharedContext {
classify_syntax(node, cursor)
}
/// Get the real definition of a compilation.
/// Note: must be called after compilation.
pub(crate) fn dependencies(&self) -> EcoVec<reflexo::ImmutPath> {
let mut deps = EcoVec::new();
self.world.iter_dependencies(&mut |path| {
deps.push(path);
});
deps
}
/// Resolve extra font information.
pub fn font_info(&self, font: typst::text::Font) -> Option<Arc<DataSource>> {
self.world.font_resolver.describe_font(&font)

View file

@ -82,7 +82,7 @@ impl ReferencesWorker<'_> {
fn label_root(mut self) -> Option<Vec<LspLocation>> {
let mut ids = vec![];
for dep in self.ctx.ctx.dependencies() {
for dep in self.ctx.ctx.depended_paths() {
if let Ok(ref_fid) = self.ctx.ctx.file_id_by_path(&dep) {
ids.push(ref_fid);
}

View file

@ -36,9 +36,7 @@ impl SemanticRequest for SymbolRequest {
let mut symbols = vec![];
// todo! need compilation for iter_dependencies
for path in ctx.dependencies() {
for path in ctx.depended_paths() {
let Ok(source) = ctx.source_by_path(&path) else {
continue;
};

View file

@ -1,4 +1,5 @@
use core::fmt;
use std::path::Path;
use ecow::{eco_format, EcoString};
use once_cell::sync::Lazy;
@ -90,9 +91,13 @@ impl PathPreference {
}
}
pub fn is_match(&self, path: &Path) -> bool {
let ext = path.extension().and_then(|ext| ext.to_str());
ext.map_or(false, |ext| self.ext_matcher().is_match(ext))
}
pub fn from_ext(path: &str) -> Option<Self> {
let path = std::path::Path::new(path).extension()?.to_str()?;
PathPreference::iter().find(|preference| preference.ext_matcher().is_match(path))
PathPreference::iter().find(|preference| preference.is_match(std::path::Path::new(path)))
}
}