@unocss/autocomplete 0.29.6 → 0.30.0
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 +51 -19
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +51 -19
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -123,6 +123,21 @@ function parseAutocomplete(template, theme = {}) {
|
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
function searchUsageBoundary(line, index) {
|
|
127
|
+
let start = index;
|
|
128
|
+
let end = index;
|
|
129
|
+
const regex = /[^\s>"'`]/;
|
|
130
|
+
while (start && regex.test(line.charAt(start - 1)))
|
|
131
|
+
--start;
|
|
132
|
+
while (end < line.length && regex.test(line.charAt(end)))
|
|
133
|
+
++end;
|
|
134
|
+
return {
|
|
135
|
+
content: line.slice(start, end),
|
|
136
|
+
start,
|
|
137
|
+
end
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
126
141
|
function createAutocomplete(uno) {
|
|
127
142
|
const templateCache = /* @__PURE__ */ new Map();
|
|
128
143
|
const cache = new LRU__default({ max: 1e3 });
|
|
@@ -131,6 +146,7 @@ function createAutocomplete(uno) {
|
|
|
131
146
|
reset();
|
|
132
147
|
return {
|
|
133
148
|
suggest,
|
|
149
|
+
suggestInFile,
|
|
134
150
|
templates,
|
|
135
151
|
cache,
|
|
136
152
|
reset
|
|
@@ -146,20 +162,51 @@ function createAutocomplete(uno) {
|
|
|
146
162
|
if (cache.has(input))
|
|
147
163
|
return cache.get(input);
|
|
148
164
|
const [, processed, , variants] = uno.matchVariants(input);
|
|
149
|
-
const idx = input.search(processed);
|
|
165
|
+
const idx = processed ? input.search(processed) : input.length;
|
|
150
166
|
if (idx === -1)
|
|
151
167
|
return [];
|
|
152
168
|
const variantPrefix = input.slice(0, idx);
|
|
153
|
-
const
|
|
169
|
+
const variantSuffix = input.slice(idx + input.length);
|
|
154
170
|
const result = processSuggestions(await Promise.all([
|
|
155
171
|
suggestSelf(processed),
|
|
156
172
|
suggestStatic(processed),
|
|
157
173
|
...suggestFromPreset(processed),
|
|
158
174
|
...suggestVariant(processed, variants)
|
|
159
|
-
]), variantPrefix,
|
|
175
|
+
]), variantPrefix, variantSuffix);
|
|
160
176
|
cache.set(input, result);
|
|
161
177
|
return result;
|
|
162
178
|
}
|
|
179
|
+
async function suggestInFile(content, cursor) {
|
|
180
|
+
const byExtractor = await searchUsageByExtractor(content, cursor);
|
|
181
|
+
if (byExtractor) {
|
|
182
|
+
const suggestions2 = await suggest(byExtractor.extracted);
|
|
183
|
+
const formatted = byExtractor.transformSuggestions ? byExtractor.transformSuggestions(suggestions2) : suggestions2;
|
|
184
|
+
return {
|
|
185
|
+
suggestions: suggestions2.map((v, i) => [v, formatted[i]]),
|
|
186
|
+
resolveReplacement: byExtractor.resolveReplacement
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const regular = searchUsageBoundary(content, cursor);
|
|
190
|
+
const suggestions = await suggest(regular.content);
|
|
191
|
+
return {
|
|
192
|
+
suggestions: suggestions.map((v) => [v, v]),
|
|
193
|
+
resolveReplacement: (suggestion) => ({
|
|
194
|
+
start: regular.start,
|
|
195
|
+
end: regular.end,
|
|
196
|
+
replacement: suggestion
|
|
197
|
+
})
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
async function searchUsageByExtractor(content, cursor) {
|
|
201
|
+
if (!uno.config.autocomplete.extractors.length)
|
|
202
|
+
return null;
|
|
203
|
+
for (const extractor of uno.config.autocomplete.extractors) {
|
|
204
|
+
const res = await extractor.extract({ content, cursor });
|
|
205
|
+
if (res)
|
|
206
|
+
return res;
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
163
210
|
async function suggestSelf(input) {
|
|
164
211
|
const i = await uno.parseToken(input, "-");
|
|
165
212
|
return i ? [input] : [];
|
|
@@ -178,7 +225,7 @@ function createAutocomplete(uno) {
|
|
|
178
225
|
cache.clear();
|
|
179
226
|
staticUtils = Object.keys(uno.config.rulesStaticMap);
|
|
180
227
|
templates.length = 0;
|
|
181
|
-
templates.push(...uno.config.autocomplete || [], ...uno.config.rulesDynamic.flatMap((i) => core.toArray(i?.[2]?.autocomplete || [])));
|
|
228
|
+
templates.push(...uno.config.autocomplete.templates || [], ...uno.config.rulesDynamic.flatMap((i) => core.toArray(i?.[2]?.autocomplete || [])));
|
|
182
229
|
}
|
|
183
230
|
function processSuggestions(suggestions, prefix = "", suffix = "") {
|
|
184
231
|
return core.uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/-$/))).sort((a, b) => {
|
|
@@ -191,21 +238,6 @@ function createAutocomplete(uno) {
|
|
|
191
238
|
}
|
|
192
239
|
}
|
|
193
240
|
|
|
194
|
-
function searchUsageBoundary(line, index) {
|
|
195
|
-
let start = index;
|
|
196
|
-
let end = index;
|
|
197
|
-
const regex = /[^\s>"'`]/;
|
|
198
|
-
while (start && regex.test(line.charAt(start - 1)))
|
|
199
|
-
--start;
|
|
200
|
-
while (end < line.length && regex.test(line.charAt(end)))
|
|
201
|
-
++end;
|
|
202
|
-
return {
|
|
203
|
-
content: line.slice(start, end),
|
|
204
|
-
start,
|
|
205
|
-
end
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
241
|
exports.createAutocomplete = createAutocomplete;
|
|
210
242
|
exports.ignoredThemeKeys = ignoredThemeKeys;
|
|
211
243
|
exports.parseAutocomplete = parseAutocomplete;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { UnoGenerator, AutoCompleteFunction } from '@unocss/core';
|
|
1
|
+
import { UnoGenerator, SuggestResult, AutoCompleteFunction } from '@unocss/core';
|
|
2
2
|
import LRU from 'lru-cache';
|
|
3
3
|
|
|
4
4
|
declare function createAutocomplete(uno: UnoGenerator): {
|
|
5
5
|
suggest: (input: string) => Promise<string[]>;
|
|
6
|
+
suggestInFile: (content: string, cursor: number) => Promise<SuggestResult>;
|
|
6
7
|
templates: (string | AutoCompleteFunction)[];
|
|
7
8
|
cache: LRU<string, string[]>;
|
|
8
9
|
reset: () => void;
|
package/dist/index.mjs
CHANGED
|
@@ -115,6 +115,21 @@ function parseAutocomplete(template, theme = {}) {
|
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
function searchUsageBoundary(line, index) {
|
|
119
|
+
let start = index;
|
|
120
|
+
let end = index;
|
|
121
|
+
const regex = /[^\s>"'`]/;
|
|
122
|
+
while (start && regex.test(line.charAt(start - 1)))
|
|
123
|
+
--start;
|
|
124
|
+
while (end < line.length && regex.test(line.charAt(end)))
|
|
125
|
+
++end;
|
|
126
|
+
return {
|
|
127
|
+
content: line.slice(start, end),
|
|
128
|
+
start,
|
|
129
|
+
end
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
118
133
|
function createAutocomplete(uno) {
|
|
119
134
|
const templateCache = /* @__PURE__ */ new Map();
|
|
120
135
|
const cache = new LRU({ max: 1e3 });
|
|
@@ -123,6 +138,7 @@ function createAutocomplete(uno) {
|
|
|
123
138
|
reset();
|
|
124
139
|
return {
|
|
125
140
|
suggest,
|
|
141
|
+
suggestInFile,
|
|
126
142
|
templates,
|
|
127
143
|
cache,
|
|
128
144
|
reset
|
|
@@ -138,20 +154,51 @@ function createAutocomplete(uno) {
|
|
|
138
154
|
if (cache.has(input))
|
|
139
155
|
return cache.get(input);
|
|
140
156
|
const [, processed, , variants] = uno.matchVariants(input);
|
|
141
|
-
const idx = input.search(processed);
|
|
157
|
+
const idx = processed ? input.search(processed) : input.length;
|
|
142
158
|
if (idx === -1)
|
|
143
159
|
return [];
|
|
144
160
|
const variantPrefix = input.slice(0, idx);
|
|
145
|
-
const
|
|
161
|
+
const variantSuffix = input.slice(idx + input.length);
|
|
146
162
|
const result = processSuggestions(await Promise.all([
|
|
147
163
|
suggestSelf(processed),
|
|
148
164
|
suggestStatic(processed),
|
|
149
165
|
...suggestFromPreset(processed),
|
|
150
166
|
...suggestVariant(processed, variants)
|
|
151
|
-
]), variantPrefix,
|
|
167
|
+
]), variantPrefix, variantSuffix);
|
|
152
168
|
cache.set(input, result);
|
|
153
169
|
return result;
|
|
154
170
|
}
|
|
171
|
+
async function suggestInFile(content, cursor) {
|
|
172
|
+
const byExtractor = await searchUsageByExtractor(content, cursor);
|
|
173
|
+
if (byExtractor) {
|
|
174
|
+
const suggestions2 = await suggest(byExtractor.extracted);
|
|
175
|
+
const formatted = byExtractor.transformSuggestions ? byExtractor.transformSuggestions(suggestions2) : suggestions2;
|
|
176
|
+
return {
|
|
177
|
+
suggestions: suggestions2.map((v, i) => [v, formatted[i]]),
|
|
178
|
+
resolveReplacement: byExtractor.resolveReplacement
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const regular = searchUsageBoundary(content, cursor);
|
|
182
|
+
const suggestions = await suggest(regular.content);
|
|
183
|
+
return {
|
|
184
|
+
suggestions: suggestions.map((v) => [v, v]),
|
|
185
|
+
resolveReplacement: (suggestion) => ({
|
|
186
|
+
start: regular.start,
|
|
187
|
+
end: regular.end,
|
|
188
|
+
replacement: suggestion
|
|
189
|
+
})
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
async function searchUsageByExtractor(content, cursor) {
|
|
193
|
+
if (!uno.config.autocomplete.extractors.length)
|
|
194
|
+
return null;
|
|
195
|
+
for (const extractor of uno.config.autocomplete.extractors) {
|
|
196
|
+
const res = await extractor.extract({ content, cursor });
|
|
197
|
+
if (res)
|
|
198
|
+
return res;
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
155
202
|
async function suggestSelf(input) {
|
|
156
203
|
const i = await uno.parseToken(input, "-");
|
|
157
204
|
return i ? [input] : [];
|
|
@@ -170,7 +217,7 @@ function createAutocomplete(uno) {
|
|
|
170
217
|
cache.clear();
|
|
171
218
|
staticUtils = Object.keys(uno.config.rulesStaticMap);
|
|
172
219
|
templates.length = 0;
|
|
173
|
-
templates.push(...uno.config.autocomplete || [], ...uno.config.rulesDynamic.flatMap((i) => toArray(i?.[2]?.autocomplete || [])));
|
|
220
|
+
templates.push(...uno.config.autocomplete.templates || [], ...uno.config.rulesDynamic.flatMap((i) => toArray(i?.[2]?.autocomplete || [])));
|
|
174
221
|
}
|
|
175
222
|
function processSuggestions(suggestions, prefix = "", suffix = "") {
|
|
176
223
|
return uniq(suggestions.flat()).filter((i) => !!(i && !i.match(/-$/))).sort((a, b) => {
|
|
@@ -183,19 +230,4 @@ function createAutocomplete(uno) {
|
|
|
183
230
|
}
|
|
184
231
|
}
|
|
185
232
|
|
|
186
|
-
function searchUsageBoundary(line, index) {
|
|
187
|
-
let start = index;
|
|
188
|
-
let end = index;
|
|
189
|
-
const regex = /[^\s>"'`]/;
|
|
190
|
-
while (start && regex.test(line.charAt(start - 1)))
|
|
191
|
-
--start;
|
|
192
|
-
while (end < line.length && regex.test(line.charAt(end)))
|
|
193
|
-
++end;
|
|
194
|
-
return {
|
|
195
|
-
content: line.slice(start, end),
|
|
196
|
-
start,
|
|
197
|
-
end
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
233
|
export { createAutocomplete, ignoredThemeKeys, parseAutocomplete, searchUsageBoundary, shorthands };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unocss/autocomplete",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Autocomplete utils for UnoCSS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"unocss",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
],
|
|
33
33
|
"sideEffects": false,
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"lru-cache": "^7.
|
|
35
|
+
"lru-cache": "^7.7.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/lru-cache": "^7.
|
|
39
|
-
"@unocss/core": "0.
|
|
38
|
+
"@types/lru-cache": "^7.5.0",
|
|
39
|
+
"@unocss/core": "0.30.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "unbuild",
|