Yet more import resolution fixes

This commit is contained in:
Alexandros Naskos 2021-03-30 21:41:04 +03:00
parent 40edfadc26
commit 6edd686668
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2
2 changed files with 38 additions and 15 deletions

View File

@ -268,6 +268,14 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) anyerror!*Hand
} }
} }
handle.import_uris = try self.collectImportUris(handle);
errdefer {
for (handle.import_uris) |imp_uri| {
self.allocator.free(imp_uri);
}
self.allocator.free(handle.import_uris);
}
try self.handles.putNoClobber(uri, handle); try self.handles.putNoClobber(uri, handle);
return handle; return handle;
} }
@ -359,16 +367,14 @@ pub fn getHandle(self: *DocumentStore, uri: []const u8) ?*Handle {
return self.handles.get(uri); return self.handles.get(uri);
} }
fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const u8) !void { fn collectImportUris(self: *DocumentStore, handle: *Handle) ![]const []const u8 {
log.debug("New text for document {s}", .{handle.uri()});
handle.tree.deinit(self.allocator);
handle.tree = try std.zig.parse(self.allocator, handle.document.text);
handle.document_scope.deinit(self.allocator);
handle.document_scope = try analysis.makeDocumentScope(self.allocator, handle.tree);
var new_imports = std.ArrayList([]const u8).init(self.allocator); var new_imports = std.ArrayList([]const u8).init(self.allocator);
errdefer new_imports.deinit(); errdefer {
for (new_imports.items) |imp| {
self.allocator.free(imp);
}
new_imports.deinit();
}
try analysis.collectImports(&new_imports, handle.tree); try analysis.collectImports(&new_imports, handle.tree);
// Convert to URIs // Convert to URIs
@ -382,9 +388,27 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const
_ = new_imports.swapRemove(i); _ = new_imports.swapRemove(i);
} }
} }
return new_imports.toOwnedSlice();
}
fn refreshDocument(self: *DocumentStore, handle: *Handle) !void {
log.debug("New text for document {s}", .{handle.uri()});
handle.tree.deinit(self.allocator);
handle.tree = try std.zig.parse(self.allocator, handle.document.text);
handle.document_scope.deinit(self.allocator);
handle.document_scope = try analysis.makeDocumentScope(self.allocator, handle.tree);
const new_imports = try self.collectImportUris(handle);
errdefer {
for (new_imports) |imp| {
self.allocator.free(imp);
}
self.allocator.free(new_imports);
}
const old_imports = handle.import_uris; const old_imports = handle.import_uris;
handle.import_uris = new_imports.toOwnedSlice(); handle.import_uris = new_imports;
defer { defer {
for (old_imports) |uri| { for (old_imports) |uri| {
self.allocator.free(uri); self.allocator.free(uri);
@ -392,11 +416,11 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const
self.allocator.free(old_imports); self.allocator.free(old_imports);
} }
i = 0; var i: usize = 0;
while (i < handle.imports_used.items.len) { while (i < handle.imports_used.items.len) {
const old = handle.imports_used.items[i]; const old = handle.imports_used.items[i];
still_exists: { still_exists: {
for (new_imports.items) |new| { for (new_imports) |new| {
if (std.mem.eql(u8, new, old)) { if (std.mem.eql(u8, new, old)) {
break :still_exists; break :still_exists;
} }
@ -429,7 +453,6 @@ pub fn applyChanges(
handle: *Handle, handle: *Handle,
content_changes: std.json.Array, content_changes: std.json.Array,
offset_encoding: offsets.Encoding, offset_encoding: offsets.Encoding,
zig_lib_path: ?[]const u8,
) !void { ) !void {
const document = &handle.document; const document = &handle.document;
@ -492,7 +515,7 @@ pub fn applyChanges(
} }
} }
try self.refreshDocument(handle, zig_lib_path); try self.refreshDocument(handle);
} }
pub fn uriFromImportStr( pub fn uriFromImportStr(

View File

@ -1364,7 +1364,7 @@ fn changeDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, r
return; return;
}; };
try document_store.applyChanges(handle, req.params.contentChanges.Array, offset_encoding, config.zig_lib_path); try document_store.applyChanges(handle, req.params.contentChanges.Array, offset_encoding);
try publishDiagnostics(arena, handle.*, config); try publishDiagnostics(arena, handle.*, config);
} }