From 1b4800571d280eb6d002bfd1fff9c2250f86f7c5 Mon Sep 17 00:00:00 2001 From: rebornix Date: Tue, 13 Jun 2017 17:17:00 -0700 Subject: [PATCH] extract stash explorer --- package.json | 4 ++++ src/commands/showStashList.ts | 6 +++--- src/extension.ts | 10 +++++++--- src/views/gitStashExplorer.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/views/repositoryNode.ts | 2 -- 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/views/gitStashExplorer.ts diff --git a/package.json b/package.json index db68bf2..2a396a8 100644 --- a/package.json +++ b/package.json @@ -1400,6 +1400,10 @@ { "id": "gitlens-explorer", "name": "GitLens Explorer" + }, + { + "id": "gitstash-explorer", + "name": "Git Stashes" } ] } diff --git a/src/commands/showStashList.ts b/src/commands/showStashList.ts index a46f177..9fb3666 100644 --- a/src/commands/showStashList.ts +++ b/src/commands/showStashList.ts @@ -1,14 +1,14 @@ 'use strict'; import { TextEditor, TextEditorEdit, Uri, window } from 'vscode'; import { Commands, EditorCommand, getCommandUri } from './common'; -import { GitExplorer } from '../views/gitExplorer'; +import { GitStashExplorer } from '../views/gitStashExplorer'; import { GitService, GitUri } from '../gitService'; import { Messages } from '../messages'; import { Logger } from '../logger'; export class ShowStashListCommand extends EditorCommand { - constructor(private git: GitService, private explorer: GitExplorer) { + constructor(private git: GitService, private explorer: GitStashExplorer) { super(Commands.ShowStashList); } @@ -19,7 +19,7 @@ export class ShowStashListCommand extends EditorCommand { const repoPath = await this.git.getRepoPathFromUri(uri); if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to show stashed changes`); - this.explorer.addStash(new GitUri(uri, { repoPath: repoPath, fileName: uri!.fsPath })); + this.explorer.update(new GitUri(uri, { repoPath: repoPath, fileName: uri!.fsPath })); return undefined; } catch (ex) { diff --git a/src/extension.ts b/src/extension.ts index 8eedece..e82a655 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,7 @@ import { ApplicationInsightsKey, CommandContext, ExtensionKey, QualifiedExtensio import { CurrentLineController, LineAnnotationType } from './currentLineController'; import { GitContentProvider } from './gitContentProvider'; import { GitExplorer } from './views/gitExplorer'; +import { GitStashExplorer } from './views/gitStashExplorer'; import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider'; import { GitContextTracker, GitService } from './gitService'; import { Keyboard } from './keyboard'; @@ -93,6 +94,12 @@ export async function activate(context: ExtensionContext) { explorer = new GitExplorer(context, git); context.subscriptions.push(window.registerTreeDataProvider('gitlens-explorer', explorer)); } + let stashExplorer; + if (cfg.insiders) { + stashExplorer = new GitStashExplorer(context, git); + context.subscriptions.push(window.registerTreeDataProvider('gitstash-explorer', stashExplorer)); + context.subscriptions.push(new ShowStashListCommand(git, stashExplorer!)); + } context.subscriptions.push(new CloseUnchangedFilesCommand(git)); context.subscriptions.push(new OpenChangedFilesCommand(git)); @@ -128,9 +135,6 @@ export async function activate(context: ExtensionContext) { context.subscriptions.push(new ShowQuickFileHistoryCommand(git)); context.subscriptions.push(new ShowQuickRepoStatusCommand(git)); context.subscriptions.push(new ShowQuickStashListCommand(git)); - if (cfg.insiders) { - context.subscriptions.push(new ShowStashListCommand(git, explorer!)); - } context.subscriptions.push(new StashApplyCommand(git)); context.subscriptions.push(new StashDeleteCommand(git)); context.subscriptions.push(new StashSaveCommand(git)); diff --git a/src/views/gitStashExplorer.ts b/src/views/gitStashExplorer.ts new file mode 100644 index 0000000..fbc68eb --- /dev/null +++ b/src/views/gitStashExplorer.ts @@ -0,0 +1,43 @@ +'use strict'; +import { Event, EventEmitter, ExtensionContext, TreeDataProvider, TreeItem, Uri, window } from 'vscode'; +import { ExplorerNode, StashNode } from './gitExplorerNodes'; +import { GitService, GitUri } from '../gitService'; +import { StashCommitNode } from './stashCommitNode'; + +export * from './gitExplorerNodes'; + +export class GitStashExplorer implements TreeDataProvider { + private _node: ExplorerNode; + private _onDidChangeTreeData = new EventEmitter(); + public get onDidChangeTreeData(): Event { + return this._onDidChangeTreeData.event; + } + + constructor(private context: ExtensionContext, private git: GitService) { + const editor = window.activeTextEditor; + + const uri = (editor !== undefined && editor.document !== undefined) + ? new GitUri(editor.document.uri, { repoPath: git.repoPath, fileName: editor.document.uri.fsPath }) + : new GitUri(Uri.file(git.repoPath), { repoPath: git.repoPath, fileName: git.repoPath }); + + this._node = new StashNode(uri, this.context, this.git); + } + + async getTreeItem(node: ExplorerNode): Promise { + return node.getTreeItem(); + } + + async getChildren(node?: ExplorerNode): Promise { + if (node === undefined) return this._node.getChildren(); + return node.getChildren(); + } + + update(uri: GitUri) { + this._node = new StashNode(uri, this.context, this.git); + this.refresh(); + } + + refresh() { + this._onDidChangeTreeData.fire(); + } +} \ No newline at end of file diff --git a/src/views/repositoryNode.ts b/src/views/repositoryNode.ts index c5655ba..62f4d9b 100644 --- a/src/views/repositoryNode.ts +++ b/src/views/repositoryNode.ts @@ -5,7 +5,6 @@ import { CommitNode } from './commitNode'; import { GlyphChars } from '../constants'; import { ExplorerNode, ResourceType } from './explorerNode'; import { GitBranch, GitService, GitUri } from '../gitService'; -import { StashNode } from './stashNode'; export class RepositoryNode extends ExplorerNode { @@ -19,7 +18,6 @@ export class RepositoryNode extends ExplorerNode { async getChildren(): Promise { return [ new StatusNode(this.uri, this.context, this.git), - new StashNode(this.uri, this.context, this.git), new BranchesNode(this.uri, this.context, this.git) ]; }