提交 fc0f2188 编写于 作者: J Joao Moreno

git: open change/file

上级 01001020
......@@ -7,13 +7,10 @@
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window, workspace, QuickPickItem, OutputChannel } from 'vscode';
import { IRef, RefType } from './git';
import { Model, Resource } from './model';
import { log } from './util';
import { Model, Resource, Status } from './model';
import { decorate } from 'core-decorators';
import * as path from 'path';
type Command = (...args: any[]) => any;
function catchErrors(fn: (...args) => Promise<any>): (...args) => void {
return (...args) => fn.call(this, ...args).catch(async err => {
if (err.gitErrorCode) {
......@@ -124,14 +121,93 @@ export class CommandCenter {
return await this.model.update();
}
openChange(uri: Uri): void {
@decorate(catchErrors)
async openChange(uri: Uri): Promise<void> {
const resource = resolveGitResource(uri);
log('open change', resource);
if (!resource) {
return;
}
return this.open(resource);
}
async open(resource: Resource): Promise<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 });
}
}
openFile(uri: Uri): void {
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 '';
}
@decorate(catchErrors)
async openFile(uri: Uri): Promise<void> {
const resource = resolveGitResource(uri);
log('open file', resource);
if (!resource) {
return;
}
return commands.executeCommand<void>('vscode.open', resource.uri);
}
@decorate(catchErrors)
......
......@@ -55,13 +55,13 @@ async function init(disposables: Disposable[]): Promise<void> {
const repository = git.open(rootPath);
const repositoryRoot = await repository.getRoot();
const model = new Model(repositoryRoot, repository);
const provider = new GitSCMProvider(model);
const outputChannel = window.createOutputChannel('git');
outputChannel.appendLine(`Using git ${info.version} from ${info.path}`);
git.onOutput(str => outputChannel.append(str), null, disposables);
const commandCenter = new CommandCenter(model, outputChannel);
const provider = new GitSCMProvider(model, commandCenter);
const fsWatcher = workspace.createFileSystemWatcher('**');
const onWorkspaceChange = anyEvent(fsWatcher.onDidChange, fsWatcher.onDidCreate, fsWatcher.onDidDelete);
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册