Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wasm_hover): remove json serialization #6

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
})
}
})
Loading