Skip to content

Commit c678374

Browse files
Lazier build file loading
Co-Authored-By: Techatrix <techatrix@mailbox.org>
1 parent cf30251 commit c678374

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/DocumentStore.zig

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,11 @@ pub fn getOrLoadHandle(self: *DocumentStore, uri: Uri) ?*Handle {
762762
}
763763

764764
const file_contents = self.readFile(uri) orelse return null;
765-
return self.createAndStoreDocument(uri, file_contents, false) catch |err| {
765+
return self.createAndStoreDocument(
766+
uri,
767+
file_contents,
768+
.{ .lsp_synced = false, .load_build_file_behaviour = .never },
769+
) catch |err| {
766770
log.err("failed to store document '{s}': {}", .{ uri.raw, err });
767771
return null;
768772
};
@@ -828,7 +832,11 @@ pub fn openLspSyncedDocument(self: *DocumentStore, uri: Uri, text: []const u8) e
828832
}
829833

830834
const duped_text = try self.allocator.dupeZ(u8, text);
831-
_ = try self.createAndStoreDocument(uri, duped_text, true);
835+
_ = try self.createAndStoreDocument(
836+
uri,
837+
duped_text,
838+
.{ .lsp_synced = true, .load_build_file_behaviour = .load_but_dont_update },
839+
);
832840
}
833841

834842
/// Closes a document that has been synced over the LSP protocol (`textDocument/didClose`).
@@ -865,7 +873,7 @@ pub fn refreshLspSyncedDocument(self: *DocumentStore, uri: Uri, new_text: [:0]co
865873
log.warn("Document modified without being opened: {s}", .{uri.raw});
866874
}
867875

868-
_ = try self.createAndStoreDocument(uri, new_text, true);
876+
_ = try self.createAndStoreDocument(uri, new_text, .{ .lsp_synced = true, .load_build_file_behaviour = .only_update });
869877
}
870878

871879
/// Refreshes a document from the file system, unless said document is synced over the LSP protocol.
@@ -889,7 +897,11 @@ pub fn refreshDocumentFromFileSystem(self: *DocumentStore, uri: Uri, should_dele
889897
if (handle.isLspSynced()) return false;
890898
}
891899
const file_contents = self.readFile(uri) orelse return false;
892-
_ = try self.createAndStoreDocument(uri, file_contents, false);
900+
_ = try self.createAndStoreDocument(
901+
uri,
902+
file_contents,
903+
.{ .lsp_synced = false, .load_build_file_behaviour = .only_update },
904+
);
893905
}
894906

895907
return true;
@@ -1471,25 +1483,38 @@ fn uriInImports(
14711483
return false;
14721484
}
14731485

1486+
const CreateAndStoreOptions = struct {
1487+
lsp_synced: bool,
1488+
load_build_file_behaviour: enum { load_but_dont_update, only_update, never },
1489+
};
1490+
14741491
/// takes ownership of the `text` passed in.
14751492
/// **Thread safe** takes an exclusive lock
14761493
fn createAndStoreDocument(
14771494
self: *DocumentStore,
14781495
uri: Uri,
14791496
text: [:0]const u8,
1480-
lsp_synced: bool,
1497+
options: CreateAndStoreOptions,
14811498
) error{OutOfMemory}!*Handle {
14821499
const tracy_zone = tracy.trace(@src());
14831500
defer tracy_zone.end();
14841501

1485-
var new_handle = Handle.init(self, uri, text, lsp_synced) catch |err| {
1502+
var new_handle = Handle.init(self, uri, text, options.lsp_synced) catch |err| {
14861503
self.allocator.free(text);
14871504
return err;
14881505
};
14891506
errdefer new_handle.deinit();
14901507

14911508
if (supports_build_system and isBuildFile(uri) and !isInStd(uri)) {
1492-
_ = self.getOrLoadBuildFile(uri);
1509+
switch (options.load_build_file_behaviour) {
1510+
.load_but_dont_update => {
1511+
_ = self.getOrLoadBuildFile(uri);
1512+
},
1513+
.only_update => {
1514+
self.invalidateBuildFile(uri);
1515+
},
1516+
.never => {},
1517+
}
14931518
}
14941519

14951520
self.mutex.lockUncancelable(self.io);

0 commit comments

Comments
 (0)