mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-28 09:35:41 -05:00
Adds paging support to repo/file quick picks
Adds keyboard support to page in repo/file quick picks Adds progress indicator for repo/file quick picks Completely reworks keyboard scopes
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
'use strict';
|
||||
import { commands, Disposable, ExtensionContext, QuickPickItem } from 'vscode';
|
||||
import { commands, Disposable, QuickPickItem } from 'vscode';
|
||||
import { CommandContext, setCommandContext } from '../commands';
|
||||
import { CommandQuickPickItem, OpenFileCommandQuickPickItem } from '../quickPicks/quickPicks';
|
||||
import { Logger } from '../logger';
|
||||
|
||||
const keyNoopCommand = Object.create(null) as QuickPickItem;
|
||||
export { keyNoopCommand as KeyNoopCommand };
|
||||
|
||||
export declare type Keys = 'left' | 'right' | ',' | '.';
|
||||
export const keys: Keys[] = [
|
||||
'left',
|
||||
@@ -12,16 +15,72 @@ export const keys: Keys[] = [
|
||||
'.'
|
||||
];
|
||||
|
||||
let scopeCount = 0;
|
||||
let counters = {
|
||||
left: 0,
|
||||
right: 0,
|
||||
',': 0,
|
||||
'.': 0
|
||||
};
|
||||
export declare type KeyMapping = { [id: string]: (QuickPickItem | (() => Promise<QuickPickItem>)) };
|
||||
let mappings: KeyMapping[] = [];
|
||||
|
||||
let _instance: Keyboard;
|
||||
|
||||
export class KeyboardScope extends Disposable {
|
||||
|
||||
constructor(private mapping: KeyMapping) {
|
||||
super(() => this.dispose());
|
||||
|
||||
for (const key in mapping) {
|
||||
mapping[key] = mapping[key] || keyNoopCommand;
|
||||
}
|
||||
}
|
||||
|
||||
async dispose() {
|
||||
const index = mappings.indexOf(this.mapping);
|
||||
Logger.log('KeyboardScope.dispose', mappings.length, index);
|
||||
if (index === (mappings.length - 1)) {
|
||||
mappings.pop();
|
||||
await this.updateKeyCommandsContext(mappings[mappings.length - 1]);
|
||||
}
|
||||
else {
|
||||
mappings.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
async begin() {
|
||||
mappings.push(this.mapping);
|
||||
await this.updateKeyCommandsContext(this.mapping);
|
||||
return this;
|
||||
}
|
||||
|
||||
async clearKeyCommand(key: Keys) {
|
||||
const mapping = mappings[mappings.length - 1];
|
||||
if (mapping !== this.mapping || !mapping[key]) return;
|
||||
|
||||
Logger.log('KeyboardScope.clearKeyCommand', mappings.length, key);
|
||||
mapping[key] = undefined;
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, false);
|
||||
}
|
||||
|
||||
async setKeyCommand(key: Keys, command: QuickPickItem | (() => Promise<QuickPickItem>)) {
|
||||
const mapping = mappings[mappings.length - 1];
|
||||
if (mapping !== this.mapping) return;
|
||||
|
||||
Logger.log('KeyboardScope.setKeyCommand', mappings.length, key, !!mapping[key]);
|
||||
|
||||
if (!mapping[key]) {
|
||||
mapping[key] = command;
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, true);
|
||||
}
|
||||
else {
|
||||
mapping[key] = command;
|
||||
}
|
||||
}
|
||||
|
||||
private async updateKeyCommandsContext(mapping: KeyMapping) {
|
||||
const promises = [];
|
||||
for (const key of keys) {
|
||||
promises.push(setCommandContext(`${CommandContext.Key}:${key}`, !!(mapping && mapping[key])));
|
||||
}
|
||||
await Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
export class Keyboard extends Disposable {
|
||||
|
||||
static get instance(): Keyboard {
|
||||
@@ -30,7 +89,7 @@ export class Keyboard extends Disposable {
|
||||
|
||||
private _disposable: Disposable;
|
||||
|
||||
constructor(private context: ExtensionContext) {
|
||||
constructor() {
|
||||
super(() => this.dispose());
|
||||
|
||||
const subscriptions: Disposable[] = [];
|
||||
@@ -48,62 +107,35 @@ export class Keyboard extends Disposable {
|
||||
this._disposable && this._disposable.dispose();
|
||||
}
|
||||
|
||||
async beginScope(mapping?: KeyMapping): Promise<KeyboardScope> {
|
||||
Logger.log('Keyboard.beginScope', mappings.length);
|
||||
return await new KeyboardScope(mapping ? Object.assign(Object.create(null), mapping) : Object.create(null)).begin();
|
||||
}
|
||||
|
||||
async execute(key: Keys): Promise<{}> {
|
||||
let command = this.context.globalState.get(`gitlens:key:${key}`) as CommandQuickPickItem | (() => Promise<CommandQuickPickItem>);
|
||||
if (typeof command === 'function') {
|
||||
command = await command();
|
||||
}
|
||||
if (!command || !(command instanceof CommandQuickPickItem)) return undefined;
|
||||
if (!mappings.length) return undefined;
|
||||
|
||||
Logger.log('Keyboard.execute', key);
|
||||
try {
|
||||
const mapping = mappings[mappings.length - 1];
|
||||
|
||||
if (command instanceof OpenFileCommandQuickPickItem) {
|
||||
// Have to open this pinned right now, because vscode doesn't have a way to open a unpinned, but unfocused editor
|
||||
return await command.execute(true);
|
||||
}
|
||||
|
||||
return await command.execute();
|
||||
}
|
||||
|
||||
async enterScope(...keyCommands: [Keys, QuickPickItem | (() => Promise<QuickPickItem>)][]) {
|
||||
Logger.log('Keyboard.enterScope', scopeCount);
|
||||
scopeCount++;
|
||||
// await setCommandContext(CommandContext.Key, ++scopeCount);
|
||||
if (keyCommands && Array.isArray(keyCommands) && keyCommands.length) {
|
||||
for (const [key, command] of keyCommands) {
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, ++counters[key]);
|
||||
await this.setKeyCommand(key as Keys, command);
|
||||
let command = mapping[key] as CommandQuickPickItem | (() => Promise<CommandQuickPickItem>);
|
||||
if (typeof command === 'function') {
|
||||
command = await command();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!command || !(command instanceof CommandQuickPickItem)) return undefined;
|
||||
|
||||
async exitScope(clear: boolean = true) {
|
||||
Logger.log('Keyboard.exitScope', scopeCount);
|
||||
if (scopeCount) {
|
||||
scopeCount--;
|
||||
// await setCommandContext(CommandContext.Key, --scopeCount);
|
||||
}
|
||||
if (clear && !scopeCount) {
|
||||
for (const key of keys) {
|
||||
if (counters[key]) {
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, --counters[key]);
|
||||
}
|
||||
await this.clearKeyCommand(key);
|
||||
Logger.log('Keyboard.execute', key);
|
||||
|
||||
if (command instanceof OpenFileCommandQuickPickItem) {
|
||||
// Have to open this pinned right now, because vscode doesn't have a way to open a unpinned, but unfocused editor
|
||||
return await command.execute(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async clearKeyCommand(key: Keys) {
|
||||
Logger.log('Keyboard.clearKeyCommand', key);
|
||||
if (counters[key]) {
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, --counters[key]);
|
||||
return await command.execute();
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('Keyboard.execute', ex);
|
||||
return undefined;
|
||||
}
|
||||
await this.context.globalState.update(`gitlens:key:${key}`, undefined);
|
||||
}
|
||||
|
||||
async setKeyCommand(key: Keys, command: QuickPickItem | (() => Promise<QuickPickItem>)) {
|
||||
Logger.log('Keyboard.setKeyCommand', key);
|
||||
await setCommandContext(`${CommandContext.Key}:${key}`, ++counters[key]);
|
||||
await this.context.globalState.update(`gitlens:key:${key}`, command);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
'use strict';
|
||||
import { commands, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand, Commands } from './commands';
|
||||
import { ActiveEditorCommand, Commands } from '../commands';
|
||||
import { GitProvider, GitUri, IGitLog } from '../gitProvider';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, FileHistoryQuickPick, showQuickPickProgress } from '../quickPicks';
|
||||
import { CommandQuickPickItem, FileHistoryQuickPick } from '../quickPicks';
|
||||
import * as path from 'path';
|
||||
|
||||
export class ShowQuickFileHistoryCommand extends ActiveEditorCommand {
|
||||
@@ -12,14 +12,12 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCommand {
|
||||
super(Commands.ShowQuickFileHistory);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, maxCount?: number, goBackCommand?: CommandQuickPickItem, log?: IGitLog) {
|
||||
async execute(editor: TextEditor, uri?: Uri, maxCount?: number, goBackCommand?: CommandQuickPickItem, log?: IGitLog, nextPageCommand?: CommandQuickPickItem) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
uri = editor && editor.document && editor.document.uri;
|
||||
}
|
||||
|
||||
if (!uri) {
|
||||
return commands.executeCommand(Commands.ShowQuickRepoHistory);
|
||||
}
|
||||
if (!uri) return commands.executeCommand(Commands.ShowQuickRepoHistory);
|
||||
|
||||
const gitUri = await GitUri.fromUri(uri, this.git);
|
||||
|
||||
@@ -27,7 +25,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCommand {
|
||||
maxCount = this.git.config.advanced.maxQuickHistory;
|
||||
}
|
||||
|
||||
const progressCancellation = showQuickPickProgress(`Loading file history \u2014 ${maxCount ? ` limited to ${maxCount} commits` : ` this may take a while`}\u2026`);
|
||||
const progressCancellation = FileHistoryQuickPick.showProgress(maxCount);
|
||||
try {
|
||||
if (!log) {
|
||||
log = await this.git.getLogForFile(gitUri.fsPath, gitUri.sha, gitUri.repoPath, undefined, maxCount);
|
||||
@@ -36,7 +34,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCommand {
|
||||
|
||||
if (progressCancellation.token.isCancellationRequested) return undefined;
|
||||
|
||||
const pick = await FileHistoryQuickPick.show(log, uri, gitUri.sha, progressCancellation, goBackCommand);
|
||||
const pick = await FileHistoryQuickPick.show(log, gitUri, progressCancellation, goBackCommand, nextPageCommand);
|
||||
if (!pick) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
'use strict';
|
||||
import { commands, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand, Commands } from './commands';
|
||||
import { ActiveEditorCommand, Commands } from '../commands';
|
||||
import { GitProvider, GitUri, IGitLog } from '../gitProvider';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, RepoHistoryQuickPick, showQuickPickProgress } from '../quickPicks';
|
||||
import { CommandQuickPickItem, RepoHistoryQuickPick } from '../quickPicks';
|
||||
|
||||
export class ShowQuickRepoHistoryCommand extends ActiveEditorCommand {
|
||||
|
||||
@@ -11,30 +11,33 @@ export class ShowQuickRepoHistoryCommand extends ActiveEditorCommand {
|
||||
super(Commands.ShowQuickRepoHistory);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, sha?: string, maxCount?: number | undefined, goBackCommand?: CommandQuickPickItem, log?: IGitLog) {
|
||||
async execute(editor: TextEditor, uri?: Uri, maxCount?: number, goBackCommand?: CommandQuickPickItem, log?: IGitLog, nextPageCommand?: CommandQuickPickItem) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
uri = editor && editor.document && editor.document.uri;
|
||||
}
|
||||
|
||||
const gitUri = await GitUri.fromUri(uri, this.git);
|
||||
Logger.log(`ShowQuickRepoHistoryCommand.execute`, gitUri.shortSha);
|
||||
|
||||
if (maxCount == null) {
|
||||
maxCount = this.git.config.advanced.maxQuickHistory;
|
||||
}
|
||||
|
||||
const progressCancellation = showQuickPickProgress(`Loading repository history \u2014 ${maxCount ? ` limited to ${maxCount} commits` : ` this may take a while`}\u2026`);
|
||||
const progressCancellation = RepoHistoryQuickPick.showProgress(maxCount);
|
||||
try {
|
||||
if (!log) {
|
||||
const repoPath = await this.git.getRepoPathFromUri(uri, this.repoPath);
|
||||
const repoPath = gitUri.repoPath || await this.git.getRepoPathFromUri(uri, this.repoPath);
|
||||
if (!repoPath) return window.showWarningMessage(`Unable to show repository history`);
|
||||
|
||||
if (progressCancellation.token.isCancellationRequested) return undefined;
|
||||
|
||||
log = await this.git.getLogForRepo(repoPath, sha, maxCount);
|
||||
log = await this.git.getLogForRepo(repoPath, gitUri.sha, maxCount);
|
||||
if (!log) return window.showWarningMessage(`Unable to show repository history`);
|
||||
}
|
||||
|
||||
if (progressCancellation.token.isCancellationRequested) return undefined;
|
||||
|
||||
const pick = await RepoHistoryQuickPick.show(log, uri, sha, progressCancellation, goBackCommand);
|
||||
const pick = await RepoHistoryQuickPick.show(log, gitUri, progressCancellation, goBackCommand, nextPageCommand);
|
||||
if (!pick) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) {
|
||||
@@ -45,7 +48,7 @@ export class ShowQuickRepoHistoryCommand extends ActiveEditorCommand {
|
||||
new CommandQuickPickItem({
|
||||
label: `go back \u21A9`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 to repository history`
|
||||
}, Commands.ShowQuickRepoHistory, [uri, sha, maxCount, goBackCommand, log]),
|
||||
}, Commands.ShowQuickRepoHistory, [uri, maxCount, goBackCommand, log]),
|
||||
log);
|
||||
}
|
||||
catch (ex) {
|
||||
|
||||
Reference in New Issue
Block a user