Reworks upstream status info in quick pick

This commit is contained in:
Eric Amodio
2017-03-22 01:32:37 -04:00
parent dd0762152f
commit 671f1ca6c1

View File

@@ -1,168 +1,175 @@
'use strict'; 'use strict';
import { Iterables } from '../system'; import { Iterables } from '../system';
import { QuickPickItem, QuickPickOptions, Uri, window } from 'vscode'; import { QuickPickItem, QuickPickOptions, Uri, window } from 'vscode';
import { Commands, Keyboard } from '../commands'; import { Commands, Keyboard } from '../commands';
import { Git, GitStatusFile, GitUri, IGitStatus } from '../gitService'; import { Git, GitStatusFile, GitUri, IGitStatus } from '../gitService';
import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, OpenFileCommandQuickPickItem } from './quickPicks'; import { CommandQuickPickItem, getQuickPickIgnoreFocusOut, OpenFileCommandQuickPickItem } from './quickPicks';
import * as path from 'path'; import * as path from 'path';
export class OpenStatusFileCommandQuickPickItem extends OpenFileCommandQuickPickItem { export class OpenStatusFileCommandQuickPickItem extends OpenFileCommandQuickPickItem {
constructor(status: GitStatusFile, item?: QuickPickItem) { constructor(status: GitStatusFile, item?: QuickPickItem) {
const uri = Uri.file(path.resolve(status.repoPath, status.fileName)); const uri = Uri.file(path.resolve(status.repoPath, status.fileName));
const icon = status.getIcon(); const icon = status.getIcon();
let directory = Git.normalizePath(path.dirname(status.fileName)); let directory = Git.normalizePath(path.dirname(status.fileName));
if (!directory || directory === '.') { if (!directory || directory === '.') {
directory = undefined; directory = undefined;
} }
super(uri, item || { super(uri, item || {
label: `${status.staged ? '$(check)' : '\u00a0\u00a0\u00a0'}\u00a0\u00a0${icon}\u00a0\u00a0\u00a0${path.basename(status.fileName)}`, label: `${status.staged ? '$(check)' : '\u00a0\u00a0\u00a0'}\u00a0\u00a0${icon}\u00a0\u00a0\u00a0${path.basename(status.fileName)}`,
description: directory description: directory
}); });
} }
} }
export class OpenStatusFilesCommandQuickPickItem extends CommandQuickPickItem { export class OpenStatusFilesCommandQuickPickItem extends CommandQuickPickItem {
constructor(statuses: GitStatusFile[], item?: QuickPickItem) { constructor(statuses: GitStatusFile[], item?: QuickPickItem) {
const repoPath = statuses.length && statuses[0].repoPath; const repoPath = statuses.length && statuses[0].repoPath;
const uris = statuses.map(_ => Uri.file(path.resolve(repoPath, _.fileName))); const uris = statuses.map(_ => Uri.file(path.resolve(repoPath, _.fileName)));
super(item || { super(item || {
label: `$(file-symlink-file) Open Changed Files`, label: `$(file-symlink-file) Open Changed Files`,
description: undefined description: undefined
//detail: `Opens all of the changed files in the repository` //detail: `Opens all of the changed files in the repository`
}, Commands.OpenChangedFiles, [undefined, uris]); }, Commands.OpenChangedFiles, [undefined, uris]);
} }
} }
export class RepoStatusQuickPick { export class RepoStatusQuickPick {
static async show(status: IGitStatus, goBackCommand?: CommandQuickPickItem): Promise<OpenStatusFileCommandQuickPickItem | OpenStatusFilesCommandQuickPickItem | CommandQuickPickItem | undefined> { static async show(status: IGitStatus, goBackCommand?: CommandQuickPickItem): Promise<OpenStatusFileCommandQuickPickItem | OpenStatusFilesCommandQuickPickItem | CommandQuickPickItem | undefined> {
// Sort the status by staged and then filename // Sort the status by staged and then filename
const files = status.files; const files = status.files;
files.sort((a, b) => (a.staged ? -1 : 1) - (b.staged ? -1 : 1) || a.fileName.localeCompare(b.fileName)); files.sort((a, b) => (a.staged ? -1 : 1) - (b.staged ? -1 : 1) || a.fileName.localeCompare(b.fileName));
const added = files.filter(_ => _.status === 'A' || _.status === '?'); const added = files.filter(_ => _.status === 'A' || _.status === '?');
const deleted = files.filter(_ => _.status === 'D'); const deleted = files.filter(_ => _.status === 'D');
const changed = files.filter(_ => _.status !== 'A' && _.status !== '?' && _.status !== 'D'); const changed = files.filter(_ => _.status !== 'A' && _.status !== '?' && _.status !== 'D');
const hasStaged = files.some(_ => _.staged); const hasStaged = files.some(_ => _.staged);
let stagedStatus = ''; let stagedStatus = '';
let unstagedStatus = ''; let unstagedStatus = '';
if (hasStaged) { if (hasStaged) {
const stagedAdded = added.filter(_ => _.staged).length; const stagedAdded = added.filter(_ => _.staged).length;
const stagedChanged = changed.filter(_ => _.staged).length; const stagedChanged = changed.filter(_ => _.staged).length;
const stagedDeleted = deleted.filter(_ => _.staged).length; const stagedDeleted = deleted.filter(_ => _.staged).length;
stagedStatus = `+${stagedAdded} ~${stagedChanged} -${stagedDeleted}`; stagedStatus = `+${stagedAdded} ~${stagedChanged} -${stagedDeleted}`;
unstagedStatus = `+${added.length - stagedAdded} ~${changed.length - stagedChanged} -${deleted.length - stagedDeleted}`; unstagedStatus = `+${added.length - stagedAdded} ~${changed.length - stagedChanged} -${deleted.length - stagedDeleted}`;
} }
else { else {
unstagedStatus = `+${added.length} ~${changed.length} -${deleted.length}`; unstagedStatus = `+${added.length} ~${changed.length} -${deleted.length}`;
} }
const items = Array.from(Iterables.map(files, s => new OpenStatusFileCommandQuickPickItem(s))) as (OpenStatusFileCommandQuickPickItem | OpenStatusFilesCommandQuickPickItem | CommandQuickPickItem)[]; const items = Array.from(Iterables.map(files, s => new OpenStatusFileCommandQuickPickItem(s))) as (OpenStatusFileCommandQuickPickItem | OpenStatusFilesCommandQuickPickItem | CommandQuickPickItem)[];
if (hasStaged) { if (hasStaged) {
let index = 0; let index = 0;
const unstagedIndex = files.findIndex(_ => !_.staged); const unstagedIndex = files.findIndex(_ => !_.staged);
if (unstagedIndex > -1) { if (unstagedIndex > -1) {
items.splice(unstagedIndex, 0, new CommandQuickPickItem({ items.splice(unstagedIndex, 0, new CommandQuickPickItem({
label: `Unstaged Files`, label: `Unstaged Files`,
description: unstagedStatus description: unstagedStatus
}, Commands.ShowQuickRepoStatus, [goBackCommand])); }, Commands.ShowQuickRepoStatus, [goBackCommand]));
items.splice(unstagedIndex, 0, new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D' && _.staged), { items.splice(unstagedIndex, 0, new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D' && _.staged), {
label: `\u00a0\u00a0\u00a0\u00a0 $(file-symlink-file) Open Staged Files`, label: `\u00a0\u00a0\u00a0\u00a0 $(file-symlink-file) Open Staged Files`,
description: undefined description: undefined
})); }));
items.push(new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D' && !_.staged), { items.push(new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D' && !_.staged), {
label: `\u00a0\u00a0\u00a0\u00a0 $(file-symlink-file) Open Unstaged Files`, label: `\u00a0\u00a0\u00a0\u00a0 $(file-symlink-file) Open Unstaged Files`,
description: undefined description: undefined
})); }));
} }
items.splice(index++, 0, new CommandQuickPickItem({ items.splice(index++, 0, new CommandQuickPickItem({
label: `Staged Files`, label: `Staged Files`,
description: stagedStatus description: stagedStatus
}, Commands.ShowQuickRepoStatus, [goBackCommand])); }, Commands.ShowQuickRepoStatus, [goBackCommand]));
} }
else if (files.some(_ => !_.staged)) { else if (files.some(_ => !_.staged)) {
items.splice(0, 0, new CommandQuickPickItem({ items.splice(0, 0, new CommandQuickPickItem({
label: `Unstaged Files`, label: `Unstaged Files`,
description: unstagedStatus description: unstagedStatus
}, Commands.ShowQuickRepoStatus, [goBackCommand])); }, Commands.ShowQuickRepoStatus, [goBackCommand]));
} }
if (files.length) { if (files.length) {
items.splice(0, 0, new CommandQuickPickItem({ items.push(new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D')));
label: '$(x) Close Unchanged Files', items.push(new CommandQuickPickItem({
description: null label: '$(x) Close Unchanged Files',
}, Commands.CloseUnchangedFiles)); description: null
items.splice(0, 0, new OpenStatusFilesCommandQuickPickItem(files.filter(_ => _.status !== 'D'))); }, Commands.CloseUnchangedFiles));
} }
else {
if (status.upstream && status.state.ahead) { items.push(new CommandQuickPickItem({
items.splice(0, 0, new CommandQuickPickItem({ label: `No working changes`,
label: '$(git-commit) Show Commits Ahead', description: null
description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.branch} but not \u00a0$(git-branch) ${status.upstream}` }, Commands.ShowQuickRepoStatus, [goBackCommand]));
}, Commands.ShowQuickRepoHistory, [ }
new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.upstream}..${status.branch}` }),
status.branch, if (status.upstream && status.state.ahead) {
0, items.splice(0, 0, new CommandQuickPickItem({
new CommandQuickPickItem({ label: `$(cloud-upload)\u00a0 ${status.state.ahead} Commits ahead of \u00a0$(git-branch) ${status.upstream}`,
label: `go back \u21A9`, description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.branch} but not \u00a0$(git-branch) ${status.upstream}`
description: `\u00a0 \u2014 \u00a0\u00a0 to \u00a0$(git-branch) ${status.branch} status` }, Commands.ShowQuickRepoHistory, [
}, Commands.ShowQuickRepoStatus) new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.upstream}..${status.branch}` }),
]) status.branch,
); 0,
} new CommandQuickPickItem({
label: `go back \u21A9`,
if (status.upstream && status.state.behind) { description: `\u00a0 \u2014 \u00a0\u00a0 to \u00a0$(git-branch) ${status.branch} status`
items.splice(0, 0, new CommandQuickPickItem({ }, Commands.ShowQuickRepoStatus)
label: '$(git-commit) Show Commits Behind', ])
description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.upstream} but not \u00a0$(git-branch) ${status.branch} (since \u00a0$(git-commit) ${status.sha.substring(0, 8)})` );
}, Commands.ShowQuickRepoHistory, [ }
new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.sha}..${status.upstream}` }),
status.upstream, if (status.upstream && status.state.behind) {
0, items.splice(0, 0, new CommandQuickPickItem({
new CommandQuickPickItem({ label: `$(cloud-download)\u00a0 ${status.state.behind} Commits behind \u00a0$(git-branch) ${status.upstream}`,
label: `go back \u21A9`, description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.upstream} but not \u00a0$(git-branch) ${status.branch} (since \u00a0$(git-commit) ${status.sha.substring(0, 8)})`
description: `\u00a0 \u2014 \u00a0\u00a0 to \u00a0$(git-branch) ${status.branch} status` }, Commands.ShowQuickRepoHistory, [
}, Commands.ShowQuickRepoStatus) new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.sha}..${status.upstream}` }),
]) status.upstream,
); 0,
} new CommandQuickPickItem({
label: `go back \u21A9`,
if (goBackCommand) { description: `\u00a0 \u2014 \u00a0\u00a0 to \u00a0$(git-branch) ${status.branch} status`
items.splice(0, 0, goBackCommand); }, Commands.ShowQuickRepoStatus)
} ])
);
const scope = await Keyboard.instance.beginScope({ left: goBackCommand }); }
let syncStatus = ''; if (status.upstream && !status.state.ahead && !status.state.behind) {
if (status.upstream) { items.splice(0, 0, new CommandQuickPickItem({
syncStatus = status.state.ahead || status.state.behind label: `$(git-branch) ${status.branch} is up-to-date with \u00a0$(git-branch) ${status.upstream}`,
? ` ${status.state.behind}\u2193 ${status.state.ahead}\u2191 ` description: null
: ` \u27F3 `; }, Commands.ShowQuickRepoStatus, [goBackCommand]));
} }
const pick = await window.showQuickPick(items, {
matchOnDescription: true, if (goBackCommand) {
placeHolder: `${syncStatus} ${status.branch}${status.upstream ? ` \u00a0\u2194\u00a0 ${status.upstream}` : ''}`, items.splice(0, 0, goBackCommand);
ignoreFocusOut: getQuickPickIgnoreFocusOut(), }
onDidSelectItem: (item: QuickPickItem) => {
scope.setKeyCommand('right', item); const scope = await Keyboard.instance.beginScope({ left: goBackCommand });
}
} as QuickPickOptions); const pick = await window.showQuickPick(items, {
matchOnDescription: true,
await scope.dispose(); placeHolder: `status of ${status.branch}${status.upstream ? ` \u00a0\u2194\u00a0 ${status.upstream}` : ''}`,
ignoreFocusOut: getQuickPickIgnoreFocusOut(),
return pick; onDidSelectItem: (item: QuickPickItem) => {
} scope.setKeyCommand('right', item);
}
} as QuickPickOptions);
await scope.dispose();
return pick;
}
} }