fix: resolve relative path in subfolders (#1574)

This commit is contained in:
Myriad-Dreamin 2025-03-26 11:10:51 +08:00 committed by GitHub
parent 9790153381
commit d4fffe3543
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 92 additions and 88 deletions

View file

@ -24,6 +24,7 @@ pub fn get_link_exprs_in(node: &LinkedNode) -> Option<LinkInfo> {
}
/// A valid link target.
#[derive(Debug)]
pub enum LinkTarget {
/// A package specification.
Package(Box<PackageSpec>),
@ -40,7 +41,7 @@ impl LinkTarget {
LinkTarget::Url(url) => Some(url.as_ref().clone()),
LinkTarget::Path(id, path) => {
// Avoid creating new ids here.
let root = ctx.path_for_id(id.join("/")).ok()?;
let root = ctx.path_for_id(id.join("")).ok()?;
crate::path_res_to_url(root.join(path).ok()?).ok()
}
}
@ -48,6 +49,7 @@ impl LinkTarget {
}
/// A link object in a source file.
#[derive(Debug)]
pub struct LinkObject {
/// The range of the link expression.
pub range: Range<usize>,
@ -58,7 +60,7 @@ pub struct LinkObject {
}
/// Link information in a source file.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct LinkInfo {
/// The link objects in a source file.
pub objects: Vec<LinkObject>,

View file

@ -0,0 +1,5 @@
/// path: src/sub_path.json
{}
-----
/// path: src/main.typ
#json(/* position after */ "sub_path.json");

View file

@ -0,0 +1,11 @@
---
source: crates/tinymist-query/src/document_link.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/document_link/json_sub_path.typ
---
[
{
"range": "0:28:0:41",
"target": "src/sub_path.json"
}
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/at_def.typ
snapshot_kind: text
---
[
"/s0.typ@0:23:0:24",
"/s0.typ@1:2:1:3",
"/s0.typ@2:2:2:3"
"s0.typ@0:23:0:24",
"s0.typ@1:2:1:3",
"s0.typ@2:2:2:3"
]

View file

@ -2,9 +2,8 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/base.typ
snapshot_kind: text
---
[
"/s0.typ@0:5:0:6",
"/s0.typ@1:23:1:24"
"s0.typ@0:5:0:6",
"s0.typ@1:23:1:24"
]

View file

@ -2,11 +2,10 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_file_ref_label.typ
snapshot_kind: text
---
[
"/base1.typ@1:10:1:13",
"/base2.typ@1:10:1:13",
"/base2.typ@3:10:3:13",
"/s2.typ@6:31:6:34"
"base1.typ@1:10:1:13",
"base2.typ@1:10:1:13",
"base2.typ@3:10:3:13",
"s2.typ@6:31:6:34"
]

View file

@ -2,9 +2,8 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module.typ
snapshot_kind: text
---
[
"/base.typ@0:23:0:24",
"/s0.typ@1:1:1:2"
"base.typ@0:23:0:24",
"s0.typ@1:1:1:2"
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module2.typ
snapshot_kind: text
---
[
"/base.typ@0:23:0:24",
"/s0.typ@0:20:0:21",
"/s0.typ@1:1:1:2"
"base.typ@0:23:0:24",
"s0.typ@0:20:0:21",
"s0.typ@1:1:1:2"
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module_absolute.typ
snapshot_kind: text
---
[
"/out/base.typ@0:23:0:24",
"/out/main.typ@0:25:0:26",
"/out/main.typ@1:1:1:2"
"out/base.typ@0:23:0:24",
"out/main.typ@0:25:0:26",
"out/main.typ@1:1:1:2"
]

View file

@ -2,11 +2,10 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module_alias.typ
snapshot_kind: text
---
[
"/base.typ@0:5:0:6",
"/s1.typ@0:20:0:21",
"/s1.typ@0:43:0:45",
"/s1.typ@1:1:1:3"
"base.typ@0:5:0:6",
"s1.typ@0:20:0:21",
"s1.typ@0:43:0:45",
"s1.typ@1:1:1:3"
]

View file

@ -2,11 +2,10 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module_alias2.typ
snapshot_kind: text
---
[
"/base.typ@0:23:0:24",
"/s0.typ@0:20:0:21",
"/s0.typ@0:25:0:27",
"/s0.typ@1:1:1:3"
"base.typ@0:23:0:24",
"s0.typ@0:20:0:21",
"s0.typ@0:25:0:27",
"s0.typ@1:1:1:3"
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/cross_module_relative.typ
snapshot_kind: text
---
[
"/out/base.typ@0:23:0:24",
"/out/main.typ@0:20:0:21",
"/out/main.typ@1:1:1:2"
"out/base.typ@0:23:0:24",
"out/main.typ@0:20:0:21",
"out/main.typ@1:1:1:2"
]

View file

@ -2,8 +2,7 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/label.typ
snapshot_kind: text
---
[
"/s0.typ@5:0:5:12"
"s0.typ@5:0:5:12"
]

View file

@ -2,12 +2,11 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/recursive_import.typ
snapshot_kind: text
---
[
"/base.typ@0:23:0:24",
"/base2.typ@0:20:0:21",
"/base2.typ@1:1:1:2",
"/s0.typ@0:21:0:22",
"/s0.typ@1:1:1:2"
"base.typ@0:23:0:24",
"base2.typ@0:20:0:21",
"base2.typ@1:1:1:2",
"s0.typ@0:21:0:22",
"s0.typ@1:1:1:2"
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/recursive_import_star.typ
snapshot_kind: text
---
[
"/base.typ@0:23:0:24",
"/base2.typ@1:1:1:2",
"/s0.typ@1:1:1:2"
"base.typ@0:23:0:24",
"base2.typ@1:1:1:2",
"s0.typ@1:1:1:2"
]

View file

@ -2,12 +2,11 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/redefine.typ
snapshot_kind: text
---
[
"/s0.typ@0:23:0:24",
"/s0.typ@3:2:3:3",
"/s0.typ@3:6:3:7",
"/s0.typ@6:12:6:13",
"/s0.typ@8:9:8:10"
"s0.typ@0:23:0:24",
"s0.typ@3:2:3:3",
"s0.typ@3:6:3:7",
"s0.typ@6:12:6:13",
"s0.typ@8:9:8:10"
]

View file

@ -2,10 +2,9 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/ref_label.typ
snapshot_kind: text
---
[
"/s0.typ@5:0:5:12",
"/s0.typ@7:21:7:33",
"/s0.typ@9:0:9:12"
"s0.typ@5:0:5:12",
"s0.typ@7:21:7:33",
"s0.typ@9:0:9:12"
]

View file

@ -2,11 +2,10 @@
source: crates/tinymist-query/src/references.rs
expression: "JsonRepr::new_pure(result)"
input_file: crates/tinymist-query/src/fixtures/references/rename_issue_exercise.typ
snapshot_kind: text
---
[
"/basic/mod.typ@0:22:0:30",
"/basic/mod.typ@1:1:1:9",
"/basic/writing.typ@1:1:1:9",
"/mod.typ@0:23:0:31"
"basic/mod.typ@0:22:0:30",
"basic/mod.typ@1:1:1:9",
"basic/writing.typ@1:1:1:9",
"mod.typ@0:23:0:31"
]

View file

@ -2,7 +2,6 @@
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/module_path.typ
snapshot_kind: text
---
{
"documentChanges": [
@ -20,7 +19,7 @@ snapshot_kind: text
},
{
"kind": "rename",
"newUri": "new_name.typ",
"newUri": "variable.typ/../new_name.typ",
"oldUri": "variable.typ"
}
]

View file

@ -2,7 +2,6 @@
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/module_path_alias.typ
snapshot_kind: text
---
{
"documentChanges": [
@ -20,7 +19,7 @@ snapshot_kind: text
},
{
"kind": "rename",
"newUri": "new_name.typ",
"newUri": "variable.typ/../new_name.typ",
"oldUri": "variable.typ"
}
]

View file

@ -2,7 +2,6 @@
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/module_path_non_cano.typ
snapshot_kind: text
---
{
"documentChanges": [
@ -20,7 +19,7 @@ snapshot_kind: text
},
{
"kind": "rename",
"newUri": "new_name.typ",
"newUri": "variable.typ/../../../new_name.typ",
"oldUri": "variable.typ"
}
]

View file

@ -2,7 +2,6 @@
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/module_path_star.typ
snapshot_kind: text
---
{
"documentChanges": [
@ -20,7 +19,7 @@ snapshot_kind: text
},
{
"kind": "rename",
"newUri": "new_name.typ",
"newUri": "variable.typ/../new_name.typ",
"oldUri": "variable.typ"
}
]

View file

@ -2,7 +2,6 @@
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/resources.typ
snapshot_kind: text
---
{
"documentChanges": [
@ -24,7 +23,7 @@ snapshot_kind: text
},
{
"kind": "rename",
"newUri": "new_name.typ",
"newUri": "lib.typ/../new_name.typ",
"oldUri": "lib.typ"
}
]

View file

@ -167,11 +167,9 @@ impl ReferencesWorker<'_> {
#[cfg(test)]
mod tests {
use tinymist_std::path::unix_slash;
use super::*;
use crate::syntax::find_module_level_docs;
use crate::{tests::*, url_to_path};
use crate::tests::*;
#[test]
fn test() {
@ -191,8 +189,7 @@ mod tests {
let mut result = result.map(|v| {
v.into_iter()
.map(|loc| {
let fp = unix_slash(&url_to_path(loc.uri));
let fp = fp.strip_prefix("C:").unwrap_or(&fp);
let fp = file_path(loc.uri.as_str());
format!(
"{fp}@{}:{}:{}:{}",
loc.range.start.line,

View file

@ -12,6 +12,7 @@ use once_cell::sync::Lazy;
use serde_json::{ser::PrettyFormatter, Serializer, Value};
use tinymist_project::{CompileFontArgs, ExportTarget, LspCompileSnapshot, LspComputeGraph};
use tinymist_std::debug_loc::LspRange;
use tinymist_std::path::unix_slash;
use tinymist_std::typst::TypstDocument;
use tinymist_world::package::PackageSpec;
use tinymist_world::vfs::WorkspaceResolver;
@ -145,9 +146,9 @@ pub fn compile_doc_for_test(
pub fn run_with_sources<T>(source: &str, f: impl FnOnce(&mut LspUniverse, PathBuf) -> T) -> T {
let root = if cfg!(windows) {
PathBuf::from("C:\\")
PathBuf::from("C:\\root")
} else {
PathBuf::from("/")
PathBuf::from("/root")
};
let mut verse = LspUniverseBuilder::build(
EntryState::new_rooted(root.as_path().into(), None),
@ -181,6 +182,7 @@ pub fn run_with_sources<T>(source: &str, f: impl FnOnce(&mut LspUniverse, PathBu
};
let path = path.unwrap_or_else(|| format!("/s{idx}.typ"));
let path = path.strip_prefix("/").unwrap_or(path.as_str());
let pw = root.join(Path::new(&path));
verse
@ -415,12 +417,12 @@ impl Redact for RedactFields {
map.insert(
key.to_owned(),
Value::Object(
obj.iter().map(|(k, v)| (file_name(k), v.clone())).collect(),
obj.iter().map(|(k, v)| (file_path(k), v.clone())).collect(),
),
);
}
"uri" | "target" | "oldUri" | "newUri" | "targetUri" => {
map.insert(key.to_owned(), file_name(t.as_str().unwrap()).into());
map.insert(key.to_owned(), file_path(t.as_str().unwrap()).into());
}
"range"
| "selectionRange"
@ -457,9 +459,16 @@ impl Redact for RedactFields {
}
}
fn file_name(path: &str) -> String {
let name = Path::new(path).file_name().unwrap();
name.to_str().unwrap().to_owned()
pub(crate) fn file_path(uri: &str) -> String {
let root = if cfg!(windows) {
PathBuf::from("C:\\root")
} else {
PathBuf::from("/root")
};
let uri = uri.replace("file://", "");
let abs_path = Path::new(&uri).strip_prefix(root).map(|s| s.as_os_str());
let rel_path = abs_path.unwrap_or_else(|_| Path::new(&uri).file_name().unwrap());
unix_slash(Path::new(rel_path.to_str().unwrap()))
}
pub struct HashRepr<T>(pub T);