lsp import
This commit is contained in:
@@ -17,6 +17,31 @@ pub fn dirName(path: []const u8) []const u8 {
|
||||
return if (found) path[0..last_sep] else ".";
|
||||
}
|
||||
|
||||
/// Resolve an import path: try relative to base_dir first, fall back to cwd-relative.
|
||||
/// If root_path is provided, CWD-relative fallback paths are made absolute.
|
||||
/// Shared between compiler (resolveImports) and LSP (analyzeDocument).
|
||||
pub fn resolveImportPath(allocator: std.mem.Allocator, io: std.Io, base_dir: []const u8, raw_path: []const u8, root_path: ?[]const u8) ![]const u8 {
|
||||
if (!std.mem.eql(u8, base_dir, ".")) {
|
||||
const rel_path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ base_dir, raw_path });
|
||||
// Check if it exists as file relative to base_dir
|
||||
if (std.Io.Dir.readFileAlloc(.cwd(), io, rel_path, allocator, .limited(10 * 1024 * 1024))) |_| {
|
||||
return rel_path;
|
||||
} else |_| {}
|
||||
// Check if it exists as directory relative to base_dir
|
||||
if (std.Io.Dir.openDir(.cwd(), io, rel_path, .{})) |dir| {
|
||||
dir.close(io);
|
||||
return rel_path;
|
||||
} else |_| {}
|
||||
}
|
||||
// Fall back to raw path (cwd-relative); absolutify if root_path is known
|
||||
if (root_path) |rp| {
|
||||
if (rp.len > 0 and raw_path.len > 0 and raw_path[0] != '/') {
|
||||
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ rp, raw_path });
|
||||
}
|
||||
}
|
||||
return raw_path;
|
||||
}
|
||||
|
||||
/// A resolved module: the fully-resolved declarations of a single .sx file,
|
||||
/// with its own scope tracking which names are defined.
|
||||
pub const ResolvedModule = struct {
|
||||
@@ -142,23 +167,7 @@ pub fn resolveImports(
|
||||
}
|
||||
const imp = decl.data.import_decl;
|
||||
|
||||
// Resolve path: try relative to file dir first, then fall back to cwd-relative
|
||||
const resolved_path = resolvePath: {
|
||||
if (!std.mem.eql(u8, base_dir, ".")) {
|
||||
const rel_path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ base_dir, imp.path });
|
||||
// Check if it exists as file or directory relative to base_dir
|
||||
if (std.Io.Dir.readFileAlloc(.cwd(), io, rel_path, allocator, .limited(10 * 1024 * 1024))) |_| {
|
||||
break :resolvePath rel_path;
|
||||
} else |_| {}
|
||||
// Try as directory
|
||||
if (std.Io.Dir.openDir(.cwd(), io, rel_path, .{})) |dir| {
|
||||
dir.close(io);
|
||||
break :resolvePath rel_path;
|
||||
} else |_| {}
|
||||
}
|
||||
// Fall back to raw path (cwd-relative)
|
||||
break :resolvePath imp.path;
|
||||
};
|
||||
const resolved_path = try resolveImportPath(allocator, io, base_dir, imp.path, null);
|
||||
|
||||
// Circular import check — only along the current chain
|
||||
if (chain.contains(resolved_path)) continue;
|
||||
|
||||
Reference in New Issue
Block a user