gitActions.contribution.ts 23.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import nls = require('vs/nls');
import lifecycle = require('vs/base/common/lifecycle');
import platform = require('vs/platform/platform');
import abr = require('vs/workbench/browser/actionBarRegistry');
11
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
12 13 14 15 16 17 18 19 20 21
import editorbrowser = require('vs/editor/browser/editorBrowser');
import editorcommon = require('vs/editor/common/editorCommon');
import baseeditor = require('vs/workbench/browser/parts/editor/baseEditor');
import WorkbenchEditorCommon = require('vs/workbench/common/editor');
import tdeditor = require('vs/workbench/browser/parts/editor/textDiffEditor');
import teditor = require('vs/workbench/browser/parts/editor/textEditor');
import filesCommon = require('vs/workbench/parts/files/common/files');
import gitcontrib = require('vs/workbench/parts/git/browser/gitWorkbenchContributions');
import { IGitService, Status, IFileStatus, StatusType } from 'vs/workbench/parts/git/common/git';
import gitei = require('vs/workbench/parts/git/browser/gitEditorInputs');
J
Joao Moreno 已提交
22
import { getSelectedChanges, applyChangesToModel } from 'vs/workbench/parts/git/common/stageRanges';
E
Erich Gamma 已提交
23 24 25
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IPartService, Parts} from 'vs/workbench/services/part/common/partService';
26
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
J
Joao Moreno 已提交
27
import {IFileService} from 'vs/platform/files/common/files';
E
Erich Gamma 已提交
28
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
29
import wbar = require('vs/workbench/common/actionRegistry');
E
Erich Gamma 已提交
30
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
31
import { OpenChangeAction, OpenFileAction, SyncAction, PullAction, PushAction, PublishAction, StartGitBranchAction, StartGitCheckoutAction, InputCommitAction, UndoLastCommitAction, BaseStageAction, BaseUnstageAction } from './gitActions';
J
Joao Moreno 已提交
32 33
import paths = require('vs/base/common/paths');
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
34 35

function getStatus(gitService: IGitService, contextService: IWorkspaceContextService, input: WorkbenchEditorCommon.IFileEditorInput): IFileStatus {
J
Joao Moreno 已提交
36 37 38 39
	const model = gitService.getModel();
	const repositoryRoot = model.getRepositoryRoot();
	const statusModel = model.getStatus();
	const repositoryRelativePath = paths.normalize(paths.relative(repositoryRoot, input.getResource().fsPath));
E
Erich Gamma 已提交
40

J
Joao Moreno 已提交
41 42
	return statusModel.getWorkingTreeStatus().find(repositoryRelativePath) ||
			statusModel.getIndexStatus().find(repositoryRelativePath);
E
Erich Gamma 已提交
43 44 45 46
}

class OpenInDiffAction extends baseeditor.EditorInputAction {

J
Joao Moreno 已提交
47
	static ID = 'workbench.action.git.openInDiff';
E
Erich Gamma 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	static Label = nls.localize('switchToChangesView', "Switch to Changes View");

	private gitService: IGitService;
	private viewletService: IViewletService;
	private editorService: IWorkbenchEditorService;
	private partService: IPartService;
	private contextService: IWorkspaceContextService;
	private toDispose: lifecycle.IDisposable[];

	constructor(@IWorkbenchEditorService editorService: IWorkbenchEditorService, @IGitService gitService: IGitService, @IViewletService viewletService: IViewletService, @IPartService partService: IPartService, @IWorkspaceContextService contextService : IWorkspaceContextService) {
		super(OpenInDiffAction.ID, OpenInDiffAction.Label);

		this.class = 'git-action open-in-diff';
		this.gitService = gitService;
		this.viewletService = viewletService;
		this.editorService = editorService;
		this.partService = partService;
		this.contextService = contextService;

		this.toDispose = [this.gitService.addBulkListener2(() => this.onGitStateChanged())];

		this.enabled = this.isEnabled();
	}

	public isEnabled():boolean {
		if (!super.isEnabled()) {
			return false;
		}

B
Benjamin Pasero 已提交
77 78
		const model = this.gitService.getModel();
		if (!model || !(typeof model.getRepositoryRoot() === 'string')) {
J
Joao Moreno 已提交
79 80 81
			return false;
		}

E
Erich Gamma 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		var status = this.getStatus();

		return status && (
			status.getStatus() === Status.MODIFIED ||
			status.getStatus() === Status.INDEX_MODIFIED ||
			status.getStatus() === Status.INDEX_RENAMED
		);
	}

	private onGitStateChanged():void {
		if (this.gitService.isIdle()) {
			this.enabled = this.isEnabled();
		}
	}

	private getStatus():IFileStatus {
		return getStatus(this.gitService, this.contextService, <filesCommon.FileEditorInput> this.input);
	}

I
isidor 已提交
101 102 103 104 105
	public run(context?: WorkbenchEditorCommon.IEditorContext): TPromise<any> {
		const event = context ? context.event : null;
		const sideBySide = !!(event && (event.ctrlKey || event.metaKey));
		const editor = <editorbrowser.ICodeEditor> this.editorService.getActiveEditor().getControl();
		const viewState = editor ? editor.saveViewState() : null;
E
Erich Gamma 已提交
106 107

		return this.gitService.getInput(this.getStatus()).then((input) => {
A
Alex Dima 已提交
108
			var promise = TPromise.as(null);
E
Erich Gamma 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

			if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
				promise = this.viewletService.openViewlet(gitcontrib.VIEWLET_ID, false);
			}

			return promise.then(() => {
				var options = new WorkbenchEditorCommon.TextDiffEditorOptions();
				options.forceOpen = true;
				options.autoRevealFirstChange = false;

				return this.editorService.openEditor(input, options, sideBySide).then((editor) => {
					if (viewState) {
						var codeEditor = <editorbrowser.ICodeEditor> this.editorService.getActiveEditor().getControl();
						codeEditor.restoreViewState({
							original: { },
							modified: viewState
						});
					}
				});
			});
		});
	}

	public dispose():void {
J
Joao Moreno 已提交
133
		this.toDispose = lifecycle.dispose(this.toDispose);
E
Erich Gamma 已提交
134 135 136 137 138 139
	}
}

class OpenInEditorAction extends baseeditor.EditorInputAction {

	private static DELETED_STATES = [Status.BOTH_DELETED, Status.DELETED, Status.DELETED_BY_US, Status.INDEX_DELETED];
J
Joao Moreno 已提交
140
	static ID = 'workbench.action.git.openInEditor';
E
Erich Gamma 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	static LABEL = nls.localize('openInEditor', "Switch to Editor View");

	private gitService: IGitService;
	private fileService: IFileService;
	private viewletService: IViewletService;
	private editorService: IWorkbenchEditorService;
	private partService: IPartService;
	private contextService: IWorkspaceContextService;

	constructor(@IFileService fileService: IFileService, @IWorkbenchEditorService editorService: IWorkbenchEditorService, @IGitService gitService: IGitService, @IViewletService viewletService: IViewletService, @IPartService partService: IPartService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
		super(OpenInEditorAction.ID, OpenInEditorAction.LABEL);

		this.class = 'git-action open-in-editor';
		this.gitService = gitService;
		this.fileService = fileService;
		this.viewletService = viewletService;
		this.editorService = editorService;
		this.partService = partService;
		this.contextService = contextService;

		this.enabled = this.isEnabled();
	}

	public isEnabled():boolean {
		if (!super.isEnabled()) {
			return false;
		}

B
Benjamin Pasero 已提交
169 170
		const model = this.gitService.getModel();
		if (!model || !(typeof model.getRepositoryRoot() === 'string')) {
J
Joao Moreno 已提交
171 172 173
			return false;
		}

E
Erich Gamma 已提交
174 175 176 177 178 179 180 181
		var status:IFileStatus = (<any>this.input).getFileStatus();
		if (OpenInEditorAction.DELETED_STATES.indexOf(status.getStatus()) > -1) {
			return false;
		}

		return true;
	}

I
isidor 已提交
182
	public run(context?: WorkbenchEditorCommon.IEditorContext): TPromise<any> {
J
Joao Moreno 已提交
183 184
		const model = this.gitService.getModel();
		const resource = URI.file(paths.join(model.getRepositoryRoot(), this.getRepositoryRelativePath()));
I
isidor 已提交
185
		const event = context ? context.event : null;
J
Joao Moreno 已提交
186 187
		const sideBySide = !!(event && (event.ctrlKey || event.metaKey));
		const modifiedViewState = this.saveTextViewState();
E
Erich Gamma 已提交
188

J
Joao Moreno 已提交
189
		return this.fileService.resolveFile(resource).then(stat => {
E
Erich Gamma 已提交
190 191 192 193 194
			return this.editorService.openEditor({
				resource: stat.resource,
				options: {
					forceOpen: true
				}
J
Joao Moreno 已提交
195
			}, sideBySide).then(editor => {
E
Erich Gamma 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
				this.restoreTextViewState(modifiedViewState);

				if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
					return this.viewletService.openViewlet(filesCommon.VIEWLET_ID, false);
				}
			});
		});
	}

	private saveTextViewState():editorcommon.IEditorViewState {
		var textEditor = this.getTextEditor();
		if (textEditor) {
			return textEditor.saveViewState();
		}

		return null;
	}

	private restoreTextViewState(state:editorcommon.IEditorViewState):void {
		var textEditor = this.getTextEditor();
		if (textEditor) {
			return textEditor.restoreViewState(state);
		}
	}

	private getTextEditor(): editorcommon.ICommonCodeEditor {
		var editor = this.editorService.getActiveEditor();

		if (editor instanceof tdeditor.TextDiffEditor) {
			return (<editorbrowser.IDiffEditor>editor.getControl()).getModifiedEditor();
		} else if (editor instanceof teditor.BaseTextEditor) {
			return <editorbrowser.ICodeEditor> editor.getControl();
		}

		return null;
	}

J
Joao Moreno 已提交
233
	private getRepositoryRelativePath():string {
E
Erich Gamma 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		var status: IFileStatus = (<any> this.input).getFileStatus();

		if (status.getStatus() === Status.INDEX_RENAMED) {
			return status.getRename();
		} else {
			var indexStatus = this.gitService.getModel().getStatus().find(status.getPath(), StatusType.INDEX);

			if (indexStatus && indexStatus.getStatus() === Status.INDEX_RENAMED) {
				return indexStatus.getRename();
			} else {
				return status.getPath();
			}
		}
	}
}

250
export class WorkbenchStageAction extends BaseStageAction {
251

252 253
	static ID = 'workbench.action.git.stage';
	static LABEL = nls.localize('workbenchStage', "Stage");
254 255 256 257 258 259 260 261 262
	private contextService: IWorkspaceContextService;

	constructor(
		id = WorkbenchStageAction.ID,
		label = WorkbenchStageAction.LABEL,
		@IGitService gitService: IGitService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
263
		super(id, label, '', gitService, editorService);
264 265 266 267 268 269 270 271 272 273 274 275
		this.contextService = contextService;
		this.onGitServiceChange();
	}

	protected updateEnablement(): void {
		if (this.contextService) {
			this.enabled = this.isEnabled();
		} else {
			this.enabled = super.isEnabled();
		}
	}

276
	isEnabled():boolean {
277 278 279
		if (!super.isEnabled()) {
			return false;
		}
280 281 282

		const editor = this.editorService.getActiveEditor();
		if (!editor || !(editor instanceof baseeditor.BaseEditor)) {
283 284
			return false;
		}
285 286

		return true;
287 288
	}

289 290 291 292
	run(context?: any): TPromise<void> {
		const input = this.editorService.getActiveEditor().input;
		let fileStatus: IFileStatus;

293
		if (gitei.isGitEditorInput(input)) {
294 295 296 297 298 299 300 301
			const gitInput = input as gitei.GitDiffEditorInput;
			fileStatus = gitInput.getFileStatus();
		} else {
			fileStatus = getStatus(this.gitService, this.contextService, input as WorkbenchEditorCommon.IFileEditorInput);
		}

		if (!fileStatus) {
			return TPromise.as<void>(null);
302
		}
303 304

		return super.run(fileStatus);
305 306 307
	}
}

308
export class WorkbenchUnstageAction extends BaseUnstageAction {
309

310 311
	static ID = 'workbench.action.git.unstage';
	static LABEL = nls.localize('workbenchUnstage', "Unstage");
312 313 314 315 316 317 318 319 320
	private contextService: IWorkspaceContextService;

	constructor(
		id = WorkbenchUnstageAction.ID,
		label = WorkbenchUnstageAction.LABEL,
		@IGitService gitService: IGitService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
321
		super(id, label, '', gitService, editorService);
322 323 324 325 326 327 328 329 330 331 332 333
		this.contextService = contextService;
		this.onGitServiceChange();
	}

	protected updateEnablement(): void {
		if (this.contextService) {
			this.enabled = this.isEnabled();
		} else {
			this.enabled = super.isEnabled();
		}
	}

334
	isEnabled():boolean {
335 336 337
		if (!super.isEnabled()) {
			return false;
		}
338 339 340

		const editor = this.editorService.getActiveEditor();
		if (!editor || !(editor instanceof baseeditor.BaseEditor)) {
341 342
			return false;
		}
343 344

		return true;
345 346
	}

347 348
	run(context?: any): TPromise<void> {
		const input = this.editorService.getActiveEditor().input;
349
		let fileStatus: IFileStatus;
350

351
		if (gitei.isGitEditorInput(input)) {
352 353
			const gitInput = input as gitei.GitDiffEditorInput;
			fileStatus = gitInput.getFileStatus();
354
		} else {
355
			fileStatus = getStatus(this.gitService, this.contextService, input as WorkbenchEditorCommon.IFileEditorInput);
356
		}
357 358 359 360 361 362

		if (!fileStatus) {
			return TPromise.as<void>(null);
		}

		return super.run(fileStatus);
363 364 365
	}
}

R
rebornix 已提交
366
export abstract class BaseStageRangesAction extends baseeditor.EditorInputAction {
E
Erich Gamma 已提交
367 368 369 370
	private gitService: IGitService;
	private editorService: IWorkbenchEditorService;
	private editor:editorbrowser.IDiffEditor;

R
rebornix 已提交
371 372
	constructor(id: string, label: string, editor:tdeditor.TextDiffEditor, @IGitService gitService: IGitService, @IWorkbenchEditorService editorService : IWorkbenchEditorService) {
		super(id, label);
E
Erich Gamma 已提交
373 374 375 376

		this.editorService = editorService;
		this.gitService = gitService;
		this.editor = editor.getControl();
A
Alex Dima 已提交
377
		this.editor.onDidChangeCursorSelection(() => this.updateEnablement());
A
Alex Dima 已提交
378
		this.editor.onDidUpdateDiff(() => this.updateEnablement());
E
Erich Gamma 已提交
379 380 381 382 383 384 385 386 387 388 389
		this.class = 'git-action stage-ranges';
	}

	public isEnabled():boolean {
		if (!super.isEnabled()) {
			return false;
		}

		if (!this.gitService || !this.editorService) {
			return false;
		}
390 391 392 393 394 395 396 397

		var changes = this.editor.getLineChanges();
		var selections = this.editor.getSelections();

		if (!changes || !selections || selections.length === 0) {
			return false;
		}

J
Joao Moreno 已提交
398
		return getSelectedChanges(changes, selections).length > 0;
E
Erich Gamma 已提交
399 400
	}

R
rebornix 已提交
401
	protected getRangesAppliedResult(editor: editorbrowser.IDiffEditor) {
J
Joao Moreno 已提交
402 403 404
		var selections = editor.getSelections();
		var changes = getSelectedChanges(editor.getLineChanges(), selections);
		return applyChangesToModel(editor.getModel().original, editor.getModel().modified, changes);
R
rebornix 已提交
405 406
	}

A
Alex Dima 已提交
407
	public run():TPromise<any> {
R
rebornix 已提交
408
		var result = this.getRangesAppliedResult(this.editor);
E
Erich Gamma 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

		var status = (<gitei.GitWorkingTreeDiffEditorInput>this.input).getFileStatus();
		var path = status.getPath();
		var viewState = this.editor.saveViewState();

		return this.gitService.stage(status.getPath(), result).then(() => {
			var statusModel = this.gitService.getModel().getStatus();

			status = statusModel.getWorkingTreeStatus().find(path) || statusModel.getIndexStatus().find(path);

			if (status) {
				return this.gitService.getInput(status).then((input) => {
					var options = new WorkbenchEditorCommon.TextDiffEditorOptions();
					options.forceOpen = true;
					options.autoRevealFirstChange = false;

					return this.editorService.openEditor(input, options, this.position).then(() => {
						this.editor.restoreViewState(viewState);
					});
				});
			}
		});
	}

	private updateEnablement():void {
		this.enabled = this.isEnabled();
	}
}

R
rebornix 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
export class StageRangesAction extends BaseStageRangesAction {
	static ID = 'workbench.action.git.stageRanges';
	static LABEL = nls.localize('stageSelectedLines', "Stage Selected Lines");

	constructor(editor:tdeditor.TextDiffEditor, @IGitService gitService: IGitService, @IWorkbenchEditorService editorService : IWorkbenchEditorService) {
		super(StageRangesAction.ID, StageRangesAction.LABEL, editor, gitService, editorService);
	}
}

export class UnstageRangesAction extends BaseStageRangesAction {
	static ID = 'workbench.action.git.unstageRanges';
	static LABEL = nls.localize('unstageSelectedLines', "Unstage Selected Lines");

	constructor(editor:tdeditor.TextDiffEditor, @IGitService gitService: IGitService, @IWorkbenchEditorService editorService : IWorkbenchEditorService) {
		super(UnstageRangesAction.ID, UnstageRangesAction.LABEL, editor, gitService, editorService);
	}

	protected getRangesAppliedResult(editor: editorbrowser.IDiffEditor) {
J
Joao Moreno 已提交
456 457 458 459 460 461 462 463 464 465
	const selections = editor.getSelections();
	const changes = getSelectedChanges(editor.getLineChanges(), selections)
		.map(c => ({
			modifiedStartLineNumber: c.originalStartLineNumber,
			modifiedEndLineNumber: c.originalEndLineNumber,
			originalStartLineNumber: c.modifiedStartLineNumber,
			originalEndLineNumber: c.modifiedEndLineNumber
		}));

	return applyChangesToModel(editor.getModel().modified, editor.getModel().original, changes);
R
rebornix 已提交
466 467 468
	}
}

E
Erich Gamma 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
class FileEditorActionContributor extends baseeditor.EditorInputActionContributor {
	private instantiationService:IInstantiationService;

	constructor(@IInstantiationService instantiationService: IInstantiationService) {
		super();

		this.instantiationService = instantiationService;
	}

	public hasActionsForEditorInput(context:baseeditor.IEditorInputActionContext):boolean {
		return context.input instanceof filesCommon.FileEditorInput;
	}

	public getActionsForEditorInput(context:baseeditor.IEditorInputActionContext):baseeditor.IEditorInputAction[] {
		return [ this.instantiationService.createInstance(OpenInDiffAction) ];
	}
}

class GitEditorActionContributor extends baseeditor.EditorInputActionContributor {
	private instantiationService:IInstantiationService;

	constructor(@IInstantiationService instantiationService: IInstantiationService) {
		super();

		this.instantiationService = instantiationService;
	}

	public hasActionsForEditorInput(context:baseeditor.IEditorInputActionContext):boolean {
		return gitei.isGitEditorInput(context.input);
	}

	public getActionsForEditorInput(context:baseeditor.IEditorInputActionContext):baseeditor.IEditorInputAction[] {
		return [ this.instantiationService.createInstance(OpenInEditorAction) ];
	}
}

class GitWorkingTreeDiffEditorActionContributor extends baseeditor.EditorInputActionContributor {
	private instantiationService:IInstantiationService;

	constructor(@IInstantiationService instantiationService: IInstantiationService) {
		super();

		this.instantiationService = instantiationService;
	}

	public hasSecondaryActionsForEditorInput(context:baseeditor.IEditorInputActionContext):boolean {
R
rebornix 已提交
515
		return (context.input instanceof gitei.GitDiffEditorInput && context.editor instanceof tdeditor.TextDiffEditor);
E
Erich Gamma 已提交
516 517 518
	}

	public getSecondaryActionsForEditorInput(context:baseeditor.IEditorInputActionContext):baseeditor.IEditorInputAction[] {
R
rebornix 已提交
519 520 521 522
		if (context.input instanceof gitei.GitIndexDiffEditorInput) {
			return [ this.instantiationService.createInstance(UnstageRangesAction, <tdeditor.TextDiffEditor>context.editor) ];
		}

E
Erich Gamma 已提交
523 524 525 526 527 528
		return [ this.instantiationService.createInstance(StageRangesAction, <tdeditor.TextDiffEditor>context.editor) ];
	}
}

class GlobalOpenChangeAction extends OpenChangeAction {

J
Joao Moreno 已提交
529
	static ID = 'workbench.action.git.globalOpenChange';
E
Erich Gamma 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	static LABEL = nls.localize('openChange', "Open Change");

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IGitService gitService: IGitService,
		@IWorkspaceContextService protected contextService: IWorkspaceContextService,
		@IViewletService protected viewletService: IViewletService,
		@IPartService protected partService: IPartService
	) {
		super(editorService, gitService);
	}

	public getInput(): WorkbenchEditorCommon.IFileEditorInput {
		return WorkbenchEditorCommon.asFileEditorInput(this.editorService.getActiveEditorInput());
	}

A
Alex Dima 已提交
548
	public run(context?: any): TPromise<any> {
E
Erich Gamma 已提交
549 550 551
		let input = this.getInput();

		if (!input) {
A
Alex Dima 已提交
552
			return TPromise.as(null);
E
Erich Gamma 已提交
553 554 555 556 557
		}

		let status = getStatus(this.gitService, this.contextService, input);

		if (!status) {
A
Alex Dima 已提交
558
			return TPromise.as(null);
E
Erich Gamma 已提交
559 560 561 562 563 564 565
		}

		var sideBySide = !!(context && (context.ctrlKey || context.metaKey));
		var editor = <editorbrowser.ICodeEditor> this.editorService.getActiveEditor().getControl();
		var viewState = editor ? editor.saveViewState() : null;

		return this.gitService.getInput(status).then((input) => {
A
Alex Dima 已提交
566
			var promise = TPromise.as(null);
E
Erich Gamma 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

			if (this.partService.isVisible(Parts.SIDEBAR_PART)) {
				promise = this.viewletService.openViewlet(gitcontrib.VIEWLET_ID, false);
			}

			return promise.then(() => {
				var options = new WorkbenchEditorCommon.TextDiffEditorOptions();
				options.forceOpen = true;
				options.autoRevealFirstChange = false;

				return this.editorService.openEditor(input, options, sideBySide).then((editor) => {
					if (viewState) {
						var codeEditor = <editorbrowser.ICodeEditor> this.editorService.getActiveEditor().getControl();
						codeEditor.restoreViewState({
							original: { },
							modified: viewState
						});
					}
				});
			});
		});
	}
}

J
Joao Moreno 已提交
591 592
class GlobalOpenInEditorAction extends OpenFileAction {

J
Joao Moreno 已提交
593
	static ID = 'workbench.action.git.globalOpenFile';
J
Joao Moreno 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	static LABEL = nls.localize('openFile', "Open File");

	constructor(
		id = GlobalOpenInEditorAction.ID,
		label = GlobalOpenInEditorAction.LABEL,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IFileService fileService: IFileService,
		@IGitService gitService: IGitService,
		@IWorkspaceContextService contextService: IWorkspaceContextService
	) {
		super(editorService, fileService, gitService, contextService);
	}

	public run(event?: any): TPromise<any> {
		const input = WorkbenchEditorCommon.asFileEditorInput(this.editorService.getActiveEditorInput(), true);

		if (!input) {
			return TPromise.as(null);
		}

		const status = getStatus(this.gitService, this.contextService, input);

		if (!status) {
			return TPromise.as(null);
		}

		return super.run(status);
	}
}

E
Erich Gamma 已提交
624 625 626 627 628 629 630
var actionBarRegistry = <abr.IActionBarRegistry> platform.Registry.as(abr.Extensions.Actionbar);
actionBarRegistry.registerActionBarContributor(abr.Scope.EDITOR, FileEditorActionContributor);
actionBarRegistry.registerActionBarContributor(abr.Scope.EDITOR, GitEditorActionContributor);
actionBarRegistry.registerActionBarContributor(abr.Scope.EDITOR, GitWorkingTreeDiffEditorActionContributor);

let workbenchActionRegistry = (<wbar.IWorkbenchActionRegistry> platform.Registry.as(wbar.Extensions.WorkbenchActions));

631 632
// Register Actions
const category = nls.localize('git', "Git");
633 634 635 636 637 638 639 640 641 642
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(GlobalOpenChangeAction, GlobalOpenChangeAction.ID, GlobalOpenChangeAction.LABEL), 'Git: Open Change', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(GlobalOpenInEditorAction, GlobalOpenInEditorAction.ID, GlobalOpenInEditorAction.LABEL), 'Git: Open File', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(PullAction, PullAction.ID, PullAction.LABEL), 'Git: Pull', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(PushAction, PushAction.ID, PushAction.LABEL), 'Git: Push', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(SyncAction, SyncAction.ID, SyncAction.LABEL), 'Git: Sync', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(PublishAction, PublishAction.ID, PublishAction.LABEL), 'Git: Publish', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(StartGitBranchAction, StartGitBranchAction.ID, StartGitBranchAction.LABEL), 'Git: Branch', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(StartGitCheckoutAction, StartGitCheckoutAction.ID, StartGitCheckoutAction.LABEL), 'Git: Checkout', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(InputCommitAction, InputCommitAction.ID, InputCommitAction.LABEL), 'Git: Commit', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(UndoLastCommitAction, UndoLastCommitAction.ID, UndoLastCommitAction.LABEL), 'Git: Undo Last Commit', category);
643 644
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(WorkbenchStageAction, WorkbenchStageAction.ID, WorkbenchStageAction.LABEL), 'Git: Stage', category);
workbenchActionRegistry.registerWorkbenchAction(new SyncActionDescriptor(WorkbenchUnstageAction, WorkbenchUnstageAction.ID, WorkbenchUnstageAction.LABEL), 'Git: Unstage', category);