mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-03 01:42:14 +00:00
dev: move import analyzer code
This commit is contained in:
parent
549d74858f
commit
4e6e25f816
3 changed files with 36 additions and 34 deletions
|
@ -1,10 +1,8 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use comemo::Tracked;
|
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
use typst::syntax::ast::Ident;
|
use typst::syntax::ast::Ident;
|
||||||
use typst::syntax::VirtualPath;
|
|
||||||
use typst::World;
|
use typst::World;
|
||||||
use typst::{
|
use typst::{
|
||||||
foundations::{Func, Value},
|
foundations::{Func, Value},
|
||||||
|
@ -15,7 +13,8 @@ use typst::{
|
||||||
};
|
};
|
||||||
use typst_ts_core::TypstFileId;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct VariableDefinition<'a> {
|
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]
|
#[comemo::memoize]
|
||||||
fn find_definition_in_module<'a>(
|
fn find_definition_in_module<'a>(
|
||||||
world: Tracked<'a, dyn World>,
|
world: Tracked<'a, dyn World>,
|
||||||
|
|
|
@ -4,6 +4,39 @@ use log::debug;
|
||||||
use typst::syntax::{ast, LinkedNode, Source, SyntaxKind, VirtualPath};
|
use typst::syntax::{ast, LinkedNode, Source, SyntaxKind, VirtualPath};
|
||||||
use typst_ts_core::{typst::prelude::EcoVec, TypstFileId};
|
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(
|
pub fn find_imports(
|
||||||
source: &Source,
|
source: &Source,
|
||||||
def_id: Option<TypstFileId>,
|
def_id: Option<TypstFileId>,
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use comemo::Track;
|
pub use comemo::{Track, Tracked};
|
||||||
pub use itertools::{Format, Itertools};
|
pub use itertools::{Format, Itertools};
|
||||||
pub use log::{error, trace};
|
pub use log::{error, trace};
|
||||||
pub use tower_lsp::lsp_types::{
|
pub use tower_lsp::lsp_types::{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue