11import { toHtml } from "hast-util-to-html" ;
2- import { getSharedHighlighter } from "@pierre/diffs" ;
3- import { describe , expect , it } from "vite-plus/test" ;
2+ import { disposeHighlighter , getSharedHighlighter } from "@pierre/diffs" ;
3+ import { afterAll , beforeAll , describe , expect , it } from "vite-plus/test" ;
44
55import { createIncrementalHighlightedDocument } from "./incrementalHighlighting" ;
66
@@ -18,65 +18,91 @@ const samples = {
1818 sql : "SELECT 'multi\nline'\nFROM table_name;\n" ,
1919} as const ;
2020
21- const highlighterPromise = getSharedHighlighter ( {
21+ const highlighterOptions = {
2222 langs : Object . keys ( samples ) as Array < keyof typeof samples > ,
2323 themes : [ "pierre-dark" , "pierre-light" ] ,
24- preferredHighlighter : "shiki-wasm" ,
25- } ) ;
24+ } ;
2625
27- describe ( "incremental code highlighting" , ( ) => {
28- it . each ( Object . entries ( samples ) ) (
29- "matches full HTML at every streaming prefix in %s" ,
30- async ( language , code ) => {
31- const highlighter = await highlighterPromise ;
32- for ( const theme of [ "pierre-dark" , "pierre-light" ] as const ) {
33- const highlight = createIncrementalHighlightedDocument ( highlighter , language , theme ) ;
34- for ( let end = 0 ; end <= code . length ; end ++ ) {
35- const text = code . slice ( 0 , end ) ;
36- expect ( toHtml ( highlight ( text ) ) , `${ theme } , prefix ${ end } ` ) . toBe (
37- highlighter . codeToHtml ( text , { lang : language , theme } ) ,
38- ) ;
26+ describe . each ( [ "shiki-wasm" , "highlights" ] as const ) (
27+ "incremental code highlighting (%s)" ,
28+ ( engine ) => {
29+ beforeAll ( ( ) => disposeHighlighter ( ) ) ;
30+ afterAll ( ( ) => disposeHighlighter ( ) ) ;
31+ it . each ( Object . entries ( samples ) ) (
32+ "matches full HTML at every streaming prefix in %s" ,
33+ async ( language , code ) => {
34+ const highlighter = await getSharedHighlighter ( {
35+ ...highlighterOptions ,
36+ preferredHighlighter : engine ,
37+ } ) ;
38+ for ( const theme of [ "pierre-dark" , "pierre-light" ] as const ) {
39+ const highlight = createIncrementalHighlightedDocument ( highlighter , language , theme ) ;
40+ for ( let end = 0 ; end <= code . length ; end ++ ) {
41+ const text = code . slice ( 0 , end ) ;
42+ expect ( toHtml ( highlight ( text ) ) , `${ theme } , prefix ${ end } ` ) . toBe (
43+ highlighter . codeToHtml ( text , { lang : language , theme } ) ,
44+ ) ;
45+ }
46+ highlight . dispose ( ) ;
3947 }
40- }
41- } ,
42- ) ;
43-
44- it ( "resets after edits and truncation, including edits to a completed line" , async ( ) => {
45- const highlighter = await highlighterPromise ;
46- const highlight = createIncrementalHighlightedDocument (
47- highlighter ,
48- "typescript" ,
49- "pierre-dark" ,
48+ } ,
5049 ) ;
51- const inputs = [
52- "/* open\ncomment\n" ,
53- "/* open\ncomment\n*/\nconst x = 1;" ,
54- "const edited = 2;\nconst x = 1;" ,
55- "const edited = 2;\nconst x = 10;" ,
56- "const edited = 2;\n" ,
57- "" ,
58- "\n\n\nconst fresh = true;\n" ,
59- ] ;
60- for ( const text of inputs ) {
61- expect ( toHtml ( highlight ( text ) ) ) . toBe (
62- highlighter . codeToHtml ( text , { lang : "typescript" , theme : "pierre-dark" } ) ,
50+
51+ it ( "resets after edits and truncation, including edits to a completed line" , async ( ) => {
52+ const highlighter = await getSharedHighlighter ( {
53+ ...highlighterOptions ,
54+ preferredHighlighter : engine ,
55+ } ) ;
56+ const highlight = createIncrementalHighlightedDocument (
57+ highlighter ,
58+ "typescript" ,
59+ "pierre-dark" ,
6360 ) ;
64- }
65- } ) ;
61+ const inputs = [
62+ "/* open\ncomment\n" ,
63+ "/* open\ncomment\n*/\nconst x = 1;" ,
64+ "const edited = 2;\nconst x = 1;" ,
65+ "const edited = 2;\nconst x = 10;" ,
66+ "const edited = 2;\n" ,
67+ "" ,
68+ "\n\n\nconst fresh = true;\n" ,
69+ ] ;
70+ for ( const text of inputs ) {
71+ expect ( toHtml ( highlight ( text ) ) ) . toBe (
72+ highlighter . codeToHtml ( text , { lang : "typescript" , theme : "pierre-dark" } ) ,
73+ ) ;
74+ }
75+ highlight . dispose ( ) ;
76+ } ) ;
6677
67- it ( "preserves partial CRLF and CR line endings" , async ( ) => {
68- const highlighter = await highlighterPromise ;
69- const highlight = createIncrementalHighlightedDocument (
70- highlighter ,
71- "typescript" ,
72- "pierre-dark" ,
73- ) ;
74- const code = "/* multi\r\nline */\r\nconst x = 1;\r\n" ;
75- for ( let end = 0 ; end <= code . length ; end ++ ) {
76- const text = code . slice ( 0 , end ) ;
77- expect ( toHtml ( highlight ( text ) ) ) . toBe (
78- highlighter . codeToHtml ( text , { lang : "typescript" , theme : "pierre-dark" } ) ,
78+ // MarkdownCodeBlock creates the document outside its plain-text fallback.
79+ it ( "creates a document for a language the highlighter has not loaded" , async ( ) => {
80+ const highlighter = await getSharedHighlighter ( {
81+ ...highlighterOptions ,
82+ preferredHighlighter : engine ,
83+ } ) ;
84+ expect ( ( ) =>
85+ createIncrementalHighlightedDocument ( highlighter , "cobol" , "pierre-dark" ) . dispose ( ) ,
86+ ) . not . toThrow ( ) ;
87+ } ) ;
88+
89+ it ( "preserves partial CRLF and CR line endings" , async ( ) => {
90+ const highlighter = await getSharedHighlighter ( {
91+ ...highlighterOptions ,
92+ preferredHighlighter : engine ,
93+ } ) ;
94+ const highlight = createIncrementalHighlightedDocument (
95+ highlighter ,
96+ "typescript" ,
97+ "pierre-dark" ,
7998 ) ;
80- }
81- } ) ;
82- } ) ;
99+ const code = "/* multi\r\nline */\r\nconst x = 1;\r\n" ;
100+ for ( let end = 0 ; end <= code . length ; end ++ ) {
101+ const text = code . slice ( 0 , end ) ;
102+ expect ( toHtml ( highlight ( text ) ) ) . toBe (
103+ highlighter . codeToHtml ( text , { lang : "typescript" , theme : "pierre-dark" } ) ,
104+ ) ;
105+ }
106+ } ) ;
107+ } ,
108+ ) ;
0 commit comments