Skip to content

Commit

Permalink
Merge pull request #890 from meeseeksmachine/auto-backport-of-pr-877-…
Browse files Browse the repository at this point in the history
…on-jlab-2

Backport PR #877 on branch jlab-2 (Implement Git context menu in the file browser)
  • Loading branch information
fcollonval authored Mar 6, 2021
2 parents 0f4baeb + abcebcc commit 0bad949
Show file tree
Hide file tree
Showing 13 changed files with 676 additions and 325 deletions.
655 changes: 465 additions & 190 deletions src/commandsAndMenu.tsx

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/components/CommitBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
commitButtonClass
} from '../style/CommitBox';
import { CommandRegistry } from '@lumino/commands';
import { SUBMIT_COMMIT_COMMAND } from '../commandsAndMenu';
import { CommandIDs } from '../tokens';

/**
* Interface describing component properties.
Expand Down Expand Up @@ -136,7 +136,7 @@ export class CommitBox extends React.Component<
*/
private _getSubmitKeystroke = (): string => {
const binding = this.props.commands.keyBindings.find(
binding => binding.command === SUBMIT_COMMIT_COMMAND
binding => binding.command === CommandIDs.gitSubmitCommand
);
return binding.keys.join(' ');
};
Expand Down Expand Up @@ -201,7 +201,7 @@ export class CommitBox extends React.Component<
_: CommandRegistry,
commandArgs: CommandRegistry.ICommandExecutedArgs
): void => {
if (commandArgs.id === SUBMIT_COMMIT_COMMAND && this._canCommit()) {
if (commandArgs.id === CommandIDs.gitSubmitCommand && this._canCommit()) {
this._onCommitSubmit();
}
};
Expand Down
164 changes: 85 additions & 79 deletions src/components/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Menu } from '@lumino/widgets';
import * as React from 'react';
import AutoSizer from 'react-virtualized-auto-sizer';
import { ListChildComponentProps } from 'react-window';
import { CommandIDs } from '../commandsAndMenu';
import { addMenuItems, CommandArguments } from '../commandsAndMenu';
import { GitExtension } from '../model';
import { hiddenButtonStyle } from '../style/ActionButtonStyle';
import { fileListWrapperClass } from '../style/FileListStyle';
Expand All @@ -16,7 +16,7 @@ import {
openIcon,
removeIcon
} from '../style/icons';
import { Git } from '../tokens';
import { ContextCommandIDs, Git } from '../tokens';
import { ActionButton } from './ActionButton';
import { isDiffSupported } from './diff/Diff';
import { FileItem } from './FileItem';
Expand Down Expand Up @@ -45,6 +45,58 @@ export interface IFileListProps {
settings: ISettingRegistry.ISettings;
}

export type ContextCommands = Record<Git.Status, ContextCommandIDs[]>;

export const CONTEXT_COMMANDS: ContextCommands = {
'partially-staged': [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileUnstage,
ContextCommandIDs.gitFileDiff
],
unstaged: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileStage,
ContextCommandIDs.gitFileDiscard,
ContextCommandIDs.gitFileDiff
],
untracked: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileTrack,
ContextCommandIDs.gitIgnore,
ContextCommandIDs.gitIgnoreExtension,
ContextCommandIDs.gitFileDelete
],
staged: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileUnstage,
ContextCommandIDs.gitFileDiff
]
};

const SIMPLE_CONTEXT_COMMANDS: ContextCommands = {
'partially-staged': [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileDiscard,
ContextCommandIDs.gitFileDiff
],
staged: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileDiscard,
ContextCommandIDs.gitFileDiff
],
unstaged: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitFileDiscard,
ContextCommandIDs.gitFileDiff
],
untracked: [
ContextCommandIDs.gitFileOpen,
ContextCommandIDs.gitIgnore,
ContextCommandIDs.gitIgnoreExtension,
ContextCommandIDs.gitFileDelete
]
};

export class FileList extends React.Component<IFileListProps, IFileListState> {
constructor(props: IFileListProps) {
super(props);
Expand All @@ -71,42 +123,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
});

const contextMenu = new Menu({ commands: this.props.commands });
const commands = [CommandIDs.gitFileOpen];
switch (selectedFile.status) {
case 'unstaged':
commands.push(
CommandIDs.gitFileStage,
CommandIDs.gitFileDiscard,
CommandIDs.gitFileDiff
);
break;
case 'untracked':
commands.push(
CommandIDs.gitFileTrack,
CommandIDs.gitIgnore,
CommandIDs.gitIgnoreExtension,
CommandIDs.gitFileDelete
);
break;
case 'staged':
commands.push(CommandIDs.gitFileUnstage, CommandIDs.gitFileDiff);
break;
}
const commands = CONTEXT_COMMANDS[selectedFile.status];
addMenuItems(commands, contextMenu, [selectedFile]);

commands.forEach(command => {
if (command === CommandIDs.gitFileDiff) {
contextMenu.addItem({
command,
args: {
filePath: selectedFile.to,
isText: !selectedFile.is_binary,
status: selectedFile.status
}
});
} else {
contextMenu.addItem({ command, args: selectedFile as any });
}
});
contextMenu.open(event.clientX, event.clientY);
};

Expand All @@ -123,34 +142,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
event.preventDefault();

const contextMenu = new Menu({ commands: this.props.commands });
const commands = [CommandIDs.gitFileOpen];
switch (selectedFile.status) {
case 'untracked':
commands.push(
CommandIDs.gitIgnore,
CommandIDs.gitIgnoreExtension,
CommandIDs.gitFileDelete
);
break;
default:
commands.push(CommandIDs.gitFileDiscard, CommandIDs.gitFileDiff);
break;
}

commands.forEach(command => {
if (command === CommandIDs.gitFileDiff) {
contextMenu.addItem({
command,
args: {
filePath: selectedFile.to,
isText: !selectedFile.is_binary,
status: selectedFile.status
}
});
} else {
contextMenu.addItem({ command, args: selectedFile as any });
}
});
const commands = SIMPLE_CONTEXT_COMMANDS[selectedFile.status];
addMenuItems(commands, contextMenu, [selectedFile]);
contextMenu.open(event.clientX, event.clientY);
};

Expand Down Expand Up @@ -216,7 +209,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {

/** Discard changes in a specific unstaged or staged file */
discardChanges = async (file: Git.IStatusFile) => {
await this.props.commands.execute(CommandIDs.gitFileDiscard, file as any);
await this.props.commands.execute(ContextCommandIDs.gitFileDiscard, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
};

/** Add all untracked files */
Expand Down Expand Up @@ -334,7 +329,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
const { data, index, style } = rowProps;
const file = data[index] as Git.IStatusFile;
const openFile = () => {
this.props.commands.execute(CommandIDs.gitFileOpen, file as any);
this.props.commands.execute(ContextCommandIDs.gitFileOpen, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
};
const diffButton = this._createDiffButton(file);
return (
Expand Down Expand Up @@ -418,7 +415,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
const { data, index, style } = rowProps;
const file = data[index] as Git.IStatusFile;
const openFile = () => {
this.props.commands.execute(CommandIDs.gitFileOpen, file as any);
this.props.commands.execute(ContextCommandIDs.gitFileOpen, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
};
const diffButton = this._createDiffButton(file);
return (
Expand Down Expand Up @@ -528,10 +527,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
icon={openIcon}
title={'Open this file'}
onClick={() => {
this.props.commands.execute(
CommandIDs.gitFileOpen,
file as any
);
this.props.commands.execute(ContextCommandIDs.gitFileOpen, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
}}
/>
<ActionButton
Expand All @@ -549,7 +547,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
model={this.props.model}
onDoubleClick={() => {
if (!doubleClickDiff) {
this.props.commands.execute(CommandIDs.gitFileOpen, file as any);
this.props.commands.execute(ContextCommandIDs.gitFileOpen, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
}
}}
selected={this._isSelectedFile(file)}
Expand Down Expand Up @@ -601,7 +601,9 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
.composite as boolean;

const openFile = () => {
this.props.commands.execute(CommandIDs.gitFileOpen, file as any);
this.props.commands.execute(ContextCommandIDs.gitFileOpen, ({
files: [file]
} as CommandArguments.IGitContextAction) as any);
};

// Default value for actions and double click
Expand Down Expand Up @@ -737,11 +739,15 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
*/
private async _openDiffView(file: Git.IStatusFile): Promise<void> {
try {
await this.props.commands.execute(CommandIDs.gitFileDiff, {
filePath: file.to,
isText: !file.is_binary,
status: file.status
});
await this.props.commands.execute(ContextCommandIDs.gitFileDiff, ({
files: [
{
filePath: file.to,
isText: !file.is_binary,
status: file.status
}
]
} as CommandArguments.IGitFileDiff) as any);
} catch (reason) {
console.error(`Failed to open diff view for ${file.to}.\n${reason}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/GitPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Signal } from '@lumino/signaling';
import Tab from '@material-ui/core/Tab';
import Tabs from '@material-ui/core/Tabs';
import * as React from 'react';
import { CommandIDs } from '../commandsAndMenu';
import { Logger } from '../logger';
import { GitExtension } from '../model';
import {
Expand All @@ -20,7 +19,7 @@ import {
tabsClass,
warningTextClass
} from '../style/GitPanel';
import { Git, ILogMessage, Level } from '../tokens';
import { CommandIDs, Git, ILogMessage, Level } from '../tokens';
import { GitAuthorForm } from '../widgets/AuthorBox';
import { CommitBox } from './CommitBox';
import { FileList } from './FileList';
Expand Down
30 changes: 17 additions & 13 deletions src/components/SinglePastCommitInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CommandRegistry } from '@lumino/commands';
import * as React from 'react';
import { FixedSizeList, ListChildComponentProps } from 'react-window';
import { classes } from 'typestyle';
import { CommandIDs } from '../commandsAndMenu';
import { CommandArguments } from '../commandsAndMenu';
import { LoggerContext } from '../logger';
import { GitExtension } from '../model';
import {
Expand All @@ -25,7 +25,7 @@ import {
iconClass,
insertionsIconClass
} from '../style/SinglePastCommitInfo';
import { Git } from '../tokens';
import { ContextCommandIDs, Git } from '../tokens';
import { ActionButton } from './ActionButton';
import { isDiffSupported } from './diff/Diff';
import { FilePath } from './FilePath';
Expand Down Expand Up @@ -337,18 +337,22 @@ export class SinglePastCommitInfo extends React.Component<
event.stopPropagation();

try {
self.props.commands.execute(CommandIDs.gitFileDiff, {
filePath: fpath,
isText: bool,
context: {
previousRef: {
gitRef: self.props.commit.pre_commit
},
currentRef: {
gitRef: self.props.commit.commit
self.props.commands.execute(ContextCommandIDs.gitFileDiff, ({
files: [
{
filePath: fpath,
isText: bool,
context: {
previousRef: {
gitRef: self.props.commit.pre_commit
},
currentRef: {
gitRef: self.props.commit.commit
}
}
}
}
});
]
} as CommandArguments.IGitFileDiff) as any);
} catch (err) {
console.error(`Failed to open diff view for ${fpath}.\n${err}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { CommandRegistry } from '@lumino/commands';
import { Badge, Tab, Tabs } from '@material-ui/core';
import * as React from 'react';
import { classes } from 'typestyle';
import { CommandIDs } from '../commandsAndMenu';
import { Logger } from '../logger';
import {
selectedTabClass,
Expand All @@ -31,7 +30,7 @@ import {
toolbarMenuWrapperClass,
toolbarNavClass
} from '../style/Toolbar';
import { Git, IGitExtension, Level } from '../tokens';
import { CommandIDs, Git, IGitExtension, Level } from '../tokens';
import { ActionButton } from './ActionButton';
import { BranchMenu } from './BranchMenu';
import { TagMenu } from './TagMenu';
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { IMainMenu } from '@jupyterlab/mainmenu';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { IStatusBar } from '@jupyterlab/statusbar';
import { addCommands, createGitMenu } from './commandsAndMenu';
import {
addCommands,
addFileBrowserContextMenu,
createGitMenu
} from './commandsAndMenu';
import { GitExtension } from './model';
import { getServerSettings } from './server';
import { gitIcon } from './style/icons';
Expand Down Expand Up @@ -169,6 +173,14 @@ async function activate(

// Add the status bar widget
addStatusBarWidget(statusBar, gitExtension, settings);

// Add the context menu items for the default file browser
addFileBrowserContextMenu(
gitExtension,
factory.tracker,
app.commands,
app.contextMenu
);
}

return gitExtension;
Expand Down
Loading

0 comments on commit 0bad949

Please sign in to comment.