Skip to content

Commit

Permalink
fix(wasm_hover): remove json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
goulvenclech committed Dec 4, 2024
1 parent aa36d93 commit 8d4d95e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 11 additions & 5 deletions crates/csslsrs/src/features/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CssCustomData> =
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<import("vscode-languageserver-types").Hover | null>;"#;
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<import("vscode-languageserver-types").Hover | null>;"#;

#[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);
Expand All @@ -361,7 +367,7 @@ mod wasm_bindings {
position,
&line_index,
encoding,
&css_data,
&CSS_DATA,
);

serde_wasm_bindgen::to_value(&hover).unwrap()
Expand Down
3 changes: 1 addition & 2 deletions packages/benchmark-wasm/benchmarks/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Bench } from "tinybench"
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"

const bench = new Bench({ name: "Hover", time: 100 })

Expand Down Expand Up @@ -32,7 +31,7 @@ export function registerHoverBenchmarks(
onlyCSSLSRS: boolean
): Bench {
bench.add("CSSLSRS(WASM) - Hover", async () => {
await get_hover(textDocument, { line: 4, character: 3 }, cssCustomData)
await get_hover(textDocument, { line: 4, character: 3 })
})

if (onlyCSSLSRS) return bench
Expand Down

0 comments on commit 8d4d95e

Please sign in to comment.