@pierre/diffs 1.4.2 → 1.5.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 (72) hide show
  1. package/dist/components/CodeView.d.ts +2 -4
  2. package/dist/components/CodeView.d.ts.map +1 -1
  3. package/dist/components/CodeView.js +23 -12
  4. package/dist/components/CodeView.js.map +1 -1
  5. package/dist/components/File.d.ts +3 -0
  6. package/dist/components/File.d.ts.map +1 -1
  7. package/dist/components/File.js +10 -0
  8. package/dist/components/File.js.map +1 -1
  9. package/dist/components/FileDiff.d.ts +19 -1
  10. package/dist/components/FileDiff.d.ts.map +1 -1
  11. package/dist/components/FileDiff.js +51 -15
  12. package/dist/components/FileDiff.js.map +1 -1
  13. package/dist/components/VirtualizedFile.d.ts.map +1 -1
  14. package/dist/components/VirtualizedFile.js +18 -19
  15. package/dist/components/VirtualizedFile.js.map +1 -1
  16. package/dist/components/VirtualizedFileDiff.d.ts.map +1 -1
  17. package/dist/components/VirtualizedFileDiff.js +25 -22
  18. package/dist/components/VirtualizedFileDiff.js.map +1 -1
  19. package/dist/constants.d.ts +1 -3
  20. package/dist/constants.d.ts.map +1 -1
  21. package/dist/constants.js +1 -3
  22. package/dist/constants.js.map +1 -1
  23. package/dist/editor/editor.d.ts.map +1 -1
  24. package/dist/editor/editor.js +21 -10
  25. package/dist/editor/editor.js.map +1 -1
  26. package/dist/editor/textMeasure.d.ts.map +1 -1
  27. package/dist/editor/textMeasure.js +27 -3
  28. package/dist/editor/textMeasure.js.map +1 -1
  29. package/dist/highlighter/languages/attachResolvedLanguages.js +8 -1
  30. package/dist/highlighter/languages/attachResolvedLanguages.js.map +1 -1
  31. package/dist/highlighter/languages/registerCustomLanguage.d.ts +3 -1
  32. package/dist/highlighter/languages/registerCustomLanguage.d.ts.map +1 -1
  33. package/dist/highlighter/languages/registerCustomLanguage.js +3 -1
  34. package/dist/highlighter/languages/registerCustomLanguage.js.map +1 -1
  35. package/dist/index.d.ts +2 -2
  36. package/dist/index.js +2 -2
  37. package/dist/react/utils/useFileDiffInstance.d.ts.map +1 -1
  38. package/dist/react/utils/useFileDiffInstance.js +47 -11
  39. package/dist/react/utils/useFileDiffInstance.js.map +1 -1
  40. package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
  41. package/dist/renderers/DiffHunksRenderer.js +20 -13
  42. package/dist/renderers/DiffHunksRenderer.js.map +1 -1
  43. package/dist/renderers/FileRenderer.d.ts.map +1 -1
  44. package/dist/renderers/FileRenderer.js +30 -11
  45. package/dist/renderers/FileRenderer.js.map +1 -1
  46. package/dist/sprite.d.ts +2 -2
  47. package/dist/sprite.d.ts.map +1 -1
  48. package/dist/sprite.js +57 -0
  49. package/dist/sprite.js.map +1 -1
  50. package/dist/utils/editSessionHunks.d.ts +5 -4
  51. package/dist/utils/editSessionHunks.d.ts.map +1 -1
  52. package/dist/utils/editSessionHunks.js +32 -9
  53. package/dist/utils/editSessionHunks.js.map +1 -1
  54. package/dist/utils/parsePatchFiles.d.ts.map +1 -1
  55. package/dist/utils/parsePatchFiles.js +21 -13
  56. package/dist/utils/parsePatchFiles.js.map +1 -1
  57. package/dist/utils/parseQuotedDiffFileName.d.ts +8 -0
  58. package/dist/utils/parseQuotedDiffFileName.d.ts.map +1 -0
  59. package/dist/utils/parseQuotedDiffFileName.js +54 -0
  60. package/dist/utils/parseQuotedDiffFileName.js.map +1 -0
  61. package/dist/utils/realignChangeContent.d.ts +13 -10
  62. package/dist/utils/realignChangeContent.d.ts.map +1 -1
  63. package/dist/utils/realignChangeContent.js +23 -19
  64. package/dist/utils/realignChangeContent.js.map +1 -1
  65. package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
  66. package/dist/worker/WorkerPoolManager.js +40 -38
  67. package/dist/worker/WorkerPoolManager.js.map +1 -1
  68. package/dist/worker/worker-portable.js +8 -1
  69. package/dist/worker/worker-portable.js.map +1 -1
  70. package/dist/worker/worker.js +8 -1
  71. package/dist/worker/worker.js.map +1 -1
  72. package/package.json +1 -1
@@ -502,13 +502,14 @@ var FileDiff = class {
502
502
  this.expandHunk(hunkIndex, direction, expansionLineCountOverride);
503
503
  };
504
504
  expandHunk = (hunkIndex, direction, expansionLineCountOverride) => {
505
- this.hunksRenderer.expandHunk(hunkIndex, direction, expansionLineCountOverride);
506
505
  this.loadFilesIfNecessary();
506
+ this.hunksRenderer.expandHunk(hunkIndex, direction, expansionLineCountOverride);
507
507
  this.rerender();
508
508
  };
509
509
  loadFilesIfNecessary() {
510
510
  const { fileDiff, options: { loadDiffFiles } } = this;
511
- if (fileDiff == null || loadDiffFiles == null || !canHydrateDiff(fileDiff) || this.pendingFiles?.fileDiff === fileDiff) return;
511
+ if (fileDiff == null || !canHydrateDiff(fileDiff) || this.pendingFiles?.fileDiff === fileDiff) return;
512
+ if (loadDiffFiles == null) throw new Error("FileDiff: loadDiffFiles is required to load full files for a partial diff");
512
513
  const promise = this.loadFilesForDiff(fileDiff, loadDiffFiles);
513
514
  const pendingFiles = this.pendingFiles = {
514
515
  fileDiff,
@@ -519,10 +520,41 @@ var FileDiff = class {
519
520
  };
520
521
  pendingFiles.promise = promise.finally(clearPendingFiles);
521
522
  }
523
+ /**
524
+ * In order to start an edit session, you must be using a `fileDiff` that
525
+ * includes the full contents for both files. In other words, `isPartial`
526
+ * must be false You can use `prepareForEditing` to hydrate if
527
+ * `loadDiffFiles` was passed in to `options`
528
+ */
529
+ __canAttachEditor() {
530
+ return this.fileDiff != null && !this.fileDiff.isPartial;
531
+ }
532
+ /**
533
+ * Load a partial diff before starting a new edit session. Call this before
534
+ * Editor.edit when the diff may still be partial; it resolves only when this
535
+ * instance has a complete diff to edit.
536
+ */
537
+ async prepareForEditing() {
538
+ if (this.__canAttachEditor()) return;
539
+ const pending = this.__prepareForEditing();
540
+ if (pending == null) {
541
+ if (this.fileDiff?.isPartial === true && !canHydrateDiff(this.fileDiff)) throw new Error("FileDiff.prepareForEditing: this partial diff cannot be hydrated; provide a complete diff");
542
+ throw new Error("FileDiff.prepareForEditing: a partial diff requires loadDiffFiles");
543
+ }
544
+ await pending;
545
+ if (!this.__canAttachEditor()) throw new Error("FileDiff.prepareForEditing: the diff did not finish loading");
546
+ }
547
+ /** Hydrate a diff if necessary */
548
+ __prepareForEditing() {
549
+ if (this.__canAttachEditor()) return;
550
+ if (this.fileDiff?.isPartial === true && !canHydrateDiff(this.fileDiff)) throw new Error("FileDiff: this partial diff cannot be hydrated; provide a complete diff to edit");
551
+ this.loadFilesIfNecessary();
552
+ return this.pendingFiles?.promise;
553
+ }
522
554
  async loadFilesForDiff(fileDiff, loadDiffFiles) {
523
555
  try {
524
556
  const files = await loadDiffFiles(fileDiff);
525
- if (!this.enabled || this.fileDiff !== fileDiff) return;
557
+ if (this.fileDiff !== fileDiff) return;
526
558
  await this.handleFilesLoaded(fileDiff, files);
527
559
  } catch (error) {
528
560
  if (this.options.disableErrorHandling === true) throw error;
@@ -533,7 +565,7 @@ var FileDiff = class {
533
565
  if (this.fileDiff !== expectedDiff || !expectedDiff.isPartial) return;
534
566
  hydratePartialDiff("merge", expectedDiff, files);
535
567
  this.setHydratedState(files);
536
- if (this.startHydratedEditSession(expectedDiff)) {
568
+ if (this.installHydratedSessionDiff(expectedDiff)) {
537
569
  this.rerender();
538
570
  return;
539
571
  }
@@ -541,17 +573,14 @@ var FileDiff = class {
541
573
  if (!this.enabled || this.fileDiff !== expectedDiff) return;
542
574
  this.rerender();
543
575
  }
544
- startHydratedEditSession(expectedDiff) {
545
- if (expectedDiff.isPartial) throw new Error("FileDiff.startHydratedEditSession: diffs cannot be partial for editing");
576
+ installHydratedSessionDiff(expectedDiff) {
577
+ if (expectedDiff.isPartial) throw new Error("FileDiff.installHydratedSessionDiff: diffs cannot be partial for editing");
546
578
  if (this.fileDiff !== expectedDiff) return false;
547
- const { editor } = this;
548
579
  if (this.editSession?.outgoingDiff != null) {
549
580
  this.installEditSession(expectedDiff);
550
581
  return true;
551
582
  }
552
- if (editor == null || this.editSession != null) return false;
553
- this.installEditSession(expectedDiff, editor.__getDocumentContents(getAdditionFile(expectedDiff)), editor.__getDocumentSessionState());
554
- return true;
583
+ return false;
555
584
  }
556
585
  updateExternalDiff(incomingExternalDiff, lineAnnotations) {
557
586
  if (areDiffTargetsEqual(this.fileDiff, incomingExternalDiff)) return false;
@@ -561,9 +590,6 @@ var FileDiff = class {
561
590
  if (incomingExternalDiff.isPartial) this.loadFilesIfNecessary();
562
591
  else this.installEditSession(incomingExternalDiff);
563
592
  if (this.editSession != null) this.editSession.outgoingDiff = outgoingDiff;
564
- } else if (this.editor != null) {
565
- if (incomingExternalDiff.isPartial) this.loadFilesIfNecessary();
566
- else this.installEditSession(incomingExternalDiff, this.editor.__getDocumentContents(getAdditionFile(incomingExternalDiff)), this.editor.__getDocumentSessionState());
567
593
  }
568
594
  if (this.editSession?.annotations != null && lineAnnotations != null) {
569
595
  this.lineAnnotations = lineAnnotations;
@@ -747,9 +773,9 @@ var FileDiff = class {
747
773
  const sync = (highlighter) => {
748
774
  if (!this.enabled || this.editor !== editor || this.fileContainer !== fileContainer || this.getLatestDiff() !== fileDiff) return;
749
775
  const replacement = this.editSession?.outgoingDiff;
750
- const externalDiff = this.fileDiff;
776
+ const { fileDiff: externalDiff } = this;
751
777
  const externalDocument = replacement != null && externalDiff != null && replacement !== fileDiff;
752
- const resetHistory = externalDocument ? shouldResetUndoState(replacement, externalDiff) : false;
778
+ const resetHistory = externalDocument && replacement != null && externalDiff != null ? shouldResetUndoState(replacement, externalDiff) : false;
753
779
  editor.__syncRenderView({
754
780
  highlighter,
755
781
  fileContainer,
@@ -846,6 +872,7 @@ var FileDiff = class {
846
872
  __attachEditor(editor) {
847
873
  if (this.type !== "file-diff") throw new Error(`FileDiff.__attachEditor: cannot attach an editor to a "${this.type}" diff`);
848
874
  if (this.editor != null) throw new Error("FileDiff.__attachEditor: an editor is already attached");
875
+ if (!this.__canAttachEditor()) throw new Error("FileDiff.__attachEditor: a complete diff is required before editing");
849
876
  const detach = () => {
850
877
  this.editor = void 0;
851
878
  this.finishEditSession();
@@ -989,6 +1016,11 @@ var FileDiff = class {
989
1016
  this.rerender();
990
1017
  this.interactionManager.setSelectionDirty();
991
1018
  }
1019
+ /** Update the private diff without editor DOM, then rehighlight on return. */
1020
+ applySuspendedDocumentChange(textDocument, newLineAnnotations) {
1021
+ this.applyDocumentChange(textDocument, newLineAnnotations);
1022
+ this.hunksRenderer.clearRenderCache();
1023
+ }
992
1024
  updateRenderCache(dirtyLines, themeType, options = {}) {
993
1025
  const editSessionDiff = this.editSession?.diff;
994
1026
  if (editSessionDiff == null) throw new Error("FileDiff.updateRenderCache: requires an active edit session");
@@ -1129,6 +1161,10 @@ var FileDiff = class {
1129
1161
  this.placeHolder.dataset.placeholder = "";
1130
1162
  shadowRoot.appendChild(this.placeHolder);
1131
1163
  }
1164
+ return this.setPlaceholderHeight(height);
1165
+ }
1166
+ setPlaceholderHeight(height) {
1167
+ if (this.placeHolder == null) return false;
1132
1168
  this.placeHolder.style.setProperty("height", `${height}px`);
1133
1169
  return true;
1134
1170
  }