@unocss/autocomplete 0.64.1 → 0.65.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -27,11 +27,11 @@ interface UnocssAutocomplete {
27
27
  suggestInFile: (content: string, cursor: number) => Promise<SuggestResult | undefined>;
28
28
  templates: (string | AutoCompleteFunction)[];
29
29
  cache: LRUCache<string, string[]>;
30
- reset: () => void;
30
+ reset: () => Promise<void>;
31
31
  enumerate: () => Promise<Set<string>>;
32
32
  }
33
33
 
34
- declare function createAutocomplete(uno: UnoGenerator, options?: AutocompleteOptions): UnocssAutocomplete;
34
+ declare function createAutocomplete(_uno: UnoGenerator | Promise<UnoGenerator>, options?: AutocompleteOptions): UnocssAutocomplete;
35
35
 
36
36
  declare const shorthands: Record<string, string>;
37
37
  declare const ignoredThemeKeys: string[];
package/dist/index.d.ts CHANGED
@@ -27,11 +27,11 @@ interface UnocssAutocomplete {
27
27
  suggestInFile: (content: string, cursor: number) => Promise<SuggestResult | undefined>;
28
28
  templates: (string | AutoCompleteFunction)[];
29
29
  cache: LRUCache<string, string[]>;
30
- reset: () => void;
30
+ reset: () => Promise<void>;
31
31
  enumerate: () => Promise<Set<string>>;
32
32
  }
33
33
 
34
- declare function createAutocomplete(uno: UnoGenerator, options?: AutocompleteOptions): UnocssAutocomplete;
34
+ declare function createAutocomplete(_uno: UnoGenerator | Promise<UnoGenerator>, options?: AutocompleteOptions): UnocssAutocomplete;
35
35
 
36
36
  declare const shorthands: Record<string, string>;
37
37
  declare const ignoredThemeKeys: string[];
package/dist/index.mjs CHANGED
@@ -239,13 +239,14 @@ function getAllCombination(parts) {
239
239
  return list;
240
240
  }
241
241
 
242
- function createAutocomplete(uno, options = {}) {
242
+ function createAutocomplete(_uno, options = {}) {
243
243
  const templateCache = /* @__PURE__ */ new Map();
244
244
  const cache = new LRUCache({ max: 5e3 });
245
245
  let staticUtils = [];
246
246
  const templates = [];
247
247
  const matchType = options.matchType ?? "prefix";
248
- reset();
248
+ let uno;
249
+ const ready = reset();
249
250
  return {
250
251
  suggest,
251
252
  suggestInFile,
@@ -279,6 +280,7 @@ function createAutocomplete(uno, options = {}) {
279
280
  return templateCache.get(template).suggest;
280
281
  }
281
282
  async function suggest(input, allowsEmptyInput = false) {
283
+ await ready;
282
284
  if (!allowsEmptyInput && input.length < 1)
283
285
  return [];
284
286
  if (cache.has(input))
@@ -286,23 +288,26 @@ function createAutocomplete(uno, options = {}) {
286
288
  const attributify = uno.config.presets.find((i) => i.name === "@unocss/preset-attributify");
287
289
  const attributifyPrefix = attributify?.options?.prefix;
288
290
  const _input = attributifyPrefix ? input.startsWith(attributifyPrefix) ? input.slice(attributifyPrefix.length) : input.replace(`:${attributifyPrefix}`, ":") : input;
289
- const [, processed, , variants] = await uno.matchVariants(_input);
290
- let idx = processed ? input.search(escapeRegExp(processed)) : input.length;
291
- if (idx === -1)
292
- idx = 0;
293
- const variantPrefix = input.slice(0, idx);
294
- const variantSuffix = input.slice(idx + input.length);
295
- let result = processSuggestions(
296
- await Promise.all([
297
- suggestSelf(processed),
298
- suggestStatic(processed),
299
- suggestUnoCache(processed),
300
- ...suggestFromPreset(processed),
301
- ...suggestVariant(processed, variants)
302
- ]),
303
- variantPrefix,
304
- variantSuffix
305
- );
291
+ const matched = await uno.matchVariants(_input);
292
+ let result = (await Promise.all(matched.map(async ([, processed, , variants]) => {
293
+ let idx = processed ? input.search(escapeRegExp(processed)) : input.length;
294
+ if (idx === -1)
295
+ idx = 0;
296
+ const variantPrefix = input.slice(0, idx);
297
+ const variantSuffix = input.slice(idx + input.length);
298
+ const result2 = processSuggestions(
299
+ await Promise.all([
300
+ suggestSelf(processed),
301
+ suggestStatic(processed),
302
+ suggestUnoCache(processed),
303
+ ...suggestFromPreset(processed),
304
+ ...suggestVariant(processed, variants)
305
+ ]),
306
+ variantPrefix,
307
+ variantSuffix
308
+ );
309
+ return result2;
310
+ }))).flat();
306
311
  if (matchType === "fuzzy") {
307
312
  const fzf = new Fzf(result, {
308
313
  tiebreakers: [byStartAsc, byLengthAsc]
@@ -313,6 +318,7 @@ function createAutocomplete(uno, options = {}) {
313
318
  return result;
314
319
  }
315
320
  async function suggestInFile(content, cursor) {
321
+ await ready;
316
322
  const isInsideAttrValue = searchAttrKey(content, cursor) !== void 0;
317
323
  const byExtractor = await searchUsageByExtractor(content, cursor);
318
324
  if (byExtractor) {
@@ -369,9 +375,12 @@ function createAutocomplete(uno, options = {}) {
369
375
  function suggestVariant(input, used) {
370
376
  return uno.config.variants.filter((v) => v.autocomplete && (v.multiPass || !used.has(v))).flatMap((v) => toArray(v.autocomplete || [])).map((fn) => typeof fn === "function" ? fn(input) : getParsed(fn)(input, matchType));
371
377
  }
372
- function reset() {
378
+ async function reset() {
373
379
  templateCache.clear();
374
380
  cache.clear();
381
+ if (!uno) {
382
+ uno = await Promise.resolve(_uno);
383
+ }
375
384
  staticUtils = [
376
385
  ...Object.keys(uno.config.rulesStaticMap),
377
386
  ...uno.config.shortcuts.filter((i) => typeof i[0] === "string").map((i) => i[0])
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/autocomplete",
3
3
  "type": "module",
4
- "version": "0.64.1",
4
+ "version": "0.65.0-beta.2",
5
5
  "description": "Autocomplete utils for UnoCSS",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "lru-cache": "^10.4.3"
38
38
  },
39
39
  "devDependencies": {
40
- "@unocss/core": "0.64.1"
40
+ "@unocss/core": "0.65.0-beta.2"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "unbuild",