UNPKG

9.55 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 SAP SE or an SAP affiliate company and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var QuickCommandService_1;
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.QuickCommandService = exports.CLOSE_QUICK_OPEN = exports.CLEAR_COMMAND_HISTORY = exports.quickCommand = void 0;
20const tslib_1 = require("tslib");
21const inversify_1 = require("inversify");
22const keybinding_1 = require("../keybinding");
23const common_1 = require("../../common");
24const context_key_service_1 = require("../context-key-service");
25const core_preferences_1 = require("../core-preferences");
26const quick_access_1 = require("./quick-access");
27const quick_input_service_1 = require("./quick-input-service");
28const widgets_1 = require("../widgets");
29exports.quickCommand = {
30 id: 'workbench.action.showCommands'
31};
32exports.CLEAR_COMMAND_HISTORY = common_1.Command.toDefaultLocalizedCommand({
33 id: 'clear.command.history',
34 label: 'Clear Command History'
35});
36exports.CLOSE_QUICK_OPEN = {
37 id: 'workbench.action.closeQuickOpen'
38};
39let QuickCommandService = QuickCommandService_1 = class QuickCommandService {
40 constructor() {
41 // The list of exempted commands not to be displayed in the recently used list.
42 this.exemptedCommands = [
43 exports.CLEAR_COMMAND_HISTORY,
44 ];
45 this.recentItems = [];
46 this.otherItems = [];
47 this.contexts = new Map();
48 }
49 registerQuickAccessProvider() {
50 this.quickAccessRegistry.registerQuickAccessProvider({
51 getInstance: () => this,
52 prefix: QuickCommandService_1.PREFIX,
53 placeholder: '',
54 helpEntries: [{ description: 'Quick Command', needsEditor: false }]
55 });
56 }
57 reset() {
58 const { recent, other } = this.getCommands();
59 this.recentItems = [];
60 this.otherItems = [];
61 this.recentItems.push(...recent.map(command => this.toItem(command)));
62 this.otherItems.push(...other.map(command => this.toItem(command)));
63 }
64 getPicks(filter, token) {
65 const items = [];
66 // Update the list of commands by fetching them from the registry.
67 this.reset();
68 const recentItems = (0, quick_input_service_1.filterItems)(this.recentItems.slice(), filter);
69 const otherItems = (0, quick_input_service_1.filterItems)(this.otherItems.slice(), filter);
70 if (recentItems.length > 0) {
71 items.push({ type: 'separator', label: common_1.nls.localizeByDefault('recently used') }, ...recentItems);
72 }
73 if (otherItems.length > 0) {
74 if (recentItems.length > 0) {
75 items.push({ type: 'separator', label: common_1.nls.localizeByDefault('other commands') });
76 }
77 items.push(...otherItems);
78 }
79 return items;
80 }
81 toItem(command) {
82 const label = (command.category) ? `${command.category}: ` + command.label : command.label;
83 const iconClasses = this.getItemIconClasses(command);
84 const activeElement = window.document.activeElement;
85 const originalLabel = command.originalLabel || command.label;
86 const originalCategory = command.originalCategory || command.category;
87 let detail = originalCategory ? `${originalCategory}: ${originalLabel}` : originalLabel;
88 if (label === detail) {
89 detail = undefined;
90 }
91 return {
92 label,
93 detail,
94 iconClasses,
95 alwaysShow: !!this.commandRegistry.getActiveHandler(command.id),
96 keySequence: this.getKeybinding(command),
97 execute: () => {
98 activeElement.focus({ preventScroll: true });
99 this.commandRegistry.executeCommand(command.id);
100 this.commandRegistry.addRecentCommand(command);
101 }
102 };
103 }
104 getKeybinding(command) {
105 const keybindings = this.keybindingRegistry.getKeybindingsForCommand(command.id);
106 if (!keybindings || keybindings.length === 0) {
107 return undefined;
108 }
109 try {
110 return this.keybindingRegistry.resolveKeybinding(keybindings[0]);
111 }
112 catch (error) {
113 return undefined;
114 }
115 }
116 getItemIconClasses(command) {
117 const toggledHandler = this.commandRegistry.getToggledHandler(command.id);
118 if (toggledHandler) {
119 return (0, widgets_1.codiconArray)('check');
120 }
121 return undefined;
122 }
123 pushCommandContext(commandId, when) {
124 const contexts = this.contexts.get(commandId) || [];
125 contexts.push(when);
126 this.contexts.set(commandId, contexts);
127 return common_1.Disposable.create(() => {
128 const index = contexts.indexOf(when);
129 if (index !== -1) {
130 contexts.splice(index, 1);
131 }
132 });
133 }
134 /**
135 * Get the list of valid commands.
136 *
137 * @param commands the list of raw commands.
138 * @returns the list of valid commands.
139 */
140 getValidCommands(raw) {
141 const valid = [];
142 raw.forEach(command => {
143 if (command.label) {
144 const contexts = this.contexts.get(command.id);
145 if (!contexts || contexts.some(when => this.contextKeyService.match(when))) {
146 valid.push(command);
147 }
148 }
149 });
150 return valid;
151 }
152 /**
153 * Get the list of recently used and other commands.
154 *
155 * @returns the list of recently used commands and other commands.
156 */
157 getCommands() {
158 // Get the list of recent commands.
159 const recentCommands = this.commandRegistry.recent;
160 // Get the list of all valid commands.
161 const allCommands = this.getValidCommands(this.commandRegistry.commands);
162 // Get the max history limit.
163 const limit = this.corePreferences['workbench.commandPalette.history'];
164 // Build the list of recent commands.
165 let rCommands = [];
166 if (limit > 0) {
167 rCommands.push(...recentCommands.filter(r => !this.exemptedCommands.some(c => common_1.Command.equals(r, c)) &&
168 allCommands.some(c => common_1.Command.equals(r, c))));
169 if (rCommands.length > limit) {
170 rCommands = rCommands.slice(0, limit);
171 }
172 }
173 // Build the list of other commands.
174 const oCommands = allCommands.filter(c => !rCommands.some(r => common_1.Command.equals(r, c)));
175 // Normalize the list of recent commands.
176 const recent = this.normalize(rCommands);
177 // Normalize, and sort the list of other commands.
178 const other = this.sort(this.normalize(oCommands));
179 return { recent, other };
180 }
181 /**
182 * Normalizes a list of commands.
183 * Normalization includes obtaining commands that have labels, are visible, and are enabled.
184 *
185 * @param commands the list of commands.
186 * @returns the list of normalized commands.
187 */
188 normalize(commands) {
189 return commands.filter((a) => a.label && (this.commandRegistry.isVisible(a.id) && this.commandRegistry.isEnabled(a.id)));
190 }
191 /**
192 * Sorts a list of commands alphabetically.
193 *
194 * @param commands the list of commands.
195 * @returns the list of sorted commands.
196 */
197 sort(commands) {
198 return commands.sort((a, b) => common_1.Command.compareCommands(a, b));
199 }
200};
201QuickCommandService.PREFIX = '>';
202(0, tslib_1.__decorate)([
203 (0, inversify_1.inject)(context_key_service_1.ContextKeyService),
204 (0, tslib_1.__metadata)("design:type", Object)
205], QuickCommandService.prototype, "contextKeyService", void 0);
206(0, tslib_1.__decorate)([
207 (0, inversify_1.inject)(common_1.CommandRegistry),
208 (0, tslib_1.__metadata)("design:type", common_1.CommandRegistry)
209], QuickCommandService.prototype, "commandRegistry", void 0);
210(0, tslib_1.__decorate)([
211 (0, inversify_1.inject)(core_preferences_1.CorePreferences),
212 (0, tslib_1.__metadata)("design:type", Object)
213], QuickCommandService.prototype, "corePreferences", void 0);
214(0, tslib_1.__decorate)([
215 (0, inversify_1.inject)(quick_access_1.QuickAccessRegistry),
216 (0, tslib_1.__metadata)("design:type", Object)
217], QuickCommandService.prototype, "quickAccessRegistry", void 0);
218(0, tslib_1.__decorate)([
219 (0, inversify_1.inject)(keybinding_1.KeybindingRegistry),
220 (0, tslib_1.__metadata)("design:type", keybinding_1.KeybindingRegistry)
221], QuickCommandService.prototype, "keybindingRegistry", void 0);
222QuickCommandService = QuickCommandService_1 = (0, tslib_1.__decorate)([
223 (0, inversify_1.injectable)()
224], QuickCommandService);
225exports.QuickCommandService = QuickCommandService;
226//# sourceMappingURL=quick-command-service.js.map
\No newline at end of file