Skip to content

Commit b541d14

Browse files
committed
Follow better command usage
1 parent bd8b5ad commit b541d14

File tree

12 files changed

+173
-222
lines changed

12 files changed

+173
-222
lines changed

src/commandsAndMenu.tsx

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Dialog,
44
InputDialog,
55
MainAreaWidget,
6+
ReactWidget,
67
showDialog,
78
showErrorMessage
89
} from '@jupyterlab/apputils';
@@ -14,8 +15,14 @@ import { ITerminal } from '@jupyterlab/terminal';
1415
import { CommandRegistry } from '@lumino/commands';
1516
import { Menu } from '@lumino/widgets';
1617
import * as React from 'react';
17-
import { openDiffView } from './components/diff/DiffWidget';
18+
import {
19+
Diff,
20+
isDiffSupported,
21+
RenderMimeProvider
22+
} from './components/diff/Diff';
23+
import { getRefValue, IDiffContext } from './components/diff/model';
1824
import { GitExtension } from './model';
25+
import { diffIcon } from './style/icons';
1926
import { Git } from './tokens';
2027
import { GitCredentialsForm } from './widgets/CredentialsBox';
2128
import { doGitClone } from './widgets/gitClone';
@@ -48,8 +55,7 @@ export namespace CommandIDs {
4855
export const gitPush = 'git:push';
4956
export const gitPull = 'git:pull';
5057
// Context menu commands
51-
export const gitFileDiffIndex = 'git:context-diffIndex';
52-
export const gitFileDiffWorking = 'git:context-diffWorking';
58+
export const gitFileDiff = 'git:context-diff';
5359
export const gitFileDiscard = 'git:context-discard';
5460
export const gitFileOpen = 'git:context-open';
5561
export const gitFileUnstage = 'git:context-unstage';
@@ -278,39 +284,66 @@ export function addCommands(
278284
}
279285
});
280286

281-
commands.addCommand(CommandIDs.gitFileDiffWorking, {
287+
commands.addCommand(CommandIDs.gitFileDiff, {
282288
label: 'Diff',
283289
caption: 'Diff selected file',
284-
execute: async args => {
285-
const selectedFile: Git.IStatusFileResult = args as any;
286-
await openDiffView(
287-
selectedFile.to,
288-
model,
289-
{
290-
currentRef: { specialRef: 'WORKING' },
290+
execute: args => {
291+
const { context, filePath, isText, status } = (args as any) as {
292+
context?: IDiffContext;
293+
filePath: string;
294+
isText: boolean;
295+
status?: Git.Status;
296+
};
297+
298+
let diffContext = context;
299+
if (!diffContext) {
300+
const specialRef = status === 'staged' ? 'INDEX' : 'WORKING';
301+
diffContext = {
302+
currentRef: { specialRef },
291303
previousRef: { gitRef: 'HEAD' }
292-
},
293-
renderMime,
294-
!selectedFile.is_binary
295-
);
296-
}
297-
});
304+
};
305+
}
298306

299-
commands.addCommand(CommandIDs.gitFileDiffIndex, {
300-
label: 'Diff',
301-
caption: 'Diff selected file',
302-
execute: async args => {
303-
const selectedFile: Git.IStatusFileResult = args as any;
304-
await openDiffView(
305-
selectedFile.to,
306-
model,
307-
{
308-
currentRef: { specialRef: 'INDEX' },
309-
previousRef: { gitRef: 'HEAD' }
310-
},
311-
renderMime,
312-
!selectedFile.is_binary
313-
);
307+
if (isDiffSupported(filePath) || isText) {
308+
const id = `nbdiff-${filePath}-${getRefValue(diffContext.currentRef)}`;
309+
const mainAreaItems = shell.widgets('main');
310+
let mainAreaItem = mainAreaItems.next();
311+
while (mainAreaItem) {
312+
if (mainAreaItem.id === id) {
313+
shell.activateById(id);
314+
break;
315+
}
316+
mainAreaItem = mainAreaItems.next();
317+
}
318+
319+
if (!mainAreaItem) {
320+
const serverRepoPath = model.getRelativeFilePath();
321+
const nbDiffWidget = ReactWidget.create(
322+
<RenderMimeProvider value={renderMime}>
323+
<Diff
324+
path={filePath}
325+
diffContext={diffContext}
326+
topRepoPath={serverRepoPath}
327+
/>
328+
</RenderMimeProvider>
329+
);
330+
nbDiffWidget.id = id;
331+
nbDiffWidget.title.label = PathExt.basename(filePath);
332+
nbDiffWidget.title.icon = diffIcon;
333+
nbDiffWidget.title.closable = true;
334+
nbDiffWidget.addClass('jp-git-diff-parent-diff-widget');
335+
336+
shell.add(nbDiffWidget, 'main');
337+
shell.activateById(nbDiffWidget.id);
338+
}
339+
} else {
340+
showErrorMessage(
341+
'Diff Not Supported',
342+
`Diff is not supported for ${PathExt.extname(
343+
filePath
344+
).toLocaleLowerCase()} files.`
345+
);
346+
}
314347
}
315348
});
316349

0 commit comments

Comments
 (0)