lsp import

This commit is contained in:
agra
2026-02-24 20:05:24 +02:00
parent 06d10541da
commit bfc784734c
4 changed files with 40 additions and 27 deletions

View File

@@ -45,6 +45,8 @@ pub const Document = struct {
pub const DocumentStore = struct {
allocator: std.mem.Allocator,
io: std.Io,
/// Workspace root path (from initialize). Used to absolutify CWD-relative import paths.
root_path: []const u8 = "",
/// All loaded documents keyed by resolved file path.
by_path: std.StringHashMap(*Document),
@@ -56,6 +58,10 @@ pub const DocumentStore = struct {
};
}
fn rootPathOpt(self: *const DocumentStore) ?[]const u8 {
return if (self.root_path.len > 0) self.root_path else null;
}
/// Get or create a document for the given file path. Reads from disk if not yet loaded.
pub fn getOrLoad(self: *DocumentStore, path: []const u8) !*Document {
if (self.by_path.get(path)) |doc| return doc;
@@ -171,17 +177,14 @@ pub const DocumentStore = struct {
const root = doc.root orelse return;
// Extract imports from AST
// Extract imports from AST — uses shared resolution logic from imports.zig
var import_list = std.ArrayList(Import).empty;
const base_dir = sx.imports.dirName(doc.path);
if (root.data == .root) {
for (root.data.root.decls) |decl| {
if (decl.data != .import_decl) continue;
const imp = decl.data.import_decl;
const resolved_path = if (std.mem.eql(u8, base_dir, "."))
imp.path
else
try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ base_dir, imp.path });
const resolved_path = try sx.imports.resolveImportPath(self.allocator, self.io, base_dir, imp.path, self.rootPathOpt());
try import_list.append(self.allocator, .{
.ns = imp.name,
.path = resolved_path,