forked from zigtools/zls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.zig
More file actions
455 lines (410 loc) · 16.1 KB
/
hover.zig
File metadata and controls
455 lines (410 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Implementation of [`textDocument/hover`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover)
const std = @import("std");
const Ast = std.zig.Ast;
const ast = @import("../ast.zig");
const types = @import("lsp").types;
const offsets = @import("../offsets.zig");
const tracy = @import("tracy");
const Analyser = @import("../analysis.zig");
const DocumentStore = @import("../DocumentStore.zig");
const data = @import("version_data");
fn hoverSymbol(
analyser: *Analyser,
arena: std.mem.Allocator,
decl_handle: Analyser.DeclWithHandle,
markup_kind: types.MarkupKind,
) error{OutOfMemory}!?[]const u8 {
var doc_strings: std.ArrayListUnmanaged([]const u8) = .empty;
return hoverSymbolRecursive(analyser, arena, decl_handle, markup_kind, &doc_strings);
}
fn hoverSymbolRecursive(
analyser: *Analyser,
arena: std.mem.Allocator,
decl_handle: Analyser.DeclWithHandle,
markup_kind: types.MarkupKind,
doc_strings: *std.ArrayListUnmanaged([]const u8),
) error{OutOfMemory}!?[]const u8 {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const handle = decl_handle.handle;
const tree = handle.tree;
if (try decl_handle.docComments(arena)) |doc|
try doc_strings.append(arena, doc);
const def_str = switch (decl_handle.decl) {
.ast_node => |node| def: {
if (try analyser.resolveVarDeclAlias(.{
.node_handle = .of(node, handle),
.container_type = decl_handle.container_type,
})) |result| {
return try hoverSymbolRecursive(analyser, arena, result, markup_kind, doc_strings);
}
switch (tree.nodeTag(node)) {
.global_var_decl,
.local_var_decl,
.aligned_var_decl,
.simple_var_decl,
=> {
const var_decl = tree.fullVarDecl(node).?;
break :def try Analyser.getVariableSignature(arena, tree, var_decl, true);
},
.container_field,
.container_field_init,
.container_field_align,
=> {
const field = tree.fullContainerField(node).?;
break :def Analyser.getContainerFieldSignature(tree, field) orelse return null;
},
.fn_proto,
.fn_proto_multi,
.fn_proto_one,
.fn_proto_simple,
.fn_decl,
=> {
var buf: [1]Ast.Node.Index = undefined;
const fn_proto = tree.fullFnProto(&buf, node).?;
break :def Analyser.getFunctionSignature(tree, fn_proto);
},
.test_decl => {
const test_name_token, const test_name = ast.testDeclNameAndToken(tree, node) orelse return null;
_ = test_name_token;
break :def test_name;
},
else => {
return null;
},
}
},
.function_parameter => |pay| def: {
const param = pay.get(tree).?;
break :def ast.paramSlice(tree, param, false);
},
.optional_payload,
.error_union_payload,
.error_union_error,
.for_loop_payload,
.assign_destructure,
.switch_payload,
.label,
.error_token,
=> tree.tokenSlice(decl_handle.nameToken()),
};
var referenced: Analyser.ReferencedType.Set = .empty;
var resolved_type_str: []const u8 = "unknown";
if (try decl_handle.resolveType(analyser)) |resolved_type| {
if (try resolved_type.docComments(arena)) |doc|
try doc_strings.append(arena, doc);
resolved_type_str = try std.fmt.allocPrint(arena, "{}", .{resolved_type.fmt(analyser, .{
.referenced = &referenced,
.truncate_container_decls = false,
})});
}
const referenced_types: []const Analyser.ReferencedType = referenced.keys();
return try hoverSymbolResolved(
arena,
markup_kind,
doc_strings.items,
def_str,
resolved_type_str,
referenced_types,
);
}
fn hoverSymbolResolved(
arena: std.mem.Allocator,
markup_kind: types.MarkupKind,
doc_strings: []const []const u8,
def_str: []const u8,
resolved_type_str: []const u8,
referenced_types: []const Analyser.ReferencedType,
) error{OutOfMemory}![]const u8 {
var hover_text: std.ArrayListUnmanaged(u8) = .empty;
const writer = hover_text.writer(arena);
if (markup_kind == .markdown) {
try writer.print("```zig\n{s}\n```\n```zig\n({s})\n```", .{ def_str, resolved_type_str });
if (referenced_types.len > 0)
try writer.print("\n\n" ++ "Go to ", .{});
for (referenced_types, 0..) |ref, index| {
if (index > 0)
try writer.print(" | ", .{});
const source_index = ref.handle.tree.tokenStart(ref.token);
const line = 1 + std.mem.count(u8, ref.handle.tree.source[0..source_index], "\n");
try writer.print("[{s}]({s}#L{d})", .{ ref.str, ref.handle.uri, line });
}
} else {
try writer.print("{s}\n({s})", .{ def_str, resolved_type_str });
}
if (doc_strings.len > 0) {
try writer.writeAll("\n\n");
for (doc_strings, 0..) |doc, i| {
try writer.writeAll(doc);
if (i != doc_strings.len - 1) try writer.writeAll("\n\n");
}
}
return hover_text.items;
}
fn hoverDefinitionLabel(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
pos_index: usize,
loc: offsets.Loc,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) error{OutOfMemory}!?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const name = offsets.locToSlice(handle.tree.source, loc);
const decl = (try Analyser.lookupLabel(handle, name, pos_index)) orelse return null;
return .{
.contents = .{
.MarkupContent = .{
.kind = markup_kind,
.value = (try hoverSymbol(analyser, arena, decl, markup_kind)) orelse return null,
},
},
.range = offsets.locToRange(handle.tree.source, loc, offset_encoding),
};
}
fn hoverDefinitionBuiltin(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
pos_index: usize,
name_loc: offsets.Loc,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) error{OutOfMemory}!?types.Hover {
_ = analyser;
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const name = offsets.locToSlice(handle.tree.source, name_loc);
var contents: std.ArrayListUnmanaged(u8) = .empty;
var writer = contents.writer(arena);
if (std.mem.eql(u8, name, "@cImport")) blk: {
const index = for (handle.cimports.items(.node), 0..) |cimport_node, index| {
const main_token = handle.tree.nodeMainToken(cimport_node);
const cimport_loc = offsets.tokenToLoc(handle.tree, main_token);
if (cimport_loc.start <= pos_index and pos_index <= cimport_loc.end) break index;
} else break :blk;
const source = handle.cimports.items(.source)[index];
switch (markup_kind) {
.plaintext, .unknown_value => {
try writer.print(
\\{s}
\\
, .{source});
},
.markdown => {
try writer.print(
\\```c
\\{s}
\\```
\\
, .{source});
},
}
}
const builtin = data.builtins.get(name) orelse return null;
switch (markup_kind) {
.plaintext, .unknown_value => {
try writer.print(
\\{s}
\\{s}
, .{ builtin.signature, builtin.documentation });
},
.markdown => {
try writer.print(
\\```zig
\\{s}
\\```
\\{s}
, .{ builtin.signature, builtin.documentation });
},
}
return .{
.contents = .{
.MarkupContent = .{
.kind = markup_kind,
.value = contents.items,
},
},
.range = offsets.locToRange(handle.tree.source, name_loc, offset_encoding),
};
}
fn hoverDefinitionGlobal(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
pos_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) error{OutOfMemory}!?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const name_token, const name_loc = Analyser.identifierTokenAndLocFromIndex(handle.tree, pos_index) orelse return null;
const name = offsets.locToSlice(handle.tree.source, name_loc);
const hover_text = blk: {
const is_escaped_identifier = handle.tree.source[handle.tree.tokenStart(name_token)] == '@';
if (!is_escaped_identifier) {
if (std.mem.eql(u8, name, "_")) return null;
if (try analyser.resolvePrimitive(name)) |ip_index| {
const resolved_type_str = try std.fmt.allocPrint(arena, "{}", .{analyser.ip.typeOf(ip_index).fmt(analyser.ip)});
break :blk try hoverSymbolResolved(arena, markup_kind, &.{}, name, resolved_type_str, &.{});
}
}
const decl = (try analyser.lookupSymbolGlobal(handle, name, pos_index)) orelse return null;
break :blk (try hoverSymbol(analyser, arena, decl, markup_kind)) orelse return null;
};
return .{
.contents = .{
.MarkupContent = .{
.kind = markup_kind,
.value = hover_text,
},
},
.range = offsets.locToRange(handle.tree.source, name_loc, offset_encoding),
};
}
fn hoverDefinitionEnumLiteral(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
source_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) error{OutOfMemory}!?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const name_loc = Analyser.identifierLocFromIndex(handle.tree, source_index) orelse return null;
const name = offsets.locToSlice(handle.tree.source, name_loc);
const decl = (try analyser.getSymbolEnumLiteral(handle, source_index, name)) orelse return null;
return .{
.contents = .{
.MarkupContent = .{
.kind = markup_kind,
.value = (try hoverSymbol(analyser, arena, decl, markup_kind)) orelse return null,
},
},
.range = offsets.locToRange(handle.tree.source, name_loc, offset_encoding),
};
}
fn hoverDefinitionFieldAccess(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
source_index: usize,
loc: offsets.Loc,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) error{OutOfMemory}!?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const name_loc = Analyser.identifierLocFromIndex(handle.tree, source_index) orelse return null;
const name = offsets.locToSlice(handle.tree.source, name_loc);
const held_loc = offsets.locMerge(loc, name_loc);
const decls = (try analyser.getSymbolFieldAccesses(arena, handle, source_index, held_loc, name)) orelse return null;
var content: std.ArrayListUnmanaged([]const u8) = try .initCapacity(arena, decls.len);
for (decls) |decl| {
content.appendAssumeCapacity(try hoverSymbol(analyser, arena, decl, markup_kind) orelse continue);
}
return .{
.contents = .{ .MarkupContent = .{
.kind = markup_kind,
.value = switch (content.items.len) {
0 => return null,
1 => content.items[0],
else => try std.mem.join(arena, "\n\n", content.items),
},
} },
.range = offsets.locToRange(handle.tree.source, name_loc, offset_encoding),
};
}
fn hoverNumberLiteral(
handle: *DocumentStore.Handle,
token_index: Ast.TokenIndex,
arena: std.mem.Allocator,
markup_kind: types.MarkupKind,
) error{OutOfMemory}!?[]const u8 {
const tree = handle.tree;
// number literals get tokenized separately from their minus sign
const is_negative = tree.tokenTag(token_index -| 1) == .minus;
const num_slice = tree.tokenSlice(token_index);
const number = blk: {
if (tree.tokenTag(token_index) == .char_literal) {
switch (std.zig.parseCharLiteral(num_slice)) {
.success => |value| break :blk value,
else => return null,
}
}
switch (std.zig.parseNumberLiteral(num_slice)) {
.int => |value| break :blk value,
else => return null,
}
};
switch (markup_kind) {
.markdown => return try std.fmt.allocPrint(arena,
\\| Base | {[value]s:<[count]} |
\\| ---- | {[dash]s:-<[count]} |
\\| BIN | {[sign]s}0b{[number]b:<[len]} |
\\| OCT | {[sign]s}0o{[number]o:<[len]} |
\\| DEC | {[sign]s}{[number]d:<[len]} |
\\| HEX | {[sign]s}0x{[number]X:<[len]} |
, .{
.sign = if (is_negative) "-" else "",
.dash = "-",
.value = "Value",
.number = number,
.count = @max(@bitSizeOf(@TypeOf(number)) - @clz(number) + "0x".len + @intFromBool(is_negative), "Value".len),
.len = @max(@bitSizeOf(@TypeOf(number)) - @clz(number), "Value".len - "0x".len),
}),
.plaintext, .unknown_value => return try std.fmt.allocPrint(
arena,
\\BIN: {[sign]s}0b{[number]b}
\\OCT: {[sign]s}0o{[number]o}
\\DEC: {[sign]s}{[number]d}
\\HEX: {[sign]s}0x{[number]X}
,
.{ .sign = if (is_negative) "-" else "", .number = number },
),
}
}
fn hoverDefinitionNumberLiteral(
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
source_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) !?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
const tree = handle.tree;
const token_index = offsets.sourceIndexToTokenIndex(tree, source_index).pickPreferred(&.{ .number_literal, .char_literal }, &tree) orelse return null;
const num_loc = offsets.tokenToLoc(tree, token_index);
const hover_text = (try hoverNumberLiteral(handle, token_index, arena, markup_kind)) orelse return null;
return .{
.contents = .{ .MarkupContent = .{
.kind = markup_kind,
.value = hover_text,
} },
.range = offsets.locToRange(handle.tree.source, num_loc, offset_encoding),
};
}
pub fn hover(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
source_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
) !?types.Hover {
const pos_context = try Analyser.getPositionContext(arena, handle.tree, source_index, true);
const response = switch (pos_context) {
.builtin => |loc| try hoverDefinitionBuiltin(analyser, arena, handle, source_index, loc, markup_kind, offset_encoding),
.var_access => try hoverDefinitionGlobal(analyser, arena, handle, source_index, markup_kind, offset_encoding),
.field_access => |loc| try hoverDefinitionFieldAccess(analyser, arena, handle, source_index, loc, markup_kind, offset_encoding),
.label_access, .label_decl => |loc| try hoverDefinitionLabel(analyser, arena, handle, source_index, loc, markup_kind, offset_encoding),
.enum_literal => try hoverDefinitionEnumLiteral(analyser, arena, handle, source_index, markup_kind, offset_encoding),
.number_literal, .char_literal => try hoverDefinitionNumberLiteral(arena, handle, source_index, markup_kind, offset_encoding),
else => null,
};
return response;
}