Browse Source

Refactors explorer commands

main
Eric Amodio 7 years ago
parent
commit
08349376ee
2 changed files with 139 additions and 115 deletions
  1. +132
    -0
      src/views/explorerCommands.ts
  2. +7
    -115
      src/views/gitExplorer.ts

+ 132
- 0
src/views/explorerCommands.ts View File

@ -0,0 +1,132 @@
import { Arrays } from '../system';
import { commands, Disposable, TextDocumentShowOptions, Uri, workspace } from 'vscode';
import { ExtensionKey } from '../constants';
import { GitExplorer, GitExplorerView } from '../views/gitExplorer';
import { configuration, GitExplorerFilesLayout } from '../configuration';
import { CommitFileNode, CommitNode, StashNode } from './explorerNodes';
import { Commands, DiffWithCommandArgs, DiffWithCommandArgsRevision, DiffWithPreviousCommandArgs, DiffWithWorkingCommandArgs, openEditor, OpenFileInRemoteCommandArgs, OpenFileRevisionCommandArgs } from '../commands';
import { GitService, GitUri } from '../gitService';
export class ExplorerCommands extends Disposable {
private _disposable: Disposable | undefined;
constructor(
private explorer: GitExplorer
) {
super(() => this.dispose());
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOn', () => this.explorer.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), true), this);
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOff', () => this.explorer.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), false), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToAuto', () => this.setFilesLayout(GitExplorerFilesLayout.Auto), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToList', () => this.setFilesLayout(GitExplorerFilesLayout.List), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToTree', () => this.setFilesLayout(GitExplorerFilesLayout.Tree), this);
commands.registerCommand('gitlens.gitExplorer.switchToHistoryView', () => this.explorer.switchTo(GitExplorerView.History), this);
commands.registerCommand('gitlens.gitExplorer.switchToRepositoryView', () => this.explorer.switchTo(GitExplorerView.Repository), this);
commands.registerCommand('gitlens.gitExplorer.refresh', this.explorer.refresh, this);
commands.registerCommand('gitlens.gitExplorer.refreshNode', this.explorer.refreshNode, this);
commands.registerCommand('gitlens.gitExplorer.openChanges', this.openChanges, this);
commands.registerCommand('gitlens.gitExplorer.openChangesWithWorking', this.openChangesWithWorking, this);
commands.registerCommand('gitlens.gitExplorer.openFile', this.openFile, this);
commands.registerCommand('gitlens.gitExplorer.openFileRevision', this.openFileRevision, this);
commands.registerCommand('gitlens.gitExplorer.openFileRevisionInRemote', this.openFileRevisionInRemote, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFiles', this.openChangedFiles, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileChanges', this.openChangedFileChanges, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileChangesWithWorking', this.openChangedFileChangesWithWorking, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileRevisions', this.openChangedFileRevisions, this);
commands.registerCommand('gitlens.gitExplorer.applyChanges', this.applyChanges, this);
}
dispose() {
this._disposable && this._disposable.dispose();
}
private async applyChanges(node: CommitNode | StashNode) {
await this.explorer.git.checkoutFile(node.uri);
return this.openFile(node);
}
private openChanges(node: CommitNode | StashNode) {
const command = node.getCommand();
if (command === undefined || command.arguments === undefined) return;
const [uri, args] = command.arguments as [Uri, DiffWithPreviousCommandArgs];
args.showOptions!.preview = false;
return commands.executeCommand(command.command, uri, args);
}
private openChangesWithWorking(node: CommitNode | StashNode) {
const args: DiffWithWorkingCommandArgs = {
commit: node.commit,
showOptions: {
preserveFocus: true,
preview: false
}
};
return commands.executeCommand(Commands.DiffWithWorking, new GitUri(node.commit.uri, node.commit), args);
}
private openFile(node: CommitNode | StashNode) {
return openEditor(node.uri, { preserveFocus: true, preview: false });
}
private openFileRevision(node: CommitNode | StashNode | CommitFileNode, options: OpenFileRevisionCommandArgs = { showOptions: { preserveFocus: true, preview: false } }) {
return openEditor(options.uri || GitService.toGitContentUri(node.uri), options.showOptions || { preserveFocus: true, preview: false });
}
private async openChangedFileChanges(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = node.commit.fileStatuses
.map(s => GitUri.fromFileStatus(s, repoPath));
for (const uri of uris) {
await this.openDiffWith(repoPath,
{ uri: uri, sha: node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedSha },
{ uri: uri, sha: node.commit.sha }, options);
}
}
private async openChangedFileChangesWithWorking(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitUri.fromFileStatus(f, repoPath) : undefined);
for (const uri of uris) {
await this.openDiffWith(repoPath, { uri: uri, sha: node.commit.sha }, { uri: uri, sha: '' }, options);
}
}
private async openChangedFiles(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitUri.fromFileStatus(f, repoPath) : undefined);
for (const uri of uris) {
await openEditor(uri, options);
}
}
private async openChangedFileRevisions(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitService.toGitContentUri(node.commit.sha, f.fileName, node.commit.repoPath, f.originalFileName) : undefined);
for (const uri of uris) {
await openEditor(uri, options);
}
}
private async openDiffWith(repoPath: string, lhs: DiffWithCommandArgsRevision, rhs: DiffWithCommandArgsRevision, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const diffArgs: DiffWithCommandArgs = {
repoPath: repoPath,
lhs: lhs,
rhs: rhs,
showOptions: options
};
return commands.executeCommand(Commands.DiffWith, diffArgs);
}
private async openFileRevisionInRemote(node: CommitNode | StashNode) {
return commands.executeCommand(Commands.OpenFileInRemote, new GitUri(node.commit.uri, node.commit), { range: false } as OpenFileInRemoteCommandArgs);
}
private async setFilesLayout(layout: GitExplorerFilesLayout) {
return workspace.getConfiguration(ExtensionKey).update(configuration.name('gitExplorer')('files')('layout').value, layout, true);
}
}

+ 7
- 115
src/views/gitExplorer.ts View File

@ -1,11 +1,11 @@
'use strict';
import { Arrays, Functions } from '../system';
import { commands, ConfigurationChangeEvent, Disposable, Event, EventEmitter, ExtensionContext, TextDocumentShowOptions, TextEditor, TreeDataProvider, TreeItem, Uri, window, workspace } from 'vscode';
import { Commands, DiffWithCommandArgs, DiffWithCommandArgsRevision, DiffWithPreviousCommandArgs, DiffWithWorkingCommandArgs, openEditor, OpenFileInRemoteCommandArgs } from '../commands';
import { Functions } from '../system';
import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, ExtensionContext, TextDocumentShowOptions, TextEditor, TreeDataProvider, TreeItem, Uri, window } from 'vscode';
import { UriComparer } from '../comparers';
import { configuration, ExtensionKey, GitExplorerFilesLayout, IGitExplorerConfig } from '../configuration';
import { configuration, IGitExplorerConfig } from '../configuration';
import { CommandContext, GlyphChars, setCommandContext, WorkspaceState } from '../constants';
import { BranchHistoryNode, CommitFileNode, CommitNode, ExplorerNode, HistoryNode, MessageNode, RepositoriesNode, RepositoryNode, StashNode } from './explorerNodes';
import { ExplorerCommands } from './explorerCommands';
import { BranchHistoryNode, ExplorerNode, HistoryNode, MessageNode, RepositoriesNode, RepositoryNode } from './explorerNodes';
import { GitChangeEvent, GitChangeReason, GitService, GitUri } from '../gitService';
import { Logger } from '../logger';
@ -56,27 +56,8 @@ export class GitExplorer implements TreeDataProvider {
public readonly context: ExtensionContext,
public readonly git: GitService
) {
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOn', () => this.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), true), this);
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOff', () => this.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), false), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToAuto', () => this.setFilesLayout(GitExplorerFilesLayout.Auto), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToList', () => this.setFilesLayout(GitExplorerFilesLayout.List), this);
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToTree', () => this.setFilesLayout(GitExplorerFilesLayout.Tree), this);
commands.registerCommand('gitlens.gitExplorer.switchToHistoryView', () => this.switchTo(GitExplorerView.History), this);
commands.registerCommand('gitlens.gitExplorer.switchToRepositoryView', () => this.switchTo(GitExplorerView.Repository), this);
commands.registerCommand('gitlens.gitExplorer.refresh', this.refresh, this);
commands.registerCommand('gitlens.gitExplorer.refreshNode', this.refreshNode, this);
commands.registerCommand('gitlens.gitExplorer.openChanges', this.openChanges, this);
commands.registerCommand('gitlens.gitExplorer.openChangesWithWorking', this.openChangesWithWorking, this);
commands.registerCommand('gitlens.gitExplorer.openFile', this.openFile, this);
commands.registerCommand('gitlens.gitExplorer.openFileRevision', this.openFileRevision, this);
commands.registerCommand('gitlens.gitExplorer.openFileRevisionInRemote', this.openFileRevisionInRemote, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFiles', this.openChangedFiles, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileChanges', this.openChangedFileChanges, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileChangesWithWorking', this.openChangedFileChangesWithWorking, this);
commands.registerCommand('gitlens.gitExplorer.openChangedFileRevisions', this.openChangedFileRevisions, this);
commands.registerCommand('gitlens.gitExplorer.applyChanges', this.applyChanges, this);
context.subscriptions.push(
new ExplorerCommands(this),
window.onDidChangeActiveTextEditor(Functions.debounce(this.onActiveEditorChanged, 500), this),
window.onDidChangeVisibleTextEditors(Functions.debounce(this.onVisibleEditorsChanged, 500), this),
configuration.onDidChange(this.onConfigurationChanged, this)
@ -302,94 +283,9 @@ export class GitExplorer implements TreeDataProvider {
this.reset(view, true);
}
private async applyChanges(node: CommitNode | StashNode) {
await this.git.checkoutFile(node.uri);
return this.openFile(node);
}
private openChanges(node: CommitNode | StashNode) {
const command = node.getCommand();
if (command === undefined || command.arguments === undefined) return;
const [uri, args] = command.arguments as [Uri, DiffWithPreviousCommandArgs];
args.showOptions!.preview = false;
return commands.executeCommand(command.command, uri, args);
}
private openChangesWithWorking(node: CommitNode | StashNode) {
const args: DiffWithWorkingCommandArgs = {
commit: node.commit,
showOptions: {
preserveFocus: true,
preview: false
}
};
return commands.executeCommand(Commands.DiffWithWorking, new GitUri(node.commit.uri, node.commit), args);
}
private openFile(node: CommitNode | StashNode) {
return openEditor(node.uri, { preserveFocus: true, preview: false });
}
private openFileRevision(node: CommitNode | StashNode | CommitFileNode, options: OpenFileRevisionCommandArgs = { showOptions: { preserveFocus: true, preview: false } }) {
return openEditor(options.uri || GitService.toGitContentUri(node.uri), options.showOptions || { preserveFocus: true, preview: false });
}
private async openChangedFileChanges(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = node.commit.fileStatuses
.map(s => GitUri.fromFileStatus(s, repoPath));
for (const uri of uris) {
await this.openDiffWith(repoPath,
{ uri: uri, sha: node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedSha },
{ uri: uri, sha: node.commit.sha }, options);
}
}
private async openChangedFileChangesWithWorking(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitUri.fromFileStatus(f, repoPath) : undefined);
for (const uri of uris) {
await this.openDiffWith(repoPath, { uri: uri, sha: node.commit.sha }, { uri: uri, sha: '' }, options);
}
}
private async openChangedFiles(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitUri.fromFileStatus(f, repoPath) : undefined);
for (const uri of uris) {
await openEditor(uri, options);
}
}
private async openChangedFileRevisions(node: CommitNode | StashNode, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const uris = Arrays.filterMap(node.commit.fileStatuses,
f => f.status !== 'D' ? GitService.toGitContentUri(node.commit.sha, f.fileName, node.commit.repoPath, f.originalFileName) : undefined);
for (const uri of uris) {
await openEditor(uri, options);
}
}
private async openDiffWith(repoPath: string, lhs: DiffWithCommandArgsRevision, rhs: DiffWithCommandArgsRevision, options: TextDocumentShowOptions = { preserveFocus: false, preview: false }) {
const diffArgs: DiffWithCommandArgs = {
repoPath: repoPath,
lhs: lhs,
rhs: rhs,
showOptions: options
};
return commands.executeCommand(Commands.DiffWith, diffArgs);
}
private async openFileRevisionInRemote(node: CommitNode | StashNode) {
return commands.executeCommand(Commands.OpenFileInRemote, new GitUri(node.commit.uri, node.commit), { range: false } as OpenFileInRemoteCommandArgs);
}
private _autoRefreshDisposable: Disposable | undefined;
private async setAutoRefresh(enabled: boolean, workspaceEnabled?: boolean) {
async setAutoRefresh(enabled: boolean, workspaceEnabled?: boolean) {
if (this._autoRefreshDisposable !== undefined) {
this._autoRefreshDisposable.dispose();
this._autoRefreshDisposable = undefined;
@ -419,8 +315,4 @@ export class GitExplorer implements TreeDataProvider {
this.refresh(RefreshReason.AutoRefreshChanged);
}
}
private async setFilesLayout(layout: GitExplorerFilesLayout) {
return workspace.getConfiguration(ExtensionKey).update(configuration.name('gitExplorer')('files')('layout').value, layout, true);
}
}

Loading…
Cancel
Save