added: `--lspinfo` for new LSP needs
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 2 Nov 2023 15:13:28 +0000 (10:13 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 2 Nov 2023 15:13:28 +0000 (10:13 -0500)
compiler/include/astnodes.h
compiler/include/doc.h
compiler/src/doc.c
compiler/src/onyx.c
compiler/src/symres.c
compiler/src/utils.c
docs/symbol_info.md

index 1bf3145000a6c75747a646be2621294114ef7623..7ce5ab5c1c345f2988781d70ffd181612efa505f 100644 (file)
@@ -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
index 45214503c275faabeda0c6831fe48c25681873f1..8ac81777c17d3bc3afedb6adf1bb05ce3886090d 100644 (file)
@@ -24,6 +24,7 @@ struct SymbolInfo {
        u32 file_id;
        u32 line;
        u32 column;
+       OnyxToken *documentation;
 };
 
 typedef struct SymbolResolution SymbolResolution;
index 8d838c11094c0075446f86de7d3f7f42240436ea..00150c297a5d65da1db5f5bf530b0c42e4d1daab 100644 (file)
@@ -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);
index d2fca61d8e6a801b96eadc08099d03de0cf4c16f..a73dc0a9c1e059027d92db35e9c297a3c17b67ad 100644 (file)
@@ -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 <doc_file>        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 <target_file> Generates a symbol resolution information file. Used by onyx-lsp.\n"
+    "\t--syminfo <target_file> (DEPRECATED) Generates a symbol resolution information file. Used by onyx-lsp.\n"
+    "\t--lspinfo <target_file> 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;
index 9ec553ca7da2d1326012f428cff45ff4fd23ff3c..447b1a4aae51be097113982cb2be47f7cd062ee8 100644 (file)
@@ -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) {
index e027a52af98f3048e7ff30dbcaec1cb4de7b115d..1262c01fe66e5bef8f917aab81f99e4590f90f6d 100644 (file)
@@ -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;
index 901e2b0629b744a2cdc80f9c6570a98077bbe4f0..8e2c3ef470bd8cc88bb788ba0932756a4f8cc970 100644 (file)
@@ -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