|
| 1 | +import { MergeView } from '@codemirror/merge'; |
| 2 | +import { basicSetup } from 'codemirror'; |
| 3 | +import { EditorView } from '@codemirror/view'; |
| 4 | +import { jupyterTheme } from '@jupyterlab/codemirror'; |
| 5 | +import { Widget } from '@lumino/widgets'; |
| 6 | +import type { IDocumentWidget } from '@jupyterlab/docregistry'; |
| 7 | +import type { FileEditor } from '@jupyterlab/fileeditor'; |
| 8 | +import type { TranslationBundle } from '@jupyterlab/translation'; |
| 9 | +import type { CodeMirrorEditor } from '@jupyterlab/codemirror'; |
| 10 | + |
| 11 | +export interface ISplitFileDiffOptions { |
| 12 | + /** |
| 13 | + * The file editor widget (document widget) that contains the CodeMirror editor. |
| 14 | + * This is optional but helpful for toolbar placement or context. |
| 15 | + */ |
| 16 | + fileEditorWidget?: IDocumentWidget<FileEditor>; |
| 17 | + |
| 18 | + /** |
| 19 | + * The CodeMirrorEditor instance for the file being compared |
| 20 | + */ |
| 21 | + editor: CodeMirrorEditor; |
| 22 | + |
| 23 | + /** |
| 24 | + * Original source text |
| 25 | + */ |
| 26 | + originalSource: string; |
| 27 | + |
| 28 | + /** |
| 29 | + * New / modified source text |
| 30 | + */ |
| 31 | + newSource: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * Translation bundle (optional, kept for parity with other APIs) |
| 35 | + */ |
| 36 | + trans?: TranslationBundle; |
| 37 | + |
| 38 | + /** |
| 39 | + * Whether to open the diff immediately (defaults to true). |
| 40 | + */ |
| 41 | + openDiff?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * A Lumino widget that contains a CodeMirror MergeView (side-by-side) |
| 46 | + * for file diffs. This is view-only (both editors are non-editable). |
| 47 | + */ |
| 48 | +export class CodeMirrorSplitFileWidget extends Widget { |
| 49 | + private _originalCode: string; |
| 50 | + private _modifiedCode: string; |
| 51 | + private _mergeView: MergeView | null = null; |
| 52 | + private _openDiff: boolean; |
| 53 | + private _scrollWrapper: HTMLElement; |
| 54 | + |
| 55 | + constructor(options: ISplitFileDiffOptions) { |
| 56 | + super(); |
| 57 | + this.addClass('jp-SplitFileDiffView'); |
| 58 | + this._originalCode = options.originalSource; |
| 59 | + this._modifiedCode = options.newSource; |
| 60 | + this._openDiff = options.openDiff ?? true; |
| 61 | + |
| 62 | + this.node.style.display = 'flex'; |
| 63 | + this.node.style.flexDirection = 'column'; |
| 64 | + this.node.style.height = '100%'; |
| 65 | + this.node.style.width = '100%'; |
| 66 | + |
| 67 | + this._scrollWrapper = document.createElement('div'); |
| 68 | + this._scrollWrapper.classList.add('jp-SplitDiff-scroll'); |
| 69 | + this.node.appendChild(this._scrollWrapper); |
| 70 | + } |
| 71 | + |
| 72 | + protected onAfterAttach(): void { |
| 73 | + this._createSplitView(); |
| 74 | + } |
| 75 | + |
| 76 | + protected onBeforeDetach(): void { |
| 77 | + this._destroySplitView(); |
| 78 | + } |
| 79 | + |
| 80 | + private _createSplitView(): void { |
| 81 | + if (this._mergeView) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // MergeView options: left (a) = original, right (b) = modified |
| 86 | + //TODO: Currently Both panes are non-editable, but have to make right pane editable. |
| 87 | + this._mergeView = new MergeView({ |
| 88 | + a: { |
| 89 | + doc: this._originalCode, |
| 90 | + extensions: [ |
| 91 | + basicSetup, |
| 92 | + EditorView.editable.of(false), |
| 93 | + EditorView.lineWrapping, |
| 94 | + jupyterTheme |
| 95 | + ] |
| 96 | + }, |
| 97 | + b: { |
| 98 | + doc: this._modifiedCode, |
| 99 | + extensions: [ |
| 100 | + basicSetup, |
| 101 | + EditorView.editable.of(false), |
| 102 | + EditorView.lineWrapping, |
| 103 | + jupyterTheme |
| 104 | + ] |
| 105 | + }, |
| 106 | + parent: this._scrollWrapper |
| 107 | + }); |
| 108 | + |
| 109 | + if (!this._openDiff) { |
| 110 | + this.hide(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private _destroySplitView(): void { |
| 115 | + if (this._mergeView) { |
| 116 | + try { |
| 117 | + this._mergeView.destroy(); |
| 118 | + } catch (err) { |
| 119 | + // best-effort cleanup |
| 120 | + console.warn('Error destroying split-file merge view', err); |
| 121 | + } |
| 122 | + this._mergeView = null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + dispose(): void { |
| 127 | + this._destroySplitView(); |
| 128 | + super.dispose(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Factory to create a CodeMirrorSplitFileWidget. |
| 134 | + * Keep the signature async to match other factories and future expansion. |
| 135 | + */ |
| 136 | +export async function createCodeMirrorSplitFileWidget( |
| 137 | + options: ISplitFileDiffOptions |
| 138 | +): Promise<Widget> { |
| 139 | + const widget = new CodeMirrorSplitFileWidget(options); |
| 140 | + return widget; |
| 141 | +} |
0 commit comments