From a9b88c657d62887c0b5d2fb525a802e67ed899cd Mon Sep 17 00:00:00 2001 From: Goulven Clec'h Date: Wed, 4 Dec 2024 20:14:17 +0100 Subject: [PATCH] fix(wasm_hover): remove json serialization --- crates/csslsrs/src/features/hover.rs | 16 ++++-- .../benchmark-wasm/benchmarks/hover.bench.ts | 55 +++++++++---------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/crates/csslsrs/src/features/hover.rs b/crates/csslsrs/src/features/hover.rs index dfc9e64..cb56f63 100644 --- a/crates/csslsrs/src/features/hover.rs +++ b/crates/csslsrs/src/features/hover.rs @@ -339,19 +339,25 @@ mod wasm_bindings { }; use biome_rowan::AstNode; use lsp_types::Position; + use serde_json::from_str; use serde_wasm_bindgen; + use std::sync::LazyLock; use wasm_bindgen::prelude::*; - extern crate console_error_panic_hook; + // TMP: Embed the JSON data at compile time + // We'll eventually use the language service when it'll be available in WASM + static CSS_SCHEMA_JSON: &str = include_str!("../../data/css-schema.json"); + static CSS_DATA: LazyLock = + LazyLock::new(|| from_str(CSS_SCHEMA_JSON).expect("Failed to parse css-schema.json")); + #[wasm_bindgen(typescript_custom_section)] - const TS_APPEND_CONTENT: &'static str = r#"export async function get_hover(document: import("vscode-languageserver-textdocument").TextDocument, position: import("vscode-languageserver-types").Position, cssData: any): Promise;"#; + const TS_APPEND_CONTENT: &'static str = r#"export async function get_hover(document: import("vscode-languageserver-textdocument").TextDocument, position: import("vscode-languageserver-types").Position): Promise;"#; #[wasm_bindgen(skip_typescript)] - pub fn get_hover(document: JsValue, position: JsValue, css_data: JsValue) -> JsValue { + pub fn get_hover(document: JsValue, position: JsValue) -> JsValue { let parsed_text_document = create_text_document(document); let position: Position = serde_wasm_bindgen::from_value(position).unwrap(); - let css_data: CssCustomData = serde_wasm_bindgen::from_value(css_data).unwrap(); let css_parse = parse_css(&parsed_text_document.text); let line_index = LineIndex::new(&parsed_text_document.text); let encoding = PositionEncoding::Wide(WideEncoding::Utf16); @@ -361,7 +367,7 @@ mod wasm_bindings { position, &line_index, encoding, - &css_data, + &CSS_DATA, ); serde_wasm_bindgen::to_value(&hover).unwrap() diff --git a/packages/benchmark-wasm/benchmarks/hover.bench.ts b/packages/benchmark-wasm/benchmarks/hover.bench.ts index e1ec73a..d70c8b4 100644 --- a/packages/benchmark-wasm/benchmarks/hover.bench.ts +++ b/packages/benchmark-wasm/benchmarks/hover.bench.ts @@ -1,12 +1,11 @@ -import { get_hover } from "../../csslsrs/dist/generated/csslsrs"; -import { getCSSLanguageService } from "vscode-css-languageservice"; -import { TextDocument } from "vscode-languageserver-textdocument"; -import cssCustomData from "../../../crates/csslsrs/data/css-schema.json"; -import { bench, describe } from "vitest"; +import { get_hover } from "../../csslsrs/dist/generated/csslsrs" +import { getCSSLanguageService } from "vscode-css-languageservice" +import { TextDocument } from "vscode-languageserver-textdocument" +import { bench, describe } from "vitest" describe("Hover", async () => { - const vscodeLanguageService = getCSSLanguageService(); - const content = ` + const vscodeLanguageService = getCSSLanguageService() + const content = ` body { background-color: #fff; } @@ -22,26 +21,26 @@ h1.foo { h1 > span { color: linear-gradient(to right, red, #fff); } -`; +` - const textDocument = TextDocument.create( - "file:///test.css", - "css", - 0, - content - ); + const textDocument = TextDocument.create( + "file:///test.css", + "css", + 0, + content + ) - bench("CSSLSRS(WASM) - Hover", async () => { - await get_hover(textDocument, { line: 4, character: 3 }, cssCustomData); - }); - if (!process.env.CODSPEED) { - bench("vscode-css-languageservice - Hover", () => { - const stylesheet = vscodeLanguageService.parseStylesheet(textDocument); - vscodeLanguageService.doHover( - textDocument, - { line: 4, character: 3 }, - stylesheet - ); - }); - } -}); + bench("CSSLSRS(WASM) - Hover", async () => { + await get_hover(textDocument, { line: 4, character: 3 }) + }) + if (!process.env.CODSPEED) { + bench("vscode-css-languageservice - Hover", () => { + const stylesheet = vscodeLanguageService.parseStylesheet(textDocument) + vscodeLanguageService.doHover( + textDocument, + { line: 4, character: 3 }, + stylesheet + ) + }) + } +})