so... jai :D
This commit is contained in:
48
src/lsp/document.zig
Normal file
48
src/lsp/document.zig
Normal file
@@ -0,0 +1,48 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const DocumentStore = struct {
|
||||
documents: std.StringHashMap(Document),
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub const Document = struct {
|
||||
uri: []const u8,
|
||||
text: []const u8,
|
||||
version: i64,
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) DocumentStore {
|
||||
return .{
|
||||
.documents = std.StringHashMap(Document).init(allocator),
|
||||
.allocator = allocator,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn open(self: *DocumentStore, uri: []const u8, text: []const u8, version: i64) !void {
|
||||
const uri_copy = try self.allocator.dupe(u8, uri);
|
||||
const text_copy = try self.allocator.dupe(u8, text);
|
||||
try self.documents.put(uri_copy, .{
|
||||
.uri = uri_copy,
|
||||
.text = text_copy,
|
||||
.version = version,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update(self: *DocumentStore, uri: []const u8, text: []const u8, version: i64) !void {
|
||||
if (self.documents.getPtr(uri)) |doc| {
|
||||
self.allocator.free(doc.text);
|
||||
doc.text = try self.allocator.dupe(u8, text);
|
||||
doc.version = version;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(self: *DocumentStore, uri: []const u8) void {
|
||||
if (self.documents.fetchRemove(uri)) |kv| {
|
||||
self.allocator.free(kv.value.text);
|
||||
self.allocator.free(kv.key);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(self: *const DocumentStore, uri: []const u8) ?*const Document {
|
||||
return self.documents.getPtr(uri);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user