Use a local arena to free all memory with one call.
This commit is contained in:
parent
e9d9c57ff4
commit
a7e4d0b5e3
27
src/main.zig
27
src/main.zig
@ -161,28 +161,17 @@ pub fn publishDiagnostics(document: types.TextDocument) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
|
pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
|
||||||
|
// The tree uses its own arena, so we just pass our main allocator.
|
||||||
const tree = try std.zig.parse(allocator, document.text);
|
const tree = try std.zig.parse(allocator, document.text);
|
||||||
defer tree.deinit();
|
defer tree.deinit();
|
||||||
|
|
||||||
if (tree.errors.len > 0) return try respondGeneric(id, no_completions_response);
|
if (tree.errors.len > 0) return try respondGeneric(id, no_completions_response);
|
||||||
|
|
||||||
var completions = std.ArrayList(types.CompletionItem).init(allocator);
|
// We use a local arena allocator to deallocate all temporary data without iterating
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
defer {
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||||
// Free the completion data.
|
// Deallocate all temporary data.
|
||||||
// @TODO: Check what the tree functions (e.g. tokenSlice) return.
|
defer arena.deinit();
|
||||||
// We may need to dupe them to free the tree
|
|
||||||
// TODO: Maybe split off into a function or add a method to MarkupContent/CompletionItem?
|
|
||||||
for (completions.items) |comp| {
|
|
||||||
if (comp.documentation) |doc| {
|
|
||||||
if (doc.value.len > 0) {
|
|
||||||
allocator.free(doc.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
completions.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// try log("{}", .{&tree.root_node.decls});
|
// try log("{}", .{&tree.root_node.decls});
|
||||||
var decls = tree.root_node.decls.iterator(0);
|
var decls = tree.root_node.decls.iterator(0);
|
||||||
@ -192,7 +181,7 @@ pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
|
|||||||
switch (decl.id) {
|
switch (decl.id) {
|
||||||
.FnProto => {
|
.FnProto => {
|
||||||
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
||||||
var doc_comments = try analysis.getDocComments(allocator, tree, decl);
|
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
|
||||||
var doc = types.MarkupContent{
|
var doc = types.MarkupContent{
|
||||||
.kind = types.MarkupKind.Markdown,
|
.kind = types.MarkupKind.Markdown,
|
||||||
.value = doc_comments orelse ""
|
.value = doc_comments orelse ""
|
||||||
@ -206,7 +195,7 @@ pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
|
|||||||
},
|
},
|
||||||
.VarDecl => {
|
.VarDecl => {
|
||||||
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
|
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
|
||||||
var doc_comments = try analysis.getDocComments(allocator, tree, decl);
|
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
|
||||||
var doc = types.MarkupContent{
|
var doc = types.MarkupContent{
|
||||||
.kind = types.MarkupKind.Markdown,
|
.kind = types.MarkupKind.Markdown,
|
||||||
.value = doc_comments orelse ""
|
.value = doc_comments orelse ""
|
||||||
|
Loading…
Reference in New Issue
Block a user