feat: find module path destination

This commit is contained in:
Myriad-Dreamin 2024-03-10 00:20:25 +08:00
parent e0c9d95c5a
commit 549d74858f
3 changed files with 33 additions and 1 deletions

View file

@ -90,11 +90,12 @@ fn advance_prev_adjacent(node: LinkedNode) -> Option<LinkedNode> {
}
}
fn find_source_by_import(
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) => {
@ -395,6 +396,22 @@ pub(crate) fn find_definition<'a>(
is_ident_only = true;
may_ident
}
ast::Expr::Str(..) => {
if let Some(parent) = ancestor.parent() {
let e = parent.cast::<ast::ModuleImport>()?;
let source = find_source_by_import(world, current, e)?;
let src = ancestor.find(e.source().span())?;
return Some(Definition::Module(ModuleDefinition {
module: source.id(),
use_site: src,
span: source.root().span(),
}));
}
return None;
}
ast::Expr::Import(..) => {
return None;
}
_ => {
debug!("unsupported kind {kind:?}", kind = ancestor.kind());
return None;

View file

@ -0,0 +1,3 @@
// path: base.typ
-----
#import /* position after */ "base.typ"

View file

@ -0,0 +1,12 @@
---
source: crates/tinymist-query/src/goto_definition.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/goto_definition/import_path_inner.typ
---
[
{
"originSelectionRange": "0:29:0:39",
"targetRange": "0:0:0:0",
"targetSelectionRange": "0:0:0:0"
}
]