Skip to content

Commit

Permalink
Don't rely on dom node title and open file even if it is not currentl…
Browse files Browse the repository at this point in the history
…y modified
  • Loading branch information
fcollonval committed Jul 8, 2022
1 parent 4b6ca08 commit 014a52a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
41 changes: 32 additions & 9 deletions src/commandsAndMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -755,25 +755,48 @@ export function addCommands(
label: trans.__('Open File'),
caption: trans.__('Open file from its diff view'),
execute: async _ => {
const widget = app.contextMenuHitTest((node: HTMLElement) => {
const domNode = app.contextMenuHitTest((node: HTMLElement) => {
const nodeId = node.dataset.id;
return nodeId && nodeId.substring(0, 8) === 'git-diff';
});
if (!widget) {
if (!domNode) {
return;
}

const filename = widget.title;
const file = gitModel.status.files.find(
fileStatus => `${gitModel.pathRepository}/${fileStatus.to}` === filename
const matches = toArray(shell.widgets('main')).filter(
widget => widget.id === domNode.dataset.id
);
if (!file) {

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

commands.execute(ContextCommandIDs.gitFileOpen, {
files: [file]
} as CommandArguments.IGitContextAction as any);
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)
});
}
}
});

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
20 changes: 12 additions & 8 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

0 comments on commit 014a52a

Please sign in to comment.