diff --git a/server/completition.jai b/server/completition.jai index 69e5c6a..0533de0 100644 --- a/server/completition.jai +++ b/server/completition.jai @@ -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); } diff --git a/server/hover.jai b/server/hover.jai index 5362a36..58fe62e 100644 --- a/server/hover.jai +++ b/server/hover.jai @@ -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) { diff --git a/server/lsp_interface.jai b/server/lsp_interface.jai index 53c064a..ba3a4d8 100644 --- a/server/lsp_interface.jai +++ b/server/lsp_interface.jai @@ -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 {