dev: move import analyzer code

This commit is contained in:
Myriad-Dreamin 2024-03-10 00:59:34 +08:00
parent 549d74858f
commit 4e6e25f816
3 changed files with 36 additions and 34 deletions

View file

@ -1,10 +1,8 @@
use std::ops::Deref;
use std::path::Path;
use comemo::Tracked;
use log::{debug, trace};
use typst::syntax::ast::Ident;
use typst::syntax::VirtualPath;
use typst::World;
use typst::{
foundations::{Func, Value},
@ -15,7 +13,8 @@ use typst::{
};
use typst_ts_core::TypstFileId;
use crate::{prelude::analyze_expr, TypstSpan};
use crate::analysis::find_source_by_import;
use crate::{prelude::*, TypstSpan};
#[derive(Debug, Clone)]
pub struct VariableDefinition<'a> {
@ -90,36 +89,6 @@ fn advance_prev_adjacent(node: LinkedNode) -> Option<LinkedNode> {
}
}
pub fn find_source_by_import(
world: Tracked<'_, dyn World>,
current: TypstFileId,
import_node: ast::ModuleImport,
) -> Option<Source> {
// todo: this could be vaild: import("path.typ"), where v is parenthesized
let v = import_node.source();
match v {
ast::Expr::Str(s) => {
let s = s.get();
if s.starts_with('@') {
// todo: import from package
return None;
}
let path = Path::new(s.as_str());
let vpath = if path.is_relative() {
current.vpath().join(path)
} else {
VirtualPath::new(path)
};
let id = TypstFileId::new(current.package().cloned(), vpath);
world.source(id).ok()
}
_ => None,
}
}
#[comemo::memoize]
fn find_definition_in_module<'a>(
world: Tracked<'a, dyn World>,

View file

@ -4,6 +4,39 @@ use log::debug;
use typst::syntax::{ast, LinkedNode, Source, SyntaxKind, VirtualPath};
use typst_ts_core::{typst::prelude::EcoVec, TypstFileId};
use crate::prelude::*;
pub fn find_source_by_import(
world: Tracked<'_, dyn World>,
current: TypstFileId,
import_node: ast::ModuleImport,
) -> Option<Source> {
// todo: this could be vaild: import("path.typ"), where v is parenthesized
let v = import_node.source();
match v {
ast::Expr::Str(s) => {
let s = s.get();
if s.starts_with('@') {
// todo: import from package
return None;
}
let path = Path::new(s.as_str());
let vpath = if path.is_relative() {
current.vpath().join(path)
} else {
VirtualPath::new(path)
};
let id = TypstFileId::new(current.package().cloned(), vpath);
world.source(id).ok()
}
_ => None,
}
}
// todo: bad peformance
pub fn find_imports(
source: &Source,
def_id: Option<TypstFileId>,

View file

@ -5,7 +5,7 @@ pub use std::{
sync::Arc,
};
pub use comemo::Track;
pub use comemo::{Track, Tracked};
pub use itertools::{Format, Itertools};
pub use log::{error, trace};
pub use tower_lsp::lsp_types::{