diff --git a/ic10lsp_wasm/Cargo.lock b/ic10lsp_wasm/Cargo.lock index 1f9f4a3..5812c9c 100644 --- a/ic10lsp_wasm/Cargo.lock +++ b/ic10lsp_wasm/Cargo.lock @@ -98,13 +98,13 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.77" +version = "0.1.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +checksum = "461abc97219de0eaaf81fe3ef974a540158f3d079c2ab200f891f1a2ef201e85" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -115,7 +115,7 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -220,7 +220,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -329,7 +329,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -395,7 +395,7 @@ checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" [[package]] name = "ic10lsp" version = "0.7.4" -source = "git+https://github.com/Ryex/ic10lsp.git?branch=wasm#08f9813a396944588f4dd44310854534813b8306" +source = "git+https://github.com/Ryex/ic10lsp.git?branch=wasm#902fdff6e6c42b4a26f28c5df54903cfd61483df" dependencies = [ "clap", "const-crc32", @@ -605,7 +605,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -634,7 +634,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -755,7 +755,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -777,7 +777,7 @@ checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -839,9 +839,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.52" +version = "2.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" dependencies = [ "proc-macro2", "quote", @@ -865,7 +865,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -910,7 +910,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -979,7 +979,7 @@ checksum = "84fd902d4e0b9a4b27f2f440108dc034e1758628a9b702f8ec61ad66355422fa" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -1007,7 +1007,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", ] [[package]] @@ -1043,7 +1043,7 @@ dependencies = [ [[package]] name = "tree-sitter-ic10" version = "0.5.2" -source = "git+https://github.com/Ryex/tree-sitter-ic10.git?branch=wasm#1afad59d8f46a267ecde4d543f6e56b07c446042" +source = "git+https://github.com/Ryex/tree-sitter-ic10.git?branch=wasm#8fc265c9e69da58f9e8fb2ac2840d2f55d1503a8" dependencies = [ "cc", "tree-sitter", @@ -1126,15 +1126,15 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.31" +version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de9a9cec1733468a8c657e57fa2413d2ae2c0129b95e87c5b72b8ace4d13f31f" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" dependencies = [ "cfg-if", "futures-core", @@ -1161,7 +1161,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn 2.0.53", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/www/src/index.js b/www/src/index.js index 9ddc2a7..e650ccb 100644 --- a/www/src/index.js +++ b/www/src/index.js @@ -51,20 +51,49 @@ function docReady(fn) { init(); -const demoCode = `# This is a comment -define a_def 10 -define a_hash HASH("This is a String") -alias a_var r0 -alias a_device d0 -s d0 12 0 -move r2 LogicType.Temperature -move r3 pinf -main: +const demoCode = `# Highlighting Demo +# This is a comment +# Hover a define id anywhere to see it's definition +define a_def 10 + +# Hover HASH("String")'s to see computed crc32 +# hover here vvvvvvvvvvvvvvvv +define a_hash HASH("This is a String") + +# hover over an alias anywhere in the code +# to see it's definition +alias a_var r0 +alias a_device d0 + +# instructions have Auto Completion, +# numeric logic types are identified on hover +s db 12 0 +# ^^ +# hover here + +# Enums and their values are Known, Hover them! +# vvvvvvvvvvvvvvvvvv +move r2 LogicType.Temperature + +# same with constants +# vvvv +move r3 pinf + +# Labels are known +main: l r1 dr15 RatioWater move r2 100000.001 + +# Hover Hash Strings of Known prefab names +# to get their documentation +# vvvvvvvvvvvvvvv move r0 HASH("AccessCardBlack") beqz r1 test + +# -2045627372 is the crc32 hash of a SolarPanel, +# hover it to see the documentation! +# vvvvvvvvvv move r1 -2045627372 jal test move r1 $FF @@ -103,11 +132,90 @@ async function setupLsp(editor, mode) { } +function base64url_encode(buffer) { + return btoa(Array.from(new Uint8Array(buffer), b => String.fromCharCode(b)).join('')) + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/, ''); +} + +function base64url_decode(value) { + const m = value.length % 4; + return Uint8Array.from(atob( + value.replace(/-/g, '+') + .replace(/_/g, '/') + .padEnd(value.length + (m === 0 ? 0 : 4 - m), '=') + ), c => c.charCodeAt(0)).buffer +} + +async function concatUintArrays(arrays) { + const blob = new Blob(arrays); + const buffer = await blob.arrayBuffer(); + return new Uint8Array(buffer); +} + +async function compress(bytes) { + const s = new Blob([bytes]).stream(); + const cs = s.pipeThrough( + new CompressionStream('gzip') + ); + const chunks = []; + for await (const chunk of cs) { + chunks.push(chunk); + } + return await concatUintArrays(chunks); +} + +async function decompress(bytes) { + const s = new Blob([bytes]).stream(); + const ds = s.pipeThrough( + new DecompressionStream('gzip') + ); + const chunks = []; + for await (const chunk of ds) { + chunks.push(chunk); + } + return await concatUintArrays(chunks); +} + + +async function setDocFragment(content) { + const bytes = new TextEncoder().encode(content); + try { + const c_bytes = await compress(bytes); + const fragment = base64url_encode(c_bytes); + window.history.replaceState(null, "", `#${fragment}`); + } catch (e) { + console.log("Error compressing content fragment:", e); + return; + } +} + +async function getContentFromFragment(editor, default_content) { + const fragment = window.location.hash.slice(1); + console.log("fragment is", fragment); + if (fragment.length > 0) { + const c_bytes = base64url_decode(fragment); + console.log("compressed", c_bytes); + try { + const bytes = await decompress(c_bytes); + console.log("bytes", bytes); + const content = new TextDecoder().decode(bytes); + console.log("content is", content); + editor.getSession().setValue(content); + } catch (e) { + console.log("Error decompressing content fragment:", e); + return; + } + } else { + editor.getSession().setValue(default_content); + } +} + docReady(() => { const mode = new IC10Mode() var editor = ace.edit("editor", { mode: mode, - value: demoCode, enableBasicAutocompletion: true, enableLiveAutocompletion: true, enableSnippets: true, @@ -117,6 +225,11 @@ docReady(() => { firstLineNumber: 0, printMarginColumn: 52, }); + editor.getSession().on('change', () => { + var val = editor.getSession().getValue(); + setDocFragment(val); + }); + editor.setTheme("ace/theme/one_dark"); var statusBar = new ace_ext_statusbar.StatusBar(editor, document.getElementById("statusBar")); statusBar.updateStatus(editor); @@ -126,6 +239,12 @@ docReady(() => { editor.session.setValue(demoCode) setupLsp(editor, mode); + + getContentFromFragment(editor, demoCode); + + window.addEventListener('hashchange', (event) => { + getContentFromFragment(editor, ""); + }); });