From dc26d90ebcd68a044b31ce82555f2520cfff38e9 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 18 Feb 2017 02:08:43 -0500 Subject: [PATCH] Adds Open Files command to commit files quick pick Adds Open File command to commit quick pick --- src/commands/quickPickItems.ts | 88 ++++++++++++++++++++++++++++++++++++++++-- src/commands/quickPicks.ts | 21 +++++++--- src/constants.ts | 3 +- 3 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/commands/quickPickItems.ts b/src/commands/quickPickItems.ts index 0562eec..63ec49a 100644 --- a/src/commands/quickPickItems.ts +++ b/src/commands/quickPickItems.ts @@ -1,6 +1,6 @@ 'use strict'; -import { commands, QuickPickItem, Uri } from 'vscode'; -import { Commands } from '../constants'; +import { commands, QuickPickItem, TextEditor, Uri, window, workspace } from 'vscode'; +import { BuiltInCommands, Commands } from '../constants'; import { GitCommit, GitUri } from '../gitProvider'; import * as moment from 'moment'; import * as path from 'path'; @@ -10,15 +10,70 @@ export class CommandQuickPickItem implements QuickPickItem { description: string; detail: string; - constructor(item: QuickPickItem, public command: Commands, public args?: any[]) { + constructor(item: QuickPickItem, protected command: Commands, protected args?: any[]) { Object.assign(this, item); } - execute() { + execute(): Thenable<{}> { return commands.executeCommand(this.command, ...(this.args || [])); } } +export class OpenFilesCommandQuickPickItem extends CommandQuickPickItem { + label: string; + description: string; + detail: string; + + constructor(private commit: GitCommit, private fileNames?: string[]) { + super({ + label: `$(file-symlink-file) Open Files`, + description: `\u00a0 \u2014 \u00a0\u00a0 $(file-text) ${commit.fileName}`, + detail: `Opens all the files in commit $(git-commit) ${commit.sha}` + }, undefined, undefined); + + if (!this.fileNames) { + this.fileNames = commit.fileName.split(', ').filter(_ => !!_); + } + } + + async execute(): Promise<{}> { + const repoPath = this.commit.repoPath; + for (const file of this.fileNames) { + try { + const uri = Uri.file(path.resolve(repoPath, file)); + const document = await workspace.openTextDocument(uri); + await window.showTextDocument(document, 1, true); + } + catch (ex) { } + } + return undefined; + } +} + +export class OpenFileCommandQuickPickItem extends CommandQuickPickItem { + label: string; + description: string; + detail: string; + + constructor(private commit: GitCommit) { + super({ + label: `$(file-symlink-file) Open File`, + description: `\u00a0 \u2014 \u00a0\u00a0 $(file-text) ${commit.fileName}` + }, undefined, undefined); + } + + async execute(): Promise<{}> { + const repoPath = this.commit.repoPath; + try { + const file = path.resolve(repoPath, this.commit.fileName); + return await commands.executeCommand(BuiltInCommands.Open, Uri.file(file)); + } + catch (ex) { + return undefined; + } + } +} + export class CommitQuickPickItem implements QuickPickItem { label: string; @@ -47,4 +102,29 @@ export class FileQuickPickItem implements QuickPickItem { this.sha = commit.sha; this.uri = GitUri.fromUri(Uri.file(path.resolve(commit.repoPath, fileName))); } + + async open(): Promise { + let document = workspace.textDocuments.find(_ => _.fileName === this.uri.fsPath); + const existing = !!document; + try { + if (!document) { + document = await workspace.openTextDocument(this.uri); + } + + const editor = await window.showTextDocument(document, 1, true); + return existing ? undefined : editor; + } + catch (ex) { + return undefined; + } + } + + async preview(): Promise<{}> { + try { + return await commands.executeCommand(BuiltInCommands.Open, this.uri); + } + catch (ex) { + return undefined; + } + } } \ No newline at end of file diff --git a/src/commands/quickPicks.ts b/src/commands/quickPicks.ts index 4b5c27a..f91668f 100644 --- a/src/commands/quickPicks.ts +++ b/src/commands/quickPicks.ts @@ -4,7 +4,7 @@ import { QuickPickOptions, Uri, window, workspace } from 'vscode'; import { IAdvancedConfig } from '../configuration'; import { Commands } from '../constants'; import { GitCommit, GitUri, IGitLog } from '../gitProvider'; -import { CommandQuickPickItem, CommitQuickPickItem, FileQuickPickItem } from './quickPickItems'; +import { CommandQuickPickItem, CommitQuickPickItem, FileQuickPickItem, OpenFileCommandQuickPickItem, OpenFilesCommandQuickPickItem } from './quickPickItems'; import * as moment from 'moment'; import * as path from 'path'; @@ -29,6 +29,8 @@ export class CommitQuickPick { description: `\u00a0 \u2014 \u00a0\u00a0 $(git-commit) ${commit.sha} \u00a0 $(git-compare) \u00a0 $(file-text) ${workingFileName || commit.fileName}` }, Commands.DiffWithWorking, [uri, commit])); + items.push(new OpenFileCommandQuickPickItem(commit)); + items.push(new CommandQuickPickItem({ label: `$(clippy) Copy Commit Sha to Clipboard`, description: `\u00a0 \u2014 \u00a0\u00a0 $(git-commit) ${commit.sha}` @@ -78,21 +80,28 @@ export class CommitQuickPick { export class CommitFilesQuickPick { static async show(commit: GitCommit, uri: Uri, goBackCommand?: CommandQuickPickItem): Promise { - const items: (FileQuickPickItem | CommandQuickPickItem)[] = commit.fileName - .split(', ') - .filter(_ => !!_) - .map(f => new FileQuickPickItem(commit, f)); + const fileNames = commit.fileName.split(', ').filter(_ => !!_); + const items: (FileQuickPickItem | CommandQuickPickItem)[] = fileNames.map(f => new FileQuickPickItem(commit, f)); + + items.splice(0, 0, new OpenFilesCommandQuickPickItem(commit, fileNames)); if (goBackCommand) { items.splice(0, 0, goBackCommand); } - return await window.showQuickPick(items, { + const result = await window.showQuickPick(items, { matchOnDescription: true, matchOnDetail: true, placeHolder: `${commit.sha} \u2022 ${commit.author}, ${moment(commit.date).fromNow()} \u2022 ${commit.message}`, ignoreFocusOut: getQuickPickIgnoreFocusOut() + // onDidSelectItem: (item: QuickPickItem) => { + // if (item instanceof FileQuickPickItem) { + // item.preview(); + // } + // } } as QuickPickOptions); + + return result; } } diff --git a/src/constants.ts b/src/constants.ts index d27dc06..0693f5f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,13 +2,14 @@ export const RepoPath = 'repoPath'; -export type BuiltInCommands = 'cursorMove' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace' | 'editorScroll' | 'revealLine' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'setContext'; +export type BuiltInCommands = 'cursorMove' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace' | 'editorScroll' | 'revealLine' | 'setContext' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'vscode.open'; export const BuiltInCommands = { CursorMove: 'cursorMove' as BuiltInCommands, Diff: 'vscode.diff' as BuiltInCommands, EditorScroll: 'editorScroll' as BuiltInCommands, ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as BuiltInCommands, ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as BuiltInCommands, + Open: 'vscode.open' as BuiltInCommands, RevealLine: 'revealLine' as BuiltInCommands, SetContext: 'setContext' as BuiltInCommands, ShowReferences: 'editor.action.showReferences' as BuiltInCommands,