Git/SCM: Use vscode.open and vscode.diff for a better editor opening experience (#110733)

* wip: try to use vscode.open and vscode.diff in git/scm

related to #110397

* 💄

* revert change to commands.converter.toInternal

* complete usage os vscode.open and vscode.diff in git extension
This commit is contained in:
João Moreno
2020-11-16 16:19:26 +01:00
committed by GitHub
parent da9a12b837
commit 62e830be77
7 changed files with 280 additions and 202 deletions

View File

@@ -75,13 +75,21 @@ export class Resource implements SourceControlResourceState {
return this._resourceUri;
}
@memoize
get leftUri(): Uri | undefined {
return this.resources[0];
}
get rightUri(): Uri {
return this.resources[1];
}
get command(): Command {
return {
command: 'git.openResource',
title: localize('open', "Open"),
arguments: [this]
};
return this._commandResolver.resolveDefaultCommand(this);
}
@memoize
private get resources(): [Uri | undefined, Uri] {
return this._commandResolver.getResources(this);
}
get resourceGroupType(): ResourceGroupType { return this._resourceGroupType; }
@@ -262,12 +270,28 @@ export class Resource implements SourceControlResourceState {
}
constructor(
private _commandResolver: ResourceCommandResolver,
private _resourceGroupType: ResourceGroupType,
private _resourceUri: Uri,
private _type: Status,
private _useIcons: boolean,
private _renameResourceUri?: Uri
private _renameResourceUri?: Uri,
) { }
async open(): Promise<void> {
const command = this.command;
await commands.executeCommand<void>(command.command, ...(command.arguments || []));
}
async openFile(): Promise<void> {
const command = this._commandResolver.resolveFileCommand(this);
await commands.executeCommand<void>(command.command, ...(command.arguments || []));
}
async openChange(): Promise<void> {
const command = this._commandResolver.resolveChangeCommand(this);
await commands.executeCommand<void>(command.command, ...(command.arguments || []));
}
}
export const enum Operation {
@@ -553,6 +577,142 @@ class DotGitWatcher implements IFileWatcher {
}
}
class ResourceCommandResolver {
constructor(private repository: Repository) { }
resolveDefaultCommand(resource: Resource): Command {
const config = workspace.getConfiguration('git', Uri.file(this.repository.root));
const openDiffOnClick = config.get<boolean>('openDiffOnClick', true);
return openDiffOnClick ? this.resolveChangeCommand(resource) : this.resolveFileCommand(resource);
}
resolveFileCommand(resource: Resource): Command {
return {
command: 'vscode.open',
title: localize('open', "Open"),
arguments: [resource.resourceUri]
};
}
resolveChangeCommand(resource: Resource): Command {
const title = this.getTitle(resource);
if (!resource.leftUri) {
return {
command: 'vscode.open',
title: localize('open', "Open"),
arguments: [resource.rightUri, { override: resource.type === Status.BOTH_MODIFIED ? false : undefined }, title]
};
} else {
return {
command: 'vscode.diff',
title: localize('open', "Open"),
arguments: [resource.leftUri, resource.rightUri, title]
};
}
}
getResources(resource: Resource): [Uri | undefined, Uri] {
for (const submodule of this.repository.submodules) {
if (path.join(this.repository.root, submodule.path) === resource.resourceUri.fsPath) {
return [undefined, toGitUri(resource.resourceUri, resource.resourceGroupType === ResourceGroupType.Index ? 'index' : 'wt', { submoduleOf: this.repository.root })];
}
}
return [this.getLeftResource(resource), this.getRightResource(resource)];
}
private getLeftResource(resource: Resource): Uri | undefined {
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_RENAMED:
case Status.INDEX_ADDED:
return toGitUri(resource.original, 'HEAD');
case Status.MODIFIED:
case Status.UNTRACKED:
return toGitUri(resource.resourceUri, '~');
case Status.DELETED_BY_US:
case Status.DELETED_BY_THEM:
return toGitUri(resource.resourceUri, '~1');
}
return undefined;
}
private getRightResource(resource: Resource): Uri {
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_ADDED:
case Status.INDEX_COPIED:
case Status.INDEX_RENAMED:
return toGitUri(resource.resourceUri, '');
case Status.INDEX_DELETED:
case Status.DELETED:
return toGitUri(resource.resourceUri, 'HEAD');
case Status.DELETED_BY_US:
return toGitUri(resource.resourceUri, '~3');
case Status.DELETED_BY_THEM:
return toGitUri(resource.resourceUri, '~2');
case Status.MODIFIED:
case Status.UNTRACKED:
case Status.IGNORED:
case Status.INTENT_TO_ADD:
const uriString = resource.resourceUri.toString();
const [indexStatus] = this.repository.indexGroup.resourceStates.filter(r => r.resourceUri.toString() === uriString);
if (indexStatus && indexStatus.renameResourceUri) {
return indexStatus.renameResourceUri;
}
return resource.resourceUri;
case Status.BOTH_ADDED:
case Status.BOTH_MODIFIED:
return resource.resourceUri;
}
throw new Error('Should never happen');
}
private getTitle(resource: Resource): string {
const basename = path.basename(resource.resourceUri.fsPath);
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_RENAMED:
case Status.INDEX_ADDED:
return localize('git.title.index', '{0} (Index)', basename);
case Status.MODIFIED:
case Status.BOTH_ADDED:
case Status.BOTH_MODIFIED:
return localize('git.title.workingTree', '{0} (Working Tree)', basename);
case Status.INDEX_DELETED:
case Status.DELETED:
return localize('git.title.deleted', '{0} (Deleted)', basename);
case Status.DELETED_BY_US:
return localize('git.title.theirs', '{0} (Theirs)', basename);
case Status.DELETED_BY_THEM:
return localize('git.title.ours', '{0} (Ours)', basename);
case Status.UNTRACKED:
return localize('git.title.untracked', '{0} (Untracked)', basename);
default:
return '';
}
}
}
export class Repository implements Disposable {
private _onDidChangeRepository = new EventEmitter<Uri>();
@@ -683,6 +843,7 @@ export class Repository implements Disposable {
private isRepositoryHuge = false;
private didWarnAboutLimit = false;
private resourceCommandResolver = new ResourceCommandResolver(this);
private disposables: Disposable[] = [];
constructor(
@@ -753,6 +914,7 @@ export class Repository implements Disposable {
e.affectsConfiguration('git.branchSortOrder', root)
|| e.affectsConfiguration('git.untrackedChanges', root)
|| e.affectsConfiguration('git.ignoreSubmodules', root)
|| e.affectsConfiguration('git.openDiffOnClick', root)
)(this.updateModelState, this, this.disposables);
const updateInputBoxVisibility = () => {
@@ -1637,36 +1799,36 @@ export class Repository implements Disposable {
switch (raw.x + raw.y) {
case '??': switch (untrackedChanges) {
case 'mixed': return workingTree.push(new Resource(ResourceGroupType.WorkingTree, uri, Status.UNTRACKED, useIcons));
case 'separate': return untracked.push(new Resource(ResourceGroupType.Untracked, uri, Status.UNTRACKED, useIcons));
case 'mixed': return workingTree.push(new Resource(this.resourceCommandResolver, ResourceGroupType.WorkingTree, uri, Status.UNTRACKED, useIcons));
case 'separate': return untracked.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Untracked, uri, Status.UNTRACKED, useIcons));
default: return undefined;
}
case '!!': switch (untrackedChanges) {
case 'mixed': return workingTree.push(new Resource(ResourceGroupType.WorkingTree, uri, Status.IGNORED, useIcons));
case 'separate': return untracked.push(new Resource(ResourceGroupType.Untracked, uri, Status.IGNORED, useIcons));
case 'mixed': return workingTree.push(new Resource(this.resourceCommandResolver, ResourceGroupType.WorkingTree, uri, Status.IGNORED, useIcons));
case 'separate': return untracked.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Untracked, uri, Status.IGNORED, useIcons));
default: return undefined;
}
case 'DD': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.BOTH_DELETED, useIcons));
case 'AU': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.ADDED_BY_US, useIcons));
case 'UD': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.DELETED_BY_THEM, useIcons));
case 'UA': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.ADDED_BY_THEM, useIcons));
case 'DU': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.DELETED_BY_US, useIcons));
case 'AA': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.BOTH_ADDED, useIcons));
case 'UU': return merge.push(new Resource(ResourceGroupType.Merge, uri, Status.BOTH_MODIFIED, useIcons));
case 'DD': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.BOTH_DELETED, useIcons));
case 'AU': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.ADDED_BY_US, useIcons));
case 'UD': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.DELETED_BY_THEM, useIcons));
case 'UA': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.ADDED_BY_THEM, useIcons));
case 'DU': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.DELETED_BY_US, useIcons));
case 'AA': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.BOTH_ADDED, useIcons));
case 'UU': return merge.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Merge, uri, Status.BOTH_MODIFIED, useIcons));
}
switch (raw.x) {
case 'M': index.push(new Resource(ResourceGroupType.Index, uri, Status.INDEX_MODIFIED, useIcons)); break;
case 'A': index.push(new Resource(ResourceGroupType.Index, uri, Status.INDEX_ADDED, useIcons)); break;
case 'D': index.push(new Resource(ResourceGroupType.Index, uri, Status.INDEX_DELETED, useIcons)); break;
case 'R': index.push(new Resource(ResourceGroupType.Index, uri, Status.INDEX_RENAMED, useIcons, renameUri)); break;
case 'C': index.push(new Resource(ResourceGroupType.Index, uri, Status.INDEX_COPIED, useIcons, renameUri)); break;
case 'M': index.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Index, uri, Status.INDEX_MODIFIED, useIcons)); break;
case 'A': index.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Index, uri, Status.INDEX_ADDED, useIcons)); break;
case 'D': index.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Index, uri, Status.INDEX_DELETED, useIcons)); break;
case 'R': index.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Index, uri, Status.INDEX_RENAMED, useIcons, renameUri)); break;
case 'C': index.push(new Resource(this.resourceCommandResolver, ResourceGroupType.Index, uri, Status.INDEX_COPIED, useIcons, renameUri)); break;
}
switch (raw.y) {
case 'M': workingTree.push(new Resource(ResourceGroupType.WorkingTree, uri, Status.MODIFIED, useIcons, renameUri)); break;
case 'D': workingTree.push(new Resource(ResourceGroupType.WorkingTree, uri, Status.DELETED, useIcons, renameUri)); break;
case 'A': workingTree.push(new Resource(ResourceGroupType.WorkingTree, uri, Status.INTENT_TO_ADD, useIcons, renameUri)); break;
case 'M': workingTree.push(new Resource(this.resourceCommandResolver, ResourceGroupType.WorkingTree, uri, Status.MODIFIED, useIcons, renameUri)); break;
case 'D': workingTree.push(new Resource(this.resourceCommandResolver, ResourceGroupType.WorkingTree, uri, Status.DELETED, useIcons, renameUri)); break;
case 'A': workingTree.push(new Resource(this.resourceCommandResolver, ResourceGroupType.WorkingTree, uri, Status.INTENT_TO_ADD, useIcons, renameUri)); break;
}
return undefined;