@pierre/diffs 1.1.7 → 1.1.9

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 (65) hide show
  1. package/dist/components/AdvancedVirtualizedFileDiff.d.ts.map +1 -1
  2. package/dist/components/File.d.ts.map +1 -1
  3. package/dist/components/FileDiff.js +2 -2
  4. package/dist/components/FileDiff.js.map +1 -1
  5. package/dist/components/UnresolvedFile.d.ts.map +1 -1
  6. package/dist/components/VirtualizedFileDiff.js +1 -1
  7. package/dist/components/VirtualizedFileDiff.js.map +1 -1
  8. package/dist/index.d.ts +4 -3
  9. package/dist/index.js +2 -1
  10. package/dist/managers/InteractionManager.d.ts +15 -2
  11. package/dist/managers/InteractionManager.d.ts.map +1 -1
  12. package/dist/managers/InteractionManager.js +143 -40
  13. package/dist/managers/InteractionManager.js.map +1 -1
  14. package/dist/react/index.d.ts +2 -2
  15. package/dist/renderers/DiffHunksRenderer.js +11 -10
  16. package/dist/renderers/DiffHunksRenderer.js.map +1 -1
  17. package/dist/renderers/FileRenderer.js +3 -1
  18. package/dist/renderers/FileRenderer.js.map +1 -1
  19. package/dist/ssr/FileDiffReact.js +1 -1
  20. package/dist/ssr/index.d.ts +2 -2
  21. package/dist/ssr/preloadDiffs.js +1 -1
  22. package/dist/ssr/preloadDiffs.js.map +1 -1
  23. package/dist/style.js +1 -1
  24. package/dist/style.js.map +1 -1
  25. package/dist/types.d.ts +23 -2
  26. package/dist/types.d.ts.map +1 -1
  27. package/dist/utils/areDiffRenderOptionsEqual.d.ts +7 -0
  28. package/dist/utils/areDiffRenderOptionsEqual.d.ts.map +1 -0
  29. package/dist/utils/areDiffRenderOptionsEqual.js +10 -0
  30. package/dist/utils/areDiffRenderOptionsEqual.js.map +1 -0
  31. package/dist/utils/areOptionsEqual.d.ts +2 -1
  32. package/dist/utils/areOptionsEqual.d.ts.map +1 -1
  33. package/dist/utils/areOptionsEqual.js +8 -1
  34. package/dist/utils/areOptionsEqual.js.map +1 -1
  35. package/dist/utils/createTransformerWithState.d.ts +1 -1
  36. package/dist/utils/createTransformerWithState.d.ts.map +1 -1
  37. package/dist/utils/createTransformerWithState.js +27 -2
  38. package/dist/utils/createTransformerWithState.js.map +1 -1
  39. package/dist/utils/parseDiffFromFile.js +1 -0
  40. package/dist/utils/parseDiffFromFile.js.map +1 -1
  41. package/dist/utils/renderDiffWithHighlighter.js +6 -3
  42. package/dist/utils/renderDiffWithHighlighter.js.map +1 -1
  43. package/dist/utils/renderFileWithHighlighter.d.ts +2 -1
  44. package/dist/utils/renderFileWithHighlighter.d.ts.map +1 -1
  45. package/dist/utils/renderFileWithHighlighter.js +2 -2
  46. package/dist/utils/renderFileWithHighlighter.js.map +1 -1
  47. package/dist/utils/shouldUseTokenTransformer.d.ts +9 -0
  48. package/dist/utils/shouldUseTokenTransformer.d.ts.map +1 -0
  49. package/dist/utils/shouldUseTokenTransformer.js +8 -0
  50. package/dist/utils/shouldUseTokenTransformer.js.map +1 -0
  51. package/dist/utils/wrapTokenFragments.d.ts +10 -0
  52. package/dist/utils/wrapTokenFragments.d.ts.map +1 -0
  53. package/dist/utils/wrapTokenFragments.js +82 -0
  54. package/dist/utils/wrapTokenFragments.js.map +1 -0
  55. package/dist/worker/WorkerPoolManager.d.ts +4 -0
  56. package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
  57. package/dist/worker/WorkerPoolManager.js +11 -6
  58. package/dist/worker/WorkerPoolManager.js.map +1 -1
  59. package/dist/worker/types.d.ts +2 -0
  60. package/dist/worker/types.d.ts.map +1 -1
  61. package/dist/worker/worker-portable.js +142 -8
  62. package/dist/worker/worker-portable.js.map +1 -1
  63. package/dist/worker/worker.js +124 -8
  64. package/dist/worker/worker.js.map +1 -1
  65. package/package.json +1 -1
@@ -14421,9 +14421,106 @@ function processLine(node, line, state) {
14421
14421
  return node;
14422
14422
  }
14423
14423
 
14424
+ //#endregion
14425
+ //#region src/utils/wrapTokenFragments.ts
14426
+ const NO_TOKEN = Symbol("no-token");
14427
+ const MULTIPLE_TOKENS = Symbol("multiple-tokens");
14428
+ function wrapTokenFragments(container) {
14429
+ const ownTokenChar = getTokenChar(container);
14430
+ if (ownTokenChar != null) {
14431
+ return ownTokenChar;
14432
+ }
14433
+ let containerTokenState = NO_TOKEN;
14434
+ const wrappedChildren = [];
14435
+ let currentTokenChildren = [];
14436
+ let currentTokenChar;
14437
+ const flushTokenChildren = () => {
14438
+ if (currentTokenChildren.length === 0 || currentTokenChar == null) {
14439
+ currentTokenChildren = [];
14440
+ currentTokenChar = undefined;
14441
+ return;
14442
+ }
14443
+ if (currentTokenChildren.length === 1) {
14444
+ const child = currentTokenChildren[0];
14445
+ if (child?.type === "element") {
14446
+ setTokenChar(child, currentTokenChar);
14447
+ for (const grandChild of child.children) {
14448
+ stripTokenChar(grandChild);
14449
+ }
14450
+ } else {
14451
+ stripTokenChar(child);
14452
+ }
14453
+ wrappedChildren.push(child);
14454
+ currentTokenChildren = [];
14455
+ currentTokenChar = undefined;
14456
+ return;
14457
+ }
14458
+ for (const child of currentTokenChildren) {
14459
+ stripTokenChar(child);
14460
+ }
14461
+ wrappedChildren.push(createHastElement({
14462
+ tagName: "span",
14463
+ properties: { "data-char": currentTokenChar },
14464
+ children: currentTokenChildren
14465
+ }));
14466
+ currentTokenChildren = [];
14467
+ currentTokenChar = undefined;
14468
+ };
14469
+ const mergeContainerTokenState = (childTokenState) => {
14470
+ if (childTokenState === NO_TOKEN) {
14471
+ return;
14472
+ }
14473
+ if (childTokenState === MULTIPLE_TOKENS) {
14474
+ containerTokenState = MULTIPLE_TOKENS;
14475
+ return;
14476
+ }
14477
+ if (containerTokenState === NO_TOKEN) {
14478
+ containerTokenState = childTokenState;
14479
+ return;
14480
+ }
14481
+ if (containerTokenState !== childTokenState) {
14482
+ containerTokenState = MULTIPLE_TOKENS;
14483
+ }
14484
+ };
14485
+ for (const child of container.children) {
14486
+ const childTokenState = child.type === "element" ? wrapTokenFragments(child) : NO_TOKEN;
14487
+ mergeContainerTokenState(childTokenState);
14488
+ if (typeof childTokenState !== "number") {
14489
+ flushTokenChildren();
14490
+ wrappedChildren.push(child);
14491
+ continue;
14492
+ }
14493
+ if (currentTokenChar != null && currentTokenChar !== childTokenState) {
14494
+ flushTokenChildren();
14495
+ }
14496
+ currentTokenChar ??= childTokenState;
14497
+ currentTokenChildren.push(child);
14498
+ }
14499
+ flushTokenChildren();
14500
+ container.children = wrappedChildren;
14501
+ return containerTokenState;
14502
+ }
14503
+ function getTokenChar(node) {
14504
+ const value = node.properties["data-char"];
14505
+ if (typeof value === "number") {
14506
+ return value;
14507
+ }
14508
+ return undefined;
14509
+ }
14510
+ function stripTokenChar(node) {
14511
+ if (node.type !== "element") return;
14512
+ node.properties["data-char"] = undefined;
14513
+ for (const child of node.children) {
14514
+ stripTokenChar(child);
14515
+ }
14516
+ }
14517
+ function setTokenChar(node, char) {
14518
+ node.properties["data-char"] = char;
14519
+ }
14520
+
14424
14521
  //#endregion
14425
14522
  //#region src/utils/createTransformerWithState.ts
14426
- function createTransformerWithState(useCSSClasses = false) {
14523
+ function createTransformerWithState(useTokenTransformer = false, useCSSClasses = false) {
14427
14524
  const state = { lineInfo: [] };
14428
14525
  const transformers = [{
14429
14526
  line(node) {
@@ -14437,13 +14534,42 @@ function createTransformerWithState(useCSSClasses = false) {
14437
14534
  let index = 1;
14438
14535
  for (const node of code.children) {
14439
14536
  if (node.type !== "element") continue;
14537
+ if (useTokenTransformer) {
14538
+ wrapTokenFragments(node);
14539
+ }
14440
14540
  children.push(processLine(node, index, state));
14441
14541
  index++;
14442
14542
  }
14443
14543
  code.children = children;
14444
14544
  }
14445
14545
  return pre;
14446
- }
14546
+ },
14547
+ ...useTokenTransformer ? {
14548
+ tokens(lines) {
14549
+ for (const line of lines) {
14550
+ let col = 0;
14551
+ for (const token$1 of line) {
14552
+ const tokenWithOriginalRange = token$1;
14553
+ tokenWithOriginalRange.__lineChar ??= col;
14554
+ col += token$1.content.length;
14555
+ }
14556
+ }
14557
+ },
14558
+ preprocess(_code, options) {
14559
+ options.mergeWhitespaces = "never";
14560
+ },
14561
+ span(hast, _line, _char, _lineElement, token$1) {
14562
+ if (token$1?.offset != null && token$1.content != null) {
14563
+ const tokenWithOriginalRange = token$1;
14564
+ const tokenChar = tokenWithOriginalRange.__lineChar;
14565
+ if (tokenChar != null) {
14566
+ hast.properties["data-char"] = tokenChar;
14567
+ }
14568
+ return hast;
14569
+ }
14570
+ return hast;
14571
+ }
14572
+ } : null
14447
14573
  }];
14448
14574
  if (useCSSClasses) {
14449
14575
  transformers.push(tokenStyleNormalizer, toClass);
@@ -15454,6 +15580,7 @@ function renderDiffWithHighlighter(diff, highlighter$1, options, { forcePlainTex
15454
15580
  deletionLines: [],
15455
15581
  additionLines: []
15456
15582
  };
15583
+ const { maxLineDiffLength } = options;
15457
15584
  const shouldGroupAll = !forcePlainText && !diff.isPartial;
15458
15585
  const expandedHunksForIteration = forcePlainText ? expandedHunks : undefined;
15459
15586
  const buckets = new Map();
@@ -15496,7 +15623,8 @@ function renderDiffWithHighlighter(diff, highlighter$1, options, { forcePlainTex
15496
15623
  additionLineIndex: bucket.additionContent.length,
15497
15624
  deletionDecorations: bucket.deletionDecorations,
15498
15625
  additionDecorations: bucket.additionDecorations,
15499
- lineDiffType
15626
+ lineDiffType,
15627
+ maxLineDiffLength
15500
15628
  });
15501
15629
  }
15502
15630
  if (deletionLine != null) {
@@ -15572,12 +15700,15 @@ function renderDiffWithHighlighter(diff, highlighter$1, options, { forcePlainTex
15572
15700
  baseThemeType
15573
15701
  };
15574
15702
  }
15575
- function computeLineDiffDecorations({ deletionLine, additionLine, deletionLineIndex, additionLineIndex, deletionDecorations, additionDecorations, lineDiffType }) {
15703
+ function computeLineDiffDecorations({ deletionLine, additionLine, deletionLineIndex, additionLineIndex, deletionDecorations, additionDecorations, lineDiffType, maxLineDiffLength }) {
15576
15704
  if (deletionLine == null || additionLine == null || lineDiffType === "none") {
15577
15705
  return;
15578
15706
  }
15579
15707
  deletionLine = cleanLastNewline(deletionLine);
15580
15708
  additionLine = cleanLastNewline(additionLine);
15709
+ if (deletionLine.length > maxLineDiffLength || additionLine.length > maxLineDiffLength) {
15710
+ return;
15711
+ }
15581
15712
  const lineDiff$1 = lineDiffType === "char" ? diffChars(deletionLine, additionLine) : diffWordsWithSpace(deletionLine, additionLine);
15582
15713
  const deletionSpans = [];
15583
15714
  const additionSpans = [];
@@ -15668,7 +15799,7 @@ function createBucket() {
15668
15799
  function renderTwoFiles({ deletionFile, additionFile, deletionInfo, additionInfo, highlighter: highlighter$1, deletionDecorations, additionDecorations, languageOverride, options: { theme: themeOrThemes = DEFAULT_THEMES,...options } }) {
15669
15800
  const deletionLang = languageOverride ?? getFiletypeFromFileName(deletionFile.name);
15670
15801
  const additionLang = languageOverride ?? getFiletypeFromFileName(additionFile.name);
15671
- const { state, transformers } = createTransformerWithState();
15802
+ const { state, transformers } = createTransformerWithState(options.useTokenTransformer);
15672
15803
  const hastConfig = (() => {
15673
15804
  return typeof themeOrThemes === "string" ? {
15674
15805
  ...options,
@@ -15779,7 +15910,7 @@ function splitFileContents(contents) {
15779
15910
  //#endregion
15780
15911
  //#region src/utils/renderFileWithHighlighter.ts
15781
15912
  const DEFAULT_PLAIN_TEXT_OPTIONS = { forcePlainText: false };
15782
- function renderFileWithHighlighter(file, highlighter$1, { theme = DEFAULT_THEMES, tokenizeMaxLineLength }, { forcePlainText, startingLine, totalLines, lines } = DEFAULT_PLAIN_TEXT_OPTIONS) {
15913
+ function renderFileWithHighlighter(file, highlighter$1, { theme = DEFAULT_THEMES, tokenizeMaxLineLength, useTokenTransformer }, { forcePlainText, startingLine, totalLines, lines } = DEFAULT_PLAIN_TEXT_OPTIONS) {
15783
15914
  if (forcePlainText) {
15784
15915
  startingLine ??= 0;
15785
15916
  totalLines ??= Infinity;
@@ -15788,7 +15919,7 @@ function renderFileWithHighlighter(file, highlighter$1, { theme = DEFAULT_THEMES
15788
15919
  totalLines = Infinity;
15789
15920
  }
15790
15921
  const isWindowedHighlight = startingLine > 0 || totalLines < Infinity;
15791
- const { state, transformers } = createTransformerWithState();
15922
+ const { state, transformers } = createTransformerWithState(useTokenTransformer);
15792
15923
  const lang = forcePlainText ? "text" : file.lang ?? getFiletypeFromFileName(file.name);
15793
15924
  const baseThemeType = typeof theme === "string" ? highlighter$1.getTheme(theme).type : undefined;
15794
15925
  const themeStyles = getHighlighterThemeStyles({
@@ -15849,8 +15980,10 @@ function extractWindowedFileContent(lines, startingLine, totalLines) {
15849
15980
  let highlighter;
15850
15981
  let renderOptions = {
15851
15982
  theme: DEFAULT_THEMES,
15983
+ useTokenTransformer: false,
15852
15984
  tokenizeMaxLineLength: 1e3,
15853
- lineDiffType: "word-alt"
15985
+ lineDiffType: "word-alt",
15986
+ maxLineDiffLength: 1e3
15854
15987
  };
15855
15988
  self.addEventListener("error", (event) => {
15856
15989
  console.error("[Shiki Worker] Unhandled error:", event.error);
@@ -15921,6 +16054,7 @@ async function handleRenderFile({ id, file, resolvedLanguages }) {
15921
16054
  }
15922
16055
  const fileOptions = {
15923
16056
  theme: renderOptions.theme,
16057
+ useTokenTransformer: renderOptions.useTokenTransformer,
15924
16058
  tokenizeMaxLineLength: renderOptions.tokenizeMaxLineLength
15925
16059
  };
15926
16060
  sendFileSuccess(id, renderFileWithHighlighter(file, highlighter$1, fileOptions), fileOptions);