Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'open file' command to context menu on diff tab #1135

Merged
merged 6 commits into from
Jul 9, 2022
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
6 changes: 6 additions & 0 deletions schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@
"label": "Git",
"icon": "git"
}
},
{
"type": "command",
"command": "git:open-file-from-diff",
"selector": ".jp-git-diff-title",
"rank": 1
}
]
}
Expand Down
52 changes: 51 additions & 1 deletion src/commandsAndMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ export function addCommands(
getDiffProvider(fullPath) ?? (isText && createPlainTextDiff);

if (buildDiffWidget) {
const id = `diff-${fullPath}-${model.reference.label}-${model.challenger.label}`;
const id = `git-diff-${fullPath}-${model.reference.label}-${model.challenger.label}`;
const mainAreaItems = shell.widgets('main');
let mainAreaItem = mainAreaItems.next();
while (mainAreaItem) {
Expand All @@ -519,6 +519,7 @@ export function addCommands(
diffWidget.title.caption = fullPath;
diffWidget.title.icon = diffIcon;
diffWidget.title.closable = true;
diffWidget.title.className = 'jp-git-diff-title';
diffWidget.addClass('jp-git-diff-parent-widget');

shell.add(diffWidget, 'main');
Expand Down Expand Up @@ -750,6 +751,55 @@ export function addCommands(
icon: openIcon.bindprops({ stylesheet: 'menuItem' })
});

commands.addCommand(ContextCommandIDs.openFileFromDiff, {
label: trans.__('Open File'),
caption: trans.__('Open file from its diff view'),
execute: async _ => {
BoscoCHW marked this conversation as resolved.
Show resolved Hide resolved
const domNode = app.contextMenuHitTest((node: HTMLElement) => {
const nodeId = node.dataset.id;
return nodeId && nodeId.substring(0, 8) === 'git-diff';
});
if (!domNode) {
return;
}

const matches = toArray(shell.widgets('main')).filter(
widget => widget.id === domNode.dataset.id
);

if (matches.length === 0) {
return;
}

const diffModel = (
((matches[0] as MainAreaWidget).content as Panel)
.widgets[0] as Git.Diff.IDiffWidget
).model;

const filename = diffModel.filename;

if (
diffModel.reference.source === Git.Diff.SpecialRef.INDEX ||
diffModel.reference.source === Git.Diff.SpecialRef.WORKING ||
diffModel.challenger.source === Git.Diff.SpecialRef.INDEX ||
diffModel.challenger.source === Git.Diff.SpecialRef.WORKING
) {
const file = gitModel.status.files.find(
fileStatus => fileStatus.from === filename
);
if (file) {
commands.execute(ContextCommandIDs.gitFileOpen, {
files: [file]
} as any);
}
} else {
commands.execute('docmanager:open', {
path: gitModel.getRelativeFilePath(filename)
});
}
}
});

commands.addCommand(ContextCommandIDs.gitFileDiff, {
label: trans.__('Diff'),
caption: pluralizedContextLabel(
Expand Down
7 changes: 7 additions & 0 deletions src/components/diff/NotebookDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ export class NotebookDiff extends Panel implements Git.Diff.IDiffWidget {
return this._model.hasConflict;
}

/**
* Diff model
*/
get model(): Git.Diff.IModel {
return this._model;
}

/**
* Nbdime notebook widget.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/components/diff/PlainTextDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export class PlainTextDiff extends Widget implements Git.Diff.IDiffWidget {
return true;
}

/**
* Diff model
*/
get model(): Git.Diff.IModel {
return this._model;
}

/**
* Promise which fulfills when the widget is ready.
*/
Expand Down
23 changes: 14 additions & 9 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,24 @@ export namespace Git {
*/
export interface IDiffWidget extends Widget {
/**
* Refresh the diff widget
*
* Note: Update the content and recompute the diff
*/
refresh(): Promise<void>;
/**
* Checks if the conflicted file has been resolved.
* Diff model
*/
isFileResolved: boolean;
readonly model: Git.Diff.IModel;
/**
* Gets the file model of a resolved merge conflict,
* and rejects if unable to retrieve
*/
getResolvedFile(): Promise<Partial<Contents.IModel>>;
/**
* Checks if the conflicted file has been resolved.
*/
readonly isFileResolved: boolean;
/**
* Refresh the diff widget
*
* Note: Update the content and recompute the diff
*/
refresh(): Promise<void>;
}

/**
Expand Down Expand Up @@ -1149,7 +1153,8 @@ export enum ContextCommandIDs {
gitFileHistory = 'git:context-history',
gitIgnore = 'git:context-ignore',
gitIgnoreExtension = 'git:context-ignoreExtension',
gitNoAction = 'git:no-action'
gitNoAction = 'git:no-action',
openFileFromDiff = 'git:open-file-from-diff'
}

/**
Expand Down