Skip to content

Commit

Permalink
wrap number hover in code block on zed
Browse files Browse the repository at this point in the history
  • Loading branch information
xdBronch committed Nov 23, 2024
1 parent 0353740 commit 690fd13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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
13 changes: 11 additions & 2 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,21 @@ 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,
\\{[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]} |
\\{[md_ticks]s}
, .{
.md_ticks = if (is_zed) "```" else "",
.sign = if (is_negative) "-" else "",
.dash = "-",
.value = "Value",
Expand All @@ -422,14 +429,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 +455,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 +465,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

0 comments on commit 690fd13

Please sign in to comment.