Skip to content

Commit

Permalink
feat(folding): support regions comments & eof & more (#1)
Browse files Browse the repository at this point in the history
* more tests and more stuff

* fix(folding): correctly detect single-line region comments

* fix(folding): correctly handle nested region comments

* fix: wrong block kind

* feat(folding): Add benchmarks

* fix: run all benchs

* Actually, Erika wants us to use the text again

* fix: lint

* fix bed tests && tests in another file

* fix TS test

* perf(folding): reuse cached line indices

* fix lint

* doc: jsdoc on wasm function

* typo

---------

Co-authored-by: Erika <[email protected]>
  • Loading branch information
goulvenclech and Princesseuh committed Nov 29, 2024
1 parent ae1165b commit 9d64614
Show file tree
Hide file tree
Showing 10 changed files with 764 additions and 226 deletions.
2 changes: 1 addition & 1 deletion csslsrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ rustc-hash = "2.0.0"
criterion = { package = "codspeed-criterion-compat", version = "*" }

[[bench]]
name = "color"
name = "bench_main"
harness = false
5 changes: 5 additions & 0 deletions csslsrs/benches/bench_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use criterion::criterion_main;

mod features;

criterion_main!(features::folding::benches, features::color::benches);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, Criterion};
use csslsrs::service::LanguageService;
use lsp_types::{TextDocumentItem, Uri};
use std::{hint::black_box, str::FromStr};
Expand All @@ -19,4 +19,3 @@ fn get_colors_benchmark(c: &mut Criterion) {
}

criterion_group!(benches, get_colors_benchmark);
criterion_main!(benches);
56 changes: 56 additions & 0 deletions csslsrs/benches/features/folding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use criterion::{criterion_group, Criterion};
use csslsrs::service::LanguageService;
use lsp_types::{TextDocumentItem, Uri};
use std::{hint::black_box, str::FromStr};

static TEST_CASE: &str = r#"
body {
background-color: #fff;
}
a {
color: red;
}
h1 {
color: rgba(0, 0, 0, 0.5);
}
h2 {
color: linear-gradient(to right, red, #fff);
}
//#region Outer Region
h3 {
color: hsl(120, 100%, 50%);
}
//#region Inner Region
h4 {
color: hwb(120, 0%, 0%);
}
//#endregion
//#endregion
@media (max-width: 600px) {
body {
background-color: #000;
}
}
"#;

fn get_folding_ranges_benchmark(c: &mut Criterion) {
let mut ls = LanguageService::default();

let document = TextDocumentItem {
uri: Uri::from_str("file:///test.css").unwrap(),
language_id: "css".to_string(),
version: 0,
text: TEST_CASE.to_string(),
};

c.bench_function("get_folding_ranges", |b| {
b.iter(|| ls.get_folding_ranges(black_box(document.clone())))
});
}

criterion_group!(benches, get_folding_ranges_benchmark);
2 changes: 2 additions & 0 deletions csslsrs/benches/features/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod color;
pub mod folding;
55 changes: 55 additions & 0 deletions csslsrs/packages/benchmark-wasm/benchmarks/folding_ranges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Bench } from "tinybench";
import { get_folding_ranges } from "csslsrs";
import { getCSSLanguageService } from "vscode-css-languageservice";
import { TextDocument } from "vscode-languageserver-textdocument";

const bench = new Bench({ name: "Folding Ranges", time: 100 });

const vscodeLanguageService = getCSSLanguageService();
const content = `
body {
background-color: #fff;
}
a {
color: red;
}
h1 {
color: rgba(0, 0, 0, 0.5);
}
h2 {
color: linear-gradient(to right, red, #fff);
}
//#region Outer Region
h3 {
color: hsl(120, 100%, 50%);
}
//#region Inner Region
h4 {
color: hwb(120, 0%, 0%);
}
//#endregion
//#endregion
@media (max-width: 600px) {
body {
background-color: #000;
}
}
`;

const textDocument = TextDocument.create("file:///test.css", "css", 0, content);

bench
.add("CSSLSRS - WASM", async () => {
await get_folding_ranges(textDocument);
})
.add("vscode-css-languageservice", () => {
const stylesheet = vscodeLanguageService.parseStylesheet(textDocument);
vscodeLanguageService.getFoldingRanges(textDocument, stylesheet);
});

export default bench;
2 changes: 2 additions & 0 deletions csslsrs/packages/benchmark-wasm/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import colorBenchmark from "./benchmarks/colors.js";
import foldingRangesBenchmark from "./benchmarks/folding_ranges.js";

const benchmarks = [
colorBenchmark,
foldingRangesBenchmark,
// Add more benchmarks here
];

Expand Down
1 change: 0 additions & 1 deletion csslsrs/packages/csslsrs/test/features/folding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe("Folding", () => {
{
endLine: 3,
startLine: 0,
kind: "region",
},
] satisfies FoldingRange[]);
});
Expand Down
Loading

0 comments on commit 9d64614

Please sign in to comment.