From: Brendan Hansen Date: Thu, 2 Nov 2023 15:13:28 +0000 (-0500) Subject: added: `--lspinfo` for new LSP needs X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fbefcb2b74bb45bbf33b94aafd8b3096af6c26a0;p=onyx.git added: `--lspinfo` for new LSP needs --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 1bf31450..7ce5ab5c 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1820,6 +1820,7 @@ struct CompileOptions { b32 generate_tag_file : 1; b32 generate_symbol_info_file : 1; + b32 generate_lsp_info_file : 1; Runtime runtime; @@ -2081,6 +2082,7 @@ b32 resolve_intrinsic_interface_constraint(AstConstraint *constraint); void track_declaration_for_tags(AstNode *); void track_declaration_for_symbol_info(OnyxFilePos, AstNode *); +void track_documentation_for_symbol_info(AstNode *, OnyxToken *); void track_resolution_for_symbol_info(AstNode *original, AstNode *resolved); // NOTE: Useful inlined functions diff --git a/compiler/include/doc.h b/compiler/include/doc.h index 45214503..8ac81777 100644 --- a/compiler/include/doc.h +++ b/compiler/include/doc.h @@ -24,6 +24,7 @@ struct SymbolInfo { u32 file_id; u32 line; u32 column; + OnyxToken *documentation; }; typedef struct SymbolResolution SymbolResolution; diff --git a/compiler/src/doc.c b/compiler/src/doc.c index 8d838c11..00150c29 100644 --- a/compiler/src/doc.c +++ b/compiler/src/doc.c @@ -89,11 +89,27 @@ void onyx_docs_emit_symbol_info(const char *dest) { bh_buffer sym_def_section; bh_buffer_init(&sym_def_section, global_heap_allocator, 2048); + + bh_buffer docs_section; + bh_buffer_init(&docs_section, global_heap_allocator, 4096); + bh_arr_each(SymbolInfo, sym, syminfo->symbols) { bh_buffer_write_u32(&sym_def_section, sym->id); bh_buffer_write_u32(&sym_def_section, sym->file_id); bh_buffer_write_u32(&sym_def_section, sym->line); bh_buffer_write_u32(&sym_def_section, sym->column); + + if (context.options->generate_lsp_info_file) { + if (sym->documentation) { + bh_buffer_write_u32(&sym_def_section, docs_section.length); + bh_buffer_write_u32(&sym_def_section, sym->documentation->length); + + bh_buffer_append(&docs_section, sym->documentation->text, sym->documentation->length); + } else { + bh_buffer_write_u32(&sym_def_section, 0); + bh_buffer_write_u32(&sym_def_section, 0); + } + } } bh_buffer sym_res_section; @@ -109,19 +125,36 @@ void onyx_docs_emit_symbol_info(const char *dest) { bh_buffer header_section; bh_buffer_init(&header_section, global_heap_allocator, 16); bh_buffer_append(&header_section, "OSYM", 4); - bh_buffer_write_u32(&header_section, 1); - bh_buffer_write_u32(&header_section, 32); + + u32 header_size = 32; + if (context.options->generate_lsp_info_file) { + bh_buffer_write_u32(&header_section, 2); + header_size = 40; + } else { + bh_buffer_write_u32(&header_section, 1); + } + + bh_buffer_write_u32(&header_section, header_size); bh_buffer_write_u32(&header_section, shlenu(syminfo->files)); - bh_buffer_write_u32(&header_section, 32 + file_section.length); + bh_buffer_write_u32(&header_section, header_size + file_section.length); bh_buffer_write_u32(&header_section, bh_arr_length(syminfo->symbols)); - bh_buffer_write_u32(&header_section, 32 + file_section.length + sym_def_section.length); + bh_buffer_write_u32(&header_section, header_size + file_section.length + sym_def_section.length); bh_buffer_write_u32(&header_section, bh_arr_length(syminfo->symbols_resolutions)); + if (context.options->generate_lsp_info_file) { + bh_buffer_write_u32(&header_section, header_size + file_section.length + sym_def_section.length + sym_res_section.length); + bh_buffer_write_u32(&header_section, docs_section.length); + } + bh_file_write(&sym_file, header_section.data, header_section.length); bh_file_write(&sym_file, file_section.data, file_section.length); bh_file_write(&sym_file, sym_def_section.data, sym_def_section.length); bh_file_write(&sym_file, sym_res_section.data, sym_res_section.length); + if (context.options->generate_lsp_info_file) { + bh_file_write(&sym_file, docs_section.data, docs_section.length); + } + bh_file_close(&sym_file); bh_buffer_free(&header_section); diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index d2fca61d..a73dc0a9 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -19,7 +19,7 @@ extern struct bh_allocator global_heap_allocator; #include "wasm_emit.h" #include "doc.h" -#define VERSION "v0.1.7" +#define VERSION "v0.1.8" Context context; @@ -66,7 +66,8 @@ static const char *build_docstring = DOCSTRING_HEADER "\t Automatically enabled for \"onyx\" runtime.\n" "\t--doc Generates an O-DOC file, a.k.a an Onyx documentation file. Used by onyx-doc-gen.\n" "\t--tag Generates a C-Tag file.\n" - "\t--syminfo Generates a symbol resolution information file. Used by onyx-lsp.\n" + "\t--syminfo (DEPRECATED) Generates a symbol resolution information file. Used by onyx-lsp.\n" + "\t--lspinfo Generates an LSP information file. Used by onyx-lsp.\n" "\t--stack-trace Enable dynamic stack trace.\n" "\t--no-std Disable automatically including \"core/std\".\n" "\t--no-stale-code Disables use of `#allow_stale_code` directive\n" @@ -119,6 +120,7 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .generate_tag_file = 0, .generate_symbol_info_file = 0, + .generate_lsp_info_file = 0, }; bh_arr_new(alloc, options.files, 2); @@ -279,6 +281,11 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg options.generate_symbol_info_file = 1; options.symbol_info_file = argv[++i]; } + else if (!strcmp(argv[i], "--lspinfo")) { + options.generate_symbol_info_file = 1; + options.generate_lsp_info_file = 1; + options.symbol_info_file = argv[++i]; + } else if (!strcmp(argv[i], "--debug")) { options.debug_session = 1; options.debug_info_enabled = 1; diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 9ec553ca..447b1a4a 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -1903,6 +1903,7 @@ void symres_entity(Entity* ent) { switch (ent->type) { case Entity_Type_Binding: { symbol_introduce(current_scope, ent->binding->token, ent->binding->node); + track_documentation_for_symbol_info(ent->binding->node, ent->binding->documentation); track_declaration_for_tags((AstNode *) ent->binding); if (context.doc_info) { diff --git a/compiler/src/utils.c b/compiler/src/utils.c index e027a52a..1262c01f 100644 --- a/compiler/src/utils.c +++ b/compiler/src/utils.c @@ -1576,6 +1576,19 @@ void track_declaration_for_symbol_info(OnyxFilePos pos, AstNode *node) { bh_imap_put(&syminfo->node_to_id, (u64) node, (u64) symbol_id); } +void track_documentation_for_symbol_info(AstNode *node, OnyxToken *documentation) { + if (!context.options->generate_lsp_info_file) return; + if (!context.options->generate_symbol_info_file) return; + + SymbolInfoTable *syminfo = context.symbol_info; + assert(syminfo); + + if (!bh_imap_has(&syminfo->node_to_id, (u64) node)) return; + + u64 symbol_id = bh_imap_get(&syminfo->node_to_id, (u64) node); + syminfo->symbols[symbol_id].documentation = documentation; +} + void track_resolution_for_symbol_info(AstNode *original, AstNode *resolved) { if (!context.options->generate_symbol_info_file) return; if (!resolved) return; diff --git a/docs/symbol_info.md b/docs/symbol_info.md index 901e2b06..8e2c3ef4 100644 --- a/docs/symbol_info.md +++ b/docs/symbol_info.md @@ -15,6 +15,8 @@ File contains: - File ID - Line - Column + * Documentation offset + * Documentation length - Symbol resolution table (sorted in some way to speed up lookup) (20 bytes) - File ID @@ -31,6 +33,9 @@ File contains: * Scope symbols table * Symbol ID (-1 for end of list) +* Documentation table + * Long list of bytes + Byte respresentation of the file: magic bytes: O S Y M 0x0 0x0 0x0 0x1 @@ -39,6 +44,7 @@ Byte respresentation of the file: file definition offset file definition entry count symbol definition offset symbol definition entry count symbol resolution offset symbol resolution entry count + * docs offset docs length file section: file defintions