return const Handle from getHandle

This commit is contained in:
Techatrix 2022-10-17 20:33:37 +02:00
parent ba007b3558
commit 89be8e0211
2 changed files with 10 additions and 6 deletions

View File

@ -106,11 +106,11 @@ pub fn deinit(self: *DocumentStore) void {
} }
/// returns a handle to the given document /// returns a handle to the given document
pub fn getHandle(self: *DocumentStore, uri: Uri) ?*Handle { pub fn getHandle(self: *DocumentStore, uri: Uri) ?*const Handle {
return self.getHandleInternal(uri) catch null; return self.getHandleInternal(uri) catch null;
} }
fn getHandleInternal(self: *DocumentStore, uri: Uri) !?*Handle { fn getHandleInternal(self: *DocumentStore, uri: Uri) !?*const Handle {
if (self.handles.get(uri)) |handle| return handle; if (self.handles.get(uri)) |handle| return handle;
var handle = try self.allocator.create(Handle); var handle = try self.allocator.create(Handle);
@ -176,10 +176,16 @@ pub fn closeDocument(self: *DocumentStore, uri: Uri) void {
self.garbageCollectionBuildFiles() catch {}; self.garbageCollectionBuildFiles() catch {};
} }
pub fn refreshDocument(self: *DocumentStore, handle: *Handle) !void { /// takes ownership of `new_text` which has to be allocated with `self.allocator`
pub fn refreshDocument(self: *DocumentStore, uri: Uri, new_text: [:0]const u8) !void {
const tracy_zone = tracy.trace(@src()); const tracy_zone = tracy.trace(@src());
defer tracy_zone.end(); defer tracy_zone.end();
const handle = self.handles.get(uri) orelse unreachable;
self.allocator.free(handle.text);
handle.text = new_text;
var new_tree = try std.zig.parse(self.allocator, handle.text); var new_tree = try std.zig.parse(self.allocator, handle.text);
handle.tree.deinit(self.allocator); handle.tree.deinit(self.allocator);
handle.tree = new_tree; handle.tree = new_tree;

View File

@ -1756,10 +1756,8 @@ fn changeDocumentHandler(server: *Server, writer: anytype, id: types.RequestId,
const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse return; const handle = server.document_store.getHandle(req.params.textDocument.uri) orelse return;
const new_text = try diff.applyTextEdits(server.allocator, handle.text, req.params.contentChanges, server.offset_encoding); const new_text = try diff.applyTextEdits(server.allocator, handle.text, req.params.contentChanges, server.offset_encoding);
server.allocator.free(handle.text);
handle.text = new_text;
try server.document_store.refreshDocument(handle); try server.document_store.refreshDocument(handle.uri, new_text);
try server.publishDiagnostics(writer, handle.*); try server.publishDiagnostics(writer, handle.*);
} }