From 9392c4032cb56b8b4db9ab6efd3e27391267f88a Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 3 Jun 2020 11:32:05 +0300 Subject: [PATCH] Only refresh packages when saving a build.zig file, not on any change --- src/document_store.zig | 23 +++++++++++++---------- src/main.zig | 11 ++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/document_store.zig b/src/document_store.zig index c1c1568..c21659b 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -410,6 +410,19 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const } } +pub fn applySave(self: *DocumentStore, handle: *Handle) !void { + if (handle.is_build_file) |build_file| { + loadPackages(.{ + .build_file = build_file, + .allocator = self.allocator, + .build_runner_path = self.build_runner_path, + .zig_exe_path = self.zig_exe_path.?, + }) catch |err| { + std.debug.warn("Failed to load packages of build file {} (error: {})\n", .{ build_file.uri, err }); + }; + } +} + pub fn applyChanges( self: *DocumentStore, handle: *Handle, @@ -469,16 +482,6 @@ pub fn applyChanges( } try self.refreshDocument(handle, zig_lib_path); - if (handle.is_build_file) |build_file| { - loadPackages(.{ - .build_file = build_file, - .allocator = self.allocator, - .build_runner_path = self.build_runner_path, - .zig_exe_path = self.zig_exe_path.?, - }) catch |err| { - std.debug.warn("Failed to load packages of build file {} (error: {})\n", .{ build_file.uri, err }); - }; - } } pub fn uriFromImportStr( diff --git a/src/main.zig b/src/main.zig index d74f50e..8933565 100644 --- a/src/main.zig +++ b/src/main.zig @@ -742,7 +742,16 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v const local_config = configFromUriOr(uri, config); try document_store.applyChanges(handle, content_changes, local_config.zig_lib_path); try publishDiagnostics(handle.*, local_config); - } else if (std.mem.eql(u8, method, "textDocument/didSave") or std.mem.eql(u8, method, "textDocument/willSave")) { + } else if (std.mem.eql(u8, method, "textDocument/didSave")) { + const text_document = params.getValue("textDocument").?.Object; + const uri = text_document.getValue("uri").?.String; + const handle = document_store.getHandle(uri) orelse { + std.debug.warn("Trying to save non existent document {}", .{uri}); + return; + }; + + try document_store.applySave(handle); + } else if (std.mem.eql(u8, method, "textDocument/willSave")) { // noop } else if (std.mem.eql(u8, method, "textDocument/didClose")) { const document = params.getValue("textDocument").?.Object;