Skip to content

Commit

Permalink
feat: typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Dec 8, 2024
1 parent e12a846 commit 190abc2
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 37 deletions.
10 changes: 8 additions & 2 deletions crates/csslsrs/src/features/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,15 @@ mod wasm_bindings {
use serde_wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
declare function get_document_colors(documentUri: string): import("vscode-languageserver-types").ColorInformation[];
declare function get_color_presentations(color: import("vscode-languageserver-types").ColorInformation): import("vscode-languageserver-types").ColorPresentation[];
"#;

#[wasm_bindgen]
impl WASMLanguageService {
#[wasm_bindgen]
#[wasm_bindgen(skip_typescript, js_name = getDocumentColors)]
pub fn get_document_colors(&self, document_uri: String) -> JsValue {
let store_document = self.store.get(&Uri::from_str(&document_uri).unwrap());

Expand All @@ -178,7 +184,7 @@ mod wasm_bindings {
serde_wasm_bindgen::to_value(&document_colors).unwrap()
}

#[wasm_bindgen]
#[wasm_bindgen(skip_typescript, js_name = getColorPresentations)]
pub fn get_color_presentations(&self, color: JsValue) -> JsValue {
let color = serde_wasm_bindgen::from_value(color).unwrap();
let color_presentations = compute_color_presentations(color);
Expand Down
7 changes: 6 additions & 1 deletion crates/csslsrs/src/features/folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ mod wasm_bindings {
use lsp_types::Uri;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
declare function get_folding_ranges(documentUri: string): import("vscode-languageserver-types").FoldingRange[];
"#;

#[wasm_bindgen]
impl WASMLanguageService {
#[wasm_bindgen]
#[wasm_bindgen(skip_typescript, js_name = getFoldingRanges)]
pub fn get_folding_ranges(&self, document_uri: String) -> JsValue {
let store_document = self.store.get(&Uri::from_str(&document_uri).unwrap());

Expand Down
7 changes: 6 additions & 1 deletion crates/csslsrs/src/features/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,14 @@ mod wasm_bindings {
use serde_wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
declare function get_hover(documentUri: string, position: import("vscode-languageserver-types").Position): import("vscode-languageserver-types").FoldingRange[];
"#;

#[wasm_bindgen]
impl WASMLanguageService {
#[wasm_bindgen]
#[wasm_bindgen(skip_typescript, js_name = getHover)]
pub fn get_hover(&self, document_uri: String, position: JsValue) -> JsValue {
let store_document = self.store.get(&Uri::from_str(&document_uri).unwrap());

Expand Down
33 changes: 24 additions & 9 deletions crates/csslsrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::{
};
use lsp_types::{TextDocumentItem, Uri};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

/// The Language Service is the main entry point for interacting with CSSlsrs.
/// It contains a DocumentStore, a PositionEncoding and a reference to the CSS data.
Expand All @@ -19,7 +17,7 @@ pub struct LanguageService {
}

impl LanguageService {
/// Create a new LanguageService. This will create a {DocumentStore} internally. See {LanguageServiceOptions} for more information on the options available or {LanguageService::new_with_store} if you want to use an existing {DocumentStore}.
/// Create a new LanguageService. This will create a `DocumentStore`` internally. See `LanguageServiceOptions` for more information on the options available or `LanguageService::new_with_store` if you want to use an existing `DocumentStore`.
///
/// ## Example
/// ```rust
Expand All @@ -46,7 +44,7 @@ impl LanguageService {
}
}

/// Create a new LanguageService with an already existing DocumentStore. This can be useful to share the same DocumentStore between multiple LanguageServices. If you do not need to share the DocumentStore, you can use the LanguageService::new() method instead.
/// Create a new `LanguageService` with an already existing `DocumentStore`. This can be useful to share the same `DocumentStore` between multiple `LanguageService`s. If you do not need to share the `DocumentStore`, you can use the `LanguageService::new()` method instead.
///
/// ## Example
///
Expand Down Expand Up @@ -74,7 +72,7 @@ impl LanguageService {
}
}

/// Add custom CSS data to the LanguageService. This can be useful to add custom CSS properties, at-rules, or pseudo-classes to the LanguageService.
/// Add custom CSS data to the `LanguageService`. This can be useful to add custom CSS properties, at-rules, or pseudo-classes to the `LanguageService`.
///
/// ## Example
///
Expand Down Expand Up @@ -136,7 +134,6 @@ impl Default for LanguageService {
}

#[derive(Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct LanguageServiceOptions {
#[serde(default)]
pub encoding: PositionEncoding,
Expand Down Expand Up @@ -168,17 +165,35 @@ pub mod wasm_bindings {

// We use a different struct for the WASM bindings because otherwise implementations conflicts with the non-WASM version
// which works just fine when using a single feature, but makes development harder when using both features at the same time.
#[wasm_bindgen]
#[wasm_bindgen(skip_typescript)]
pub struct WASMLanguageService {
pub(crate) store: DocumentStore,
pub(crate) options: LanguageServiceOptions,
base_css_data: &'static CssCustomData,
pub(crate) css_data: Vec<CssCustomData>,
}

#[wasm_bindgen(typescript_custom_section)]
const TS_TYPE: &'static str = r#"
export interface LanguageServiceOptions {
encoding?: PositionEncoding;
include_base_css_custom_data?: boolean;
}
export class WASMLanguageService {
constructor(options: LanguageServiceOptions);
upsertDocument(document: import("vscode-languageserver-textdocument").TextDocument): void;
getHover: typeof get_hover;
getDocumentColors: typeof get_document_colors;
getColorPresentations: typeof get_color_presentations;
getFoldingRanges: typeof get_folding_ranges;
free(): void;
}
"#;

#[wasm_bindgen]
impl WASMLanguageService {
#[wasm_bindgen(constructor)]
#[wasm_bindgen(constructor, skip_typescript)]
pub fn new(options: JsValue) -> Self {
console_error_panic_hook::set_once();
let options: LanguageServiceOptions = serde_wasm_bindgen::from_value(options).unwrap();
Expand All @@ -198,7 +213,7 @@ pub mod wasm_bindings {
}
}

#[wasm_bindgen]
#[wasm_bindgen(skip_typescript, js_name = upsertDocument)]
pub fn upsert_document(&mut self, document: JsValue) {
let document = create_text_document(document);
self.store.upsert_document(document);
Expand Down
12 changes: 6 additions & 6 deletions packages/benchmark-wasm/benchmarks/colors.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ const ls = new LanguageService({
include_base_css_custom_data: true,
});

await ls.upsert_document(textDocument);
ls.upsertDocument(textDocument);

const color = (await ls.get_document_colors(textDocument.uri))[0];
const color = ls.getDocumentColors(textDocument.uri)[0];

describe("Document colors", async () => {
bench("CSSLSRS(WASM) - Document colors", async () => {
await ls.get_document_colors(textDocument.uri);
bench("CSSLSRS(WASM) - Document colors", () => {
ls.getDocumentColors(textDocument.uri);
});
if (!process.env.CODSPEED) {
bench("vscode-css-languageservice - Document colors", () => {
Expand All @@ -44,8 +44,8 @@ describe("Document colors", async () => {
});

describe("Color Presentations", async () => {
bench("CSSLSRS(WASM) - Color Presentations", async () => {
await ls.get_color_presentations(color);
bench("CSSLSRS(WASM) - Color Presentations", () => {
ls.getColorPresentations(color);
});

if (!process.env.CODSPEED) {
Expand Down
6 changes: 3 additions & 3 deletions packages/benchmark-wasm/benchmarks/folding_ranges.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const ls = new LanguageService({
include_base_css_custom_data: false,
});

ls.upsert_document(textDocument);
ls.upsertDocument(textDocument);

describe("Folding Ranges", async () => {
bench("CSSLSRS(WASM) - Folding Ranges", async () => {
await ls.get_folding_ranges(textDocument.uri);
bench("CSSLSRS(WASM) - Folding Ranges", () => {
ls.getFoldingRanges(textDocument.uri);
});
if (!process.env.CODSPEED) {
bench("vscode-css-languageservice - Folding Ranges", () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/benchmark-wasm/benchmarks/hover.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ h1 > span {
const ls = new LanguageService({
include_base_css_custom_data: true,
});
await ls.upsert_document(textDocument);
ls.upsertDocument(textDocument);

bench("CSSLSRS(WASM) - Hover", async () => {
await ls.get_hover(textDocument.uri, {
bench("CSSLSRS(WASM) - Hover", () => {
ls.getHover(textDocument.uri, {
line: 14,
character: 3,
});
Expand Down
12 changes: 6 additions & 6 deletions packages/csslsrs/test/features/colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe("Colors", () => {
"body {\n color: red;\n background-color: #fff;\n}\n"
);

ls.upsert_document(document);
ls.upsertDocument(document);
});

it("Can return document colors", async () => {
const colors = await ls.get_document_colors(document.uri);
it("Can return document colors", () => {
const colors = ls.getDocumentColors(document.uri);

expect(colors).to.deep.equal([
{
Expand Down Expand Up @@ -65,11 +65,11 @@ describe("Colors", () => {
]);
});

it("Can return color presentations", async () => {
const colors = await ls.get_document_colors(document.uri);
it("Can return color presentations", () => {
const colors = ls.getDocumentColors(document.uri);
const firstcolor = colors[0];

const colorPresentations = await ls.get_color_presentations(firstcolor);
const colorPresentations = ls.getColorPresentations(firstcolor);

expect(colorPresentations).not.to.be.empty;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/csslsrs/test/features/folding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TextDocument } from "vscode-languageserver-textdocument";
import type { FoldingRange } from "vscode-languageserver-types";

describe("Folding", () => {
it("Can return folding ranges", async () => {
it("Can return folding ranges", () => {
const myDocument = TextDocument.create(
"file:///test.css",
"css",
Expand All @@ -16,9 +16,9 @@ describe("Folding", () => {
const ls = new LanguageService({
include_base_css_custom_data: true,
});
ls.upsert_document(myDocument);
ls.upsertDocument(myDocument);

const foldingRanges = await ls.get_folding_ranges(myDocument.uri);
const foldingRanges = ls.getFoldingRanges(myDocument.uri);

expect(foldingRanges).to.deep.equal([
{
Expand Down
6 changes: 3 additions & 3 deletions packages/csslsrs/test/features/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe("Hover", () => {
}`
);

ls.upsert_document(document);
ls.upsertDocument(document);
});

it("Can return hover", async () => {
const hover = await ls.get_hover(document.uri, { line: 0, character: 3 });
it("Can return hover", () => {
const hover = ls.getHover(document.uri, { line: 0, character: 3 });

expect(hover).to.deep.equal({
contents: {
Expand Down

0 comments on commit 190abc2

Please sign in to comment.