Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap number hover in code block #2058

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,15 @@ fn hoverHandler(server: *Server, arena: std.mem.Allocator, request: types.HoverP
var analyser = server.initAnalyser(handle);
defer analyser.deinit();

return hover_handler.hover(&analyser, arena, handle, source_index, markup_kind, server.offset_encoding);
return hover_handler.hover(
&analyser,
arena,
handle,
source_index,
markup_kind,
server.offset_encoding,
server.client_capabilities.client_name,
);
}

fn documentSymbolsHandler(server: *Server, arena: std.mem.Allocator, request: types.DocumentSymbolParams) Error!lsp.ResultType("textDocument/documentSymbol") {
Expand Down
15 changes: 11 additions & 4 deletions src/features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ fn hoverNumberLiteral(
token_index: Ast.TokenIndex,
arena: std.mem.Allocator,
markup_kind: types.MarkupKind,
client_name: ?[]const u8,
) error{OutOfMemory}!?[]const u8 {
const tree = handle.tree;
// number literals get tokenized separately from their minus sign
Expand All @@ -388,15 +389,19 @@ fn hoverNumberLiteral(
}
};

// Zed currently doesn't render markdown unless wrapped in code blocks
// Remove this when this issue is closed https://github.com/zed-industries/zed/issues/5386
const is_zed = if (client_name) |name| std.mem.startsWith(u8, name, "Zed") else false;
switch (markup_kind) {
.markdown => return try std.fmt.allocPrint(arena,
\\| Base | {[value]s:<[count]} |
\\{[md_ticks]s}| 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]} |
\\| HEX | {[sign]s}0x{[number]X:<[len]} |{[md_ticks]s}
, .{
.md_ticks = if (is_zed) "\n```" else "",
.sign = if (is_negative) "-" else "",
.dash = "-",
.value = "Value",
Expand All @@ -422,14 +427,15 @@ fn hoverDefinitionNumberLiteral(
source_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
client_name: ?[]const u8,
) !?types.Hover {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

const tree = handle.tree;
const token_index = offsets.sourceIndexToTokenIndex(tree, source_index);
const num_loc = offsets.tokenToLoc(tree, token_index);
const hover_text = (try hoverNumberLiteral(handle, token_index, arena, markup_kind)) orelse return null;
const hover_text = (try hoverNumberLiteral(handle, token_index, arena, markup_kind, client_name)) orelse return null;

return .{
.contents = .{ .MarkupContent = .{
Expand All @@ -447,6 +453,7 @@ pub fn hover(
source_index: usize,
markup_kind: types.MarkupKind,
offset_encoding: offsets.Encoding,
client_name: ?[]const u8,
) !?types.Hover {
const pos_context = try Analyser.getPositionContext(arena, handle.tree, source_index, true);

Expand All @@ -456,7 +463,7 @@ pub fn hover(
.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),
.number_literal, .char_literal => try hoverDefinitionNumberLiteral(arena, handle, source_index, markup_kind, offset_encoding, client_name),
else => null,
};

Expand Down