@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
247 lines • 10.3 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2021 SAP SE or an SAP affiliate company and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
var QuickCommandService_1;
import { __decorate } from "tslib";
import { inject, injectable, optional, postConstruct } from 'inversify';
import { KeybindingRegistry } from '../keybinding.js';
import { Disposable, Command, CommandRegistry, nls } from '../../common/index.js';
import { ContextKeyService } from '../context-key-service.js';
import { CorePreferences } from '../../common/core-preferences.js';
import { QuickAccessRegistry } from './quick-access.js';
import { filterItems, QuickInputService } from './quick-input-service.js';
import { codiconArray } from '../widgets/index.js';
import { WindowFocusService } from '../window/window-focus-service.js';
export const quickCommand = {
id: 'workbench.action.showCommands'
};
export const CLEAR_COMMAND_HISTORY = Command.toDefaultLocalizedCommand({
id: 'clear.command.history',
label: 'Clear Command History'
});
export const CLOSE_QUICK_OPEN = {
id: 'workbench.action.closeQuickOpen'
};
let QuickCommandService = class QuickCommandService {
constructor() {
// The list of exempted commands not to be displayed in the recently used list.
this.exemptedCommands = [
CLEAR_COMMAND_HISTORY,
];
this.recentItems = [];
this.otherItems = [];
this.contexts = new Map();
}
static { QuickCommandService_1 = this; }
static { this.PREFIX = '>'; }
init() {
this.quickInputService?.onHide(() => this.commandContextElement = undefined);
}
registerQuickAccessProvider() {
this.quickAccessRegistry.registerQuickAccessProvider({
getInstance: () => this,
prefix: QuickCommandService_1.PREFIX,
placeholder: '',
helpEntries: [{ description: 'Quick Command', needsEditor: false }]
});
}
reset() {
this.updateCommandContextElement();
this.withCommandContext(() => {
const { recent, other } = this.getCommands();
this.recentItems = recent.map(command => this.toItem(command));
this.otherItems = other.map(command => this.toItem(command));
});
}
getPicks(filter, token) {
const items = [];
// Update the list of commands by fetching them from the registry.
this.reset();
const recentItems = filterItems(this.recentItems.slice(), filter);
const otherItems = filterItems(this.otherItems.slice(), filter);
if (recentItems.length > 0) {
items.push({ type: 'separator', label: nls.localizeByDefault('recently used') }, ...recentItems);
}
if (otherItems.length > 0) {
if (recentItems.length > 0) {
items.push({ type: 'separator', label: nls.localizeByDefault('other commands') });
}
items.push(...otherItems);
}
return items;
}
toItem(command) {
const label = (command.category) ? `${command.category}: ` + command.label : command.label;
const iconClasses = this.getItemIconClasses(command);
const activeElement = window.document.activeElement;
const commandContextElement = this.commandContextElement ?? (activeElement instanceof HTMLElement ? activeElement : undefined);
const originalLabel = command.originalLabel || command.label;
const originalCategory = command.originalCategory || command.category;
let detail = originalCategory ? `${originalCategory}: ${originalLabel}` : originalLabel;
if (label === detail) {
detail = undefined;
}
return {
label,
detail,
iconClasses,
alwaysShow: !!this.commandRegistry.getActiveHandler(command.id),
keySequence: this.getKeybinding(command),
execute: () => {
commandContextElement?.focus({ preventScroll: true });
this.commandRegistry.executeCommand(command.id);
this.commandRegistry.addRecentCommand(command);
}
};
}
updateCommandContextElement() {
// When the palette is opened directly, the picks are computed before the quick input is shown and focused.
// The palette may be opened in a secondary window, use the active element of the focused window.
const element = this.quickInputService?.previousFocusElement ?? (this.windowFocusService?.focusedWindow ?? window).document.activeElement;
// not using `instanceof HTMLElement`, the element may have been created in a secondary window, i.e. in another JavaScript realm
this.commandContextElement = element && typeof element.focus === 'function' ? element : undefined;
}
withCommandContext(callback) {
const element = this.commandContextElement;
return element?.isConnected ? this.contextKeyService.withContext(element, callback) : callback();
}
getKeybinding(command) {
const keybindings = this.keybindingRegistry.getKeybindingsForCommand(command.id);
if (!keybindings || keybindings.length === 0) {
return undefined;
}
try {
return this.keybindingRegistry.resolveKeybinding(keybindings[0]);
}
catch (error) {
return undefined;
}
}
getItemIconClasses(command) {
const toggledHandler = this.commandRegistry.getToggledHandler(command.id);
if (toggledHandler) {
return codiconArray('check');
}
return undefined;
}
pushCommandContext(commandId, when) {
const contexts = this.contexts.get(commandId) || [];
contexts.push(when);
this.contexts.set(commandId, contexts);
return Disposable.create(() => {
const index = contexts.indexOf(when);
if (index !== -1) {
contexts.splice(index, 1);
}
});
}
/**
* Get the list of valid commands.
*
* @param commands the list of raw commands.
* @returns the list of valid commands.
*/
getValidCommands(raw) {
const valid = [];
raw.forEach(command => {
if (command.label) {
const contexts = this.contexts.get(command.id);
if (!contexts || contexts.some(when => this.contextKeyService.match(when))) {
valid.push(command);
}
}
});
return valid;
}
/**
* Get the list of recently used and other commands.
*
* @returns the list of recently used commands and other commands.
*/
getCommands() {
// Get the list of recent commands.
const recentCommands = this.commandRegistry.recent;
// Get the list of all valid commands.
const allCommands = this.getValidCommands(this.commandRegistry.commands);
// Get the max history limit.
const limit = this.corePreferences['workbench.commandPalette.history'];
// Build the list of recent commands.
let rCommands = [];
if (limit > 0) {
rCommands.push(...recentCommands.filter(r => !this.exemptedCommands.some(c => Command.equals(r, c)) &&
allCommands.some(c => Command.equals(r, c))));
if (rCommands.length > limit) {
rCommands = rCommands.slice(0, limit);
}
}
// Build the list of other commands.
const oCommands = allCommands.filter(c => !rCommands.some(r => Command.equals(r, c)));
// Normalize the list of recent commands.
const recent = this.normalize(rCommands);
// Normalize, and sort the list of other commands.
const other = this.sort(this.normalize(oCommands));
return { recent, other };
}
/**
* Normalizes a list of commands.
* Normalization includes obtaining commands that have labels, are visible, and are enabled.
*
* @param commands the list of commands.
* @returns the list of normalized commands.
*/
normalize(commands) {
return commands.filter((a) => a.label && (this.commandRegistry.isVisible(a.id) && this.commandRegistry.isEnabled(a.id)));
}
/**
* Sorts a list of commands alphabetically.
*
* @param commands the list of commands.
* @returns the list of sorted commands.
*/
sort(commands) {
return commands.sort((a, b) => Command.compareCommands(a, b));
}
};
__decorate([
inject(ContextKeyService)
], QuickCommandService.prototype, "contextKeyService", void 0);
__decorate([
inject(CommandRegistry)
], QuickCommandService.prototype, "commandRegistry", void 0);
__decorate([
inject(CorePreferences)
], QuickCommandService.prototype, "corePreferences", void 0);
__decorate([
inject(QuickAccessRegistry)
], QuickCommandService.prototype, "quickAccessRegistry", void 0);
__decorate([
inject(KeybindingRegistry)
], QuickCommandService.prototype, "keybindingRegistry", void 0);
__decorate([
inject(QuickInputService),
optional()
], QuickCommandService.prototype, "quickInputService", void 0);
__decorate([
inject(WindowFocusService),
optional()
], QuickCommandService.prototype, "windowFocusService", void 0);
__decorate([
postConstruct()
], QuickCommandService.prototype, "init", null);
QuickCommandService = QuickCommandService_1 = __decorate([
injectable()
], QuickCommandService);
export { QuickCommandService };
//# sourceMappingURL=quick-command-service.js.map