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 5501f68 commit a9b88c6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 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
55 changes: 27 additions & 28 deletions packages/benchmark-wasm/benchmarks/hover.bench.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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
)
})
}
})

0 comments on commit a9b88c6

Please sign in to comment.