UNPKG

6.59 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox 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// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.findMatches = exports.filterItems = exports.QuickInputService = exports.quickInputServicePath = exports.QuickInputHideReason = exports.QuickInputButton = exports.QuickPickSeparator = exports.QuickPickItem = exports.QuickPickService = exports.quickPickServicePath = void 0;
19const uri_1 = require("./uri");
20const fuzzy = require("fuzzy");
21const vscode_uri_1 = require("vscode-uri");
22exports.quickPickServicePath = '/services/quickPick';
23exports.QuickPickService = Symbol('QuickPickService');
24var QuickPickItem;
25(function (QuickPickItem) {
26 function is(item) {
27 // if it's not a separator, it's an item
28 return item.type !== 'separator';
29 }
30 QuickPickItem.is = is;
31})(QuickPickItem = exports.QuickPickItem || (exports.QuickPickItem = {}));
32var QuickPickSeparator;
33(function (QuickPickSeparator) {
34 function is(item) {
35 return item.type === 'separator';
36 }
37 QuickPickSeparator.is = is;
38})(QuickPickSeparator = exports.QuickPickSeparator || (exports.QuickPickSeparator = {}));
39var QuickInputButton;
40(function (QuickInputButton) {
41 function normalize(button) {
42 var _a;
43 if (!button) {
44 return button;
45 }
46 let iconPath = undefined;
47 if (button.iconPath instanceof uri_1.default) {
48 iconPath = { dark: button.iconPath['codeUri'] };
49 }
50 else if (button.iconPath && 'dark' in button.iconPath) {
51 const dark = vscode_uri_1.URI.isUri(button.iconPath.dark) ? button.iconPath.dark : button.iconPath.dark['codeUri'];
52 const light = vscode_uri_1.URI.isUri(button.iconPath.light) ? button.iconPath.light : (_a = button.iconPath.light) === null || _a === void 0 ? void 0 : _a['codeUri'];
53 iconPath = { dark, light };
54 }
55 return {
56 ...button,
57 iconPath,
58 };
59 }
60 QuickInputButton.normalize = normalize;
61})(QuickInputButton = exports.QuickInputButton || (exports.QuickInputButton = {}));
62var QuickInputHideReason;
63(function (QuickInputHideReason) {
64 /**
65 * Focus was moved away from the input, but the user may not have explicitly closed it.
66 */
67 QuickInputHideReason[QuickInputHideReason["Blur"] = 1] = "Blur";
68 /**
69 * An explicit close gesture, like striking the Escape key
70 */
71 QuickInputHideReason[QuickInputHideReason["Gesture"] = 2] = "Gesture";
72 /**
73 * Any other reason
74 */
75 QuickInputHideReason[QuickInputHideReason["Other"] = 3] = "Other";
76})(QuickInputHideReason = exports.QuickInputHideReason || (exports.QuickInputHideReason = {}));
77exports.quickInputServicePath = '/services/quickInput';
78exports.QuickInputService = Symbol('QuickInputService');
79/**
80 * Filter the list of quick pick items based on the provided filter.
81 * Items are filtered based on if:
82 * - their `label` satisfies the filter using `fuzzy`.
83 * - their `description` satisfies the filter using `fuzzy`.
84 * - their `detail` satisfies the filter using `fuzzy`.
85 * Filtered items are also updated to display proper highlights based on how they were filtered.
86 * @param items the list of quick pick items.
87 * @param filter the filter to search for.
88 * @returns the list of quick pick items that satisfy the filter.
89 */
90function filterItems(items, filter) {
91 filter = filter.trim().toLowerCase();
92 if (filter.length === 0) {
93 for (const item of items) {
94 if (item.type !== 'separator') {
95 item.highlights = undefined; // reset highlights from previous filtering.
96 }
97 }
98 return items;
99 }
100 const filteredItems = [];
101 for (const item of items) {
102 if (item.type === 'separator') {
103 filteredItems.push(item);
104 }
105 else if (fuzzy.test(filter, item.label) ||
106 (item.description && fuzzy.test(filter, item.description)) ||
107 (item.detail && fuzzy.test(filter, item.detail))) {
108 item.highlights = {
109 label: findMatches(item.label, filter),
110 description: item.description ? findMatches(item.description, filter) : undefined,
111 detail: item.detail ? findMatches(item.detail, filter) : undefined
112 };
113 filteredItems.push(item);
114 }
115 }
116 return filteredItems;
117}
118exports.filterItems = filterItems;
119/**
120 * Find match highlights when testing a word against a pattern.
121 * @param word the word to test.
122 * @param pattern the word to match against.
123 * @returns the list of highlights if present.
124 */
125function findMatches(word, pattern) {
126 word = word.toLocaleLowerCase();
127 pattern = pattern.toLocaleLowerCase();
128 if (pattern.trim().length === 0) {
129 return undefined;
130 }
131 const delimiter = '\u0000'; // null byte that shouldn't appear in the input and is used to denote matches.
132 const matchResult = fuzzy.match(pattern.replace(/\u0000/gu, ''), word, { pre: delimiter, post: delimiter });
133 if (!matchResult) {
134 return undefined;
135 }
136 const match = matchResult.rendered;
137 const highlights = [];
138 let lastIndex = 0;
139 /** We need to account for the extra markers by removing them from the range */
140 let offset = 0;
141 while (true) {
142 const start = match.indexOf(delimiter, lastIndex);
143 if (start === -1) {
144 break;
145 }
146 const end = match.indexOf(delimiter, start + 1);
147 if (end === -1) {
148 break;
149 }
150 highlights.push({
151 start: start - offset++,
152 end: end - offset++
153 });
154 lastIndex = end + 1;
155 }
156 return highlights.length > 0 ? highlights : undefined;
157}
158exports.findMatches = findMatches;
159//# sourceMappingURL=quick-pick-service.js.map
\No newline at end of file