svelte-highlight 7.16.2 → 7.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (364) hide show
  1. package/HighlightStream.svelte +218 -3
  2. package/HighlightStream.svelte.d.ts +25 -0
  3. package/HighlightVirtual.svelte +13 -20
  4. package/LICENSE.highlight.txt +1 -1
  5. package/README.md +19 -10
  6. package/languages/1c.js +1 -1
  7. package/languages/abnf.js +1 -1
  8. package/languages/accesslog.js +1 -1
  9. package/languages/actionscript.js +1 -1
  10. package/languages/ada.js +7 -6
  11. package/languages/all.js +8 -0
  12. package/languages/angelscript.js +1 -1
  13. package/languages/apache.js +1 -1
  14. package/languages/applescript.js +1 -1
  15. package/languages/arcade.js +1 -1
  16. package/languages/arduino.js +91 -67
  17. package/languages/armasm.js +1 -1
  18. package/languages/asciidoc.js +1 -1
  19. package/languages/aspectj.js +1 -1
  20. package/languages/autohotkey.js +1 -1
  21. package/languages/autoit.js +1 -1
  22. package/languages/avrasm.js +1 -1
  23. package/languages/awk.js +1 -1
  24. package/languages/axapta.js +1 -1
  25. package/languages/bash.js +1 -1
  26. package/languages/basic.js +1 -1
  27. package/languages/bnf.js +1 -1
  28. package/languages/brainfuck.js +1 -1
  29. package/languages/c.js +91 -66
  30. package/languages/cal.js +1 -1
  31. package/languages/capnproto.js +1 -1
  32. package/languages/ceylon.js +1 -1
  33. package/languages/clean.js +1 -1
  34. package/languages/clojure-repl.js +1 -1
  35. package/languages/clojure.js +1 -1
  36. package/languages/cmake.js +4 -2
  37. package/languages/coffeescript.js +1 -1
  38. package/languages/coq.js +1 -1
  39. package/languages/cos.js +1 -1
  40. package/languages/cpp.js +90 -65
  41. package/languages/cql.d.ts +2 -0
  42. package/languages/cql.js +203 -0
  43. package/languages/crmsh.js +1 -1
  44. package/languages/crystal.js +27 -34
  45. package/languages/csharp.js +4 -4
  46. package/languages/csp.js +8 -1
  47. package/languages/css.js +20 -13
  48. package/languages/d.js +1 -1
  49. package/languages/dart.js +18 -3
  50. package/languages/delphi.js +1 -1
  51. package/languages/diff.js +2 -2
  52. package/languages/django.js +1 -1
  53. package/languages/dns.js +71 -43
  54. package/languages/dockerfile.js +1 -1
  55. package/languages/dos.js +3 -3
  56. package/languages/dsconfig.js +1 -1
  57. package/languages/dts.js +1 -1
  58. package/languages/dust.js +1 -1
  59. package/languages/ebnf.js +1 -1
  60. package/languages/elixir.js +59 -50
  61. package/languages/elm.js +1 -1
  62. package/languages/erb.js +1 -1
  63. package/languages/erlang-repl.js +1 -1
  64. package/languages/erlang.js +1 -1
  65. package/languages/excel.js +1 -1
  66. package/languages/fix.js +1 -1
  67. package/languages/flix.js +1 -1
  68. package/languages/flux.d.ts +2 -0
  69. package/languages/flux.js +272 -0
  70. package/languages/fortran.js +1 -1
  71. package/languages/freedesktop.d.ts +2 -0
  72. package/languages/freedesktop.js +45 -0
  73. package/languages/fsharp.js +1 -1
  74. package/languages/gams.js +1 -1
  75. package/languages/gauss.js +1 -1
  76. package/languages/gcode.js +1 -1
  77. package/languages/gherkin.js +36 -38
  78. package/languages/glsl.js +1 -1
  79. package/languages/gml.js +1 -1
  80. package/languages/go.js +10 -3
  81. package/languages/golo.js +1 -1
  82. package/languages/gradle.js +1 -1
  83. package/languages/graphql.js +1 -1
  84. package/languages/groovy.js +60 -5
  85. package/languages/haml.js +1 -1
  86. package/languages/handlebars.js +1 -1
  87. package/languages/haskell.js +2 -1
  88. package/languages/haxe.js +1 -1
  89. package/languages/hsp.js +1 -1
  90. package/languages/http.js +1 -1
  91. package/languages/hy.js +1 -1
  92. package/languages/index.d.ts +8 -0
  93. package/languages/index.js +4 -0
  94. package/languages/inform7.js +1 -1
  95. package/languages/ini.js +1 -1
  96. package/languages/irpf90.js +1 -1
  97. package/languages/isbl.js +1 -1
  98. package/languages/java.js +28 -25
  99. package/languages/javascript.js +9 -3
  100. package/languages/jboss-cli.js +1 -1
  101. package/languages/json.js +12 -9
  102. package/languages/julia-repl.js +1 -1
  103. package/languages/julia.js +1 -1
  104. package/languages/kotlin.js +3 -3
  105. package/languages/lasso.js +1 -1
  106. package/languages/latex.js +1 -1
  107. package/languages/ldif.js +1 -1
  108. package/languages/leaf.js +7 -2
  109. package/languages/less.js +60 -45
  110. package/languages/lisp.js +7 -2
  111. package/languages/livecodeserver.js +1 -1
  112. package/languages/livescript.js +1 -1
  113. package/languages/llvm.js +18 -3
  114. package/languages/lsl.js +1 -1
  115. package/languages/lua.js +2 -1
  116. package/languages/makefile.js +1 -1
  117. package/languages/markdown.js +8 -4
  118. package/languages/mathematica.js +1 -1
  119. package/languages/matlab.js +1 -1
  120. package/languages/maxima.js +1 -1
  121. package/languages/mel.js +1 -1
  122. package/languages/mercury.js +1 -1
  123. package/languages/mipsasm.js +1 -1
  124. package/languages/mizar.js +1 -1
  125. package/languages/mojolicious.js +1 -1
  126. package/languages/monkey.js +1 -1
  127. package/languages/moonscript.js +1 -1
  128. package/languages/n1ql.js +1 -1
  129. package/languages/nestedtext.js +1 -1
  130. package/languages/nginx.js +1 -1
  131. package/languages/nim.js +1 -1
  132. package/languages/nix.js +1 -1
  133. package/languages/node-repl.js +1 -1
  134. package/languages/nsis.js +20 -5
  135. package/languages/objectivec.js +1 -1
  136. package/languages/ocaml.js +1 -1
  137. package/languages/openscad.js +1 -1
  138. package/languages/oxygene.js +1 -1
  139. package/languages/parser3.js +1 -1
  140. package/languages/perl.js +1 -1
  141. package/languages/pf.js +1 -1
  142. package/languages/pgsql.js +1 -1
  143. package/languages/php-template.js +1 -1
  144. package/languages/php.js +150 -24
  145. package/languages/plaintext.js +1 -1
  146. package/languages/pony.js +1 -1
  147. package/languages/powershell.js +1 -1
  148. package/languages/processing.js +1 -1
  149. package/languages/profile.js +1 -1
  150. package/languages/prolog.js +1 -1
  151. package/languages/properties.js +1 -1
  152. package/languages/protobuf.js +1 -1
  153. package/languages/prql.d.ts +2 -0
  154. package/languages/prql.js +160 -0
  155. package/languages/puppet.js +1 -1
  156. package/languages/purebasic.js +1 -1
  157. package/languages/python-repl.js +1 -1
  158. package/languages/python.js +31 -11
  159. package/languages/q.js +1 -1
  160. package/languages/qml.js +1 -1
  161. package/languages/r.js +1 -1
  162. package/languages/reasonml.js +1 -1
  163. package/languages/rib.js +1 -1
  164. package/languages/roboconf.js +1 -1
  165. package/languages/routeros.js +1 -1
  166. package/languages/rsl.js +1 -1
  167. package/languages/ruby.js +2 -2
  168. package/languages/ruleslanguage.js +1 -1
  169. package/languages/rust.js +23 -11
  170. package/languages/sas.js +1 -1
  171. package/languages/scala.js +1 -1
  172. package/languages/scheme.js +1 -1
  173. package/languages/scilab.js +1 -1
  174. package/languages/scss.js +16 -9
  175. package/languages/shell.js +2 -2
  176. package/languages/smali.js +1 -1
  177. package/languages/smalltalk.js +1 -1
  178. package/languages/sml.js +1 -1
  179. package/languages/sqf.js +175 -10
  180. package/languages/sql.js +1 -1
  181. package/languages/stan.js +1 -1
  182. package/languages/stata.js +1 -1
  183. package/languages/step21.js +1 -1
  184. package/languages/stylus.js +18 -11
  185. package/languages/subunit.js +1 -1
  186. package/languages/swift.js +1 -1
  187. package/languages/taggerscript.js +1 -1
  188. package/languages/tap.js +1 -1
  189. package/languages/tcl.js +1 -1
  190. package/languages/thrift.js +1 -1
  191. package/languages/tp.js +1 -1
  192. package/languages/twig.js +1 -1
  193. package/languages/typescript.js +9 -3
  194. package/languages/vala.js +1 -1
  195. package/languages/vbnet.js +1 -1
  196. package/languages/vbscript-html.js +1 -1
  197. package/languages/vbscript.js +1 -1
  198. package/languages/verilog.js +1 -1
  199. package/languages/vhdl.js +1 -1
  200. package/languages/vim.js +1 -1
  201. package/languages/wasm.js +1 -1
  202. package/languages/wren.js +1 -1
  203. package/languages/x86asm.js +1 -1
  204. package/languages/xl.js +1 -1
  205. package/languages/xml.js +3 -3
  206. package/languages/xquery.js +1 -1
  207. package/languages/yaml.js +1 -1
  208. package/languages/zephir.js +1 -1
  209. package/package.json +1 -1
  210. package/styles/a11y-dark.css +1 -1
  211. package/styles/a11y-dark.js +1 -1
  212. package/styles/a11y-light.css +1 -1
  213. package/styles/a11y-light.js +1 -1
  214. package/styles/an-old-hope.css +1 -1
  215. package/styles/an-old-hope.js +1 -1
  216. package/styles/arduino-light.css +1 -1
  217. package/styles/arduino-light.js +1 -1
  218. package/styles/arta.css +1 -1
  219. package/styles/arta.js +1 -1
  220. package/styles/brown-paper.css +1 -1
  221. package/styles/brown-paper.js +1 -1
  222. package/styles/codepen-embed.css +1 -1
  223. package/styles/codepen-embed.js +1 -1
  224. package/styles/cybertopia-cherry.css +10 -9
  225. package/styles/cybertopia-cherry.js +10 -9
  226. package/styles/cybertopia-dimmer.css +10 -9
  227. package/styles/cybertopia-dimmer.js +10 -9
  228. package/styles/cybertopia-icecap.css +10 -9
  229. package/styles/cybertopia-icecap.js +10 -9
  230. package/styles/cybertopia-saturated.css +10 -9
  231. package/styles/cybertopia-saturated.js +10 -9
  232. package/styles/dark.css +1 -1
  233. package/styles/dark.js +1 -1
  234. package/styles/docco.css +1 -1
  235. package/styles/docco.js +1 -1
  236. package/styles/equinox.css +25 -0
  237. package/styles/equinox.d.ts +1 -0
  238. package/styles/equinox.js +27 -0
  239. package/styles/far.css +1 -1
  240. package/styles/far.js +1 -1
  241. package/styles/gml.css +1 -1
  242. package/styles/gml.js +1 -1
  243. package/styles/googlecode.css +1 -1
  244. package/styles/googlecode.js +1 -1
  245. package/styles/hybrid.css +1 -1
  246. package/styles/hybrid.js +1 -1
  247. package/styles/index.d.ts +2 -0
  248. package/styles/index.js +2 -0
  249. package/styles/intellij-light.css +1 -1
  250. package/styles/intellij-light.js +1 -1
  251. package/styles/ir-black.css +1 -1
  252. package/styles/ir-black.js +1 -1
  253. package/styles/isbl-editor-dark.css +1 -1
  254. package/styles/isbl-editor-dark.js +1 -1
  255. package/styles/isbl-editor-light.css +1 -1
  256. package/styles/isbl-editor-light.js +1 -1
  257. package/styles/magula.css +1 -1
  258. package/styles/magula.js +1 -1
  259. package/styles/mono-blue.css +1 -1
  260. package/styles/mono-blue.js +1 -1
  261. package/styles/monokai-sublime.css +1 -1
  262. package/styles/monokai-sublime.js +1 -1
  263. package/styles/monokai.css +1 -1
  264. package/styles/monokai.js +1 -1
  265. package/styles/nnfx-dark.css +1 -1
  266. package/styles/nnfx-dark.js +1 -1
  267. package/styles/nnfx-light.css +1 -1
  268. package/styles/nnfx-light.js +1 -1
  269. package/styles/obsidian.css +1 -1
  270. package/styles/obsidian.js +1 -1
  271. package/styles/paraiso-dark.css +1 -1
  272. package/styles/paraiso-dark.js +1 -1
  273. package/styles/paraiso-light.css +1 -1
  274. package/styles/paraiso-light.js +1 -1
  275. package/styles/pojoaque.css +1 -1
  276. package/styles/pojoaque.js +1 -1
  277. package/styles/purebasic.css +1 -1
  278. package/styles/purebasic.js +1 -1
  279. package/styles/qtcreator-dark.css +1 -1
  280. package/styles/qtcreator-dark.js +1 -1
  281. package/styles/qtcreator-light.css +1 -1
  282. package/styles/qtcreator-light.js +1 -1
  283. package/styles/rainbow.css +1 -1
  284. package/styles/rainbow.js +1 -1
  285. package/styles/routeros.css +1 -1
  286. package/styles/routeros.js +1 -1
  287. package/styles/school-book.css +1 -1
  288. package/styles/school-book.js +1 -1
  289. package/styles/shades-of-purple.css +1 -1
  290. package/styles/shades-of-purple.js +1 -1
  291. package/styles/srcery.css +1 -1
  292. package/styles/srcery.js +1 -1
  293. package/styles/stackoverflow-dark.css +1 -1
  294. package/styles/stackoverflow-dark.js +1 -1
  295. package/styles/stackoverflow-light.css +1 -1
  296. package/styles/stackoverflow-light.js +1 -1
  297. package/styles/tomorrow-night-blue.css +1 -1
  298. package/styles/tomorrow-night-blue.js +1 -1
  299. package/styles/tomorrow-night-bright.css +1 -1
  300. package/styles/tomorrow-night-bright.js +1 -1
  301. package/styles/vs-dark.css +21 -0
  302. package/styles/vs-dark.d.ts +1 -0
  303. package/styles/vs-dark.js +23 -0
  304. package/styles/vs.css +1 -1
  305. package/styles/vs.js +1 -1
  306. package/styles/xcode.css +1 -1
  307. package/styles/xcode.js +1 -1
  308. package/styles/xt256.css +1 -1
  309. package/styles/xt256.js +1 -1
  310. package/themes/a11y-dark.css +1 -1
  311. package/themes/a11y-dark.js +1 -1
  312. package/themes/a11y-light.css +1 -1
  313. package/themes/a11y-light.js +1 -1
  314. package/themes/an-old-hope.css +1 -1
  315. package/themes/an-old-hope.js +1 -1
  316. package/themes/arta.css +1 -1
  317. package/themes/arta.js +1 -1
  318. package/themes/brown-paper.css +1 -1
  319. package/themes/brown-paper.js +1 -1
  320. package/themes/codepen-embed.css +1 -1
  321. package/themes/codepen-embed.js +1 -1
  322. package/themes/cybertopia-cherry.css +1 -1
  323. package/themes/cybertopia-cherry.js +49 -48
  324. package/themes/cybertopia-dimmer.css +1 -1
  325. package/themes/cybertopia-dimmer.js +49 -48
  326. package/themes/cybertopia-icecap.css +1 -1
  327. package/themes/cybertopia-icecap.js +49 -48
  328. package/themes/cybertopia-saturated.css +1 -1
  329. package/themes/cybertopia-saturated.js +49 -48
  330. package/themes/dark.css +1 -1
  331. package/themes/dark.js +1 -1
  332. package/themes/docco.css +1 -1
  333. package/themes/docco.js +1 -1
  334. package/themes/equinox.css +1 -0
  335. package/themes/equinox.d.ts +1 -0
  336. package/themes/equinox.js +51 -0
  337. package/themes/far.css +1 -1
  338. package/themes/far.js +1 -1
  339. package/themes/googlecode.css +1 -1
  340. package/themes/googlecode.js +1 -1
  341. package/themes/index.d.ts +2 -0
  342. package/themes/index.js +2 -0
  343. package/themes/ir-black.css +1 -1
  344. package/themes/ir-black.js +1 -1
  345. package/themes/paraiso-dark.css +1 -1
  346. package/themes/paraiso-dark.js +1 -1
  347. package/themes/paraiso-light.css +1 -1
  348. package/themes/paraiso-light.js +1 -1
  349. package/themes/pojoaque.css +1 -1
  350. package/themes/pojoaque.js +1 -1
  351. package/themes/rainbow.css +1 -1
  352. package/themes/rainbow.js +1 -1
  353. package/themes/school-book.css +1 -1
  354. package/themes/school-book.js +1 -1
  355. package/themes/tomorrow-night-blue.css +1 -1
  356. package/themes/tomorrow-night-blue.js +1 -1
  357. package/themes/tomorrow-night-bright.css +1 -1
  358. package/themes/tomorrow-night-bright.js +1 -1
  359. package/themes/vs-dark.css +1 -0
  360. package/themes/vs-dark.d.ts +1 -0
  361. package/themes/vs-dark.js +45 -0
  362. package/themes/vs.css +1 -1
  363. package/themes/vs.js +1 -1
  364. package/virtual-window.js +67 -0
@@ -28,10 +28,39 @@
28
28
  */
29
29
  export let autoScroll = false;
30
30
 
31
+ /**
32
+ * Render only the lines within the scrolled viewport (plus `overscan`)
33
+ * instead of the whole growing buffer, so a long-running stream costs a
34
+ * bounded number of DOM nodes instead of one per line. Backed by
35
+ * `createTokenizedDocument` rather than the default sealed-chunk session,
36
+ * so output always reflects the streaming (non-canonicalized) parse, even
37
+ * once `done` - the same tradeoff `HighlightVirtual` makes. `on:highlight`
38
+ * is not dispatched in this mode, since materializing the full HTML on
39
+ * every repaint would defeat the point of windowing.
40
+ * @type {boolean}
41
+ */
42
+ export let virtualize = false;
43
+
44
+ /**
45
+ * Extra lines rendered above and below the viewport when `virtualize` is
46
+ * set.
47
+ * @type {number}
48
+ */
49
+ export let overscan = 12;
50
+
51
+ /**
52
+ * Lines between engine checkpoints when `virtualize` is set (forwarded to
53
+ * `createTokenizedDocument`).
54
+ * @type {number}
55
+ */
56
+ export let checkpointInterval = 100;
57
+
31
58
  import { createEventDispatcher, onMount, tick } from "svelte";
32
59
  import { extendLines } from "./engine.js";
33
60
  import { ensureRegistered, registry } from "./registry.js";
34
61
  import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
62
+ import { createTokenizedDocument } from "./tokenized-document.js";
63
+ import { watchLineHeight, windowRange } from "./virtual-window.js";
35
64
 
36
65
  // Lines between sealed chunks. Once a chunk fills, its line spans are
37
66
  // joined into one immutable HTML string and never touched again - keyed
@@ -60,6 +89,28 @@
60
89
  // Stick to bottom until the user scrolls away from it.
61
90
  let stickToBottom = true;
62
91
 
92
+ // `virtualize` state: a random-access tokenized document (rather than the
93
+ // sealed-chunk session above) windowed the same way `HighlightVirtual`
94
+ // windows a static document.
95
+ /** @type {HTMLElement} */
96
+ let probe;
97
+ let vLineHeight = 16;
98
+ let vScrollTop = 0;
99
+ let vClientHeight = 0;
100
+ /** @type {ReturnType<typeof requestAnimationFrame> | undefined} */
101
+ let vFrame;
102
+ /** @type {ResizeObserver | undefined} */
103
+ let resizeObserver;
104
+ /** @type {ReturnType<typeof createTokenizedDocument> | undefined} */
105
+ let vdoc;
106
+ let vdocLanguageName = "";
107
+ let vdocCheckpointInterval;
108
+ let vLineCount = 0;
109
+ let vStart = 0;
110
+ let vEnd = 0;
111
+ /** @type {string[]} */
112
+ let vVisibleLines = [];
113
+
63
114
  /** @type {ReturnType<typeof registry.createSession> | undefined} */
64
115
  let session;
65
116
  let sessionLanguageName = "";
@@ -204,6 +255,7 @@
204
255
  const gap =
205
256
  container.scrollHeight - container.scrollTop - container.clientHeight;
206
257
  stickToBottom = gap <= 4;
258
+ if (virtualize) scheduleVirtualRepaint();
207
259
  }
208
260
 
209
261
  function cancelFrame() {
@@ -222,10 +274,94 @@
222
274
  });
223
275
  }
224
276
 
277
+ function ensureVirtualDoc() {
278
+ if (
279
+ vdoc &&
280
+ vdocLanguageName === language.name &&
281
+ vdocCheckpointInterval === checkpointInterval
282
+ ) {
283
+ return;
284
+ }
285
+ vdoc = createTokenizedDocument({ language, checkpointInterval });
286
+ vdocLanguageName = language.name;
287
+ vdocCheckpointInterval = checkpointInterval;
288
+ }
289
+
290
+ function computeVirtualWindow() {
291
+ if (!vdoc) return;
292
+ const total = vdoc.lineCount();
293
+ vLineCount = total;
294
+ ({ start: vStart, end: vEnd } = windowRange({
295
+ scrollTop: vScrollTop,
296
+ clientHeight: vClientHeight,
297
+ lineHeight: vLineHeight,
298
+ overscan,
299
+ total,
300
+ }));
301
+ vVisibleLines = vdoc.lineRange(vStart, vEnd);
302
+ }
303
+
304
+ // Mirrors `scrollToBottom`/the shrink-clamp in `HighlightVirtual`, merged:
305
+ // while streaming with `autoScroll`, stick to the (growing) bottom; once
306
+ // the user scrolls away, just keep the scroll position in bounds.
307
+ async function syncVirtualFromContainer() {
308
+ await tick();
309
+ if (!container) return;
310
+ if (autoScroll && stickToBottom) {
311
+ container.scrollTop = container.scrollHeight;
312
+ } else {
313
+ const maxScrollTop = Math.max(
314
+ 0,
315
+ container.scrollHeight - container.clientHeight,
316
+ );
317
+ if (container.scrollTop > maxScrollTop) {
318
+ container.scrollTop = maxScrollTop;
319
+ }
320
+ }
321
+ vScrollTop = container.scrollTop;
322
+ vClientHeight = container.clientHeight;
323
+ }
324
+
325
+ function cancelVirtualFrame() {
326
+ if (vFrame != null) {
327
+ cancelAnimationFrame(vFrame);
328
+ vFrame = undefined;
329
+ }
330
+ }
331
+
332
+ // Coalesce scroll bursts into one window recompute per frame.
333
+ function scheduleVirtualRepaint() {
334
+ if (vFrame != null) return;
335
+ vFrame = requestAnimationFrame(() => {
336
+ vFrame = undefined;
337
+ if (container) vScrollTop = container.scrollTop;
338
+ });
339
+ }
340
+
341
+ function measureVirtualLineHeight() {
342
+ return watchLineHeight(
343
+ () => probe,
344
+ () => vLineHeight,
345
+ (height) => (vLineHeight = height),
346
+ );
347
+ }
348
+
225
349
  $: {
226
350
  void code;
227
351
  void language;
228
- if (mounted && !done) {
352
+ if (virtualize) {
353
+ // Content/window updates are handled by the virtualize-specific
354
+ // reactive blocks below; this block only tracks `done` dispatch so
355
+ // both modes share the same guard/reset semantics.
356
+ if (mounted && done) {
357
+ if (!doneDispatched) {
358
+ doneDispatched = true;
359
+ dispatch("done");
360
+ }
361
+ } else {
362
+ doneDispatched = false;
363
+ }
364
+ } else if (mounted && !done) {
229
365
  doneDispatched = false;
230
366
  scheduleRepaint();
231
367
  } else {
@@ -239,20 +375,99 @@
239
375
  }
240
376
  }
241
377
 
378
+ // Rebuilds/updates the virtualized document whenever its content or shape
379
+ // changes. Deliberately separate from the scroll-driven block below, same
380
+ // reasoning as `HighlightVirtual`.
381
+ $: if (virtualize && mounted) {
382
+ void code;
383
+ void language;
384
+ void checkpointInterval;
385
+ ensureVirtualDoc();
386
+ vdoc.setCode(code);
387
+ vLineCount = vdoc.lineCount();
388
+ computeVirtualWindow();
389
+ syncVirtualFromContainer();
390
+ }
391
+
392
+ // Scroll/resize/overscan/lineHeight-driven window recompute.
393
+ $: if (virtualize && mounted) {
394
+ void overscan;
395
+ void vLineHeight;
396
+ void vScrollTop;
397
+ void vClientHeight;
398
+ void vLineCount;
399
+ computeVirtualWindow();
400
+ }
401
+
242
402
  $: useSplitRendering = mounted && !done;
243
403
  $: showCaret = useSplitRendering && caret;
244
404
 
245
405
  onMount(() => {
246
406
  mounted = true;
247
- return cancelFrame;
407
+ if (virtualize) {
408
+ measureVirtualLineHeight();
409
+ syncVirtualFromContainer();
410
+ if (typeof ResizeObserver !== "undefined" && container) {
411
+ resizeObserver = new ResizeObserver(() => {
412
+ if (container) vClientHeight = container.clientHeight;
413
+ });
414
+ resizeObserver.observe(container);
415
+ }
416
+ }
417
+ return () => {
418
+ cancelFrame();
419
+ cancelVirtualFrame();
420
+ resizeObserver?.disconnect();
421
+ };
248
422
  });
249
423
  </script>
250
424
 
251
- <pre bind:this={container} on:scroll={onScroll} {...$$restProps}><code
425
+ {#if virtualize}
426
+ <pre
427
+ bind:this={container}
428
+ class:hljs={true}
429
+ class:shl-virtual={true}
430
+ on:scroll={onScroll}
431
+ {...$$restProps}
432
+ ><code>{#if !mounted}{code}{:else}<span class="shl-virtual-sizer" style="height: {vLineCount * vLineHeight}px;"><span class="shl-virtual-window" style="transform: translateY({vStart * vLineHeight}px);">{#each vVisibleLines as line, i (vStart + i)}<span class="highlight-stream-line" data-line={vStart + i}>{@html line}</span>{#if showCaret && vEnd === vLineCount && i === vVisibleLines.length - 1}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{"\n"}{/each}</span></span>{/if}</code><span
433
+ bind:this={probe}
434
+ class="shl-virtual-probe highlight-stream-line"
435
+ aria-hidden="true"
436
+ >&nbsp;</span></pre>
437
+ {:else}
438
+ <pre bind:this={container} on:scroll={onScroll} {...$$restProps}><code
252
439
  class:hljs={true}
253
440
  >{#if useSplitRendering}{#each sealedChunks as chunk, c (c)}{@html chunk}{/each}{#each tailLines as line, li (sealedLineCount + li)}{#if sealedLineCount + li > 0}{"\n"}{/if}<span class="highlight-stream-line" data-line={sealedLineCount + li}>{@html line}</span>{/each}{#if showCaret}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{:else}{@html highlighted}{/if}</code></pre>
441
+ {/if}
254
442
 
255
443
  <style>
444
+ .shl-virtual {
445
+ display: block;
446
+ position: relative;
447
+ overflow: auto;
448
+ white-space: pre;
449
+ margin: 0;
450
+ }
451
+
452
+ .shl-virtual-sizer {
453
+ display: block;
454
+ position: relative;
455
+ }
456
+
457
+ .shl-virtual-window {
458
+ display: block;
459
+ position: absolute;
460
+ top: 0;
461
+ left: 0;
462
+ right: 0;
463
+ }
464
+
465
+ .shl-virtual-probe {
466
+ position: absolute;
467
+ visibility: hidden;
468
+ pointer-events: none;
469
+ }
470
+
256
471
  .highlight-stream-caret {
257
472
  display: inline-block;
258
473
  width: var(--caret-width, 0.6em);
@@ -37,6 +37,31 @@ export type HighlightStreamProps = HTMLAttributes<HTMLPreElement> & {
37
37
  */
38
38
  autoScroll?: boolean;
39
39
 
40
+ /**
41
+ * Render only the lines within the scrolled viewport (plus `overscan`)
42
+ * instead of the whole growing buffer, so a long-running stream costs a
43
+ * bounded number of DOM nodes instead of one per line. Backed by
44
+ * `createTokenizedDocument` rather than the sealed-chunk session used
45
+ * otherwise, so output always reflects the streaming (non-canonicalized)
46
+ * parse, even once `done` -- the same tradeoff `HighlightVirtual` makes.
47
+ * `on:highlight` is not dispatched in this mode.
48
+ * @default false
49
+ */
50
+ virtualize?: boolean;
51
+
52
+ /**
53
+ * Extra lines rendered above and below the viewport when `virtualize` is set.
54
+ * @default 12
55
+ */
56
+ overscan?: number;
57
+
58
+ /**
59
+ * Lines between engine checkpoints when `virtualize` is set (forwarded to
60
+ * `createTokenizedDocument`).
61
+ * @default 100
62
+ */
63
+ checkpointInterval?: number;
64
+
40
65
  /**
41
66
  * Width of the blinking caret.
42
67
  * @default "0.6em"
@@ -23,6 +23,7 @@
23
23
 
24
24
  import { onMount, tick } from "svelte";
25
25
  import { createTokenizedDocument } from "./tokenized-document.js";
26
+ import { watchLineHeight, windowRange } from "./virtual-window.js";
26
27
 
27
28
  /** @type {HTMLElement} */
28
29
  let container;
@@ -78,13 +79,13 @@
78
79
  if (!doc) return;
79
80
  const total = doc.lineCount();
80
81
  lineCount = total;
81
- const first = Math.max(0, Math.floor(scrollTop / lineHeight) - overscan);
82
- const last = Math.min(
82
+ ({ start, end } = windowRange({
83
+ scrollTop,
84
+ clientHeight,
85
+ lineHeight,
86
+ overscan,
83
87
  total,
84
- Math.ceil((scrollTop + clientHeight) / lineHeight) + overscan,
85
- );
86
- start = Math.min(first, total);
87
- end = Math.max(start, last);
88
+ }));
88
89
  visibleLines = doc.lineRange(start, end);
89
90
  }
90
91
 
@@ -124,20 +125,12 @@
124
125
  scheduleRepaint();
125
126
  }
126
127
 
127
- async function measureLineHeight() {
128
- await tick();
129
- if (probe) {
130
- const height = probe.getBoundingClientRect().height;
131
- if (height > 0) lineHeight = height;
132
- }
133
- if (typeof document !== "undefined" && document.fonts?.ready) {
134
- document.fonts.ready.then(async () => {
135
- await tick();
136
- if (!probe) return;
137
- const height = probe.getBoundingClientRect().height;
138
- if (height > 0 && height !== lineHeight) lineHeight = height;
139
- });
140
- }
128
+ function measureLineHeight() {
129
+ return watchLineHeight(
130
+ () => probe,
131
+ () => lineHeight,
132
+ (height) => (lineHeight = height),
133
+ );
141
134
  }
142
135
 
143
136
  // Rebuilds/updates the document whenever its content or shape changes.
@@ -2,7 +2,7 @@ Third-party notice
2
2
  ==================
3
3
 
4
4
  svelte-highlight ships grammar definitions derived from highlight.js
5
- 11.11.1 (https://github.com/highlightjs/highlight.js), used under the
5
+ 11.12.0 (https://github.com/highlightjs/highlight.js), used under the
6
6
  BSD-3-Clause license below. Each derived grammar module carries a banner
7
7
  pointing back to this file. The svelte-highlight engine itself and all
8
8
  custom (non-hljs-derived) grammars are original code under this package's
package/README.md CHANGED
@@ -1379,7 +1379,13 @@ Set `autoScroll` to keep the container pinned to the bottom as output grows -- i
1379
1379
  <HighlightStream language={typescript} {code} {done} autoScroll style="max-height: 20em; overflow-y: auto;" />
1380
1380
  ```
1381
1381
 
1382
- Per-chunk work is O(tail), not O(stream length so far): finished output is sealed into immutable chunks the DOM never re-diffs, so a response that's ten times longer doesn't cost ten times more per repaint. DOM updates stay proportional to the changed lines -- fine for chat-sized output up to very long responses, not a substitute for `HighlightVirtual` below if you also need to *scroll* through a huge, already-complete document.
1382
+ Per-chunk work is O(tail), not O(stream length so far): finished output is sealed into immutable chunks the DOM never re-diffs, so a response that's ten times longer doesn't cost ten times more per repaint. DOM updates stay proportional to the changed lines -- fine for chat-sized output up to very long responses, but every line ever streamed still stays in the DOM; for a stream that runs long enough to *scroll* through, set `virtualize` to bound that too.
1383
+
1384
+ ```svelte
1385
+ <HighlightStream language={typescript} {code} {done} virtualize style="height: 20em;" />
1386
+ ```
1387
+
1388
+ `virtualize` renders only the lines within the scrolled viewport (plus `overscan`), the same windowing `HighlightVirtual` does for static documents -- a stream that runs to tens of thousands of lines still costs a couple dozen DOM nodes. It swaps the sealed-chunk session for `TokenizedDocument` (see [Large documents](#large-documents) below), so output always reflects the streaming (non-canonicalized) parse, even once `done` -- unlike the default mode, which upgrades to a canonical final render. `on:highlight` isn't dispatched in this mode, since materializing the full HTML on every repaint would defeat the point of windowing; `on:done`, the caret, and `autoScroll` all keep working.
1383
1389
 
1384
1390
  ## Large documents
1385
1391
 
@@ -1737,19 +1743,22 @@ Use `bind:this`, then call `undo()`, `redo()`, `focus()`, `selectAll()`, `insert
1737
1743
 
1738
1744
  #### Props
1739
1745
 
1740
- | Name | Type | Default value |
1741
- | :--------- | :--------------------------------------------- | :------------- |
1742
- | code | `string` | `""` |
1743
- | language | { name: `string`; register: `object` } | N/A (required) |
1744
- | done | `boolean` | `false` |
1745
- | caret | `boolean` | `true` |
1746
- | autoScroll | `boolean` | `false` |
1746
+ | Name | Type | Default value |
1747
+ | :----------------- | :-------------------------------------- | :------------- |
1748
+ | code | `string` | `""` |
1749
+ | language | { name: `string`; register: `object` } | N/A (required) |
1750
+ | done | `boolean` | `false` |
1751
+ | caret | `boolean` | `true` |
1752
+ | autoScroll | `boolean` | `false` |
1753
+ | virtualize | `boolean` | `false` |
1754
+ | overscan | `number` | `12` |
1755
+ | checkpointInterval | `number` | `100` |
1747
1756
 
1748
- `$$restProps` are forwarded to the top-level `pre` element.
1757
+ `$$restProps` are forwarded to the top-level `pre` element. `overscan` and `checkpointInterval` only apply when `virtualize` is set.
1749
1758
 
1750
1759
  #### Dispatched Events
1751
1760
 
1752
- - **on:highlight**: fired after each highlight pass, with `{ highlighted }`
1761
+ - **on:highlight**: fired after each highlight pass, with `{ highlighted }` -- not dispatched when `virtualize` is set
1753
1762
  - **on:done**: fired after the final full highlight once `done` is set
1754
1763
 
1755
1764
  ```svelte
package/languages/1c.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "1c" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "1c" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const _1c = /** @type {import("./index.d.ts").LanguageType<"1c">} */ ({
package/languages/abnf.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "abnf" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "abnf" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const abnf =
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "accesslog" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "accesslog" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const accesslog =
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "actionscript" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "actionscript" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const actionscript =
package/languages/ada.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "ada" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "ada" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
@@ -84,6 +84,7 @@ export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
84
84
  mod: ["keyword", 1],
85
85
  requeue: ["keyword", 1],
86
86
  xor: ["keyword", 1],
87
+ parallel: ["keyword", 1],
87
88
  true: ["literal", 1],
88
89
  false: ["literal", 1],
89
90
  },
@@ -128,7 +129,7 @@ export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
128
129
  end: "(is|$)",
129
130
  excludeBegin: true,
130
131
  excludeEnd: true,
131
- illegal: "[]\\{\\}%#'\"",
132
+ illegal: "\\{\\}%#'\"",
132
133
  keywords: { package: ["keyword", 1], body: ["keyword", 1] },
133
134
  keywordPattern: "\\w+",
134
135
  },
@@ -155,13 +156,13 @@ export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
155
156
  end: "(\\(|\\s+|$)",
156
157
  excludeBegin: true,
157
158
  excludeEnd: true,
158
- illegal: "[]\\{\\}%#'\"",
159
+ illegal: "\\{\\}%#'\"",
159
160
  },
160
161
  {
161
162
  rules: [13, 14, 15],
162
163
  begin: "\\s+:\\s+",
163
164
  end: "\\s*(:=|;|\\)|=>|$)",
164
- illegal: "[]\\{\\}%#'\"",
165
+ illegal: "\\{\\}%#'\"",
165
166
  },
166
167
  {
167
168
  relevance: 0,
@@ -216,7 +217,7 @@ export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
216
217
  endsParent: true,
217
218
  excludeBegin: true,
218
219
  excludeEnd: true,
219
- illegal: "[]\\{\\}%#'\"",
220
+ illegal: "\\{\\}%#'\"",
220
221
  keywords: { return: ["keyword", 1] },
221
222
  keywordPattern: "\\w+",
222
223
  },
@@ -226,7 +227,7 @@ export const ada = /** @type {import("./index.d.ts").LanguageType<"ada">} */ ({
226
227
  begin: "\\b(sub)?type\\s+",
227
228
  end: "\\s+",
228
229
  excludeBegin: true,
229
- illegal: "[]\\{\\}%#'\"",
230
+ illegal: "\\{\\}%#'\"",
230
231
  keywords: { type: ["keyword", 1] },
231
232
  keywordPattern: "\\w+",
232
233
  },
package/languages/all.js CHANGED
@@ -39,6 +39,7 @@ import coffeescript from "./coffeescript.js";
39
39
  import coq from "./coq.js";
40
40
  import cos from "./cos.js";
41
41
  import cpp from "./cpp.js";
42
+ import cql from "./cql.js";
42
43
  import crmsh from "./crmsh.js";
43
44
  import crystal from "./crystal.js";
44
45
  import csharp from "./csharp.js";
@@ -71,7 +72,9 @@ import excel from "./excel.js";
71
72
  import fish from "./fish.js";
72
73
  import fix from "./fix.js";
73
74
  import flix from "./flix.js";
75
+ import flux from "./flux.js";
74
76
  import fortran from "./fortran.js";
77
+ import freedesktop from "./freedesktop.js";
75
78
  import fsharp from "./fsharp.js";
76
79
  import gams from "./gams.js";
77
80
  import gauss from "./gauss.js";
@@ -174,6 +177,7 @@ import prolog from "./prolog.js";
174
177
  import promql from "./promql.js";
175
178
  import properties from "./properties.js";
176
179
  import protobuf from "./protobuf.js";
180
+ import prql from "./prql.js";
177
181
  import pug from "./pug.js";
178
182
  import puppet from "./puppet.js";
179
183
  import purebasic from "./purebasic.js";
@@ -288,6 +292,7 @@ export default [
288
292
  coq,
289
293
  cos,
290
294
  cpp,
295
+ cql,
291
296
  crmsh,
292
297
  crystal,
293
298
  csharp,
@@ -320,7 +325,9 @@ export default [
320
325
  fish,
321
326
  fix,
322
327
  flix,
328
+ flux,
323
329
  fortran,
330
+ freedesktop,
324
331
  fsharp,
325
332
  gams,
326
333
  gauss,
@@ -423,6 +430,7 @@ export default [
423
430
  promql,
424
431
  properties,
425
432
  protobuf,
433
+ prql,
426
434
  pug,
427
435
  puppet,
428
436
  purebasic,
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "angelscript" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "angelscript" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const angelscript =
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "apache" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "apache" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const apache =
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "applescript" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "applescript" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const applescript =
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "arcade" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "arcade" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const arcade =