Skip to content

Commit bd8b5ad

Browse files
committed
Use commands execution
1 parent e62d5da commit bd8b5ad

File tree

3 files changed

+75
-92
lines changed

3 files changed

+75
-92
lines changed

src/commandsAndMenu.ts renamed to src/commandsAndMenu.tsx

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import {
66
showDialog,
77
showErrorMessage
88
} from '@jupyterlab/apputils';
9+
import { PathExt } from '@jupyterlab/coreutils';
910
import { FileBrowser } from '@jupyterlab/filebrowser';
1011
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
1112
import { ISettingRegistry } from '@jupyterlab/settingregistry';
1213
import { ITerminal } from '@jupyterlab/terminal';
1314
import { CommandRegistry } from '@lumino/commands';
1415
import { Menu } from '@lumino/widgets';
16+
import * as React from 'react';
17+
import { openDiffView } from './components/diff/DiffWidget';
18+
import { GitExtension } from './model';
1519
import { Git } from './tokens';
1620
import { GitCredentialsForm } from './widgets/CredentialsBox';
1721
import { doGitClone } from './widgets/gitClone';
1822
import { GitPullPushDialog, Operation } from './widgets/gitPushPull';
19-
import { openListedFile, discardChanges } from './utils';
20-
import { GitExtension } from './model';
21-
import { openDiffView } from './components/diff/DiffWidget';
22-
import { PathExt } from '@jupyterlab/coreutils';
2323

2424
const RESOURCES = [
2525
{
@@ -254,10 +254,28 @@ export function addCommands(
254254
label: 'Open',
255255
caption: 'Open selected file',
256256
execute: async args => {
257-
const selectedFile: Git.IStatusFileResult = args as any;
258-
await openListedFile(selectedFile, model);
259-
},
260-
isEnabled: args => args && args.to !== undefined
257+
const file: Git.IStatusFileResult = args as any;
258+
259+
const { x, y, to } = file;
260+
if (x === 'D' || y === 'D') {
261+
await showErrorMessage(
262+
'Open File Failed',
263+
'This file has been deleted!'
264+
);
265+
return;
266+
}
267+
try {
268+
if (to[to.length - 1] !== '/') {
269+
commands.execute('docmanager:open', {
270+
path: model.getRelativeFilePath(to)
271+
});
272+
} else {
273+
console.log('Cannot open a folder here');
274+
}
275+
} catch (err) {
276+
console.error(`Fail to open ${to}.`);
277+
}
278+
}
261279
});
262280

263281
commands.addCommand(CommandIDs.gitFileDiffWorking, {
@@ -275,8 +293,7 @@ export function addCommands(
275293
renderMime,
276294
!selectedFile.is_binary
277295
);
278-
},
279-
isEnabled: args => args && args.to !== undefined
296+
}
280297
});
281298

282299
commands.addCommand(CommandIDs.gitFileDiffIndex, {
@@ -294,8 +311,7 @@ export function addCommands(
294311
renderMime,
295312
!selectedFile.is_binary
296313
);
297-
},
298-
isEnabled: args => args && args.to !== undefined
314+
}
299315
});
300316

301317
commands.addCommand(CommandIDs.gitFileStage, {
@@ -304,8 +320,7 @@ export function addCommands(
304320
execute: async args => {
305321
const selectedFile: Git.IStatusFile = args as any;
306322
await model.add(selectedFile.to);
307-
},
308-
isEnabled: args => args && args.to !== undefined
323+
}
309324
});
310325

311326
commands.addCommand(CommandIDs.gitFileTrack, {
@@ -314,8 +329,7 @@ export function addCommands(
314329
execute: async args => {
315330
const selectedFile: Git.IStatusFile = args as any;
316331
await model.add(selectedFile.to);
317-
},
318-
isEnabled: args => args && args.to !== undefined
332+
}
319333
});
320334

321335
commands.addCommand(CommandIDs.gitFileUnstage, {
@@ -326,18 +340,47 @@ export function addCommands(
326340
if (selectedFile.x !== 'D') {
327341
await model.reset(selectedFile.to);
328342
}
329-
},
330-
isEnabled: args => args && args.to !== undefined
343+
}
331344
});
332345

333346
commands.addCommand(CommandIDs.gitFileDiscard, {
334347
label: 'Discard',
335348
caption: 'Discard recent changes of selected file',
336349
execute: async args => {
337-
const selectedFile: Git.IStatusFile = args as any;
338-
await discardChanges(selectedFile, model);
339-
},
340-
isEnabled: args => args && args.to !== undefined
350+
const file: Git.IStatusFile = args as any;
351+
352+
const result = await showDialog({
353+
title: 'Discard changes',
354+
body: (
355+
<span>
356+
Are you sure you want to permanently discard changes to{' '}
357+
<b>{file.to}</b>? This action cannot be undone.
358+
</span>
359+
),
360+
buttons: [
361+
Dialog.cancelButton(),
362+
Dialog.warnButton({ label: 'Discard' })
363+
]
364+
});
365+
if (result.button.accept) {
366+
try {
367+
if (file.status === 'staged' || file.status === 'partially-staged') {
368+
await model.reset(file.to);
369+
}
370+
if (
371+
file.status === 'unstaged' ||
372+
(file.status === 'partially-staged' && file.x !== 'A')
373+
) {
374+
// resetting an added file moves it to untracked category => checkout will fail
375+
await model.checkout({ filename: file.to });
376+
}
377+
} catch (reason) {
378+
showErrorMessage(`Discard changes for ${file.to} failed.`, reason, [
379+
Dialog.warnButton({ label: 'DISMISS' })
380+
]);
381+
}
382+
}
383+
}
341384
});
342385

343386
commands.addCommand(CommandIDs.gitIgnore, {
@@ -348,8 +391,7 @@ export function addCommands(
348391
if (selectedFile) {
349392
await model.ignore(selectedFile.to, false);
350393
}
351-
},
352-
isEnabled: args => args && args.to !== undefined
394+
}
353395
});
354396

355397
commands.addCommand(CommandIDs.gitIgnoreExtension, {
@@ -379,7 +421,6 @@ export function addCommands(
379421
}
380422
}
381423
},
382-
isEnabled: args => args && args.to !== undefined,
383424
isVisible: args => {
384425
const selectedFile: Git.IStatusFile = args as any;
385426
const extension = PathExt.extname(selectedFile.to);

src/components/FileList.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
removeIcon
1717
} from '../style/icons';
1818
import { Git } from '../tokens';
19-
import { discardChanges, openListedFile } from '../utils';
2019
import { ActionButton } from './ActionButton';
2120
import { isDiffSupported } from './diff/Diff';
2221
import { openDiffView } from './diff/DiffWidget';
@@ -175,7 +174,7 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
175174

176175
/** Discard changes in a specific unstaged or staged file */
177176
discardChanges = async (file: Git.IStatusFile) => {
178-
await discardChanges(file, this.props.model);
177+
await this._commands.execute(CommandIDs.gitFileDiscard, file as any);
179178
};
180179

181180
/** Add all untracked files */
@@ -281,7 +280,7 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
281280
>
282281
{files.map((file: Git.IStatusFile) => {
283282
const openFile = () => {
284-
openListedFile(file, this.props.model);
283+
this._commands.execute(CommandIDs.gitFileOpen, file as any);
285284
};
286285
const diffButton = this._createDiffButton(file, 'INDEX');
287286
return (
@@ -355,7 +354,7 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
355354
>
356355
{files.map((file: Git.IStatusFile) => {
357356
const openFile = () => {
358-
openListedFile(file, this.props.model);
357+
this._commands.execute(CommandIDs.gitFileOpen, file as any);
359358
};
360359
const diffButton = this._createDiffButton(file, 'WORKING');
361360
return (
@@ -436,7 +435,10 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
436435
icon={openIcon}
437436
title={'Open this file'}
438437
onClick={() => {
439-
openListedFile(file, this.props.model);
438+
this._commands.execute(
439+
CommandIDs.gitFileOpen,
440+
file as any
441+
);
440442
}}
441443
/>
442444
<ActionButton
@@ -454,7 +456,7 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
454456
model={this.props.model}
455457
onDoubleClick={() => {
456458
if (!doubleClickDiff) {
457-
openListedFile(file, this.props.model);
459+
this._commands.execute(CommandIDs.gitFileOpen, file as any);
458460
}
459461
}}
460462
selected={this._isSelectedFile(file)}
@@ -485,7 +487,7 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
485487
>
486488
{files.map((file: Git.IStatusFile) => {
487489
const openFile = () => {
488-
openListedFile(file, this.props.model);
490+
this._commands.execute(CommandIDs.gitFileOpen, file as any);
489491
};
490492

491493
// Default value for actions and double click

src/utils.tsx renamed to src/utils.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { Dialog, showDialog, showErrorMessage } from '@jupyterlab/apputils';
21
import { PathExt } from '@jupyterlab/coreutils';
3-
import * as React from 'react';
4-
import { GitExtension } from './model';
52
import {
63
folderFileIconStyle,
74
genericFileIconStyle,
@@ -43,63 +40,6 @@ export function decodeStage(x: string, y: string): Git.Status {
4340
return null;
4441
}
4542

46-
export async function discardChanges(
47-
file: Git.IStatusFile,
48-
model: GitExtension
49-
) {
50-
const result = await showDialog({
51-
title: 'Discard changes',
52-
body: (
53-
<span>
54-
Are you sure you want to permanently discard changes to <b>{file.to}</b>
55-
? This action cannot be undone.
56-
</span>
57-
),
58-
buttons: [Dialog.cancelButton(), Dialog.warnButton({ label: 'Discard' })]
59-
});
60-
if (result.button.accept) {
61-
try {
62-
if (file.status === 'staged' || file.status === 'partially-staged') {
63-
await model.reset(file.to);
64-
}
65-
if (
66-
file.status === 'unstaged' ||
67-
(file.status === 'partially-staged' && file.x !== 'A')
68-
) {
69-
// resetting an added file moves it to untracked category => checkout will fail
70-
await model.checkout({ filename: file.to });
71-
}
72-
} catch (reason) {
73-
showErrorMessage(`Discard changes for ${file.to} failed.`, reason, [
74-
Dialog.warnButton({ label: 'DISMISS' })
75-
]);
76-
}
77-
}
78-
}
79-
80-
/** Open a file in the git listing */
81-
export async function openListedFile(
82-
file: Git.IStatusFileResult,
83-
model: GitExtension
84-
) {
85-
const { x, y, to } = file;
86-
if (x === 'D' || y === 'D') {
87-
await showErrorMessage('Open File Failed', 'This file has been deleted!');
88-
return;
89-
}
90-
try {
91-
if (to[to.length - 1] !== '/') {
92-
model.commands.execute('docmanager:open', {
93-
path: model.getRelativeFilePath(to)
94-
});
95-
} else {
96-
console.log('Cannot open a folder here');
97-
}
98-
} catch (err) {
99-
console.error(`Fail to open ${to}.`);
100-
}
101-
}
102-
10343
/**
10444
* Get the extension of a given file
10545
*

0 commit comments

Comments
 (0)