1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | var QuickCommandService_1;
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | exports.QuickCommandService = exports.CLEAR_COMMAND_HISTORY = exports.quickCommand = void 0;
|
29 | const inversify_1 = require("inversify");
|
30 | const keybinding_1 = require("../keybinding");
|
31 | const common_1 = require("../../common");
|
32 | const context_key_service_1 = require("../context-key-service");
|
33 | const core_preferences_1 = require("../core-preferences");
|
34 | const quick_access_1 = require("./quick-access");
|
35 | const quick_input_service_1 = require("./quick-input-service");
|
36 | const widgets_1 = require("../widgets");
|
37 | exports.quickCommand = {
|
38 | id: 'workbench.action.showCommands'
|
39 | };
|
40 | exports.CLEAR_COMMAND_HISTORY = common_1.Command.toDefaultLocalizedCommand({
|
41 | id: 'clear.command.history',
|
42 | label: 'Clear Command History'
|
43 | });
|
44 | let QuickCommandService = QuickCommandService_1 = class QuickCommandService {
|
45 | constructor() {
|
46 |
|
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 |
|
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 |
|
141 |
|
142 |
|
143 |
|
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 |
|
159 |
|
160 |
|
161 |
|
162 | getCommands() {
|
163 |
|
164 | const recentCommands = this.commandRegistry.recent;
|
165 |
|
166 | const allCommands = this.getValidCommands(this.commandRegistry.commands);
|
167 |
|
168 | const limit = this.corePreferences['workbench.commandPalette.history'];
|
169 |
|
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 |
|
179 | const oCommands = allCommands.filter(c => !rCommands.some(r => common_1.Command.equals(r, c)));
|
180 |
|
181 | const recent = this.normalize(rCommands);
|
182 |
|
183 | const other = this.sort(this.normalize(oCommands));
|
184 | return { recent, other };
|
185 | }
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | normalize(commands) {
|
194 | return commands.filter((a) => a.label && (this.commandRegistry.isVisible(a.id) && this.commandRegistry.isEnabled(a.id)));
|
195 | }
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | sort(commands) {
|
203 | return commands.sort((a, b) => common_1.Command.compareCommands(a, b));
|
204 | }
|
205 | };
|
206 | QuickCommandService.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);
|
227 | QuickCommandService = QuickCommandService_1 = __decorate([
|
228 | (0, inversify_1.injectable)()
|
229 | ], QuickCommandService);
|
230 | exports.QuickCommandService = QuickCommandService;
|
231 |
|
\ | No newline at end of file |