Import files from folders above: import ..a.b, import ...a.b
Leading dots go up one folder less than their count (.. is one up, ... is two). The language server index and formatter understand them, and a missing file is reported with its real path. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b496a7b03
commit
910bde692a
11 files changed
+139
-8
No files matched your search
@@ -113,3 +113,9 @@ fn step_as_a_name_and_c_imports() {
|
||||
"import \"raylib.h\" { Color, RED }\nimport \"zlib.h\" link \"z\"\n\nfunc step(dt: f32) {}\n\nfunc main() {\n step(1.0)\n for i in 0..10 step 2 { print(i) }\n}\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_paths_going_up_keep_their_dots() {
|
||||
let src = "import ...engine.engine\nimport ..side\nimport a.b\n\nfunc main() {\n for i in 0..3 { print(i) }\n}\n";
|
||||
assert_eq!(fmt(src), src);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//! Importing files from other folders: `import ..a.b` (one folder up), `import ...a.b` (two up).
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn tree(files: &[(&str, &str)]) -> PathBuf {
|
||||
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||
let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let dir = std::env::temp_dir().join(format!("rex-imports-{}-{n}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
for (name, text) in files {
|
||||
let f = dir.join(name);
|
||||
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
|
||||
std::fs::write(f, text).unwrap();
|
||||
}
|
||||
dir
|
||||
}
|
||||
|
||||
fn rex(dir: &PathBuf, sub: &str, file: &str) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_rex")).args(["run", file]).current_dir(dir.join(sub)).output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_to_file() {
|
||||
use rex::loader::key_file;
|
||||
assert_eq!(key_file("utils"), "utils.rex");
|
||||
assert_eq!(key_file("a.b"), "a/b.rex");
|
||||
assert_eq!(key_file("..a"), "../a.rex");
|
||||
assert_eq!(key_file("..a.b"), "../a/b.rex");
|
||||
assert_eq!(key_file("...a.b"), "../../a/b.rex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_from_a_folder_above() {
|
||||
let dir = tree(&[
|
||||
("engine/engine.rex", "pub struct Ball {\n pub x: float\n pub func moved(): float { return x + 1.0 }\n}\n\npub func hi(): int { return 7 }\n"),
|
||||
("game/app/main.rex", "import ...engine.engine\n\nfunc main() {\n var b = engine.Ball{2.0}\n print(engine.hi(), b.moved())\n}\n"),
|
||||
("game/side.rex", "pub func two(): int { return 2 }\n"),
|
||||
("game/app/other.rex", "import ..side\n\nfunc main() {\n print(side.two())\n}\n"),
|
||||
]);
|
||||
let o = rex(&dir, "game/app", "main.rex");
|
||||
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
|
||||
assert_eq!(String::from_utf8_lossy(&o.stdout), "7 3.0\n");
|
||||
let o = rex(&dir, "game/app", "other.rex");
|
||||
assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr));
|
||||
assert_eq!(String::from_utf8_lossy(&o.stdout), "2\n");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_above_names_the_path() {
|
||||
let dir = tree(&[("a/main.rex", "import ..nope\n\nfunc main() { }\n")]);
|
||||
let o = rex(&dir, "a", "main.rex");
|
||||
let err = String::from_utf8_lossy(&o.stderr).to_string();
|
||||
assert!(!o.status.success());
|
||||
assert!(err.contains("cannot find module `..nope`") && err.contains("../nope.rex"), "{err}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_dot_is_not_an_import_path() {
|
||||
let dir = tree(&[("a/main.rex", "import .nope\n\nfunc main() { }\n")]);
|
||||
let o = rex(&dir, "a", "main.rex");
|
||||
assert!(!o.status.success());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -267,3 +267,23 @@ fn hover_and_hints_for_c_functions() {
|
||||
let s = with_header(src, "textDocument/signatureHelp", 3, 22);
|
||||
assert_eq!(s.get("activeParameter").and_then(Json::as_i64), Some(2), "{s:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imports_from_a_folder_above_are_found() {
|
||||
let dir = std::env::temp_dir().join(format!("rex-lsp-up-{}", std::process::id()));
|
||||
std::fs::create_dir_all(dir.join("engine")).unwrap();
|
||||
std::fs::create_dir_all(dir.join("game")).unwrap();
|
||||
std::fs::write(dir.join("engine/engine.rex"), "pub func twice(x: int): int { return x * 2 }\n").unwrap();
|
||||
let uri = format!("file://{}/game/main.rex", dir.display());
|
||||
let src = "import ..engine.engine\nfunc main() {\n print(engine.twice(2))\n}\n";
|
||||
// hover on `twice` reads the signature from the file above
|
||||
let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, src)];
|
||||
msgs.push(request(2, "textDocument/hover", at(&uri, 2, 17)));
|
||||
msgs.extend(exit_messages());
|
||||
let (out, _) = talk(msgs);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let diags = out.iter().find(|m| m.get("method").is_some()).unwrap();
|
||||
assert_eq!(diags.path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{diags:?}");
|
||||
let hover = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap();
|
||||
assert!(format!("{hover:?}").contains("twice"), "{hover:?}");
|
||||
}
|
||||
Reference in new issue
Block a user