Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@jupyterlab/coreutils": "^6.0.0",
"@jupyterlab/notebook": "^4.0.0",
"@jupyterlab/services": "^7.0.0",
"@jupyterlab/translation": "^4.0.0",
"@jupyterlab/ui-components": "^4.0.0",
"@lumino/widgets": "^2.0.0",
"codemirror": "^6.0.2",
Expand Down
4 changes: 3 additions & 1 deletion src/diff/codemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export async function createCodeMirrorDiffWidget(
cellFooterTracker,
originalSource,
newSource,
trans,
showActionButtons = true,
openDiff = true
} = options;
Expand All @@ -101,7 +102,8 @@ export async function createCodeMirrorDiffWidget(
cell,
cellFooterTracker,
showActionButtons,
openDiff
openDiff,
trans
});

diffWidget.addClass('jupyterlab-cell-diff');
Expand Down
6 changes: 4 additions & 2 deletions src/diff/nbdime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ export async function createNBDimeDiffWidget(
originalSource,
newSource,
diffData,
trans,
showActionButtons = true,
openDiff = true
} = options;

if (!diffData || !diffData.diff) {
throw new Error('NBDime strategy requires diff data');
throw new Error(trans.__('NBDime strategy requires diff data'));
}

const diff = createPatchStringDiffModel(
Expand All @@ -68,7 +69,8 @@ export async function createNBDimeDiffWidget(
originalSource,
newSource,
showActionButtons,
openDiff
openDiff,
trans
});

diffWidget.addClass('jupyterlab-cell-diff');
Expand Down
79 changes: 54 additions & 25 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import {
} from '@jupyterlab/application';
import { ICellModel } from '@jupyterlab/cells';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';

import { requestAPI } from './handler';
import { IDiffWidgetOptions } from './widget';
import { createNBDimeDiffWidget } from './diff/nbdime';
import { createCodeMirrorDiffWidget } from './diff/codemirror';

/**
* The translation namespace for the plugin.
*/
const TRANSLATION_NAMESPACE = 'jupyterlab-cell-diff';

/**
* Find a notebook by path using the notebook tracker
*/
Expand Down Expand Up @@ -56,43 +62,50 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-cell-diff:codemirror-plugin',
description: 'Expose a command to show cell diffs using CodeMirror',
requires: [ICellFooterTracker, INotebookTracker],
optional: [ITranslator],
autoStart: true,
activate: async (
app: JupyterFrontEnd,
cellFooterTracker: ICellFooterTracker,
notebookTracker: INotebookTracker
notebookTracker: INotebookTracker,
translator: ITranslator | null
) => {
const { commands } = app;
const trans = (translator ?? nullTranslator).load(TRANSLATION_NAMESPACE);

commands.addCommand('jupyterlab-cell-diff:show-codemirror', {
label: 'Show Cell Diff (CodeMirror)',
label: trans.__('Show Cell Diff (CodeMirror)'),
describedBy: {
args: {
type: 'object',
properties: {
cellId: {
type: 'string',
description: 'ID of the cell to show diff for'
description: trans.__('ID of the cell to show diff for')
},
originalSource: {
type: 'string',
description: 'Original source code to compare against'
description: trans.__('Original source code to compare against')
},
newSource: {
type: 'string',
description: 'New source code to compare with'
description: trans.__('New source code to compare with')
},
showActionButtons: {
type: 'boolean',
description: 'Whether to show action buttons in the diff widget'
description: trans.__(
'Whether to show action buttons in the diff widget'
)
},
notebookPath: {
type: 'string',
description: 'Path to the notebook containing the cell'
description: trans.__('Path to the notebook containing the cell')
},
openDiff: {
type: 'boolean',
description: 'Whether to open the diff widget automatically'
description: trans.__(
'Whether to open the diff widget automatically'
)
}
}
}
Expand All @@ -115,14 +128,16 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
const cell = findCell(currentNotebook, cellId);
if (!cell) {
console.error(
'Missing required arguments: cellId (or no active cell found)'
trans.__(
'Missing required arguments: cellId (or no active cell found)'
)
);
return;
}

const footer = cellFooterTracker.getFooter(cell.id);
if (!footer) {
console.error(`Footer not found for cell ${cell.id}`);
console.error(trans.__('Footer not found for cell %1', cell.id));
return;
}

Expand All @@ -133,12 +148,13 @@ const codeMirrorPlugin: JupyterFrontEndPlugin<void> = {
originalSource,
newSource,
showActionButtons,
openDiff
openDiff,
trans
};

await createCodeMirrorDiffWidget(options);
} catch (error) {
console.error('Failed to create diff widget:', error);
console.error(trans.__('Failed to create diff widget: %1'), error);
}
}
});
Expand All @@ -152,43 +168,50 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-cell-diff:nbdime-plugin',
description: 'Expose a command to show cell diffs using NBDime',
requires: [ICellFooterTracker, INotebookTracker],
optional: [ITranslator],
autoStart: true,
activate: async (
app: JupyterFrontEnd,
cellFooterTracker: ICellFooterTracker,
notebookTracker: INotebookTracker
notebookTracker: INotebookTracker,
translator: ITranslator | null
) => {
const { commands } = app;
const trans = (translator ?? nullTranslator).load(TRANSLATION_NAMESPACE);

commands.addCommand('jupyterlab-cell-diff:show-nbdime', {
label: 'Show Cell Diff (NBDime)',
label: trans.__('Show Cell Diff (NBDime)'),
describedBy: {
args: {
type: 'object',
properties: {
cellId: {
type: 'string',
description: 'ID of the cell to show diff for'
description: trans.__('ID of the cell to show diff for')
},
originalSource: {
type: 'string',
description: 'Original source code to compare against'
description: trans.__('Original source code to compare against')
},
newSource: {
type: 'string',
description: 'New source code to compare with'
description: trans.__('New source code to compare with')
},
showActionButtons: {
type: 'boolean',
description: 'Whether to show action buttons in the diff widget'
description: trans.__(
'Whether to show action buttons in the diff widget'
)
},
notebookPath: {
type: 'string',
description: 'Path to the notebook containing the cell'
description: trans.__('Path to the notebook containing the cell')
},
openDiff: {
type: 'boolean',
description: 'Whether to open the diff widget automatically'
description: trans.__(
'Whether to open the diff widget automatically'
)
}
}
}
Expand All @@ -211,7 +234,9 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
const cell = findCell(currentNotebook, cellId);
if (!cell) {
console.error(
'Missing required arguments: cellId (or no active cell found)'
trans.__(
'Missing required arguments: cellId (or no active cell found)'
)
);
return;
}
Expand All @@ -228,12 +253,15 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
});
diffData = (response as any).diff;
} catch (error) {
console.warn('Failed to fetch diff data from server:', error);
console.warn(
trans.__('Failed to fetch diff data from server: %1'),
error
);
}

const footer = cellFooterTracker.getFooter(cell.id);
if (!footer) {
console.error(`Footer not found for cell ${cell.id}`);
console.error(trans.__('Footer not found for cell %1', cell.id));
return;
}

Expand All @@ -246,12 +274,13 @@ const nbdimePlugin: JupyterFrontEndPlugin<void> = {
diffData: diffData ? { diff: diffData } : undefined,
cellId: cell.id,
showActionButtons,
openDiff
openDiff,
trans
};

await createNBDimeDiffWidget(options);
} catch (error) {
console.error('Failed to create diff widget:', error);
console.error(trans.__('Failed to create diff widget: %1'), error);
}
}
});
Expand Down
14 changes: 11 additions & 3 deletions src/widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ICellModel } from '@jupyterlab/cells';
import { TranslationBundle } from '@jupyterlab/translation';
import { checkIcon, ToolbarButton, undoIcon } from '@jupyterlab/ui-components';
import { Widget } from '@lumino/widgets';
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
Expand Down Expand Up @@ -27,6 +28,11 @@ export interface IDiffWidgetOptions {
*/
newSource: string;

/**
* The translation bundle
*/
trans: TranslationBundle;

/**
* Additional diff data (for nbdime strategy)
*/
Expand Down Expand Up @@ -61,6 +67,7 @@ export abstract class BaseDiffWidget extends Widget {
this._cellFooterTracker = options.cellFooterTracker;
this._originalSource = options.originalSource;
this._newSource = options.newSource || options.cell.sharedModel.getSource();
this._trans = options.trans;
this._showActionButtons = options.showActionButtons ?? true;
this._openDiff = options.openDiff ?? true;
}
Expand Down Expand Up @@ -128,7 +135,7 @@ export abstract class BaseDiffWidget extends Widget {
*/
private _createButtons(footer: any): void {
this._toggleButton = new ToolbarButton({
label: 'Compare changes',
label: this._trans.__('Compare changes'),
enabled: true,
className: 'jp-DiffView-toggle',
onClick: () => {
Expand All @@ -141,15 +148,15 @@ export abstract class BaseDiffWidget extends Widget {
if (this._showActionButtons) {
const rejectButton = new ToolbarButton({
icon: undoIcon,
tooltip: 'Reject Changes',
tooltip: this._trans.__('Reject Changes'),
enabled: true,
className: 'jp-DiffView-reject',
onClick: () => this.onRejectClick()
});

const acceptButton = new ToolbarButton({
icon: checkIcon,
tooltip: 'Accept Changes',
tooltip: this._trans.__('Accept Changes'),
enabled: true,
className: 'jp-DiffView-accept',
onClick: () => this.onAcceptClick()
Expand Down Expand Up @@ -191,4 +198,5 @@ export abstract class BaseDiffWidget extends Widget {
protected _showActionButtons: boolean;
protected _openDiff: boolean;
protected _toggleButton: ToolbarButton | null = null;
private _trans: TranslationBundle;
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3714,6 +3714,7 @@ __metadata:
"@jupyterlab/coreutils": ^6.0.0
"@jupyterlab/notebook": ^4.0.0
"@jupyterlab/services": ^7.0.0
"@jupyterlab/translation": ^4.0.0
"@jupyterlab/ui-components": ^4.0.0
"@lumino/widgets": ^2.0.0
"@types/json-schema": ^7.0.11
Expand Down