diff --git a/crates/tinymist-query/src/analysis/global.rs b/crates/tinymist-query/src/analysis/global.rs index e0e086a6..23ec2160 100644 --- a/crates/tinymist-query/src/analysis/global.rs +++ b/crates/tinymist-query/src/analysis/global.rs @@ -355,6 +355,39 @@ impl LocalContext { } } + /// Get all depended files in the workspace, inclusively. + pub fn depended_source_files(&self) -> Vec { + 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 { + 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 { + 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 { - 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> { self.world.font_resolver.describe_font(&font) diff --git a/crates/tinymist-query/src/references.rs b/crates/tinymist-query/src/references.rs index 30866c73..0f7ac67c 100644 --- a/crates/tinymist-query/src/references.rs +++ b/crates/tinymist-query/src/references.rs @@ -82,7 +82,7 @@ impl ReferencesWorker<'_> { fn label_root(mut self) -> Option> { 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); } diff --git a/crates/tinymist-query/src/symbol.rs b/crates/tinymist-query/src/symbol.rs index f1e0aab2..51924463 100644 --- a/crates/tinymist-query/src/symbol.rs +++ b/crates/tinymist-query/src/symbol.rs @@ -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; }; diff --git a/crates/tinymist-query/src/ty/builtin.rs b/crates/tinymist-query/src/ty/builtin.rs index 1d7720bd..9e7e01e0 100644 --- a/crates/tinymist-query/src/ty/builtin.rs +++ b/crates/tinymist-query/src/ty/builtin.rs @@ -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 { - 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))) } }