add separate function for loading handles with getOrLoadHandle

This commit is contained in:
Techatrix 2022-10-17 20:43:11 +02:00
parent 89be8e0211
commit 2158a201ad
2 changed files with 11 additions and 5 deletions

View File

@ -107,10 +107,16 @@ pub fn deinit(self: *DocumentStore) void {
/// returns a handle to the given document
pub fn getHandle(self: *DocumentStore, uri: Uri) ?*const Handle {
return self.getHandleInternal(uri) catch null;
return self.handles.get(uri);
}
fn getHandleInternal(self: *DocumentStore, uri: Uri) !?*const Handle {
/// returns a handle to the given document
/// will load the document from disk if it hasn't been already
pub fn getOrLoadHandle(self: *DocumentStore, uri: Uri) ?*const Handle {
return self.getOrLoadHandleInternal(uri) catch null;
}
fn getOrLoadHandleInternal(self: *DocumentStore, uri: Uri) !?*const Handle {
if (self.handles.get(uri)) |handle| return handle;
var handle = try self.allocator.create(Handle);
@ -569,7 +575,7 @@ fn uriInImports(
// consider it checked even if a failure happens
try checked_uris.put(try self.allocator.dupe(u8, source_uri), {});
const handle = self.getHandle(source_uri) orelse return false;
const handle = self.getOrLoadHandle(source_uri) orelse return false;
for (handle.import_uris.items) |import_uri| {
if (std.mem.eql(u8, uri, import_uri))

View File

@ -914,14 +914,14 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl
const import_str = tree.tokenSlice(main_tokens[import_param]);
const import_uri = (try store.uriFromImportStr(arena.allocator(), handle.*, import_str[1 .. import_str.len - 1])) orelse return null;
const new_handle = store.getHandle(import_uri) orelse return null;
const new_handle = store.getOrLoadHandle(import_uri) orelse return null;
// reference to node '0' which is root
return TypeWithHandle.typeVal(.{ .node = 0, .handle = new_handle });
} else if (std.mem.eql(u8, call_name, "@cImport")) {
const cimport_uri = (try store.resolveCImport(handle.*, node)) orelse return null;
const new_handle = store.getHandle(cimport_uri) orelse return null;
const new_handle = store.getOrLoadHandle(cimport_uri) orelse return null;
// reference to node '0' which is root
return TypeWithHandle.typeVal(.{ .node = 0, .handle = new_handle });