Skip to content

Commit 249205b

Browse files
authored
Adding inlineDiffs for unified-cell diffs (#32)
* Adding inlineDiffs for unified-cell diffs * making `inlineDiffs` working with `unified-file-diff` and updating documentation. * changes done
1 parent 5cde286 commit 249205b

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ window.jupyterapp.commands.execute('jupyterlab-diff:split-cell-diff', {
117117
| `originalSource` | `string` | Yes | Original source code to compare against |
118118
| `newSource` | `string` | Yes | New source code to compare with |
119119
| `showActionButtons` | `boolean` | No | Whether to show action buttons for chunk acceptance (default: `true`) |
120+
| `allowInlineDiffs` | `boolean` | No | Whether to show inline diffs in the diff widget (default: `false`) |
120121
| `notebookPath` | `string` | No | Path to the notebook containing the cell. If not provided, uses the current notebook |
121122

122123
#### `jupyterlab-diff:unified-file-diff` (File Diff)
@@ -127,6 +128,7 @@ window.jupyterapp.commands.execute('jupyterlab-diff:split-cell-diff', {
127128
| `originalSource` | `string` | Yes | Original source code to compare against |
128129
| `newSource` | `string` | Yes | New source code to compare with |
129130
| `showActionButtons` | `boolean` | No | Whether to show action buttons for chunk acceptance (default: `true`) |
131+
| `allowInlineDiffs` | `boolean` | No | Whether to show inline diffs in the diff widget (default: `false`) |
130132

131133
## Architecture
132134

src/diff/base-unified-diff.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface IBaseUnifiedDiffOptions {
3434
* Whether to show accept/reject buttons
3535
*/
3636
showActionButtons?: boolean;
37+
38+
/**
39+
* Whether to allow inline diffs
40+
*/
41+
allowInlineDiffs?: boolean;
3742
}
3843

3944
/**
@@ -49,6 +54,7 @@ export abstract class BaseUnifiedDiffManager {
4954
this._newSource = options.newSource;
5055
this.trans = options.trans;
5156
this.showActionButtons = options.showActionButtons ?? true;
57+
this.allowInlineDiffs = options.allowInlineDiffs ?? false;
5258
this._isInitialized = false;
5359
this._isDisposed = false;
5460
this._diffCompartment = new Compartment();
@@ -166,7 +172,8 @@ export abstract class BaseUnifiedDiffManager {
166172
newSource: this._newSource,
167173
isInitialized: this._isInitialized,
168174
sharedModel: this.getSharedModel(),
169-
onChunkChange: () => this.deactivate()
175+
onChunkChange: () => this.deactivate(),
176+
allowInlineDiffs: this.allowInlineDiffs
170177
});
171178

172179
this._isInitialized = true;
@@ -182,6 +189,7 @@ export abstract class BaseUnifiedDiffManager {
182189
protected editor: CodeMirrorEditor;
183190
protected trans: TranslationBundle;
184191
protected showActionButtons: boolean;
192+
protected allowInlineDiffs: boolean;
185193
protected acceptAllButton: ToolbarButton | null = null;
186194
protected rejectAllButton: ToolbarButton | null = null;
187195
private _originalSource: string;

src/diff/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export function createMergeExtension(
4040
): Extension {
4141
return unifiedMergeView({
4242
original: originalSource,
43-
...options,
44-
// TODO: make configurable
45-
// allowInlineDiffs: true,
43+
allowInlineDiffs: options?.allowInlineDiffs ?? false,
4644
mergeControls: (
4745
type: 'accept' | 'reject',
4846
action: (e: MouseEvent) => void
@@ -98,6 +96,11 @@ export interface IApplyDiffOptions {
9896
* Optional callback when chunks are resolved
9997
*/
10098
onChunkChange?: () => void;
99+
100+
/**
101+
* Whether to allow inline diffs
102+
*/
103+
allowInlineDiffs?: boolean;
101104
}
102105

103106
/**
@@ -111,10 +114,13 @@ export function applyDiff(options: IApplyDiffOptions): void {
111114
newSource,
112115
isInitialized,
113116
sharedModel,
114-
onChunkChange
117+
onChunkChange,
118+
allowInlineDiffs = false
115119
} = options;
116120

117-
const mergeExtension = createMergeExtension(originalSource);
121+
const mergeExtension = createMergeExtension(originalSource, {
122+
allowInlineDiffs
123+
});
118124

119125
// Create an update listener to track chunk resolution
120126
const updateListener = EditorView.updateListener.of(update => {

src/plugin.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ const unifiedCellDiffPlugin: JupyterFrontEndPlugin<void> = {
218218
'Whether to show action buttons for chunk acceptance'
219219
)
220220
},
221+
allowInlineDiffs: {
222+
type: 'boolean',
223+
description: trans.__(
224+
'Enable inline diffs (true) or disable (false)'
225+
)
226+
},
221227
notebookPath: {
222228
type: 'string',
223229
description: trans.__('Path to the notebook containing the cell')
@@ -232,6 +238,7 @@ const unifiedCellDiffPlugin: JupyterFrontEndPlugin<void> = {
232238
originalSource,
233239
newSource,
234240
showActionButtons = true,
241+
allowInlineDiffs = false,
235242
notebookPath
236243
} = args;
237244

@@ -280,6 +287,7 @@ const unifiedCellDiffPlugin: JupyterFrontEndPlugin<void> = {
280287
originalSource,
281288
newSource,
282289
showActionButtons,
290+
allowInlineDiffs,
283291
trans
284292
});
285293
cellDiffManagers.set(cell.id, manager);
@@ -333,6 +341,12 @@ const unifiedFileDiffPlugin: JupyterFrontEndPlugin<void> = {
333341
description: trans.__(
334342
'Whether to show action buttons for chunk acceptance. Defaults to true.'
335343
)
344+
},
345+
allowInlineDiffs: {
346+
type: 'boolean',
347+
description: trans.__(
348+
'Enable inline diffs (true) or disable (false)'
349+
)
336350
}
337351
},
338352
required: ['originalSource', 'newSource']
@@ -343,7 +357,8 @@ const unifiedFileDiffPlugin: JupyterFrontEndPlugin<void> = {
343357
filePath,
344358
originalSource,
345359
newSource,
346-
showActionButtons = true
360+
showActionButtons = true,
361+
allowInlineDiffs = false
347362
} = args;
348363

349364
if (!originalSource || !newSource) {
@@ -399,6 +414,7 @@ const unifiedFileDiffPlugin: JupyterFrontEndPlugin<void> = {
399414
originalSource,
400415
newSource,
401416
showActionButtons,
417+
allowInlineDiffs,
402418
trans
403419
});
404420
fileDiffManagers.set(managerKey, manager);

0 commit comments

Comments
 (0)