git: open change/file

This commit is contained in:
Joao Moreno
2017-01-12 11:20:56 +01:00
parent 0100102027
commit fc0f2188e9
3 changed files with 90 additions and 77 deletions

View File

@@ -5,9 +5,9 @@
'use strict';
import { scm, Uri, Disposable, SCMProvider, SCMResourceGroup, Event, commands, ProviderResult } from 'vscode';
import { Model, Status, Resource, ResourceGroup } from './model';
import * as path from 'path';
import { scm, Uri, Disposable, SCMProvider, SCMResourceGroup, Event, ProviderResult } from 'vscode';
import { Model, Resource, ResourceGroup } from './model';
import { CommandCenter } from './commands';
export class GitSCMProvider implements SCMProvider {
@@ -17,7 +17,7 @@ export class GitSCMProvider implements SCMProvider {
get onDidChange(): Event<SCMResourceGroup[]> { return this.model.onDidChange; }
get label(): string { return 'Git'; }
constructor(private model: Model) {
constructor(private model: Model, private commandCenter: CommandCenter) {
model.update();
scm.registerSCMProvider('git', this);
}
@@ -29,70 +29,7 @@ export class GitSCMProvider implements SCMProvider {
}
open(resource: Resource): ProviderResult<void> {
const left = this.getLeftResource(resource);
const right = this.getRightResource(resource);
const title = this.getTitle(resource);
if (!left) {
if (!right) {
// TODO
console.error('oh no');
return;
}
return commands.executeCommand<void>('vscode.open', right);
}
return commands.executeCommand<void>('vscode.diff', left, right, title);
}
private getLeftResource(resource: Resource): Uri | undefined {
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_RENAMED:
return resource.uri.with({ scheme: 'git', query: 'HEAD' });
case Status.MODIFIED:
const uriString = resource.uri.toString();
const [indexStatus] = this.model.indexGroup.resources.filter(r => r.uri.toString() === uriString);
const query = indexStatus ? '~' : 'HEAD';
return resource.uri.with({ scheme: 'git', query });
}
}
private getRightResource(resource: Resource): Uri | undefined {
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_ADDED:
case Status.INDEX_COPIED:
case Status.INDEX_RENAMED:
return resource.uri.with({ scheme: 'git' });
case Status.INDEX_DELETED:
case Status.DELETED:
return resource.uri.with({ scheme: 'git', query: 'HEAD' });
case Status.MODIFIED:
case Status.UNTRACKED:
case Status.IGNORED:
case Status.BOTH_MODIFIED:
return resource.uri;
}
}
private getTitle(resource: Resource): string {
const basename = path.basename(resource.uri.fsPath);
switch (resource.type) {
case Status.INDEX_MODIFIED:
case Status.INDEX_RENAMED:
return `${basename} (Index)`;
case Status.MODIFIED:
return `${basename} (Working Tree)`;
}
return '';
return this.commandCenter.open(resource);
}
drag(resource: Resource, resourceGroup: ResourceGroup): void {