Skip to content
Open
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
14 changes: 14 additions & 0 deletions server/completition.jai
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ send_completions_decls :: (request: LSP_Request_Message_Completion, decls: []*De
item.labelDetails.description = ast_print_to_string(preview_node);
}

// Documentation shown in the suggestion details panel (toggled with ctrl+space in VS Code).
doc_builder: String_Builder;
append(*doc_builder, "```jai\n");
append_decl_for_hover(*doc_builder, decl);
append(*doc_builder, "\n```");

doc_comment := get_doc_comment_for_decl(decl);
if doc_comment.count > 0 {
append(*doc_builder, "\n");
append(*doc_builder, doc_comment);
}

item.documentation.value = builder_to_string(*doc_builder);

array_add(*completions, item);
}

Expand Down
47 changes: 47 additions & 0 deletions server/hover.jai
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,58 @@ handle_hover :: (request: LSP_Request_Message_Hover) {
append_decl_for_hover(*builder, target_decl);
append(*builder, "\n```");

doc_comment := get_doc_comment_for_decl(target_decl);
if doc_comment.count > 0 {
append(*builder, "\n");
append(*builder, doc_comment);
}

hover: LSP_Hover;
hover.contents.value = builder_to_string(*builder);
lsp_respond(request.id, hover);
}

get_doc_comment_for_decl :: (decl: *Declaration) -> string {
file := get_file(decl.location.file);
if !file return "";

line_above := decl.location.l0 - 1;
if line_above < 0 return "";

// Multiline comment (/* ... */) ending directly above the declaration.
for *comment: file.comments {
if comment.multiline && comment.l1 == line_above {
return trim(comment.string_value);
}
}

// Contiguous run of single-line comments (//) directly above the declaration.
lines: [..]string;
current_line := line_above;
while current_line >= 0 {
found := false;
for *comment: file.comments {
if !comment.multiline && comment.l0 == current_line {
array_add(*lines, trim(comment.string_value));
found = true;
break;
}
}
if !found break;
current_line -= 1;
}

if lines.count == 0 return "";

builder: String_Builder;
for < line: lines {
append(*builder, line);
if it_index > 0 append(*builder, "\n");
}

return builder_to_string(*builder);
}

append_decl_for_hover :: (builder: *String_Builder, decl: *Declaration) {
expr := decl.expression;
if expr && (expr.kind == .STRUCT || expr.kind == .ENUM || expr.kind == .UNION) {
Expand Down
5 changes: 4 additions & 1 deletion server/lsp_interface.jai
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,10 @@ LSP_Completion_Item :: struct {
label: string;
detail: string;
insertText: string;
documentation: string;
documentation: struct {
kind := "markdown";
value: string;
};
deprecated: bool;

labelDetails: struct {
Expand Down