fix: import type inference result from other modules (#2168)
Some checks failed
tinymist::auto_tag / auto-tag (push) Has been cancelled
tinymist::ci / Duplicate Actions Detection (push) Has been cancelled
tinymist::ci / Check Clippy, Formatting, Completion, Documentation, and Tests (Linux) (push) Has been cancelled
tinymist::ci / Check Minimum Rust version and Tests (Windows) (push) Has been cancelled
tinymist::ci / prepare-build (push) Has been cancelled
tinymist::gh_pages / build-gh-pages (push) Has been cancelled
tinymist::ci / announce (push) Has been cancelled
tinymist::ci / build (push) Has been cancelled

fix #2131
This commit is contained in:
Myriad-Dreamin 2025-10-15 11:22:39 +08:00 committed by GitHub
parent d80199d744
commit 29a10c144e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 215 additions and 39 deletions

View file

@ -11,7 +11,7 @@ use rustc_hash::FxHashMap;
use tinymist_analysis::docs::DocString;
use tinymist_analysis::stats::AllocStats;
use tinymist_analysis::syntax::classify_def_loosely;
use tinymist_analysis::ty::term_value;
use tinymist_analysis::ty::{BuiltinTy, InsTy, term_value};
use tinymist_analysis::{analyze_expr_, analyze_import_};
use tinymist_lint::{KnownIssues, LintInfo};
use tinymist_project::{LspComputeGraph, LspWorld, TaskWhen};
@ -700,51 +700,68 @@ impl SharedContext {
})
}
/// Get a module by file id.
/// Gets a module by file id.
pub fn module_by_id(&self, fid: TypstFileId) -> SourceResult<Module> {
let source = self.source_by_id(fid).at(Span::detached())?;
self.module_by_src(source)
}
/// Get a module by string.
/// Gets a module by string.
pub fn module_by_str(&self, rr: String) -> Option<Module> {
let src = Source::new(*DETACHED_ENTRY, rr);
self.module_by_src(src).ok()
}
/// Get (Create) a module by source.
/// Gets (Creates) a module by source.
pub fn module_by_src(&self, source: Source) -> SourceResult<Module> {
eval_compat(&self.world, &source)
}
/// Try to load a module from the current source file.
pub fn module_by_syntax(&self, source: &SyntaxNode) -> Option<Value> {
/// Gets a module value from a given source file.
pub fn module_by_syntax(self: &Arc<Self>, source: &SyntaxNode) -> Option<Value> {
self.module_term_by_syntax(source, true)
.and_then(|ty| ty.value())
}
/// Gets a module term from a given source file. If `value` is true, it will
/// prefer to get a value instead of a term.
pub fn module_term_by_syntax(self: &Arc<Self>, source: &SyntaxNode, value: bool) -> Option<Ty> {
let (src, scope) = self.analyze_import(source);
if let Some(scope) = scope {
return Some(scope);
return Some(match scope {
Value::Module(m) if m.file_id().is_some() => {
Ty::Builtin(BuiltinTy::Module(Decl::module(m.file_id()?).into()))
}
scope => Ty::Value(InsTy::new(scope)),
});
}
match src {
Some(Value::Str(s)) => {
let id = resolve_id_by_path(&self.world, source.span().id()?, s.as_str())?;
self.module_by_id(id).ok().map(Value::Module)
Some(if value {
Ty::Value(InsTy::new(Value::Module(self.module_by_id(id).ok()?)))
} else {
Ty::Builtin(BuiltinTy::Module(Decl::module(id).into()))
})
}
_ => None,
}
}
/// Get the expression information of a source file.
/// Gets the expression information of a source file.
pub(crate) fn expr_stage_by_id(self: &Arc<Self>, fid: TypstFileId) -> Option<ExprInfo> {
Some(self.expr_stage(&self.source_by_id(fid).ok()?))
}
/// Get the expression information of a source file.
/// Gets the expression information of a source file.
pub(crate) fn expr_stage(self: &Arc<Self>, source: &Source) -> ExprInfo {
let mut route = ExprRoute::default();
self.expr_stage_(source, &mut route)
}
/// Get the expression information of a source file.
/// Gets the expression information of a source file.
pub(crate) fn expr_stage_(
self: &Arc<Self>,
source: &Source,
@ -769,13 +786,13 @@ impl SharedContext {
Some(self.expr_stage_(source, route).exports.clone())
}
/// Get the type check information of a source file.
/// Gets the type check information of a source file.
pub(crate) fn type_check(self: &Arc<Self>, source: &Source) -> Arc<TypeInfo> {
let mut route = TypeEnv::default();
self.type_check_(source, &mut route)
}
/// Get the type check information of a source file.
/// Gets the type check information of a source file.
pub(crate) fn type_check_(
self: &Arc<Self>,
source: &Source,
@ -796,7 +813,7 @@ impl SharedContext {
})
}
/// Get the lint result of a source file.
/// Gets the lint result of a source file.
#[typst_macros::time(span = source.root().span())]
pub(crate) fn lint(self: &Arc<Self>, source: &Source, issues: &KnownIssues) -> LintInfo {
let ei = self.expr_stage(source);