2023-01-20 21:24:39 +00:00
|
|
|
const std = @import("std");
|
2023-01-23 20:28:15 +00:00
|
|
|
const InternPool = @import("../InternPool.zig");
|
|
|
|
const types = @import("../lsp.zig");
|
2023-01-20 21:24:39 +00:00
|
|
|
|
|
|
|
const Ast = std.zig.Ast;
|
|
|
|
|
2023-01-23 21:23:01 +00:00
|
|
|
|
|
|
|
/// TODO use std.ArrayListUnmanaged instead of returning a slice
|
2023-01-20 21:24:39 +00:00
|
|
|
pub fn dotCompletions(
|
|
|
|
arena: std.mem.Allocator,
|
|
|
|
ip: *InternPool,
|
|
|
|
ty: InternPool.Index,
|
2023-01-23 20:28:15 +00:00
|
|
|
val: InternPool.Index,
|
|
|
|
node: ?Ast.Node.Index
|
2023-01-20 21:24:39 +00:00
|
|
|
) error{OutOfMemory}![]types.CompletionItem {
|
|
|
|
_ = node;
|
|
|
|
|
|
|
|
var completions = std.ArrayListUnmanaged(types.CompletionItem){};
|
|
|
|
|
2023-01-23 20:28:15 +00:00
|
|
|
const key = ip.indexToKey(ty);
|
|
|
|
const inner_key = switch (key) {
|
|
|
|
.pointer_type => |info| if (info.size == .One) ip.indexToKey(info.elem_type) else key,
|
|
|
|
else => key,
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (inner_key) {
|
|
|
|
.simple => |simple| switch (simple) {
|
|
|
|
.type => {
|
|
|
|
const ty_key = ip.indexToKey(val);
|
2023-01-23 21:23:01 +00:00
|
|
|
const namespace = ty_key.getNamespace();
|
|
|
|
if (namespace != .none) {
|
2023-01-23 20:28:15 +00:00
|
|
|
// TODO lookup in namespace
|
|
|
|
}
|
|
|
|
switch (ty_key) {
|
|
|
|
.error_set_type => |error_set_info| {
|
|
|
|
for (error_set_info.names) |name| {
|
|
|
|
const error_name = ip.indexToKey(name).bytes;
|
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = error_name,
|
|
|
|
.kind = .Constant,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "error.{s}", .{std.zig.fmtId(error_name)}),
|
2023-01-23 20:28:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.union_type => {}, // TODO
|
|
|
|
.enum_type => |enum_info|{
|
|
|
|
for (enum_info.fields) |field| {
|
|
|
|
const field_name = ip.indexToKey(field.name).bytes;
|
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = field_name,
|
|
|
|
.kind = .Constant,
|
|
|
|
// include field.val?
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
},
|
2023-01-23 21:23:01 +00:00
|
|
|
else => {},
|
2023-01-23 20:28:15 +00:00
|
|
|
},
|
2023-01-20 21:24:39 +00:00
|
|
|
.pointer_type => |pointer_info| {
|
2023-01-23 21:23:01 +00:00
|
|
|
if (pointer_info.size == .Slice) {
|
2023-01-23 20:28:15 +00:00
|
|
|
var many_ptr_info = InternPool.Key{ .pointer_type = pointer_info };
|
|
|
|
many_ptr_info.pointer_type.size = .Many;
|
2023-01-20 21:24:39 +00:00
|
|
|
|
2023-01-23 20:28:15 +00:00
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "{}", .{many_ptr_info.fmtType(ip.*)}),
|
2023-01-23 20:28:15 +00:00
|
|
|
});
|
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
.detail = "usize",
|
|
|
|
});
|
|
|
|
} else if(ip.indexToKey(pointer_info.elem_type) == .array_type) {
|
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
.detail = "usize",
|
|
|
|
});
|
2023-01-20 21:24:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.array_type => |array_info| {
|
|
|
|
try completions.append(arena, types.CompletionItem{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "usize ({d})", .{array_info.len}), // TODO how should this be displayed
|
2023-01-20 21:24:39 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
.struct_type => |struct_info| {
|
|
|
|
for (struct_info.fields) |field| {
|
2023-01-23 20:28:15 +00:00
|
|
|
const field_name = ip.indexToKey(field.name).bytes;
|
2023-01-20 21:24:39 +00:00
|
|
|
try completions.append(arena, types.CompletionItem{
|
2023-01-23 20:28:15 +00:00
|
|
|
.label = field_name,
|
2023-01-20 21:24:39 +00:00
|
|
|
.kind = .Field,
|
|
|
|
// TODO include alignment and comptime
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "{}", .{field.ty.fmtType(ip.*)}),
|
2023-01-20 21:24:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.optional_type => |optional_info| {
|
|
|
|
try completions.append(arena, .{
|
|
|
|
.label = "?",
|
|
|
|
.kind = .Operator,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "{}", .{optional_info.payload_type.fmtType(ip.*)}),
|
2023-01-20 21:24:39 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
.enum_type => |enum_info| {
|
|
|
|
for (enum_info.fields) |field| {
|
2023-01-23 20:28:15 +00:00
|
|
|
const field_name = ip.indexToKey(field.name).bytes;
|
2023-01-20 21:24:39 +00:00
|
|
|
try completions.append(arena, .{
|
2023-01-23 20:28:15 +00:00
|
|
|
.label = field_name,
|
2023-01-20 21:24:39 +00:00
|
|
|
.kind = .Field,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "{}", .{field.val.fmtValue(enum_info.tag_type, ip.*)}),
|
2023-01-20 21:24:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.union_type => |union_info| {
|
|
|
|
for (union_info.fields) |field| {
|
2023-01-23 20:28:15 +00:00
|
|
|
const field_name = ip.indexToKey(field.name).bytes;
|
2023-01-20 21:24:39 +00:00
|
|
|
try completions.append(arena, .{
|
2023-01-23 20:28:15 +00:00
|
|
|
.label = field_name,
|
2023-01-20 21:24:39 +00:00
|
|
|
.kind = .Field,
|
|
|
|
.detail = if (field.alignment != 0)
|
2023-01-23 21:23:01 +00:00
|
|
|
try std.fmt.allocPrint(arena, "align({d}) {}", .{ field.alignment, field.ty.fmtType(ip.*) })
|
2023-01-20 21:24:39 +00:00
|
|
|
else
|
2023-01-23 21:23:01 +00:00
|
|
|
try std.fmt.allocPrint(arena, "{}", .{field.ty.fmtType(ip.*)}),
|
2023-01-20 21:24:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2023-01-23 20:28:15 +00:00
|
|
|
.tuple_type => |tuple_info| {
|
|
|
|
for (tuple_info.types) |tuple_ty,i| {
|
|
|
|
try completions.append(arena, .{
|
2023-01-23 21:23:01 +00:00
|
|
|
.label = try std.fmt.allocPrint(arena, "{d}", .{i}),
|
2023-01-23 20:28:15 +00:00
|
|
|
.kind = .Field,
|
2023-01-23 21:23:01 +00:00
|
|
|
.detail = try std.fmt.allocPrint(arena, "{}", .{tuple_ty.fmtType(ip.*)}),
|
2023-01-23 20:28:15 +00:00
|
|
|
});
|
|
|
|
}
|
2023-01-20 21:24:39 +00:00
|
|
|
},
|
2023-01-23 20:28:15 +00:00
|
|
|
.int_type,
|
2023-01-23 21:23:01 +00:00
|
|
|
.error_set_type,
|
2023-01-23 20:28:15 +00:00
|
|
|
.error_union_type,
|
|
|
|
.function_type,
|
|
|
|
.vector_type,
|
2023-01-20 21:24:39 +00:00
|
|
|
.anyframe_type => {},
|
|
|
|
|
|
|
|
.int_u64_value,
|
|
|
|
.int_i64_value,
|
|
|
|
.int_big_value,
|
|
|
|
.float_16_value,
|
|
|
|
.float_32_value,
|
|
|
|
.float_64_value,
|
|
|
|
.float_80_value,
|
|
|
|
.float_128_value,
|
2023-01-23 20:28:15 +00:00
|
|
|
=> unreachable,
|
2023-01-20 21:24:39 +00:00
|
|
|
|
|
|
|
.bytes,
|
|
|
|
.aggregate,
|
|
|
|
.union_value,
|
2023-01-23 20:28:15 +00:00
|
|
|
=> unreachable,
|
2023-01-20 21:24:39 +00:00
|
|
|
}
|
|
|
|
return try completions.toOwnedSlice(arena);
|
|
|
|
}
|