1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var QuickCommandService_1;
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.QuickCommandService = exports.CLOSE_QUICK_OPEN = exports.CLEAR_COMMAND_HISTORY = exports.quickCommand = void 0;
|
20 | const tslib_1 = require("tslib");
|
21 | const inversify_1 = require("inversify");
|
22 | const keybinding_1 = require("../keybinding");
|
23 | const common_1 = require("../../common");
|
24 | const context_key_service_1 = require("../context-key-service");
|
25 | const core_preferences_1 = require("../core-preferences");
|
26 | const quick_access_1 = require("./quick-access");
|
27 | const quick_input_service_1 = require("./quick-input-service");
|
28 | const widgets_1 = require("../widgets");
|
29 | exports.quickCommand = {
|
30 | id: 'workbench.action.showCommands'
|
31 | };
|
32 | exports.CLEAR_COMMAND_HISTORY = common_1.Command.toDefaultLocalizedCommand({
|
33 | id: 'clear.command.history',
|
34 | label: 'Clear Command History'
|
35 | });
|
36 | exports.CLOSE_QUICK_OPEN = {
|
37 | id: 'workbench.action.closeQuickOpen'
|
38 | };
|
39 | let QuickCommandService = QuickCommandService_1 = class QuickCommandService {
|
40 | constructor() {
|
41 |
|
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 |
|
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 |
|
136 |
|
137 |
|
138 |
|
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 |
|
154 |
|
155 |
|
156 |
|
157 | getCommands() {
|
158 |
|
159 | const recentCommands = this.commandRegistry.recent;
|
160 |
|
161 | const allCommands = this.getValidCommands(this.commandRegistry.commands);
|
162 |
|
163 | const limit = this.corePreferences['workbench.commandPalette.history'];
|
164 |
|
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 |
|
174 | const oCommands = allCommands.filter(c => !rCommands.some(r => common_1.Command.equals(r, c)));
|
175 |
|
176 | const recent = this.normalize(rCommands);
|
177 |
|
178 | const other = this.sort(this.normalize(oCommands));
|
179 | return { recent, other };
|
180 | }
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | normalize(commands) {
|
189 | return commands.filter((a) => a.label && (this.commandRegistry.isVisible(a.id) && this.commandRegistry.isEnabled(a.id)));
|
190 | }
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | sort(commands) {
|
198 | return commands.sort((a, b) => common_1.Command.compareCommands(a, b));
|
199 | }
|
200 | };
|
201 | QuickCommandService.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);
|
222 | QuickCommandService = QuickCommandService_1 = (0, tslib_1.__decorate)([
|
223 | (0, inversify_1.injectable)()
|
224 | ], QuickCommandService);
|
225 | exports.QuickCommandService = QuickCommandService;
|
226 |
|
\ | No newline at end of file |