UNPKG

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