Skip to content

Commit e32d546

Browse files
FnControlOptionTechatrix
authored andcommitted
Include comptime-known values in hover info
1 parent 8310251 commit e32d546

4 files changed

Lines changed: 53 additions & 32 deletions

File tree

src/analysis.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,8 +2614,8 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, options: ResolveOptions) Error
26142614
return Type.fromIP(analyser, .bool_type, null);
26152615
const typeof = try ty.typeOf(analyser);
26162616

2617-
if (typeof.data == .ip_index and typeof.data.ip_index.index != null) {
2618-
const key = analyser.ip.indexToKey(typeof.data.ip_index.index.?);
2617+
if (typeof.ipIndex()) |index| {
2618+
const key = analyser.ip.indexToKey(index);
26192619
if (key == .vector_type) {
26202620
const vector_ty_ip_index = try analyser.ip.get(.{
26212621
.vector_type = .{
@@ -3576,6 +3576,13 @@ pub const Type = struct {
35763576
}
35773577
};
35783578

3579+
pub fn ipIndex(self: Type) ?InternPool.Index {
3580+
return switch (self.data) {
3581+
.ip_index => |payload| payload.index,
3582+
else => null,
3583+
};
3584+
}
3585+
35793586
pub fn fromIP(analyser: *Analyser, ty: InternPool.Index, index: ?InternPool.Index) Type {
35803587
std.debug.assert(analyser.ip.isType(ty));
35813588
if (index) |idx| std.debug.assert(analyser.ip.typeOf(idx) == ty);

src/features/completions.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ fn declToCompletion(builder: *Builder, decl_handle: Analyser.DeclWithHandle) Ana
327327
}
328328

329329
const detail = if (maybe_resolved_ty) |ty| blk: {
330-
if (ty.is_type_val and ty.data == .ip_index and ty.data.ip_index.index != null and !builder.analyser.ip.isUnknown(ty.data.ip_index.index.?)) {
331-
break :blk try ty.stringifyTypeVal(builder.analyser, .{ .truncate_container_decls = false });
332-
} else {
333-
break :blk try ty.stringifyTypeOf(builder.analyser, .{ .truncate_container_decls = false });
330+
if (ty.ipIndex()) |index| {
331+
if (ty.is_type_val and !builder.analyser.ip.isUnknown(index)) {
332+
break :blk try ty.stringifyTypeVal(builder.analyser, .{ .truncate_container_decls = false });
333+
}
334334
}
335+
break :blk try ty.stringifyTypeOf(builder.analyser, .{ .truncate_container_decls = false });
335336
} else null;
336337

337338
const label_details: ?types.completion.Item.LabelDetails = blk: {

src/features/hover.zig

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ fn hoverSymbol(
2222
const tracy_zone = tracy.trace(@src());
2323
defer tracy_zone.end();
2424

25+
const old_resolve_number_literal_values = analyser.resolve_number_literal_values;
26+
analyser.resolve_number_literal_values = true;
27+
defer analyser.resolve_number_literal_values = old_resolve_number_literal_values;
28+
2529
var doc_strings: std.ArrayList([]const u8) = .empty;
2630

2731
var decl_handle: Analyser.DeclWithHandle = param_decl_handle;
@@ -101,17 +105,25 @@ fn hoverSymbolResolvedType(
101105
if (resolved_type_maybe) |resolved_type| {
102106
if (try resolved_type.docComments(arena)) |doc|
103107
try doc_strings.append(arena, doc);
104-
const typeof = try resolved_type.typeOf(analyser);
108+
105109
var possible_types: Analyser.Type.ArraySet = .empty;
106-
has_more = try typeof.getAllTypesWithHandlesArraySet(analyser, &possible_types);
110+
has_more = try resolved_type.getAllTypesWithHandlesArraySet(analyser, &possible_types);
111+
112+
const options: Analyser.Type.FormatOptions = .{
113+
.referenced = &referenced,
114+
.truncate_container_decls = possible_types.count() > 1,
115+
};
116+
107117
for (possible_types.keys()) |ty| {
108-
try resolved_type_strings.append(
109-
arena,
110-
try ty.stringifyTypeVal(analyser, .{
111-
.referenced = &referenced,
112-
.truncate_container_decls = possible_types.count() > 1,
113-
}),
114-
);
118+
const type_str = try ty.stringifyTypeOf(analyser, options);
119+
120+
if (ty.ipIndex() != null) {
121+
const val_str = try ty.stringifyTypeVal(analyser, options);
122+
const combined = try std.fmt.allocPrint(arena, "{s} = {s}", .{ type_str, val_str });
123+
try resolved_type_strings.append(arena, combined);
124+
} else {
125+
try resolved_type_strings.append(arena, type_str);
126+
}
115127
}
116128
}
117129
const referenced_types: []const Analyser.ReferencedType = referenced.keys();

tests/lsp_features/hover.zig

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ test "either types" {
859859
\\const T = u32
860860
\\```
861861
\\```zig
862-
\\(type)
862+
\\(type = u32)
863863
\\```
864864
\\
865865
\\small type
@@ -868,7 +868,7 @@ test "either types" {
868868
\\const T = u64
869869
\\```
870870
\\```zig
871-
\\(type)
871+
\\(type = u64)
872872
\\```
873873
\\
874874
\\large type
@@ -886,12 +886,12 @@ test "either types" {
886886
\\const bar = either.<cursor>T;
887887
,
888888
\\const T = u32
889-
\\(type)
889+
\\(type = u32)
890890
\\
891891
\\small type
892892
\\
893893
\\const T = u64
894-
\\(type)
894+
\\(type = u64)
895895
\\
896896
\\large type
897897
, .{ .markup_kind = .plaintext });
@@ -902,7 +902,8 @@ test "either type instances" {
902902
\\const EitherType<cursor> = if (undefined) u32 else f64;
903903
,
904904
\\const EitherType = if (undefined) u32 else f64
905-
\\(type)
905+
\\(type = u32)
906+
\\(type = f64)
906907
, .{ .markup_kind = .plaintext });
907908
try testHoverWithOptions(
908909
\\const EitherType = if (undefined) u32 else f64;
@@ -1009,9 +1010,9 @@ test "either type instances" {
10091010
\\ .d => {},
10101011
\\ .e => error.Foo,
10111012
\\}
1012-
\\(comptime_int)
1013-
\\(bool)
1014-
\\(comptime_float)
1013+
\\(comptime_int = 42)
1014+
\\(bool = true)
1015+
\\(comptime_float = 3.14)
10151016
\\(...)
10161017
, .{ .markup_kind = .plaintext });
10171018
}
@@ -1058,7 +1059,7 @@ test "var decl comments" {
10581059
\\const foo = 0 + 0
10591060
\\```
10601061
\\```zig
1061-
\\(comptime_int)
1062+
\\(comptime_int = 0)
10621063
\\```
10631064
\\
10641065
\\this is a comment
@@ -1085,7 +1086,7 @@ test "var decl alias" {
10851086
\\const foo = 5
10861087
\\```
10871088
\\```zig
1088-
\\(comptime_int)
1089+
\\(comptime_int = 5)
10891090
\\```
10901091
);
10911092
}
@@ -1108,7 +1109,7 @@ test "escaped identifier" {
11081109
\\const @"foo" = 42
11091110
\\```
11101111
\\```zig
1111-
\\(comptime_int)
1112+
\\(comptime_int = 42)
11121113
\\```
11131114
, .{
11141115
.highlight = "@\"foo\"",
@@ -1121,7 +1122,7 @@ test "escaped identifier" {
11211122
\\const @"hello world" = 42
11221123
\\```
11231124
\\```zig
1124-
\\(comptime_int)
1125+
\\(comptime_int = 42)
11251126
\\```
11261127
, .{
11271128
.highlight = "@\"hello world\"",
@@ -1134,7 +1135,7 @@ test "escaped identifier" {
11341135
\\const @"hello world" = 42
11351136
\\```
11361137
\\```zig
1137-
\\(comptime_int)
1138+
\\(comptime_int = 42)
11381139
\\```
11391140
, .{
11401141
.highlight = "@\"hello world\"",
@@ -1150,7 +1151,7 @@ test "escaped identifier with same name as primitive" {
11501151
\\const @"true" = 42
11511152
\\```
11521153
\\```zig
1153-
\\(comptime_int)
1154+
\\(comptime_int = 42)
11541155
\\```
11551156
, .{
11561157
.highlight = "@\"true\"",
@@ -1163,7 +1164,7 @@ test "escaped identifier with same name as primitive" {
11631164
\\const @"f32" = 42
11641165
\\```
11651166
\\```zig
1166-
\\(comptime_int)
1167+
\\(comptime_int = 42)
11671168
\\```
11681169
, .{
11691170
.highlight = "@\"f32\"",
@@ -1318,7 +1319,7 @@ test "array properties" {
13181319
\\const bar = foo.len<cursor>;
13191320
,
13201321
\\len
1321-
\\(usize)
1322+
\\(usize = 3)
13221323
, .{ .markup_kind = .plaintext });
13231324
}
13241325

@@ -1328,7 +1329,7 @@ test "tuple properties" {
13281329
\\const bar = foo.len<cursor>;
13291330
,
13301331
\\len
1331-
\\(usize)
1332+
\\(usize = 2)
13321333
, .{ .markup_kind = .plaintext });
13331334
try testHoverWithOptions(
13341335
\\const foo: struct { i32, bool } = undefined;

0 commit comments

Comments
 (0)