@unocss/autocomplete 0.29.2 → 0.29.5

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.cjs CHANGED
@@ -74,8 +74,14 @@ function parseAutocomplete(template, theme = {}) {
74
74
  while (tempParts.length) {
75
75
  const part = tempParts.shift();
76
76
  if (part.type === "static") {
77
- if (!rest.startsWith(part.value))
77
+ if (combinations.length)
78
+ combinations = combinations.map((i) => i + part.value);
79
+ if (part.value.startsWith(rest) && part.value !== rest && !combinations.length) {
80
+ combinations = [part.value];
78
81
  break;
82
+ } else if (!rest.startsWith(part.value)) {
83
+ break;
84
+ }
79
85
  matched += part.value;
80
86
  rest = rest.slice(part.value.length);
81
87
  } else if (part.type === "group") {
@@ -85,7 +91,8 @@ function parseAutocomplete(template, theme = {}) {
85
91
  rest = rest.slice(fullMatched.length);
86
92
  } else {
87
93
  combinations = part.values.filter((i) => i.startsWith(rest));
88
- break;
94
+ if (tempParts[0]?.type !== "static")
95
+ break;
89
96
  }
90
97
  } else if (part.type === "theme") {
91
98
  const keys = part.objects.flatMap((i) => Object.keys(i)).filter((i) => i && !ignoredThemeKeys.includes(i) && i[0] !== "_");
@@ -105,12 +112,11 @@ function parseAutocomplete(template, theme = {}) {
105
112
  }
106
113
  } else {
107
114
  combinations = keys.filter((i) => i.startsWith(rest));
108
- break;
115
+ if (tempParts[0]?.type !== "static")
116
+ break;
109
117
  }
110
118
  }
111
119
  }
112
- if (!matched)
113
- return [];
114
120
  if (combinations.length === 0)
115
121
  combinations.push("");
116
122
  return combinations.map((i) => matched + i).filter((i) => i.length >= input.length);
@@ -121,7 +127,7 @@ function createAutocomplete(uno) {
121
127
  const templateCache = /* @__PURE__ */ new Map();
122
128
  const cache = new LRU__default({ max: 1e3 });
123
129
  let staticUtils = [];
124
- let templates = [];
130
+ const templates = [];
125
131
  reset();
126
132
  return {
127
133
  suggest,
@@ -139,11 +145,18 @@ function createAutocomplete(uno) {
139
145
  return [];
140
146
  if (cache.has(input))
141
147
  return cache.get(input);
148
+ const [, processed, , variants] = uno.matchVariants(input);
149
+ const idx = input.search(processed);
150
+ if (idx === -1)
151
+ return [];
152
+ const variantPrefix = input.slice(0, idx);
153
+ const variantPostfix = input.slice(idx + input.length);
142
154
  const result = processSuggestions(await Promise.all([
143
- suggestSelf(input),
144
- suggestStatic(input),
145
- ...suggestFromPreset(input)
146
- ]));
155
+ suggestSelf(processed),
156
+ suggestStatic(processed),
157
+ ...suggestFromPreset(processed),
158
+ ...suggestVariant(processed, variants)
159
+ ]), variantPrefix, variantPostfix);
147
160
  cache.set(input, result);
148
161
  return result;
149
162
  }
@@ -157,23 +170,24 @@ function createAutocomplete(uno) {
157
170
  function suggestFromPreset(input) {
158
171
  return templates.map((fn) => typeof fn === "function" ? fn(input) : getParsed(fn)(input)) || [];
159
172
  }
173
+ function suggestVariant(input, used) {
174
+ return uno.config.variants.filter((v) => v.autocomplete && (v.multiPass || !used.has(v))).flatMap((v) => core.toArray(v.autocomplete || [])).map((fn) => typeof fn === "function" ? fn(input) : getParsed(fn)(input));
175
+ }
160
176
  function reset() {
161
177
  templateCache.clear();
162
178
  cache.clear();
163
179
  staticUtils = Object.keys(uno.config.rulesStaticMap);
164
- templates = [
165
- ...uno.config.autocomplete || [],
166
- ...uno.config.rulesDynamic.flatMap((i) => core.toArray(i?.[2]?.autocomplete || []))
167
- ];
180
+ templates.length = 0;
181
+ templates.push(...uno.config.autocomplete || [], ...uno.config.rulesDynamic.flatMap((i) => core.toArray(i?.[2]?.autocomplete || [])));
168
182
  }
169
- function processSuggestions(suggestions) {
170
- return core.uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/[:-]$/))).sort((a, b) => {
183
+ function processSuggestions(suggestions, prefix = "", suffix = "") {
184
+ return core.uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/-$/))).sort((a, b) => {
171
185
  const numA = +(a.match(/\d+$/)?.[0] || NaN);
172
186
  const numB = +(b.match(/\d+$/)?.[0] || NaN);
173
187
  if (!Number.isNaN(numA) && !Number.isNaN(numB))
174
188
  return numA - numB;
175
189
  return a.localeCompare(b);
176
- });
190
+ }).map((i) => prefix + i + suffix);
177
191
  }
178
192
  }
179
193
 
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { uniq, toArray } from '@unocss/core';
1
+ import { toArray, uniq } from '@unocss/core';
2
2
  import LRU from 'lru-cache';
3
3
 
4
4
  const shorthands = {
@@ -66,8 +66,14 @@ function parseAutocomplete(template, theme = {}) {
66
66
  while (tempParts.length) {
67
67
  const part = tempParts.shift();
68
68
  if (part.type === "static") {
69
- if (!rest.startsWith(part.value))
69
+ if (combinations.length)
70
+ combinations = combinations.map((i) => i + part.value);
71
+ if (part.value.startsWith(rest) && part.value !== rest && !combinations.length) {
72
+ combinations = [part.value];
70
73
  break;
74
+ } else if (!rest.startsWith(part.value)) {
75
+ break;
76
+ }
71
77
  matched += part.value;
72
78
  rest = rest.slice(part.value.length);
73
79
  } else if (part.type === "group") {
@@ -77,7 +83,8 @@ function parseAutocomplete(template, theme = {}) {
77
83
  rest = rest.slice(fullMatched.length);
78
84
  } else {
79
85
  combinations = part.values.filter((i) => i.startsWith(rest));
80
- break;
86
+ if (tempParts[0]?.type !== "static")
87
+ break;
81
88
  }
82
89
  } else if (part.type === "theme") {
83
90
  const keys = part.objects.flatMap((i) => Object.keys(i)).filter((i) => i && !ignoredThemeKeys.includes(i) && i[0] !== "_");
@@ -97,12 +104,11 @@ function parseAutocomplete(template, theme = {}) {
97
104
  }
98
105
  } else {
99
106
  combinations = keys.filter((i) => i.startsWith(rest));
100
- break;
107
+ if (tempParts[0]?.type !== "static")
108
+ break;
101
109
  }
102
110
  }
103
111
  }
104
- if (!matched)
105
- return [];
106
112
  if (combinations.length === 0)
107
113
  combinations.push("");
108
114
  return combinations.map((i) => matched + i).filter((i) => i.length >= input.length);
@@ -113,7 +119,7 @@ function createAutocomplete(uno) {
113
119
  const templateCache = /* @__PURE__ */ new Map();
114
120
  const cache = new LRU({ max: 1e3 });
115
121
  let staticUtils = [];
116
- let templates = [];
122
+ const templates = [];
117
123
  reset();
118
124
  return {
119
125
  suggest,
@@ -131,11 +137,18 @@ function createAutocomplete(uno) {
131
137
  return [];
132
138
  if (cache.has(input))
133
139
  return cache.get(input);
140
+ const [, processed, , variants] = uno.matchVariants(input);
141
+ const idx = input.search(processed);
142
+ if (idx === -1)
143
+ return [];
144
+ const variantPrefix = input.slice(0, idx);
145
+ const variantPostfix = input.slice(idx + input.length);
134
146
  const result = processSuggestions(await Promise.all([
135
- suggestSelf(input),
136
- suggestStatic(input),
137
- ...suggestFromPreset(input)
138
- ]));
147
+ suggestSelf(processed),
148
+ suggestStatic(processed),
149
+ ...suggestFromPreset(processed),
150
+ ...suggestVariant(processed, variants)
151
+ ]), variantPrefix, variantPostfix);
139
152
  cache.set(input, result);
140
153
  return result;
141
154
  }
@@ -149,23 +162,24 @@ function createAutocomplete(uno) {
149
162
  function suggestFromPreset(input) {
150
163
  return templates.map((fn) => typeof fn === "function" ? fn(input) : getParsed(fn)(input)) || [];
151
164
  }
165
+ function suggestVariant(input, used) {
166
+ 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));
167
+ }
152
168
  function reset() {
153
169
  templateCache.clear();
154
170
  cache.clear();
155
171
  staticUtils = Object.keys(uno.config.rulesStaticMap);
156
- templates = [
157
- ...uno.config.autocomplete || [],
158
- ...uno.config.rulesDynamic.flatMap((i) => toArray(i?.[2]?.autocomplete || []))
159
- ];
172
+ templates.length = 0;
173
+ templates.push(...uno.config.autocomplete || [], ...uno.config.rulesDynamic.flatMap((i) => toArray(i?.[2]?.autocomplete || [])));
160
174
  }
161
- function processSuggestions(suggestions) {
162
- return uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/[:-]$/))).sort((a, b) => {
175
+ function processSuggestions(suggestions, prefix = "", suffix = "") {
176
+ return uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/-$/))).sort((a, b) => {
163
177
  const numA = +(a.match(/\d+$/)?.[0] || NaN);
164
178
  const numB = +(b.match(/\d+$/)?.[0] || NaN);
165
179
  if (!Number.isNaN(numA) && !Number.isNaN(numB))
166
180
  return numA - numB;
167
181
  return a.localeCompare(b);
168
- });
182
+ }).map((i) => prefix + i + suffix);
169
183
  }
170
184
  }
171
185
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/autocomplete",
3
- "version": "0.29.2",
3
+ "version": "0.29.5",
4
4
  "description": "Autocomplete utils for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/lru-cache": "^7.4.0",
39
- "@unocss/core": "0.29.2"
39
+ "@unocss/core": "0.29.5"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild",