@pierre/diffs 1.3.3 → 1.3.5

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.
@@ -69,11 +69,18 @@ var DiffHunksRenderer = class {
69
69
  * Enter edit-session mode: hunk updates preserve the current region
70
70
  * skeleton instead of recomputing hunks, and rendering happens locally
71
71
  * with the token transformer forced on (worker-pool requests/results are
72
- * suspended for this renderer). Called on every editor attach, including
73
- * a re-attach after recycle.
72
+ * suspended for this renderer). An empty additions document gets one row so
73
+ * the editor has a line for its caret. Called on every editor attach,
74
+ * including a re-attach after recycle.
74
75
  */
75
76
  beginEditSession() {
76
77
  this.editSessionActive = true;
78
+ const diff = this.diffCache;
79
+ if (diff != null && !diff.isPartial && diff.additionLines.length === 0) {
80
+ Object.assign(diff, recomputeEmptyDocumentDiff(diff, this.options.parseDiffOptions));
81
+ this.markEditSessionPass(diff);
82
+ this.clearRenderCache();
83
+ }
77
84
  }
78
85
  /** Leave edit-session mode. The exit recompute is the host's concern. */
79
86
  endEditSession() {
@@ -269,7 +276,7 @@ var DiffHunksRenderer = class {
269
276
  result.code.additionLines = realignAdditionHastLines(previousAdditionLines, diff.additionLines, result.code.additionLines, textDocument);
270
277
  if (diff.additionLines.length <= 1 && diff.additionLines.join("") === "") {
271
278
  Object.assign(diff, recomputeEmptyDocumentDiff(diff, this.options.parseDiffOptions));
272
- result.code.additionLines[0] = createPlainAdditionLineElement(0, textDocument);
279
+ result.code.additionLines[0] = createPlainAdditionLineElement(0, textDocument.getLineText(0));
273
280
  this.markEditSessionPass(diff);
274
281
  } else if (this.editSessionActive) this.applySessionDocumentChange(diff);
275
282
  else Object.assign(diff, recomputeDiffHunksForEdit(diff, this.options.parseDiffOptions));
@@ -489,12 +496,29 @@ var DiffHunksRenderer = class {
489
496
  renderDiffWithHighlighter(diff, highlighter, forcePlainText = false) {
490
497
  const { options } = this.getRenderOptions(diff);
491
498
  const { collapsedContextThreshold } = this.getOptionsWithDefaults();
492
- return {
493
- result: renderDiffWithHighlighter(diff, highlighter, options, {
494
- forcePlainText,
499
+ const result = renderDiffWithHighlighter(diff, highlighter, options, {
500
+ forcePlainText,
501
+ expandedHunks: forcePlainText ? true : void 0,
502
+ collapsedContextThreshold
503
+ });
504
+ if (this.editSessionActive && diff.additionLines.length === 1 && diff.additionLines[0] === "" && result.code.additionLines[0] == null) {
505
+ let fallbackLine;
506
+ iterateOverDiff({
507
+ diff,
508
+ diffStyle: "both",
495
509
  expandedHunks: forcePlainText ? true : void 0,
496
- collapsedContextThreshold
497
- }),
510
+ collapsedContextThreshold,
511
+ callback: ({ additionLine }) => {
512
+ if (additionLine?.lineIndex !== 0) return;
513
+ fallbackLine = additionLine;
514
+ return true;
515
+ }
516
+ });
517
+ if (fallbackLine == null) throw new Error("DiffHunksRenderer: missing empty addition line");
518
+ result.code.additionLines[0] = createPlainAdditionLineElement(0, "", fallbackLine.unifiedLineIndex, fallbackLine.splitLineIndex);
519
+ }
520
+ return {
521
+ result,
498
522
  options
499
523
  };
500
524
  }
@@ -1120,16 +1144,16 @@ function realignAdditionHastLines(previousLines, nextLines, hastLines, textDocum
1120
1144
  for (let offset = 0; offset < suffix; offset++) realigned[nextContentLength - 1 - offset] = hastLines[previousContentLength - 1 - offset];
1121
1145
  if (previousContentLength < previousLines.length && nextContentLength < nextLines.length) realigned[nextLines.length - 1] = hastLines[previousLines.length - 1];
1122
1146
  for (let index = previousLines.length; index < nextLines.length; index++) realigned[index] ??= hastLines[index];
1123
- for (let index = prefix; index < nextLines.length; index++) realigned[index] ??= createPlainAdditionLineElement(index, textDocument);
1147
+ for (let index = prefix; index < nextLines.length; index++) realigned[index] ??= createPlainAdditionLineElement(index, textDocument.getLineText(index));
1124
1148
  return realigned;
1125
1149
  }
1126
- function createPlainAdditionLineElement(lineIndex, textDocument) {
1150
+ function createPlainAdditionLineElement(lineIndex, lineText, unifiedLineIndex = lineIndex, splitLineIndex = lineIndex) {
1127
1151
  return {
1128
1152
  type: "element",
1129
1153
  tagName: "div",
1130
1154
  properties: {
1131
1155
  "data-line": lineIndex + 1,
1132
- "data-line-index": `${lineIndex},${lineIndex}`,
1156
+ "data-line-index": `${unifiedLineIndex},${splitLineIndex}`,
1133
1157
  "data-line-type": "context"
1134
1158
  },
1135
1159
  children: [{
@@ -1138,7 +1162,7 @@ function createPlainAdditionLineElement(lineIndex, textDocument) {
1138
1162
  properties: { "data-char": 0 },
1139
1163
  children: [{
1140
1164
  type: "text",
1141
- value: textDocument.getLineText(lineIndex)
1165
+ value: lineText
1142
1166
  }]
1143
1167
  }]
1144
1168
  };