@stillwind/host 0.3.0 → 0.4.1
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/browser.d.ts +2 -2
- package/dist/browser.js +2 -2
- package/dist/{index-CprPvXxv.d.ts → index-DAGqsQ8n.d.ts} +104 -2
- package/dist/index.d.ts +48 -6
- package/dist/index.js +155 -13
- package/dist/index.js.map +1 -1
- package/dist/{options-BrXyddsL.js → options-BOutD9j7.js} +2189 -1989
- package/dist/options-BOutD9j7.js.map +1 -0
- package/package.json +5 -5
- package/dist/options-BrXyddsL.js.map +0 -1
|
@@ -1996,2300 +1996,2500 @@ function markerDocumentation(marker) {
|
|
|
1996
1996
|
function list(items) {
|
|
1997
1997
|
return items.length < 3 ? items.join(" and ") : `${items.slice(0, -1).join(", ")}, and ${items.at(-1)}`;
|
|
1998
1998
|
}
|
|
1999
|
-
// src/editor/
|
|
1999
|
+
// src/editor/design-system.ts
|
|
2000
2000
|
/**
|
|
2001
|
-
*
|
|
2001
|
+
* Tests whether a target contains a wildcard outside brackets and parentheses.
|
|
2002
2002
|
*
|
|
2003
|
-
* @param
|
|
2004
|
-
* @returns
|
|
2003
|
+
* @param target - Decoded target text.
|
|
2004
|
+
* @returns Whether the target is a pattern.
|
|
2005
2005
|
*/
|
|
2006
|
-
function
|
|
2007
|
-
return
|
|
2006
|
+
function isPattern(target) {
|
|
2007
|
+
return segments(target, "*").length > 1;
|
|
2008
2008
|
}
|
|
2009
|
-
// src/editor/source/order.ts
|
|
2010
2009
|
/**
|
|
2011
|
-
*
|
|
2010
|
+
* Compiles a target whose `*` matches any text, including `/` and `:`.
|
|
2012
2011
|
*
|
|
2013
|
-
*
|
|
2014
|
-
*
|
|
2015
|
-
* category retains its input order.
|
|
2016
|
-
*
|
|
2017
|
-
* @param items - Inputs in their current order, with a distinct object for each occurrence.
|
|
2018
|
-
* @param allows - Optional predicate that approves the original range covering each adjacent
|
|
2019
|
-
* exchange. Rejected exchanges can prevent the result from reaching the full category order.
|
|
2020
|
-
* @returns A new array of the original objects. The input array and its objects remain unchanged.
|
|
2012
|
+
* @param target - Decoded pattern.
|
|
2013
|
+
* @returns An anchored expression.
|
|
2021
2014
|
*/
|
|
2022
|
-
function
|
|
2023
|
-
const
|
|
2024
|
-
|
|
2025
|
-
for (let end = 0; end <= items.length; end++) if (end === items.length || items[end].order.kind === "barrier") {
|
|
2026
|
-
sorted.push(...orderRun(items.slice(start, end), allows));
|
|
2027
|
-
if (end < items.length) sorted.push(items[end]);
|
|
2028
|
-
start = end + 1;
|
|
2029
|
-
}
|
|
2030
|
-
return sorted;
|
|
2015
|
+
function patternExpression(target) {
|
|
2016
|
+
const parts = segments(target, "*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
2017
|
+
return new RegExp(`^${parts.join(".*")}$`, "s");
|
|
2031
2018
|
}
|
|
2032
2019
|
/**
|
|
2033
|
-
*
|
|
2020
|
+
* Answers a theme request.
|
|
2034
2021
|
*
|
|
2035
|
-
* @param
|
|
2036
|
-
* @param
|
|
2037
|
-
* @returns
|
|
2022
|
+
* @param context - Loaded design system.
|
|
2023
|
+
* @param request - Theme request.
|
|
2024
|
+
* @returns Tokens that match, or every token for an empty target list, and unmatched targets.
|
|
2038
2025
|
*/
|
|
2039
|
-
function
|
|
2040
|
-
const
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2026
|
+
function theme(context, request) {
|
|
2027
|
+
const tokens = context.query.themeTokens().map(({ name, value, fallback, inline }) => ({
|
|
2028
|
+
name,
|
|
2029
|
+
value,
|
|
2030
|
+
source: fallback ? "default" : "project",
|
|
2031
|
+
inline
|
|
2032
|
+
})).filter(({ source }) => !request.source || source === request.source);
|
|
2033
|
+
const targets = request.targets.map((target) => ({
|
|
2034
|
+
target,
|
|
2035
|
+
expression: patternExpression(`--${target.replace(/^--/, "")}`)
|
|
2036
|
+
}));
|
|
2037
|
+
return {
|
|
2038
|
+
tokens: targets.length ? tokens.filter(({ name }) => targets.some(({ expression }) => expression.test(name))) : tokens,
|
|
2039
|
+
candidates: [],
|
|
2040
|
+
unmatched: targets.filter(({ expression }) => !tokens.some(({ name }) => expression.test(name))).map(({ target }) => target)
|
|
2045
2041
|
};
|
|
2046
|
-
for (const item of items) if (item.order.kind === "ref") {
|
|
2047
|
-
let group = refs.get(item.order.identity);
|
|
2048
|
-
if (!group) {
|
|
2049
|
-
group = {
|
|
2050
|
-
markers: [],
|
|
2051
|
-
projections: []
|
|
2052
|
-
};
|
|
2053
|
-
refs.set(item.order.identity, group);
|
|
2054
|
-
}
|
|
2055
|
-
group[item.order.projection ? "projections" : "markers"].push(item);
|
|
2056
|
-
} else if (item.order.kind !== "barrier") groups[item.order.kind].push(item);
|
|
2057
|
-
const ordered = [
|
|
2058
|
-
...[...refs.values()].flatMap(({ markers, projections }) => [...markers, ...projections]),
|
|
2059
|
-
...groups.bases,
|
|
2060
|
-
...groups.utilities,
|
|
2061
|
-
...groups.customProperties
|
|
2062
|
-
];
|
|
2063
|
-
if (!allows) return ordered;
|
|
2064
|
-
const positions = new Map(ordered.map((item, index) => [item, index]));
|
|
2065
|
-
const sorted = [...items];
|
|
2066
|
-
for (let position = 1; position < sorted.length; position++) for (let cursor = position; cursor > 0; cursor--) {
|
|
2067
|
-
const left = sorted[cursor - 1];
|
|
2068
|
-
const right = sorted[cursor];
|
|
2069
|
-
if (positions.get(left) <= positions.get(right) || !allows(Math.min(left.start, right.start), Math.max(left.end, right.end))) break;
|
|
2070
|
-
[sorted[cursor - 1], sorted[cursor]] = [right, left];
|
|
2071
|
-
}
|
|
2072
|
-
return sorted;
|
|
2073
2042
|
}
|
|
2074
2043
|
/**
|
|
2075
|
-
*
|
|
2044
|
+
* Describes one candidate.
|
|
2076
2045
|
*
|
|
2077
|
-
* @param
|
|
2078
|
-
* @param
|
|
2079
|
-
* @param
|
|
2080
|
-
* @
|
|
2046
|
+
* @param query - Query that contains the definitions of the plugin candidates in the request.
|
|
2047
|
+
* @param candidates - Preview cache over that query.
|
|
2048
|
+
* @param annotations - Preview comment settings.
|
|
2049
|
+
* @param name - Decoded candidate, or a variant name without its trailing colon.
|
|
2050
|
+
* @param kind - Selects utility or variant resolution.
|
|
2051
|
+
* @returns The candidate, with a suggestion when it is invalid.
|
|
2052
|
+
* @throws If a matching utility definition is malformed.
|
|
2081
2053
|
*/
|
|
2082
|
-
function
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2054
|
+
function describe(query, candidates, annotations, name, kind) {
|
|
2055
|
+
const css = kind === "variants" ? candidates.variant(name, "utility") : candidates.preview(name);
|
|
2056
|
+
const marker = kind === "utilities" ? candidates.marker(name) : null;
|
|
2057
|
+
if (css || marker) return {
|
|
2058
|
+
name,
|
|
2059
|
+
kind: kind === "variants" ? "variant" : marker ? "marker" : "utility",
|
|
2060
|
+
valid: true,
|
|
2061
|
+
css: css ? query.formatCss(css, annotations) : ""
|
|
2062
|
+
};
|
|
2063
|
+
const suggestion = kind === "variants" ? query.suggestCandidate(`${name}:flex`)?.replace(/:flex$/, "") : query.suggestCandidate(name);
|
|
2064
|
+
return {
|
|
2065
|
+
name,
|
|
2066
|
+
kind: kind === "variants" ? "variant" : "utility",
|
|
2067
|
+
valid: false,
|
|
2068
|
+
css: "",
|
|
2069
|
+
...suggestion ? { suggestion } : {}
|
|
2096
2070
|
};
|
|
2097
|
-
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && [
|
|
2098
|
-
"asAnchor",
|
|
2099
|
-
"asContainer",
|
|
2100
|
-
"positionAnchor"
|
|
2101
|
-
].includes(node.expression.name.text)) {
|
|
2102
|
-
const ref = resolveAsset(source, node.expression.expression);
|
|
2103
|
-
if (ref?.identity.kind === "ref") return {
|
|
2104
|
-
kind: "ref",
|
|
2105
|
-
identity: JSON.stringify(ref.identity),
|
|
2106
|
-
projection: true
|
|
2107
|
-
};
|
|
2108
|
-
}
|
|
2109
|
-
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) return classifyInput(source, node.expression, resolveAsset);
|
|
2110
|
-
if (ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node)) {
|
|
2111
|
-
const orders = (ts.isArrayLiteralExpression(node) ? node.elements : node.properties).map((element) => classifyInput(source, element, resolveAsset));
|
|
2112
|
-
const first = orders[0];
|
|
2113
|
-
if (first && first.kind !== "barrier" && first.kind !== "ref" && orders.every((order) => order.kind === first.kind)) return first;
|
|
2114
|
-
}
|
|
2115
|
-
return { kind: "barrier" };
|
|
2116
2071
|
}
|
|
2117
|
-
// src/editor/sort.ts
|
|
2118
2072
|
/**
|
|
2119
|
-
*
|
|
2120
|
-
* Literal sorting applies only to ordinary utilities. Containers are processed from the
|
|
2121
|
-
* smallest to the largest so an outer replacement includes edits to nested inputs.
|
|
2122
|
-
* Unknown syntax, ambiguous comments, and text that cannot map exactly delimit grouping.
|
|
2073
|
+
* Returns the canonical spelling of a valid candidate under the lint options of the project.
|
|
2123
2074
|
*
|
|
2124
|
-
* @param
|
|
2125
|
-
* @param
|
|
2126
|
-
* @param
|
|
2127
|
-
* @
|
|
2128
|
-
* @
|
|
2129
|
-
* literal, each edit must replace a complete utility token. Edits inside grouped containers must
|
|
2130
|
-
* stay within one input. Replacement text must contain any JS escapes needed at its range.
|
|
2131
|
-
* @param allows - Optional predicate that approves a whole literal's content range or the
|
|
2132
|
-
* range covering an adjacent input exchange. Every range uses original UTF-16 coordinates.
|
|
2133
|
-
* @returns Edits in ascending original offset order. The source and seed array remain unchanged.
|
|
2134
|
-
* @throws If a matching utility definition has a malformed requirement or native ranking fails.
|
|
2075
|
+
* @param query - Query that contains the definitions of the plugin candidates in the request.
|
|
2076
|
+
* @param name - Decoded candidate, or a variant name without its trailing colon.
|
|
2077
|
+
* @param kind - Selects utility or variant spelling.
|
|
2078
|
+
* @returns The spelling, or undefined when `name` has no different canonical spelling.
|
|
2079
|
+
* @throws If a matching utility definition is malformed.
|
|
2135
2080
|
*/
|
|
2136
|
-
function
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2081
|
+
function canonicalSpelling(query, name, kind) {
|
|
2082
|
+
return kind === "variants" ? query.canonicalCandidate(`${name}:flex`)?.replace(/:flex$/, "") : query.canonicalCandidate(name) ?? void 0;
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Answers a utility or variant request.
|
|
2086
|
+
*
|
|
2087
|
+
* @param context - Loaded design system.
|
|
2088
|
+
* @param request - Candidate request. An empty target list means every listed name.
|
|
2089
|
+
* @returns Exact candidates, then the names that match patterns. Patterns read the completion
|
|
2090
|
+
* catalog, so they omit numeric and arbitrary values that exact candidates accept.
|
|
2091
|
+
* @throws If a plugin fails or a matching utility definition is malformed.
|
|
2092
|
+
*/
|
|
2093
|
+
async function candidates(context, request) {
|
|
2094
|
+
const { kind } = request;
|
|
2095
|
+
const catalog = new CandidateQueries(context.query);
|
|
2096
|
+
const targets = request.targets.length ? request.targets : ["*"];
|
|
2097
|
+
const exact = targets.filter((target) => !isPattern(target));
|
|
2098
|
+
const patterns = [];
|
|
2099
|
+
for (const target of targets.filter(isPattern)) {
|
|
2100
|
+
const expression = patternExpression(target);
|
|
2101
|
+
const prefix = segments(target, "*")[0];
|
|
2102
|
+
const names = [...catalog.complete(prefix).map(({ label }) => label), ...await context.plugins?.catalog(prefix) ?? []].flatMap((label) => {
|
|
2103
|
+
const variant = label.endsWith(":");
|
|
2104
|
+
const name = variant ? label.slice(0, -1) : label;
|
|
2105
|
+
return variant === (kind === "variants") && !name.endsWith("-") && expression.test(name) ? [name] : [];
|
|
2106
|
+
});
|
|
2107
|
+
patterns.push({
|
|
2108
|
+
target,
|
|
2109
|
+
names: [...new Set(names)].sort()
|
|
2160
2110
|
});
|
|
2161
|
-
const compare = candidates.order(literals.flatMap(({ literal, tokens }) => tokens.map((token) => literal.prefix + token)));
|
|
2162
|
-
for (const { literal, original, tokens, matches, spellings } of literals) {
|
|
2163
|
-
const start = literal.boundaries[0];
|
|
2164
|
-
const end = literal.boundaries.at(-1);
|
|
2165
|
-
const sorted = sortRuns(tokens, (a, b) => compare(literal.prefix + a, literal.prefix + b));
|
|
2166
|
-
if (sorted.every((token, position) => token === original[position])) continue;
|
|
2167
|
-
let cursor = start;
|
|
2168
|
-
let newText = "";
|
|
2169
|
-
const queues = /* @__PURE__ */ new Map();
|
|
2170
|
-
for (const spelling of spellings) {
|
|
2171
|
-
let queue = queues.get(spelling.token);
|
|
2172
|
-
if (!queue) {
|
|
2173
|
-
queue = {
|
|
2174
|
-
texts: [],
|
|
2175
|
-
position: 0
|
|
2176
|
-
};
|
|
2177
|
-
queues.set(spelling.token, queue);
|
|
2178
|
-
}
|
|
2179
|
-
queue.texts.push(spelling.text);
|
|
2180
|
-
}
|
|
2181
|
-
for (const [position, match] of matches.entries()) {
|
|
2182
|
-
const tokenStart = literal.boundaries[match.index];
|
|
2183
|
-
const tokenEnd = literal.boundaries[match.index + match[0].length];
|
|
2184
|
-
const queue = queues.get(sorted[position]);
|
|
2185
|
-
newText += source.document.code.slice(cursor, tokenStart) + queue.texts[queue.position++];
|
|
2186
|
-
cursor = tokenEnd;
|
|
2187
|
-
}
|
|
2188
|
-
newText += source.document.code.slice(cursor, end);
|
|
2189
|
-
edits = absorb(edits, {
|
|
2190
|
-
start,
|
|
2191
|
-
end,
|
|
2192
|
-
newText
|
|
2193
|
-
});
|
|
2194
|
-
}
|
|
2195
2111
|
}
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
end: edit.end - start
|
|
2209
|
-
}))) : "";
|
|
2210
|
-
const order = index.inputOrder(source, node);
|
|
2211
|
-
const ambiguousComment = /^[^\r\n]*(?:\/\/|\/\*)/.test(text) && node.pos > elements.pos;
|
|
2212
|
-
return {
|
|
2213
|
-
node,
|
|
2214
|
-
start,
|
|
2215
|
-
end,
|
|
2216
|
-
text,
|
|
2217
|
-
order: range && !ambiguousComment && source.document.code.slice(start, end) === node.getSourceFile().text.slice(node.getFullStart(), node.end) ? order : { kind: "barrier" }
|
|
2218
|
-
};
|
|
2219
|
-
});
|
|
2220
|
-
for (let position = 0; position < items.length; position++) {
|
|
2221
|
-
const item = items[position];
|
|
2222
|
-
const next = items[position + 1];
|
|
2223
|
-
const tail = source.document.code.slice(item.end, next?.start ?? index.range(source, container.node)?.[1] ?? item.end);
|
|
2224
|
-
if (/^[^\r\n]*(?:\/\/|\/\*)/.test(tail) || next && /^[^\r\n]*(?:\/\/|\/\*)/.test(next.text.slice(0, next.text.length - (next.node.end - next.node.getStart())))) item.order = { kind: "barrier" };
|
|
2112
|
+
const dynamic = [...exact, ...patterns.flatMap(({ names }) => names)].map((name) => kind === "variants" ? `${name}:` : name).filter((text) => context.query.dynamicCandidates(text).length > 0);
|
|
2113
|
+
const query = context.plugins && dynamic.length ? await context.plugins.query(dynamic.join(" ")) : context.query;
|
|
2114
|
+
const queries = query === context.query ? catalog : new CandidateQueries(query);
|
|
2115
|
+
const seen = new Set(exact);
|
|
2116
|
+
const listed = [];
|
|
2117
|
+
const unmatched = [];
|
|
2118
|
+
for (const { target, names } of patterns) {
|
|
2119
|
+
let matched = false;
|
|
2120
|
+
for (const name of names) {
|
|
2121
|
+
if (seen.has(name)) {
|
|
2122
|
+
matched = true;
|
|
2123
|
+
continue;
|
|
2225
2124
|
}
|
|
2226
|
-
const
|
|
2227
|
-
if (
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
continue;
|
|
2232
|
-
}
|
|
2233
|
-
const start = position++;
|
|
2234
|
-
while (position < items.length && items[position] !== sorted[position]) position++;
|
|
2235
|
-
if (items.slice(start, position).some((item) => item.order.kind === "barrier")) continue;
|
|
2236
|
-
const edit = {
|
|
2237
|
-
start: items[start].start,
|
|
2238
|
-
end: items[position - 1].end,
|
|
2239
|
-
newText: sorted.slice(start, position).map((item) => item.text).join(",")
|
|
2240
|
-
};
|
|
2241
|
-
edits = absorb(edits, edit);
|
|
2125
|
+
const candidate = describe(query, queries, context.annotations, name, kind);
|
|
2126
|
+
if (candidate.valid) {
|
|
2127
|
+
matched = true;
|
|
2128
|
+
seen.add(name);
|
|
2129
|
+
listed.push(candidate);
|
|
2242
2130
|
}
|
|
2243
2131
|
}
|
|
2132
|
+
if (!matched) unmatched.push(target);
|
|
2244
2133
|
}
|
|
2245
|
-
return
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
return source;
|
|
2134
|
+
return {
|
|
2135
|
+
tokens: [],
|
|
2136
|
+
candidates: [...exact.map((name) => {
|
|
2137
|
+
const candidate = describe(query, queries, context.annotations, name, kind);
|
|
2138
|
+
const canonical = candidate.valid ? canonicalSpelling(query, name, kind) : void 0;
|
|
2139
|
+
return canonical ? {
|
|
2140
|
+
...candidate,
|
|
2141
|
+
canonical
|
|
2142
|
+
} : candidate;
|
|
2143
|
+
}), ...listed],
|
|
2144
|
+
unmatched
|
|
2145
|
+
};
|
|
2258
2146
|
}
|
|
2259
2147
|
/**
|
|
2260
|
-
*
|
|
2148
|
+
* Answers a question about a loaded design system.
|
|
2261
2149
|
*
|
|
2262
|
-
* @param
|
|
2263
|
-
* @param
|
|
2264
|
-
* @returns
|
|
2265
|
-
*
|
|
2150
|
+
* @param context - Loaded design system.
|
|
2151
|
+
* @param request - Theme, utility, or variant request.
|
|
2152
|
+
* @returns The answer. Invalid names and unmatched targets are data.
|
|
2153
|
+
* @throws If a plugin fails or a matching utility definition is malformed.
|
|
2266
2154
|
*/
|
|
2267
|
-
function
|
|
2268
|
-
|
|
2269
|
-
let escaped = text.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`).replaceAll(" ", "\\t").replaceAll("\r", "\\r").replaceAll("\n", "\\n");
|
|
2270
|
-
if (quote === "`") escaped = escaped.replaceAll("${", "\\${");
|
|
2271
|
-
return escaped;
|
|
2155
|
+
function queryDesignSystem(context, request) {
|
|
2156
|
+
return request.kind === "theme" ? Promise.resolve(theme(context, request)) : candidates(context, request);
|
|
2272
2157
|
}
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
*/
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
/**
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
* @param edits - Existing edits in original coordinates. Every overlapping edit must be fully
|
|
2290
|
-
* contained in `replacement` and already reflected in its text.
|
|
2291
|
-
* @param replacement - Replacement whose text includes all contained edits.
|
|
2292
|
-
* @returns A new array with overlapping edits removed and the replacement appended.
|
|
2293
|
-
* The result is not sorted, and the input edit objects remain unchanged.
|
|
2294
|
-
*/
|
|
2295
|
-
function absorb(edits, replacement) {
|
|
2296
|
-
return [...edits.filter((edit) => edit.end <= replacement.start || edit.start >= replacement.end), replacement];
|
|
2297
|
-
}
|
|
2298
|
-
/**
|
|
2299
|
-
* Sorts contiguous runs of resolved utilities while keeping unresolved spellings in place.
|
|
2300
|
-
*
|
|
2301
|
-
* @param values - Decoded candidates in their original order.
|
|
2302
|
-
* @param compare - Compiler comparator prepared for these spellings. A null self-comparison
|
|
2303
|
-
* marks a boundary, and a null comparison within a run treats that pair as equal.
|
|
2304
|
-
* @returns A new array with stable ordering inside each run. `values` remains unchanged.
|
|
2305
|
-
*/
|
|
2306
|
-
function sortRuns(values, compare) {
|
|
2307
|
-
const result = [...values];
|
|
2308
|
-
let start = 0;
|
|
2309
|
-
for (let position = 0; position <= values.length; position++) if (position === values.length || compare(values[position], values[position]) === null) {
|
|
2310
|
-
result.splice(start, position - start, ...values.slice(start, position).sort((a, b) => compare(a, b) ?? 0));
|
|
2311
|
-
start = position + 1;
|
|
2158
|
+
// src/compilation/project.ts
|
|
2159
|
+
/** Keeps prepared sources and checker inputs between analyses of one project. */
|
|
2160
|
+
var CompilationProject = class {
|
|
2161
|
+
/** Sources keyed by normalized resolver ID. Windows paths ignore case. */
|
|
2162
|
+
#sources = /* @__PURE__ */ new Map();
|
|
2163
|
+
/** Import answers for the currently installed checker inputs. */
|
|
2164
|
+
#resolutions;
|
|
2165
|
+
/** Advances on invalidation so pending loads cannot publish old sources. */
|
|
2166
|
+
#revision = 0;
|
|
2167
|
+
/** Advances when sources or import links change so checker inputs are installed again. */
|
|
2168
|
+
#generation = 0;
|
|
2169
|
+
/** Last backend and inputs installed by this project. */
|
|
2170
|
+
#installed;
|
|
2171
|
+
/** Revision that a pending load must still match before it stores a source. */
|
|
2172
|
+
get revision() {
|
|
2173
|
+
return this.#revision;
|
|
2312
2174
|
}
|
|
2313
|
-
return result;
|
|
2314
|
-
}
|
|
2315
|
-
// src/semantics/checker-source.ts
|
|
2316
|
-
/**
|
|
2317
|
-
* Serializes a declaration identity as a stable checker string-literal key.
|
|
2318
|
-
*
|
|
2319
|
-
* @param identity - Native declaration identity with original UTF-8 offsets.
|
|
2320
|
-
* @returns `stillwind-identity/v1:` followed by base64url JSON of kind, module ID, range, and
|
|
2321
|
-
* binding name in a fixed field order.
|
|
2322
|
-
*/
|
|
2323
|
-
function identityKey(identity) {
|
|
2324
|
-
return `stillwind-identity/v1:${base64url(JSON.stringify({
|
|
2325
|
-
kind: identity.kind,
|
|
2326
|
-
moduleId: identity.moduleId,
|
|
2327
|
-
start: identity.start,
|
|
2328
|
-
end: identity.end,
|
|
2329
|
-
bindingName: identity.bindingName
|
|
2330
|
-
}))}`;
|
|
2331
|
-
}
|
|
2332
|
-
/**
|
|
2333
|
-
* Selects the checker filename for a prepared source.
|
|
2334
|
-
*
|
|
2335
|
-
* @param source - Source with its host checker filename and any projection syntax.
|
|
2336
|
-
* @returns The host filename for direct input, or that filename followed by the projection language
|
|
2337
|
-
* extension for projected input.
|
|
2338
|
-
*/
|
|
2339
|
-
function checkerId(source) {
|
|
2340
|
-
return source.kind === "direct" ? source.checkerFileName : `${source.checkerFileName}.${source.projection.syntax.language}`;
|
|
2341
|
-
}
|
|
2342
|
-
/**
|
|
2343
|
-
* Applies the current query's probes to the project's sources without changing other inputs.
|
|
2344
|
-
*
|
|
2345
|
-
* @param inventory - Sources currently installed in the checker.
|
|
2346
|
-
* @param requested - Sources whose probe ranges or recognitionOnly settings may have changed.
|
|
2347
|
-
* Sources outside inventory are ignored.
|
|
2348
|
-
* @returns The original array when probes match, or a new array with changed source records.
|
|
2349
|
-
* Neither input is mutated. Unchanged source records are shared.
|
|
2350
|
-
*/
|
|
2351
|
-
function withRequestedProbes(inventory, requested) {
|
|
2352
|
-
const probesById = new Map(requested.map((source) => [source.resolverId, source.probes]));
|
|
2353
|
-
let changed = false;
|
|
2354
|
-
const sources = inventory.map((source) => {
|
|
2355
|
-
const probes = probesById.get(source.resolverId);
|
|
2356
|
-
if (!probes || probes.length === source.probes.length && probes.every((probe, index) => probe.start === source.probes[index].start && probe.end === source.probes[index].end && probe.recognitionOnly === source.probes[index].recognitionOnly)) return source;
|
|
2357
|
-
changed = true;
|
|
2358
|
-
return {
|
|
2359
|
-
...source,
|
|
2360
|
-
probes
|
|
2361
|
-
};
|
|
2362
|
-
});
|
|
2363
|
-
return changed ? sources : inventory;
|
|
2364
|
-
}
|
|
2365
|
-
/**
|
|
2366
|
-
* Checker buffer with declaration identities, resolved import paths, and helper aliases inserted
|
|
2367
|
-
* for type queries. Its range maps relate checker AST expressions to original document bytes.
|
|
2368
|
-
*/
|
|
2369
|
-
var CheckerSource = class {
|
|
2370
|
-
/** Prepared source and original-coordinate requests that own this checker buffer. */
|
|
2371
|
-
source;
|
|
2372
|
-
/** Unedited input buffer, optionally restricted to one original script region. */
|
|
2373
|
-
buffer;
|
|
2374
2175
|
/**
|
|
2375
|
-
*
|
|
2376
|
-
*
|
|
2176
|
+
* Finds a source prepared since its last invalidation.
|
|
2177
|
+
*
|
|
2178
|
+
* @param id - Full resolver ID. Windows path spelling may differ in case or slashes.
|
|
2179
|
+
* @returns The shared source, or undefined when it has not been prepared. Do not mutate it.
|
|
2377
2180
|
*/
|
|
2378
|
-
|
|
2181
|
+
source(id) {
|
|
2182
|
+
return this.#sources.get(sourceKey(id))?.source;
|
|
2183
|
+
}
|
|
2184
|
+
/** Returns the normalized identities of the prepared sources, as {@link sourceKey} defines them. */
|
|
2185
|
+
sourceKeys() {
|
|
2186
|
+
return [...this.#sources.keys()];
|
|
2187
|
+
}
|
|
2188
|
+
/** Returns the prepared documents' original text so plugins can find candidates together. */
|
|
2189
|
+
texts() {
|
|
2190
|
+
return [...this.#sources.values()].map((record) => record.source.document.code);
|
|
2191
|
+
}
|
|
2379
2192
|
/**
|
|
2380
|
-
*
|
|
2193
|
+
* Finds the scan results for a prepared source.
|
|
2194
|
+
*
|
|
2195
|
+
* @param source - Source whose text has not changed since preparation.
|
|
2196
|
+
* @returns Shared scan results, or undefined when the source is not retained. Do not mutate them.
|
|
2381
2197
|
*/
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
/** Independent module that supplies helper declarations in the virtual project. */
|
|
2386
|
-
helperModule;
|
|
2387
|
-
/** Shadow UTF-16 AST ranges keyed to original UTF-8 probe ranges. */
|
|
2388
|
-
probes = /* @__PURE__ */ new Map();
|
|
2389
|
-
/** Producer call ranges after identity insertion, keyed to their asset families. */
|
|
2390
|
-
producers = /* @__PURE__ */ new Map();
|
|
2391
|
-
/** Authored module specifiers keyed by their rewritten checker UTF-16 ranges. */
|
|
2392
|
-
moduleSpecifiers = /* @__PURE__ */ new Map();
|
|
2198
|
+
summary(source) {
|
|
2199
|
+
return this.#sources.get(sourceKey(source.resolverId))?.summary;
|
|
2200
|
+
}
|
|
2393
2201
|
/**
|
|
2394
|
-
*
|
|
2395
|
-
*
|
|
2202
|
+
* Stores a source and its successful scan results, copying the source before retaining it.
|
|
2203
|
+
*
|
|
2204
|
+
* @param source - Source passed to the scanner.
|
|
2205
|
+
* @param summary - Successful scan results for that source. Do not mutate them after this call.
|
|
2206
|
+
* @param revision - Revision captured before loading. Defaults to the current revision.
|
|
2207
|
+
* Old revisions and sources already stored under the same ID have no effect.
|
|
2396
2208
|
*/
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2209
|
+
retain(source, summary, revision = this.#revision) {
|
|
2210
|
+
if (revision !== this.#revision || this.#sources.has(sourceKey(source.resolverId))) return;
|
|
2211
|
+
source = structuredClone(source);
|
|
2212
|
+
this.#sources.set(sourceKey(source.resolverId), {
|
|
2213
|
+
source,
|
|
2214
|
+
summary,
|
|
2215
|
+
linked: false,
|
|
2216
|
+
resolutions: []
|
|
2217
|
+
});
|
|
2218
|
+
this.#changed();
|
|
2219
|
+
}
|
|
2400
2220
|
/**
|
|
2401
|
-
*
|
|
2402
|
-
*
|
|
2221
|
+
* Loads and scans targets together, then resolves their imports through the host.
|
|
2222
|
+
* Analysis retries failed loads and scans so it can report each target's own error.
|
|
2223
|
+
*
|
|
2224
|
+
* @param ids - Full resolver IDs of the targets to prepare.
|
|
2225
|
+
* @param host - Host used by later analysis of these targets.
|
|
2226
|
+
* @param session - Initialized native session that will analyze the prepared sources.
|
|
2227
|
+
* @returns Completion of preparation. Invalidation stops old work from storing results.
|
|
2228
|
+
* @throws If host import resolution fails. Source loading and scan failures are left for analysis.
|
|
2403
2229
|
*/
|
|
2404
|
-
|
|
2230
|
+
async prepare(ids, host, session) {
|
|
2231
|
+
const revision = this.#revision;
|
|
2232
|
+
await Promise.allSettled(ids.map((id) => this.#load(id, host, session, revision)));
|
|
2233
|
+
if (revision !== this.#revision) return;
|
|
2234
|
+
for (const record of this.#sources.values()) {
|
|
2235
|
+
const { source, summary } = record;
|
|
2236
|
+
if (record.linked) continue;
|
|
2237
|
+
const resolutions = await Promise.all(summary.moduleSpecifiers.map(async (specifier) => {
|
|
2238
|
+
const resolved = await host.resolve(specifier.source, source.resolverId, { skipSelf: true });
|
|
2239
|
+
if (!resolved || resolved.external) return;
|
|
2240
|
+
await this.#load(resolved.id, host, session, revision).catch(() => {});
|
|
2241
|
+
return {
|
|
2242
|
+
id: this.source(resolved.id)?.resolverId ?? resolved.id,
|
|
2243
|
+
start: specifier.start,
|
|
2244
|
+
end: specifier.end
|
|
2245
|
+
};
|
|
2246
|
+
}));
|
|
2247
|
+
if (revision !== this.#revision) return;
|
|
2248
|
+
record.linked = true;
|
|
2249
|
+
this.#setResolutions(record, resolutions.filter((value) => value !== void 0));
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2405
2252
|
/**
|
|
2406
|
-
*
|
|
2253
|
+
* Removes changed sources and clears all import links and cached resolution answers.
|
|
2407
2254
|
*
|
|
2408
|
-
* @param
|
|
2409
|
-
*
|
|
2410
|
-
* @param buffer - Direct or projected checker text, optionally scoped to an original script
|
|
2411
|
-
* region.
|
|
2412
|
-
* @param index - Buffer index used to give independent client scripts distinct filenames.
|
|
2413
|
-
* @param assets - Native declaration records with original-byte producer call ranges. Repeated
|
|
2414
|
-
* records for the same generated call are inserted once.
|
|
2415
|
-
* @param ids - Resolved source identities mapped to their main checker filenames for import
|
|
2416
|
-
* rewriting.
|
|
2417
|
-
* @throws If a byte boundary is invalid, producer identities conflict, a mapped producer does not
|
|
2418
|
-
* end with a closing parenthesis, edits overlap, or projection decoding fails.
|
|
2255
|
+
* @param ids - One changed resolver ID or several IDs. Omission removes every source.
|
|
2256
|
+
* Advances the revision so pending loads cannot restore the removed sources.
|
|
2419
2257
|
*/
|
|
2420
|
-
|
|
2421
|
-
this
|
|
2422
|
-
this.
|
|
2423
|
-
|
|
2424
|
-
this.#
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
for (const asset of assets) {
|
|
2429
|
-
const targets = this.targets(asset.callStart, asset.callEnd, "producer");
|
|
2430
|
-
const key = identityKey(asset.identity);
|
|
2431
|
-
this.identities.set(key, asset.identity);
|
|
2432
|
-
for (const [start, end] of targets) {
|
|
2433
|
-
const previous = producers.get(end);
|
|
2434
|
-
if (previous) {
|
|
2435
|
-
if (previous !== key) throw new Error(`Conflicting producer identities in ${source.moduleId}`);
|
|
2436
|
-
continue;
|
|
2437
|
-
}
|
|
2438
|
-
producers.set(end, key);
|
|
2439
|
-
producerRanges.push({
|
|
2440
|
-
start,
|
|
2441
|
-
end,
|
|
2442
|
-
kind: asset.identity.kind
|
|
2443
|
-
});
|
|
2444
|
-
const call = buffer.source.slice(start, end);
|
|
2445
|
-
if (!call.endsWith(")")) throw new Error(`Cannot locate producer in ${source.moduleId}`);
|
|
2446
|
-
const separator = [...call.matchAll(/[,(]/g)].some((match) => skipTrivia(call, match.index + 1) === call.length - 1) ? "" : ",";
|
|
2447
|
-
const identity = `{readonly __stillwindDeclarationIdentity:${JSON.stringify(key)}}`;
|
|
2448
|
-
const syntax = source.kind === "direct" ? source.syntax : source.projection.syntax;
|
|
2449
|
-
const argument = syntax.language === "js" || syntax.language === "jsx" ? `/** @type {${identity}} */ (/** @type {unknown} */ (undefined))` : `undefined as unknown as ${identity}`;
|
|
2450
|
-
this.#edits.push({
|
|
2451
|
-
at: end - 1,
|
|
2452
|
-
end: end - 1,
|
|
2453
|
-
original: {
|
|
2454
|
-
start: asset.callStart,
|
|
2455
|
-
end: asset.callEnd
|
|
2456
|
-
},
|
|
2457
|
-
text: `${separator}${argument}`
|
|
2458
|
-
});
|
|
2459
|
-
}
|
|
2460
|
-
}
|
|
2461
|
-
const specifiers = [];
|
|
2462
|
-
for (const resolution of source.resolutions ?? []) for (const [at, end] of this.targets(resolution.start, resolution.end, "moduleSpecifier")) {
|
|
2463
|
-
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true);
|
|
2464
|
-
scanner.setText(buffer.source.slice(at, end));
|
|
2465
|
-
scanner.scan();
|
|
2466
|
-
specifiers.push({
|
|
2467
|
-
start: at,
|
|
2468
|
-
end,
|
|
2469
|
-
text: scanner.getTokenValue()
|
|
2470
|
-
});
|
|
2471
|
-
this.#edits.push({
|
|
2472
|
-
at,
|
|
2473
|
-
end,
|
|
2474
|
-
original: {
|
|
2475
|
-
start: resolution.start,
|
|
2476
|
-
end: resolution.end
|
|
2477
|
-
},
|
|
2478
|
-
text: JSON.stringify(path.resolve(ids.get(resolution.id) ?? resolution.id).replaceAll("\\", "/"))
|
|
2479
|
-
});
|
|
2258
|
+
invalidate(ids) {
|
|
2259
|
+
this.#revision++;
|
|
2260
|
+
if (ids === void 0) this.#sources.clear();
|
|
2261
|
+
else for (const id of typeof ids === "string" ? [ids] : ids) this.#sources.delete(sourceKey(id));
|
|
2262
|
+
for (const record of this.#sources.values()) {
|
|
2263
|
+
record.resolutions = [];
|
|
2264
|
+
if (record.checker) record.checker.resolutions = record.resolutions;
|
|
2265
|
+
record.linked = false;
|
|
2480
2266
|
}
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2267
|
+
this.#changed();
|
|
2268
|
+
}
|
|
2269
|
+
/**
|
|
2270
|
+
* Installs prepared checker inputs when their sources, import links, or supplied files change.
|
|
2271
|
+
*
|
|
2272
|
+
* @param backend - Backend used by later queries. Receives inputs if it supports setProjectSources.
|
|
2273
|
+
* @param sources - Sources requested by the current analysis, with any known import resolutions.
|
|
2274
|
+
* @param inputs - File overlays and ambient declarations for these sources.
|
|
2275
|
+
* @returns Shared checker sources in the requested order. Do not mutate them.
|
|
2276
|
+
* @throws If a requested source's checker environment or backend installation fails.
|
|
2277
|
+
* Other sources with unavailable framework dependencies are left out of the installed inputs.
|
|
2278
|
+
*/
|
|
2279
|
+
install(backend, sources, inputs) {
|
|
2280
|
+
const requested = sources.map((source) => {
|
|
2281
|
+
const record = this.#sources.get(sourceKey(source.resolverId));
|
|
2282
|
+
if (!record) return source;
|
|
2283
|
+
const checker = this.#checkerSource(record);
|
|
2284
|
+
if (!record.linked) {
|
|
2285
|
+
const resolutions = new Map(record.resolutions.map((item) => [`${item.start}:${item.end}`, item]));
|
|
2286
|
+
for (const item of source.resolutions ?? []) {
|
|
2287
|
+
const target = this.source(item.id);
|
|
2288
|
+
if (target) resolutions.set(`${item.start}:${item.end}`, {
|
|
2289
|
+
...item,
|
|
2290
|
+
id: target.resolverId
|
|
2291
|
+
});
|
|
2496
2292
|
}
|
|
2497
|
-
this.#
|
|
2498
|
-
at: binding.projectedStart,
|
|
2499
|
-
end: binding.projectedEnd,
|
|
2500
|
-
text: alias,
|
|
2501
|
-
original: {
|
|
2502
|
-
start: 0,
|
|
2503
|
-
end: 0
|
|
2504
|
-
}
|
|
2505
|
-
});
|
|
2293
|
+
this.#setResolutions(record, [...resolutions.values()]);
|
|
2506
2294
|
}
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
}
|
|
2295
|
+
return checker;
|
|
2296
|
+
});
|
|
2297
|
+
const all = /* @__PURE__ */ new Map();
|
|
2298
|
+
for (const [id, record] of this.#sources) try {
|
|
2299
|
+
all.set(id, this.#checkerSource(record));
|
|
2300
|
+
} catch {}
|
|
2301
|
+
for (const source of requested) all.set(sourceKey(source.resolverId), source);
|
|
2302
|
+
if (this.#installed?.backend !== backend || this.#installed.generation !== this.#generation || !sameFiles(this.#installed.inputs?.overlayFiles, inputs?.overlayFiles) || !sameFiles(this.#installed.inputs?.ambientDeclarations, inputs?.ambientDeclarations)) {
|
|
2303
|
+
const inventory = [...all.values()].sort((a, b) => {
|
|
2304
|
+
const left = sourceKey(a.resolverId);
|
|
2305
|
+
const right = sourceKey(b.resolverId);
|
|
2306
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
2520
2307
|
});
|
|
2308
|
+
backend.setProjectSources?.(inventory, this.assets(), inputs);
|
|
2309
|
+
this.#installed = {
|
|
2310
|
+
backend,
|
|
2311
|
+
generation: this.#generation,
|
|
2312
|
+
inputs,
|
|
2313
|
+
sources: inventory
|
|
2314
|
+
};
|
|
2315
|
+
this.#resolutions = void 0;
|
|
2521
2316
|
}
|
|
2522
|
-
|
|
2523
|
-
for (let i = 1; i < this.#edits.length; i++) if (this.#edits[i].at < this.#edits[i - 1].end || this.#edits[i].at === this.#edits[i - 1].at) throw new Error(`Overlapping checker edits in ${source.moduleId}`);
|
|
2524
|
-
for (const probe of source.probes) for (const [start, end] of this.targets(probe.start, probe.end, "expression")) this.probes.set(`${this.toShadow(start)}:${this.toShadow(end, true)}`, probe);
|
|
2525
|
-
for (const specifier of specifiers) this.moduleSpecifiers.set(`${this.toShadow(specifier.start)}:${this.toShadow(specifier.end, true)}`, specifier.text);
|
|
2526
|
-
for (const producer of producerRanges) this.producers.set(`${this.toShadow(producer.start)}:${this.toShadow(producer.end, true)}`, producer.kind);
|
|
2527
|
-
let text = buffer.source;
|
|
2528
|
-
for (const edit of [...this.#edits].reverse()) text = text.slice(0, edit.at) + edit.text + text.slice(edit.end);
|
|
2529
|
-
this.text = text;
|
|
2317
|
+
return requested;
|
|
2530
2318
|
}
|
|
2531
2319
|
/**
|
|
2532
|
-
*
|
|
2533
|
-
*
|
|
2534
|
-
* @param start - Inclusive original UTF-8 byte boundary.
|
|
2535
|
-
* @param end - Exclusive original UTF-8 byte boundary.
|
|
2536
|
-
* @param role - Frontend correspondence role, matched exactly for projected input.
|
|
2537
|
-
* @returns Deduplicated UTF-16 intervals. Independent scripts receive only contained ranges.
|
|
2538
|
-
* Their ranges are excluded from the main buffer. Unmapped projected ranges produce an empty
|
|
2539
|
-
* list.
|
|
2540
|
-
* @throws If either original byte boundary is invalid.
|
|
2320
|
+
* @returns Declarations grouped by module ID. The arrays belong to the retained scans.
|
|
2541
2321
|
*/
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
const b = byteToUtf16(this.#original, end);
|
|
2545
|
-
if (this.buffer.region) return a >= this.buffer.region.start && b <= this.buffer.region.end ? [[a - this.buffer.region.start, b - this.buffer.region.start]] : [];
|
|
2546
|
-
if (this.source.checkerBuffers?.some((buffer) => buffer.region && a >= buffer.region.start && b <= buffer.region.end)) return [];
|
|
2547
|
-
if (this.source.kind === "direct") return [[a, b]];
|
|
2548
|
-
return [...new Map((this.source.checkerSpans ?? []).filter((span) => span.start === start && span.end === end && span.role === role).map((span) => [`${span.projectedStart}:${span.projectedEnd}`, [span.projectedStart, span.projectedEnd]])).values()];
|
|
2322
|
+
assets() {
|
|
2323
|
+
return new Map([...this.#sources.values()].map(({ source, summary }) => [source.moduleId, summary.assets]));
|
|
2549
2324
|
}
|
|
2550
2325
|
/**
|
|
2551
|
-
*
|
|
2326
|
+
* Resolves imports and caches the answers until checker inputs change.
|
|
2552
2327
|
*
|
|
2553
|
-
* @param
|
|
2554
|
-
* @param
|
|
2555
|
-
*
|
|
2556
|
-
* @returns
|
|
2557
|
-
*
|
|
2328
|
+
* @param backend - Backend that resolves imports. Backends without setProjectSources query only sources.
|
|
2329
|
+
* @param sources - Sources requested by the current analysis, with any known import resolutions.
|
|
2330
|
+
* @param inputs - File overlays and ambient declarations for this analysis.
|
|
2331
|
+
* @returns Shared maps from resolver ID to authored specifier and resolved ID. Do not mutate them.
|
|
2332
|
+
* @throws If checker input preparation, installation, or import resolution fails.
|
|
2558
2333
|
*/
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2334
|
+
async resolve(backend, sources, inputs) {
|
|
2335
|
+
if (!backend.setProjectSources) return backend.resolveModules(sources, this.assets(), inputs);
|
|
2336
|
+
this.install(backend, sources, inputs);
|
|
2337
|
+
if (!this.#resolutions) {
|
|
2338
|
+
const generation = this.#generation;
|
|
2339
|
+
const resolutions = await backend.resolveModules(this.#installed.sources, this.assets(), inputs);
|
|
2340
|
+
if (generation === this.#generation) this.#resolutions = resolutions;
|
|
2341
|
+
return resolutions;
|
|
2564
2342
|
}
|
|
2565
|
-
return
|
|
2343
|
+
return this.#resolutions;
|
|
2566
2344
|
}
|
|
2567
2345
|
/**
|
|
2568
|
-
*
|
|
2346
|
+
* Loads and scans one supported source unless it is already prepared.
|
|
2569
2347
|
*
|
|
2570
|
-
* @param
|
|
2571
|
-
* @param
|
|
2572
|
-
*
|
|
2573
|
-
* @
|
|
2574
|
-
*
|
|
2348
|
+
* @param id - Full resolver ID.
|
|
2349
|
+
* @param host - Source loader and format metadata for this project.
|
|
2350
|
+
* @param session - Initialized session used to scan the source.
|
|
2351
|
+
* @param revision - Revision captured before loading. A newer revision discards this work.
|
|
2352
|
+
* @returns Completion of loading and scanning. Failed scans are not stored.
|
|
2353
|
+
* @throws If loading, projection, or native transport fails.
|
|
2575
2354
|
*/
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2355
|
+
async #load(id, host, session, revision) {
|
|
2356
|
+
if (revision !== this.#revision || this.source(id) || host.supports?.(id) === false) return;
|
|
2357
|
+
const input = await loadSource(host, id);
|
|
2358
|
+
const source = "kind" in input ? input : await prepareSource(input);
|
|
2359
|
+
if (revision !== this.#revision) return;
|
|
2360
|
+
const scan = session.scanSource(source);
|
|
2361
|
+
if (scan.kind !== "failed") this.retain(source, scan.summary, revision);
|
|
2362
|
+
}
|
|
2363
|
+
/**
|
|
2364
|
+
* Creates a source's checker inputs once, loading any required framework declarations.
|
|
2365
|
+
*
|
|
2366
|
+
* @param record - Retained source and scan results to update.
|
|
2367
|
+
* @returns Shared checker inputs. Later import updates also update this object.
|
|
2368
|
+
* @throws If the framework checker environment cannot be prepared.
|
|
2369
|
+
*/
|
|
2370
|
+
#checkerSource(record) {
|
|
2371
|
+
return record.checker ??= {
|
|
2372
|
+
...record.source,
|
|
2373
|
+
...checkerEnvironment(record.source),
|
|
2374
|
+
probes: record.summary.typeProbes,
|
|
2375
|
+
checkerSpans: record.summary.checkerSpans,
|
|
2376
|
+
checkerBindings: record.summary.checkerBindings,
|
|
2377
|
+
resolutions: record.resolutions
|
|
2378
|
+
};
|
|
2379
|
+
}
|
|
2380
|
+
/**
|
|
2381
|
+
* Replaces import links and clears cached answers when a target or range changes.
|
|
2382
|
+
*
|
|
2383
|
+
* @param record - Retained source whose links are being replaced.
|
|
2384
|
+
* @param resolutions - Resolved targets with original UTF-8 byte ranges.
|
|
2385
|
+
*/
|
|
2386
|
+
#setResolutions(record, resolutions) {
|
|
2387
|
+
if (record.resolutions.length === resolutions.length && record.resolutions.every((item, index) => item.id === resolutions[index].id && item.start === resolutions[index].start && item.end === resolutions[index].end)) return;
|
|
2388
|
+
record.resolutions = resolutions;
|
|
2389
|
+
if (record.checker) record.checker.resolutions = resolutions;
|
|
2390
|
+
this.#changed();
|
|
2391
|
+
}
|
|
2392
|
+
/**
|
|
2393
|
+
* Clears import answers and marks checker inputs for installation on the next query.
|
|
2394
|
+
*/
|
|
2395
|
+
#changed() {
|
|
2396
|
+
this.#resolutions = void 0;
|
|
2397
|
+
this.#generation++;
|
|
2398
|
+
}
|
|
2399
|
+
};
|
|
2400
|
+
/**
|
|
2401
|
+
* Normalizes a resolver ID for map lookups.
|
|
2402
|
+
*
|
|
2403
|
+
* @param id - Resolver ID, with either slash style for Windows paths.
|
|
2404
|
+
* @returns Forward slashes and, for absolute Windows paths, lowercase text.
|
|
2405
|
+
*/
|
|
2406
|
+
function sourceKey(id) {
|
|
2407
|
+
const normalized = id.replaceAll("\\", "/");
|
|
2408
|
+
return /^[a-z]:\//i.test(normalized) ? normalized.toLowerCase() : normalized;
|
|
2409
|
+
}
|
|
2410
|
+
/**
|
|
2411
|
+
* Compares supplied file contents without depending on map order.
|
|
2412
|
+
*
|
|
2413
|
+
* @param a - First map of filenames to text. Omission means no files.
|
|
2414
|
+
* @param b - Second map, interpreted in the same way.
|
|
2415
|
+
* @returns Whether both maps contain the same filenames and text.
|
|
2416
|
+
*/
|
|
2417
|
+
function sameFiles(a, b) {
|
|
2418
|
+
return (a?.size ?? 0) === (b?.size ?? 0) && [...a ?? []].every(([id, text]) => b?.get(id) === text);
|
|
2419
|
+
}
|
|
2420
|
+
// src/editor/request.ts
|
|
2421
|
+
/**
|
|
2422
|
+
* Starts an editor answer tied to the original source text.
|
|
2423
|
+
*
|
|
2424
|
+
* @param source - Complete original document text.
|
|
2425
|
+
* @returns An empty answer with the fingerprint required for its ranges and edits.
|
|
2426
|
+
*/
|
|
2427
|
+
function editorResult(source) {
|
|
2428
|
+
return { revision: fingerprint(source) };
|
|
2429
|
+
}
|
|
2430
|
+
// src/editor/source/order.ts
|
|
2431
|
+
/**
|
|
2432
|
+
* Groups `items` by syntax category without crossing barriers.
|
|
2433
|
+
*
|
|
2434
|
+
* Ref groups retain their first appearance order, and each marker precedes its
|
|
2435
|
+
* projections. Bases, utilities, and custom properties follow in that order. Each
|
|
2436
|
+
* category retains its input order.
|
|
2437
|
+
*
|
|
2438
|
+
* @param items - Inputs in their current order, with a distinct object for each occurrence.
|
|
2439
|
+
* @param allows - Optional predicate that approves the original range covering each adjacent
|
|
2440
|
+
* exchange. Rejected exchanges can prevent the result from reaching the full category order.
|
|
2441
|
+
* @returns A new array of the original objects. The input array and its objects remain unchanged.
|
|
2442
|
+
*/
|
|
2443
|
+
function orderInputs(items, allows) {
|
|
2444
|
+
const sorted = [];
|
|
2445
|
+
let start = 0;
|
|
2446
|
+
for (let end = 0; end <= items.length; end++) if (end === items.length || items[end].order.kind === "barrier") {
|
|
2447
|
+
sorted.push(...orderRun(items.slice(start, end), allows));
|
|
2448
|
+
if (end < items.length) sorted.push(items[end]);
|
|
2449
|
+
start = end + 1;
|
|
2450
|
+
}
|
|
2451
|
+
return sorted;
|
|
2452
|
+
}
|
|
2453
|
+
/**
|
|
2454
|
+
* Computes the category order for one run and applies any permitted adjacent exchanges.
|
|
2455
|
+
*
|
|
2456
|
+
* @param items - A run without barriers, with a distinct object for each occurrence.
|
|
2457
|
+
* @param allows - Optional predicate over the original range covering two adjacent inputs.
|
|
2458
|
+
* @returns The same input objects in a new array. Neither the input array nor its objects change.
|
|
2459
|
+
*/
|
|
2460
|
+
function orderRun(items, allows) {
|
|
2461
|
+
const refs = /* @__PURE__ */ new Map();
|
|
2462
|
+
const groups = {
|
|
2463
|
+
bases: [],
|
|
2464
|
+
utilities: [],
|
|
2465
|
+
customProperties: []
|
|
2466
|
+
};
|
|
2467
|
+
for (const item of items) if (item.order.kind === "ref") {
|
|
2468
|
+
let group = refs.get(item.order.identity);
|
|
2469
|
+
if (!group) {
|
|
2470
|
+
group = {
|
|
2471
|
+
markers: [],
|
|
2472
|
+
projections: []
|
|
2473
|
+
};
|
|
2474
|
+
refs.set(item.order.identity, group);
|
|
2475
|
+
}
|
|
2476
|
+
group[item.order.projection ? "projections" : "markers"].push(item);
|
|
2477
|
+
} else if (item.order.kind !== "barrier") groups[item.order.kind].push(item);
|
|
2478
|
+
const ordered = [
|
|
2479
|
+
...[...refs.values()].flatMap(({ markers, projections }) => [...markers, ...projections]),
|
|
2480
|
+
...groups.bases,
|
|
2481
|
+
...groups.utilities,
|
|
2482
|
+
...groups.customProperties
|
|
2483
|
+
];
|
|
2484
|
+
if (!allows) return ordered;
|
|
2485
|
+
const positions = new Map(ordered.map((item, index) => [item, index]));
|
|
2486
|
+
const sorted = [...items];
|
|
2487
|
+
for (let position = 1; position < sorted.length; position++) for (let cursor = position; cursor > 0; cursor--) {
|
|
2488
|
+
const left = sorted[cursor - 1];
|
|
2489
|
+
const right = sorted[cursor];
|
|
2490
|
+
if (positions.get(left) <= positions.get(right) || !allows(Math.min(left.start, right.start), Math.max(left.end, right.end))) break;
|
|
2491
|
+
[sorted[cursor - 1], sorted[cursor]] = [right, left];
|
|
2492
|
+
}
|
|
2493
|
+
return sorted;
|
|
2494
|
+
}
|
|
2495
|
+
/**
|
|
2496
|
+
* Classifies direct style input syntax and resolved declarations for grouping.
|
|
2497
|
+
*
|
|
2498
|
+
* @param source - Fixed source whose AST contains node.
|
|
2499
|
+
* @param node - Input or property to classify.
|
|
2500
|
+
* @param resolveAsset - Resolves native declaration identities and stable aliases for this source graph.
|
|
2501
|
+
* @returns An input category, or a barrier when the input cannot move safely.
|
|
2502
|
+
*/
|
|
2503
|
+
function classifyInput(source, node, resolveAsset) {
|
|
2504
|
+
if (ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node)) {
|
|
2505
|
+
if (ts.isComputedPropertyName(node.name)) return { kind: "barrier" };
|
|
2506
|
+
const name = ts.isStringLiteralLike(node.name) ? node.name.text : node.name.getText();
|
|
2507
|
+
if (name.startsWith("--")) return { kind: "customProperties" };
|
|
2508
|
+
return name === "" || name.endsWith(":") || name === "class" ? { kind: "utilities" } : { kind: "barrier" };
|
|
2509
|
+
}
|
|
2510
|
+
if (ts.isStringLiteralLike(node) || ts.isTemplateExpression(node)) return { kind: "utilities" };
|
|
2511
|
+
const asset = resolveAsset(source, node);
|
|
2512
|
+
if (asset?.identity.kind === "style") return { kind: "bases" };
|
|
2513
|
+
if (asset?.identity.kind === "ref") return {
|
|
2514
|
+
kind: "ref",
|
|
2515
|
+
identity: JSON.stringify(asset.identity),
|
|
2516
|
+
projection: false
|
|
2517
|
+
};
|
|
2518
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && [
|
|
2519
|
+
"asAnchor",
|
|
2520
|
+
"asContainer",
|
|
2521
|
+
"positionAnchor"
|
|
2522
|
+
].includes(node.expression.name.text)) {
|
|
2523
|
+
const ref = resolveAsset(source, node.expression.expression);
|
|
2524
|
+
if (ref?.identity.kind === "ref") return {
|
|
2525
|
+
kind: "ref",
|
|
2526
|
+
identity: JSON.stringify(ref.identity),
|
|
2527
|
+
projection: true
|
|
2528
|
+
};
|
|
2529
|
+
}
|
|
2530
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) return classifyInput(source, node.expression, resolveAsset);
|
|
2531
|
+
if (ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node)) {
|
|
2532
|
+
const orders = (ts.isArrayLiteralExpression(node) ? node.elements : node.properties).map((element) => classifyInput(source, element, resolveAsset));
|
|
2533
|
+
const first = orders[0];
|
|
2534
|
+
if (first && first.kind !== "barrier" && first.kind !== "ref" && orders.every((order) => order.kind === first.kind)) return first;
|
|
2535
|
+
}
|
|
2536
|
+
return { kind: "barrier" };
|
|
2537
|
+
}
|
|
2538
|
+
// src/editor/sort.ts
|
|
2539
|
+
/**
|
|
2540
|
+
* Composes existing edits with utility sorting and grouping of direct style inputs.
|
|
2541
|
+
* Literal sorting applies only to ordinary utilities. Containers are processed from the
|
|
2542
|
+
* smallest to the largest so an outer replacement includes edits to nested inputs.
|
|
2543
|
+
* Unknown syntax, ambiguous comments, and text that cannot map exactly delimit grouping.
|
|
2544
|
+
*
|
|
2545
|
+
* @param source - The same prepared source object stored in `index`, with an unchanged buffer.
|
|
2546
|
+
* @param index - Index built from that source graph before any edits are applied.
|
|
2547
|
+
* @param candidates - Candidate query for the design system that compiled the source.
|
|
2548
|
+
* @param options - Sorting targets. False returns only the seed edits in source order.
|
|
2549
|
+
* @param seed - Nonoverlapping replacements in original UTF-16 coordinates. Inside a sortable
|
|
2550
|
+
* literal, each edit must replace a complete utility token. Edits inside grouped containers must
|
|
2551
|
+
* stay within one input. Replacement text must contain any JS escapes needed at its range.
|
|
2552
|
+
* @param allows - Optional predicate that approves a whole literal's content range or the
|
|
2553
|
+
* range covering an adjacent input exchange. Every range uses original UTF-16 coordinates.
|
|
2554
|
+
* @returns Edits in ascending original offset order. The source and seed array remain unchanged.
|
|
2555
|
+
* @throws If a matching utility definition has a malformed requirement or native ranking fails.
|
|
2556
|
+
*/
|
|
2557
|
+
function sortSource(source, index, candidates, options, seed = [], allows) {
|
|
2558
|
+
if (options === false) return seed.toSorted((a, b) => a.start - b.start);
|
|
2559
|
+
let edits = [...seed];
|
|
2560
|
+
const permits = allows ?? (() => true);
|
|
2561
|
+
if (options.stringLiterals !== false) {
|
|
2562
|
+
const literals = index.literals.filter((literal) => literal.source === source && literal.context === "utility" && permits(literal.boundaries[0], literal.boundaries.at(-1))).map((literal) => {
|
|
2563
|
+
const matches = [...literal.text.matchAll(/\S+/gu)];
|
|
2564
|
+
const original = matches.map((match) => match[0]);
|
|
2565
|
+
const spellings = matches.map((match) => {
|
|
2566
|
+
const start = literal.boundaries[match.index];
|
|
2567
|
+
const end = literal.boundaries[match.index + match[0].length];
|
|
2568
|
+
const edit = seed.find((edit) => edit.start === start && edit.end === end);
|
|
2569
|
+
return {
|
|
2570
|
+
token: edit ? decodeReplacement(edit.newText, literal) : match[0],
|
|
2571
|
+
text: edit?.newText ?? source.document.code.slice(start, end)
|
|
2572
|
+
};
|
|
2573
|
+
});
|
|
2574
|
+
return {
|
|
2575
|
+
literal,
|
|
2576
|
+
original,
|
|
2577
|
+
tokens: spellings.map((spelling) => spelling.token),
|
|
2578
|
+
matches,
|
|
2579
|
+
spellings
|
|
2580
|
+
};
|
|
2581
|
+
});
|
|
2582
|
+
const compare = candidates.order(literals.flatMap(({ literal, tokens }) => tokens.map((token) => literal.prefix + token)));
|
|
2583
|
+
for (const { literal, original, tokens, matches, spellings } of literals) {
|
|
2584
|
+
const start = literal.boundaries[0];
|
|
2585
|
+
const end = literal.boundaries.at(-1);
|
|
2586
|
+
const sorted = sortRuns(tokens, (a, b) => compare(literal.prefix + a, literal.prefix + b));
|
|
2587
|
+
if (sorted.every((token, position) => token === original[position])) continue;
|
|
2588
|
+
let cursor = start;
|
|
2589
|
+
let newText = "";
|
|
2590
|
+
const queues = /* @__PURE__ */ new Map();
|
|
2591
|
+
for (const spelling of spellings) {
|
|
2592
|
+
let queue = queues.get(spelling.token);
|
|
2593
|
+
if (!queue) {
|
|
2594
|
+
queue = {
|
|
2595
|
+
texts: [],
|
|
2596
|
+
position: 0
|
|
2597
|
+
};
|
|
2598
|
+
queues.set(spelling.token, queue);
|
|
2599
|
+
}
|
|
2600
|
+
queue.texts.push(spelling.text);
|
|
2601
|
+
}
|
|
2602
|
+
for (const [position, match] of matches.entries()) {
|
|
2603
|
+
const tokenStart = literal.boundaries[match.index];
|
|
2604
|
+
const tokenEnd = literal.boundaries[match.index + match[0].length];
|
|
2605
|
+
const queue = queues.get(sorted[position]);
|
|
2606
|
+
newText += source.document.code.slice(cursor, tokenStart) + queue.texts[queue.position++];
|
|
2607
|
+
cursor = tokenEnd;
|
|
2608
|
+
}
|
|
2609
|
+
newText += source.document.code.slice(cursor, end);
|
|
2610
|
+
edits = absorb(edits, {
|
|
2611
|
+
start,
|
|
2612
|
+
end,
|
|
2613
|
+
newText
|
|
2614
|
+
});
|
|
2583
2615
|
}
|
|
2584
|
-
const generated = position - delta;
|
|
2585
|
-
const original = this.buffer.region ? generated + this.buffer.region.start : this.source.kind === "direct" ? generated : this.#points.get(generated);
|
|
2586
|
-
return original === void 0 || original === null ? null : new TextEncoder().encode(this.#original.slice(0, original)).length;
|
|
2587
2616
|
}
|
|
2588
|
-
|
|
2617
|
+
if (options.arrays !== false) {
|
|
2618
|
+
const containers = index.containers.filter((container) => container.source === source).sort((a, b) => a.node.end - a.node.pos - (b.node.end - b.node.pos));
|
|
2619
|
+
for (const container of containers) {
|
|
2620
|
+
const elements = ts.isArrayLiteralExpression(container.node) ? container.node.elements : ts.isCallExpression(container.node) ? container.node.arguments : container.node.properties;
|
|
2621
|
+
const items = [...elements].map((node) => {
|
|
2622
|
+
const range = index.range(source, node);
|
|
2623
|
+
const offset = range ? range[0] - node.getStart() : 0;
|
|
2624
|
+
const start = node.getFullStart() + offset;
|
|
2625
|
+
const end = range?.[1] ?? 0;
|
|
2626
|
+
const text = range ? applyEdits(source.document.code.slice(start, end), edits.filter((edit) => edit.start >= start && edit.end <= end).map((edit) => ({
|
|
2627
|
+
...edit,
|
|
2628
|
+
start: edit.start - start,
|
|
2629
|
+
end: edit.end - start
|
|
2630
|
+
}))) : "";
|
|
2631
|
+
const order = index.inputOrder(source, node);
|
|
2632
|
+
const ambiguousComment = /^[^\r\n]*(?:\/\/|\/\*)/.test(text) && node.pos > elements.pos;
|
|
2633
|
+
return {
|
|
2634
|
+
node,
|
|
2635
|
+
start,
|
|
2636
|
+
end,
|
|
2637
|
+
text,
|
|
2638
|
+
order: range && !ambiguousComment && source.document.code.slice(start, end) === node.getSourceFile().text.slice(node.getFullStart(), node.end) ? order : { kind: "barrier" }
|
|
2639
|
+
};
|
|
2640
|
+
});
|
|
2641
|
+
for (let position = 0; position < items.length; position++) {
|
|
2642
|
+
const item = items[position];
|
|
2643
|
+
const next = items[position + 1];
|
|
2644
|
+
const tail = source.document.code.slice(item.end, next?.start ?? index.range(source, container.node)?.[1] ?? item.end);
|
|
2645
|
+
if (/^[^\r\n]*(?:\/\/|\/\*)/.test(tail) || next && /^[^\r\n]*(?:\/\/|\/\*)/.test(next.text.slice(0, next.text.length - (next.node.end - next.node.getStart())))) item.order = { kind: "barrier" };
|
|
2646
|
+
}
|
|
2647
|
+
const sorted = orderInputs(items, allows);
|
|
2648
|
+
if (sorted.every((item, position) => item === items[position])) continue;
|
|
2649
|
+
for (let position = 0; position < items.length;) {
|
|
2650
|
+
if (items[position] === sorted[position]) {
|
|
2651
|
+
position++;
|
|
2652
|
+
continue;
|
|
2653
|
+
}
|
|
2654
|
+
const start = position++;
|
|
2655
|
+
while (position < items.length && items[position] !== sorted[position]) position++;
|
|
2656
|
+
if (items.slice(start, position).some((item) => item.order.kind === "barrier")) continue;
|
|
2657
|
+
const edit = {
|
|
2658
|
+
start: items[start].start,
|
|
2659
|
+
end: items[position - 1].end,
|
|
2660
|
+
newText: sorted.slice(start, position).map((item) => item.text).join(",")
|
|
2661
|
+
};
|
|
2662
|
+
edits = absorb(edits, edit);
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
return edits.sort((a, b) => a.start - b.start);
|
|
2667
|
+
}
|
|
2589
2668
|
/**
|
|
2590
|
-
*
|
|
2669
|
+
* Applies replacements from the end of a document so all ranges keep their original meaning.
|
|
2591
2670
|
*
|
|
2592
|
-
* @param source -
|
|
2593
|
-
* @
|
|
2594
|
-
*
|
|
2595
|
-
* @
|
|
2671
|
+
* @param source - Original document text, or a slice whose coordinates match `edits`.
|
|
2672
|
+
* @param edits - Nonoverlapping half-open UTF-16 ranges within `source`, in any order.
|
|
2673
|
+
* The caller must provide valid boundaries because this function does not validate them.
|
|
2674
|
+
* @returns The replaced text. The edit array and its objects remain unchanged.
|
|
2596
2675
|
*/
|
|
2597
|
-
function
|
|
2598
|
-
const
|
|
2599
|
-
|
|
2600
|
-
* Records a projection point and keeps conflicting mappings ambiguous.
|
|
2601
|
-
* @param generated - UTF-16 offset in the generated checker source.
|
|
2602
|
-
* @param original - Corresponding UTF-16 offset in the original document.
|
|
2603
|
-
*/
|
|
2604
|
-
const add = (generated, original) => {
|
|
2605
|
-
if (points.has(generated) && points.get(generated) !== original) points.set(generated, null);
|
|
2606
|
-
else points.set(generated, original);
|
|
2607
|
-
};
|
|
2608
|
-
if (source.kind === "direct") return points;
|
|
2609
|
-
const map = source.projection.map;
|
|
2610
|
-
if (map.kind === "volar") for (const entry of map.mappings) for (let i = 0; i < entry.sourceOffsets.length; i++) {
|
|
2611
|
-
const length = entry.generatedLengths?.[i] ?? entry.lengths[i];
|
|
2612
|
-
add(entry.generatedOffsets[i], entry.sourceOffsets[i]);
|
|
2613
|
-
if (length === entry.lengths[i]) for (let j = 1; j < length; j++) add(entry.generatedOffsets[i] + j, entry.sourceOffsets[i] + j);
|
|
2614
|
-
}
|
|
2615
|
-
else {
|
|
2616
|
-
/**
|
|
2617
|
-
* Builds line starts for converting source-map columns to offsets.
|
|
2618
|
-
* @param text - Original or generated text whose LF characters delimit lines.
|
|
2619
|
-
* @returns Ordered UTF-16 offsets, beginning with zero and including the position after every LF.
|
|
2620
|
-
*/
|
|
2621
|
-
const lines = (text) => [0, ...[...text.matchAll(/\n/g)].map((match) => match.index + 1)];
|
|
2622
|
-
const generated = lines(source.projection.code);
|
|
2623
|
-
const original = lines(source.document.code);
|
|
2624
|
-
const trace = new TraceMap(map.json);
|
|
2625
|
-
const selected = trace.sources.indexOf(map.source);
|
|
2626
|
-
for (const [line, segments] of decodedMappings(trace).entries()) for (const segment of segments) if (segment.length >= 4 && segment[1] === selected) add(generated[line] + segment[0], original[segment[2]] + segment[3]);
|
|
2627
|
-
}
|
|
2628
|
-
return points;
|
|
2676
|
+
function applyEdits(source, edits) {
|
|
2677
|
+
for (const edit of [...edits].sort((a, b) => b.start - a.start)) source = source.slice(0, edit.start) + edit.newText + source.slice(edit.end);
|
|
2678
|
+
return source;
|
|
2629
2679
|
}
|
|
2630
2680
|
/**
|
|
2631
|
-
*
|
|
2632
|
-
*
|
|
2633
|
-
* @
|
|
2681
|
+
* Escapes replacement content for an existing JS string or template token.
|
|
2682
|
+
*
|
|
2683
|
+
* @param text - Decoded replacement text without surrounding delimiters.
|
|
2684
|
+
* @param literal - Indexed literal whose AST token supplies the quote or backtick delimiter.
|
|
2685
|
+
* @returns Content without delimiters, with backslashes, the delimiter, tabs, CR, and LF escaped.
|
|
2686
|
+
* Template content also escapes `${` so it cannot introduce an interpolation.
|
|
2634
2687
|
*/
|
|
2635
|
-
function
|
|
2636
|
-
|
|
2688
|
+
function escapeContent(text, literal) {
|
|
2689
|
+
const quote = ts.isTemplateLiteralToken(literal.node) ? "`" : literal.node.getText()[0];
|
|
2690
|
+
let escaped = text.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`).replaceAll(" ", "\\t").replaceAll("\r", "\\r").replaceAll("\n", "\\n");
|
|
2691
|
+
if (quote === "`") escaped = escaped.replaceAll("${", "\\${");
|
|
2692
|
+
return escaped;
|
|
2637
2693
|
}
|
|
2638
2694
|
/**
|
|
2639
|
-
*
|
|
2640
|
-
*
|
|
2641
|
-
* @param
|
|
2642
|
-
* @
|
|
2695
|
+
* Decodes a seeded replacement using the original literal's delimiter.
|
|
2696
|
+
*
|
|
2697
|
+
* @param text - Source spelling of replacement content that can be wrapped in that delimiter.
|
|
2698
|
+
* @param literal - Indexed literal that supplies the quote or backtick delimiter.
|
|
2699
|
+
* @returns TypeScript's decoded string value, or the supplied text when parsing produces a
|
|
2700
|
+
* different initializer kind. Parser diagnostics are not checked.
|
|
2643
2701
|
*/
|
|
2644
|
-
function
|
|
2645
|
-
const
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
scanner.scan();
|
|
2649
|
-
return scanner.getTokenStart();
|
|
2702
|
+
function decodeReplacement(text, literal) {
|
|
2703
|
+
const quote = ts.isTemplateLiteralToken(literal.node) ? "`" : literal.node.getText()[0];
|
|
2704
|
+
const node = ts.createSourceFile("literal.ts", `const value=${quote}${text}${quote};`, ts.ScriptTarget.Latest, true).statements[0].declarationList.declarations[0].initializer;
|
|
2705
|
+
return node && ts.isStringLiteralLike(node) ? node.text : text;
|
|
2650
2706
|
}
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
if (!editor) continue;
|
|
2678
|
-
const sites = editor.sites().map((native) => ({
|
|
2679
|
-
native,
|
|
2680
|
-
start: byteToUtf16(source.document.code, native.start),
|
|
2681
|
-
end: byteToUtf16(source.document.code, native.end)
|
|
2682
|
-
}));
|
|
2683
|
-
this.#modules.set(source.moduleId, {
|
|
2684
|
-
editor,
|
|
2685
|
-
sites
|
|
2686
|
-
});
|
|
2687
|
-
for (const { native } of sites) if (native.identity) this.#declarations.set(identityKey(native.identity), native);
|
|
2688
|
-
}
|
|
2689
|
-
this.revision = fingerprint(JSON.stringify([revisions, graph.dependencies]));
|
|
2690
|
-
}
|
|
2691
|
-
/**
|
|
2692
|
-
* Finds the occurrences captured for one compiler module.
|
|
2693
|
-
*
|
|
2694
|
-
* @param source - Prepared source whose full module identity selects the capture.
|
|
2695
|
-
* @returns Occurrences in compiler traversal order, or an empty list for an unknown module.
|
|
2696
|
-
* The array and its entries are readonly and shared with this capture.
|
|
2697
|
-
*/
|
|
2698
|
-
sites(source) {
|
|
2699
|
-
return this.#modules.get(source.moduleId)?.sites ?? [];
|
|
2700
|
-
}
|
|
2701
|
-
/**
|
|
2702
|
-
* Finds a literal at the half-open original UTF-16 range `[start, end)`.
|
|
2703
|
-
* The range may address the literal content or include its one-character delimiters.
|
|
2704
|
-
*
|
|
2705
|
-
* @param source - Prepared source whose full module identity selects the capture.
|
|
2706
|
-
* @param start - Inclusive original UTF-16 offset of the content or opening delimiter.
|
|
2707
|
-
* @param end - Exclusive original UTF-16 offset of the content or closing delimiter.
|
|
2708
|
-
* @returns A shared occurrence with an input context, or undefined when no range matches exactly.
|
|
2709
|
-
*/
|
|
2710
|
-
input(source, start, end) {
|
|
2711
|
-
for (const site of this.sites(source)) if (site.native.context && (site.start === start && site.end === end || site.start === start + 1 && site.end === end - 1)) return site;
|
|
2712
|
-
}
|
|
2713
|
-
/**
|
|
2714
|
-
* Renders a replacement for the whole captured input while retaining its compiler conditions.
|
|
2715
|
-
* The result contains only that plan's CSS. Dependency assembly is handled by `preview`.
|
|
2716
|
-
*
|
|
2717
|
-
* @param source - Prepared source whose full module identity selects the capture.
|
|
2718
|
-
* @param start - Inclusive original UTF-8 byte offset of the captured input, taken from
|
|
2719
|
-
* `PreviewSite.native.start`.
|
|
2720
|
-
* @param candidate - Decoded replacement text without enclosing group variants.
|
|
2721
|
-
* @returns CSS for that plan, or undefined for an unknown module or input, failed emission,
|
|
2722
|
-
* or output with no CSS. Captured plans and shared generated names remain unchanged.
|
|
2723
|
-
*/
|
|
2724
|
-
previewInput(source, start, candidate) {
|
|
2725
|
-
return this.#modules.get(source.moduleId)?.editor.previewInput(start, candidate) ?? void 0;
|
|
2726
|
-
}
|
|
2727
|
-
/**
|
|
2728
|
-
* Assembles a captured declaration or style call addressed by its semantic identity.
|
|
2729
|
-
* Imported aliases reuse that identity.
|
|
2730
|
-
*
|
|
2731
|
-
* @param identity - Full native declaration identity, including original UTF-8 ranges.
|
|
2732
|
-
* @returns The preview assembled by `preview`, or undefined when the identity is absent
|
|
2733
|
-
* or its declaration and dependencies cannot supply valid, nonempty CSS.
|
|
2734
|
-
*/
|
|
2735
|
-
previewIdentity(identity) {
|
|
2736
|
-
const site = this.#declarations.get(identityKey(identity));
|
|
2737
|
-
return site ? this.preview(site) : void 0;
|
|
2707
|
+
/**
|
|
2708
|
+
* Replaces edits covered by a larger, already composed replacement.
|
|
2709
|
+
*
|
|
2710
|
+
* @param edits - Existing edits in original coordinates. Every overlapping edit must be fully
|
|
2711
|
+
* contained in `replacement` and already reflected in its text.
|
|
2712
|
+
* @param replacement - Replacement whose text includes all contained edits.
|
|
2713
|
+
* @returns A new array with overlapping edits removed and the replacement appended.
|
|
2714
|
+
* The result is not sorted, and the input edit objects remain unchanged.
|
|
2715
|
+
*/
|
|
2716
|
+
function absorb(edits, replacement) {
|
|
2717
|
+
return [...edits.filter((edit) => edit.end <= replacement.start || edit.start >= replacement.end), replacement];
|
|
2718
|
+
}
|
|
2719
|
+
/**
|
|
2720
|
+
* Sorts contiguous runs of resolved utilities while keeping unresolved spellings in place.
|
|
2721
|
+
*
|
|
2722
|
+
* @param values - Decoded candidates in their original order.
|
|
2723
|
+
* @param compare - Compiler comparator prepared for these spellings. A null self-comparison
|
|
2724
|
+
* marks a boundary, and a null comparison within a run treats that pair as equal.
|
|
2725
|
+
* @returns A new array with stable ordering inside each run. `values` remains unchanged.
|
|
2726
|
+
*/
|
|
2727
|
+
function sortRuns(values, compare) {
|
|
2728
|
+
const result = [...values];
|
|
2729
|
+
let start = 0;
|
|
2730
|
+
for (let position = 0; position <= values.length; position++) if (position === values.length || compare(values[position], values[position]) === null) {
|
|
2731
|
+
result.splice(start, position - start, ...values.slice(start, position).sort((a, b) => compare(a, b) ?? 0));
|
|
2732
|
+
start = position + 1;
|
|
2738
2733
|
}
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2734
|
+
return result;
|
|
2735
|
+
}
|
|
2736
|
+
// src/semantics/checker-source.ts
|
|
2737
|
+
/**
|
|
2738
|
+
* Serializes a declaration identity as a stable checker string-literal key.
|
|
2739
|
+
*
|
|
2740
|
+
* @param identity - Native declaration identity with original UTF-8 offsets.
|
|
2741
|
+
* @returns `stillwind-identity/v1:` followed by base64url JSON of kind, module ID, range, and
|
|
2742
|
+
* binding name in a fixed field order.
|
|
2743
|
+
*/
|
|
2744
|
+
function identityKey(identity) {
|
|
2745
|
+
return `stillwind-identity/v1:${base64url(JSON.stringify({
|
|
2746
|
+
kind: identity.kind,
|
|
2747
|
+
moduleId: identity.moduleId,
|
|
2748
|
+
start: identity.start,
|
|
2749
|
+
end: identity.end,
|
|
2750
|
+
bindingName: identity.bindingName
|
|
2751
|
+
}))}`;
|
|
2752
|
+
}
|
|
2753
|
+
/**
|
|
2754
|
+
* Selects the checker filename for a prepared source.
|
|
2755
|
+
*
|
|
2756
|
+
* @param source - Source with its host checker filename and any projection syntax.
|
|
2757
|
+
* @returns The host filename for direct input, or that filename followed by the projection language
|
|
2758
|
+
* extension for projected input.
|
|
2759
|
+
*/
|
|
2760
|
+
function checkerId(source) {
|
|
2761
|
+
return source.kind === "direct" ? source.checkerFileName : `${source.checkerFileName}.${source.projection.syntax.language}`;
|
|
2762
|
+
}
|
|
2763
|
+
/**
|
|
2764
|
+
* Applies the current query's probes to the project's sources without changing other inputs.
|
|
2765
|
+
*
|
|
2766
|
+
* @param inventory - Sources currently installed in the checker.
|
|
2767
|
+
* @param requested - Sources whose probe ranges or recognitionOnly settings may have changed.
|
|
2768
|
+
* Sources outside inventory are ignored.
|
|
2769
|
+
* @returns The original array when probes match, or a new array with changed source records.
|
|
2770
|
+
* Neither input is mutated. Unchanged source records are shared.
|
|
2771
|
+
*/
|
|
2772
|
+
function withRequestedProbes(inventory, requested) {
|
|
2773
|
+
const probesById = new Map(requested.map((source) => [source.resolverId, source.probes]));
|
|
2774
|
+
let changed = false;
|
|
2775
|
+
const sources = inventory.map((source) => {
|
|
2776
|
+
const probes = probesById.get(source.resolverId);
|
|
2777
|
+
if (!probes || probes.length === source.probes.length && probes.every((probe, index) => probe.start === source.probes[index].start && probe.end === source.probes[index].end && probe.recognitionOnly === source.probes[index].recognitionOnly)) return source;
|
|
2778
|
+
changed = true;
|
|
2779
|
+
return {
|
|
2780
|
+
...source,
|
|
2781
|
+
probes
|
|
2776
2782
|
};
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
css,
|
|
2781
|
-
runtimeNames: [...runtimeNames]
|
|
2782
|
-
} : void 0;
|
|
2783
|
-
}
|
|
2784
|
-
};
|
|
2785
|
-
// src/editor/source/bindings.ts
|
|
2783
|
+
});
|
|
2784
|
+
return changed ? sources : inventory;
|
|
2785
|
+
}
|
|
2786
2786
|
/**
|
|
2787
|
-
*
|
|
2787
|
+
* Checker buffer with declaration identities, resolved import paths, and helper aliases inserted
|
|
2788
|
+
* for type queries. Its range maps relate checker AST expressions to original document bytes.
|
|
2788
2789
|
*/
|
|
2789
|
-
var
|
|
2790
|
-
/**
|
|
2791
|
-
|
|
2792
|
-
/**
|
|
2793
|
-
|
|
2794
|
-
/** Comments with `swlint-*` directives keyed by module identity, from each source's scan. */
|
|
2795
|
-
#lintComments = /* @__PURE__ */ new Map();
|
|
2796
|
-
/** Resolved producers and cached misses for nodes in the captured checker ASTs. */
|
|
2797
|
-
#nodeAssets = /* @__PURE__ */ new WeakMap();
|
|
2798
|
-
/** Generated UTF-16 positions mapped to original positions. Null marks conflicting origins. */
|
|
2799
|
-
#points = /* @__PURE__ */ new Map();
|
|
2800
|
-
/** Binder over the in-memory files and the graph's resolved import dependencies. */
|
|
2801
|
-
#checker;
|
|
2802
|
-
/** Symbols found in assignment or increment/decrement targets, excluded from stable aliases. */
|
|
2803
|
-
#mutated = /* @__PURE__ */ new Set();
|
|
2790
|
+
var CheckerSource = class {
|
|
2791
|
+
/** Prepared source and original-coordinate requests that own this checker buffer. */
|
|
2792
|
+
source;
|
|
2793
|
+
/** Unedited input buffer, optionally restricted to one original script region. */
|
|
2794
|
+
buffer;
|
|
2804
2795
|
/**
|
|
2805
|
-
*
|
|
2806
|
-
*
|
|
2807
|
-
* @param graph - Fixed sources and resolved dependencies from one analysis. Do not mutate its sources.
|
|
2808
|
-
* @param session - Session used to scan those sources. Capture previews before constructing this binder.
|
|
2809
|
-
* @throws If source scanning or projection mapping fails.
|
|
2796
|
+
* Final checker text after identity arguments, module paths, and helper bindings have been
|
|
2797
|
+
* rewritten.
|
|
2810
2798
|
*/
|
|
2811
|
-
|
|
2812
|
-
for (const source of graph.sources) {
|
|
2813
|
-
const text = source.kind === "direct" ? source.document.code : source.projection.code;
|
|
2814
|
-
const fileName = checkerName(source.checkerFileName);
|
|
2815
|
-
const ast = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
2816
|
-
this.files.set(fileName, {
|
|
2817
|
-
source,
|
|
2818
|
-
ast
|
|
2819
|
-
});
|
|
2820
|
-
this.#points.set(source.moduleId, projectionPoints(source));
|
|
2821
|
-
const scan = session.scanSource(source);
|
|
2822
|
-
this.#assets.set(source.moduleId, assetRanges(source.document.code, scan.kind === "ready" ? scan.summary.assets : []));
|
|
2823
|
-
this.#lintComments.set(source.moduleId, scan.kind === "ready" ? scan.summary.lintComments : []);
|
|
2824
|
-
}
|
|
2825
|
-
const host = {
|
|
2826
|
-
getSourceFile: (name) => this.files.get(checkerName(name))?.ast,
|
|
2827
|
-
getDefaultLibFileName: () => "lib.d.ts",
|
|
2828
|
-
writeFile: () => {},
|
|
2829
|
-
getCurrentDirectory: () => "/",
|
|
2830
|
-
getDirectories: () => [],
|
|
2831
|
-
fileExists: (name) => this.files.has(checkerName(name)),
|
|
2832
|
-
readFile: (name) => this.files.get(checkerName(name))?.ast.text,
|
|
2833
|
-
getCanonicalFileName: checkerName,
|
|
2834
|
-
useCaseSensitiveFileNames: () => true,
|
|
2835
|
-
getNewLine: () => "\n",
|
|
2836
|
-
resolveModuleNames: (names, containingFile) => names.map((name) => {
|
|
2837
|
-
const source = this.files.get(checkerName(containingFile))?.source;
|
|
2838
|
-
const dependency = graph.dependencies.find((item) => item.importer === source?.resolverId && item.specifier === name);
|
|
2839
|
-
const target = graph.sources.find((item) => item.resolverId === dependency?.id);
|
|
2840
|
-
return target ? {
|
|
2841
|
-
resolvedFileName: checkerName(target.checkerFileName),
|
|
2842
|
-
extension: ts.Extension.Tsx
|
|
2843
|
-
} : void 0;
|
|
2844
|
-
})
|
|
2845
|
-
};
|
|
2846
|
-
const program = ts.createProgram([...this.files.keys()], {
|
|
2847
|
-
noLib: true,
|
|
2848
|
-
allowJs: true
|
|
2849
|
-
}, host);
|
|
2850
|
-
this.#checker = program.getTypeChecker();
|
|
2851
|
-
for (const { ast } of this.files.values()) walk(ast, (node) => {
|
|
2852
|
-
const target = ts.isBinaryExpression(node) && node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && node.operatorToken.kind <= ts.SyntaxKind.LastAssignment ? node.left : (ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) && [ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken].includes(node.operator) ? node.operand : void 0;
|
|
2853
|
-
if (target) {
|
|
2854
|
-
/**
|
|
2855
|
-
* Records symbols in assignment targets that can contain identifier bindings.
|
|
2856
|
-
* Property accesses do not mark their receiver as a reassigned binding.
|
|
2857
|
-
*
|
|
2858
|
-
* @param node - Identifier or supported destructuring subtree of the target.
|
|
2859
|
-
* Recognized symbols are added to `#mutated`, including nested pattern members.
|
|
2860
|
-
*/
|
|
2861
|
-
const mark = (node) => {
|
|
2862
|
-
if (ts.isIdentifier(node)) {
|
|
2863
|
-
const symbol = this.#checker.getSymbolAtLocation(node);
|
|
2864
|
-
if (symbol) this.#mutated.add(symbol);
|
|
2865
|
-
} else if (ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node) || ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node) || ts.isSpreadAssignment(node) || ts.isSpreadElement(node) || ts.isParenthesizedExpression(node)) ts.forEachChild(node, mark);
|
|
2866
|
-
};
|
|
2867
|
-
mark(target);
|
|
2868
|
-
}
|
|
2869
|
-
});
|
|
2870
|
-
}
|
|
2799
|
+
text;
|
|
2871
2800
|
/**
|
|
2872
|
-
*
|
|
2873
|
-
* Other property accesses with the same spelling do not establish producer authority.
|
|
2874
|
-
*
|
|
2875
|
-
* @param node - Expression in one of this index's checker ASTs.
|
|
2876
|
-
* @param name - Exact method spelling, such as `keyframes` or `viewTransition`.
|
|
2877
|
-
* @returns Whether the receiver is a supported value import of `style` and the method matches.
|
|
2801
|
+
* Checker filename, with a distinct client-script suffix when the buffer has an original region.
|
|
2878
2802
|
*/
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2803
|
+
id;
|
|
2804
|
+
/** Serialized identity keys mapped to the native declarations supplied during construction. */
|
|
2805
|
+
identities = /* @__PURE__ */ new Map();
|
|
2806
|
+
/** Independent module that supplies helper declarations in the virtual project. */
|
|
2807
|
+
helperModule;
|
|
2808
|
+
/** Shadow UTF-16 AST ranges keyed to original UTF-8 probe ranges. */
|
|
2809
|
+
probes = /* @__PURE__ */ new Map();
|
|
2810
|
+
/** Producer call ranges after identity insertion, keyed to their asset families. */
|
|
2811
|
+
producers = /* @__PURE__ */ new Map();
|
|
2812
|
+
/** Authored module specifiers keyed by their rewritten checker UTF-16 ranges. */
|
|
2813
|
+
moduleSpecifiers = /* @__PURE__ */ new Map();
|
|
2882
2814
|
/**
|
|
2883
|
-
*
|
|
2884
|
-
*
|
|
2885
|
-
* @param node - Expression in this index's checker ASTs. Supported imports come from
|
|
2886
|
-
* Stillwind's framework and vanilla entrypoints.
|
|
2887
|
-
* @returns Whether a value import establishes that producer. Type-only imports and symbols
|
|
2888
|
-
* marked as mutated fail this check.
|
|
2815
|
+
* Ordered, nonoverlapping edits in the input buffer. Their cumulative lengths translate between
|
|
2816
|
+
* input and checker offsets.
|
|
2889
2817
|
*/
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
const symbol = this.#checker.getSymbolAtLocation(namespace ? node.expression : node);
|
|
2894
|
-
if (!symbol || this.#mutated.has(symbol)) return false;
|
|
2895
|
-
return !!symbol.declarations?.some((declaration) => {
|
|
2896
|
-
if (namespace ? !ts.isNamespaceImport(declaration) : !ts.isImportSpecifier(declaration) || (declaration.propertyName ?? declaration.name).text !== "style" || declaration.isTypeOnly) return false;
|
|
2897
|
-
const owner = ts.isNamespaceImport(declaration) ? declaration.parent.parent : declaration.parent.parent.parent;
|
|
2898
|
-
return ts.isImportDeclaration(owner) && owner.importClause?.phaseModifier !== ts.SyntaxKind.TypeKeyword && ts.isStringLiteral(owner.moduleSpecifier) && /^stillwind\/(?:alpine|astro|lit|preact|qwik|react|solid|svelte|vanilla|vue)$/.test(owner.moduleSpecifier.text);
|
|
2899
|
-
});
|
|
2900
|
-
}
|
|
2818
|
+
#edits = [];
|
|
2819
|
+
/** Original document text used to convert byte ranges and recover diagnostic byte offsets. */
|
|
2820
|
+
#original;
|
|
2901
2821
|
/**
|
|
2902
|
-
*
|
|
2903
|
-
*
|
|
2904
|
-
* the original buffer. A conflicting mapping at the node's start prevents fallback to an anchor.
|
|
2905
|
-
*
|
|
2906
|
-
* @param source - Prepared source whose checker AST contains `node`.
|
|
2907
|
-
* @param node - Node from this index, with leading trivia excluded from its selected range.
|
|
2908
|
-
* @returns Inclusive start and exclusive end in original UTF-16 coordinates, or undefined
|
|
2909
|
-
* when a projection cannot map the text exactly.
|
|
2822
|
+
* Projected UTF-16 positions mapped to original UTF-16 positions. Null marks conflicting
|
|
2823
|
+
* correspondences. Missing keys have no known mapping.
|
|
2910
2824
|
*/
|
|
2911
|
-
|
|
2912
|
-
const ast = node.getSourceFile();
|
|
2913
|
-
const start = node.getStart(ast);
|
|
2914
|
-
const end = node.end;
|
|
2915
|
-
if (source.kind === "direct") return [start, end];
|
|
2916
|
-
const points = this.#points.get(source.moduleId);
|
|
2917
|
-
const exact = points.get(start);
|
|
2918
|
-
const anchors = exact === void 0 ? [...points].filter(([position, original]) => position <= start && original !== null).sort((a, b) => b[0] - a[0]) : [[start, exact]];
|
|
2919
|
-
for (const [position, original] of anchors) {
|
|
2920
|
-
if (original === null || original === void 0) continue;
|
|
2921
|
-
const offset = original + start - position;
|
|
2922
|
-
if (source.document.code.slice(original, offset + end - start) === ast.text.slice(position, end)) return [offset, offset + end - start];
|
|
2923
|
-
}
|
|
2924
|
-
}
|
|
2825
|
+
#points;
|
|
2925
2826
|
/**
|
|
2926
|
-
*
|
|
2927
|
-
* Both successful resolutions and misses are cached for the AST node.
|
|
2827
|
+
* Constructs checker text and range indexes without modifying the source or input buffer.
|
|
2928
2828
|
*
|
|
2929
|
-
* @param source - Prepared source
|
|
2930
|
-
*
|
|
2931
|
-
* @
|
|
2932
|
-
*
|
|
2829
|
+
* @param source - Prepared source with validated frontend correspondences, probes, resolutions,
|
|
2830
|
+
* and helper references.
|
|
2831
|
+
* @param buffer - Direct or projected checker text, optionally scoped to an original script
|
|
2832
|
+
* region.
|
|
2833
|
+
* @param index - Buffer index used to give independent client scripts distinct filenames.
|
|
2834
|
+
* @param assets - Native declaration records with original-byte producer call ranges. Repeated
|
|
2835
|
+
* records for the same generated call are inserted once.
|
|
2836
|
+
* @param ids - Resolved source identities mapped to their main checker filenames for import
|
|
2837
|
+
* rewriting.
|
|
2838
|
+
* @throws If a byte boundary is invalid, producer identities conflict, a mapped producer does not
|
|
2839
|
+
* end with a closing parenthesis, edits overlap, or projection decoding fails.
|
|
2933
2840
|
*/
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2841
|
+
constructor(source, buffer, index, assets, ids) {
|
|
2842
|
+
this.source = source;
|
|
2843
|
+
this.buffer = buffer;
|
|
2844
|
+
this.id = buffer.region ? `${source.checkerFileName}.stillwind-client-${index}.ts` : checkerId(source);
|
|
2845
|
+
this.#original = source.document.code;
|
|
2846
|
+
this.#points = projectionPoints(source);
|
|
2847
|
+
const producers = /* @__PURE__ */ new Map();
|
|
2848
|
+
const producerRanges = [];
|
|
2849
|
+
for (const asset of assets) {
|
|
2850
|
+
const targets = this.targets(asset.callStart, asset.callEnd, "producer");
|
|
2851
|
+
const key = identityKey(asset.identity);
|
|
2852
|
+
this.identities.set(key, asset.identity);
|
|
2853
|
+
for (const [start, end] of targets) {
|
|
2854
|
+
const previous = producers.get(end);
|
|
2855
|
+
if (previous) {
|
|
2856
|
+
if (previous !== key) throw new Error(`Conflicting producer identities in ${source.moduleId}`);
|
|
2857
|
+
continue;
|
|
2858
|
+
}
|
|
2859
|
+
producers.set(end, key);
|
|
2860
|
+
producerRanges.push({
|
|
2861
|
+
start,
|
|
2862
|
+
end,
|
|
2863
|
+
kind: asset.identity.kind
|
|
2864
|
+
});
|
|
2865
|
+
const call = buffer.source.slice(start, end);
|
|
2866
|
+
if (!call.endsWith(")")) throw new Error(`Cannot locate producer in ${source.moduleId}`);
|
|
2867
|
+
const separator = [...call.matchAll(/[,(]/g)].some((match) => skipTrivia(call, match.index + 1) === call.length - 1) ? "" : ",";
|
|
2868
|
+
const identity = `{readonly __stillwindDeclarationIdentity:${JSON.stringify(key)}}`;
|
|
2869
|
+
const syntax = source.kind === "direct" ? source.syntax : source.projection.syntax;
|
|
2870
|
+
const argument = syntax.language === "js" || syntax.language === "jsx" ? `/** @type {${identity}} */ (/** @type {unknown} */ (undefined))` : `undefined as unknown as ${identity}`;
|
|
2871
|
+
this.#edits.push({
|
|
2872
|
+
at: end - 1,
|
|
2873
|
+
end: end - 1,
|
|
2874
|
+
original: {
|
|
2875
|
+
start: asset.callStart,
|
|
2876
|
+
end: asset.callEnd
|
|
2877
|
+
},
|
|
2878
|
+
text: `${separator}${argument}`
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
}
|
|
2882
|
+
const specifiers = [];
|
|
2883
|
+
for (const resolution of source.resolutions ?? []) for (const [at, end] of this.targets(resolution.start, resolution.end, "moduleSpecifier")) {
|
|
2884
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true);
|
|
2885
|
+
scanner.setText(buffer.source.slice(at, end));
|
|
2886
|
+
scanner.scan();
|
|
2887
|
+
specifiers.push({
|
|
2888
|
+
start: at,
|
|
2889
|
+
end,
|
|
2890
|
+
text: scanner.getTokenValue()
|
|
2891
|
+
});
|
|
2892
|
+
this.#edits.push({
|
|
2893
|
+
at,
|
|
2894
|
+
end,
|
|
2895
|
+
original: {
|
|
2896
|
+
start: resolution.start,
|
|
2897
|
+
end: resolution.end
|
|
2898
|
+
},
|
|
2899
|
+
text: JSON.stringify(path.resolve(ids.get(resolution.id) ?? resolution.id).replaceAll("\\", "/"))
|
|
2900
|
+
});
|
|
2901
|
+
}
|
|
2902
|
+
const bindings = buffer.region ? [] : source.checkerBindings ?? [];
|
|
2903
|
+
if (bindings.length > 0) {
|
|
2904
|
+
const names = new Set(buffer.source.replaceAll(/\\u(?:\{([\da-fA-F]{1,6})\}|([\da-fA-F]{4}))/g, (escape, braced, fixed) => {
|
|
2905
|
+
const value = Number.parseInt(braced ?? fixed, 16);
|
|
2906
|
+
return value <= 1114111 ? String.fromCodePoint(value) : escape;
|
|
2907
|
+
}).match(/[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/gu));
|
|
2908
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
2909
|
+
let next = 0;
|
|
2910
|
+
for (const binding of bindings) {
|
|
2911
|
+
let alias = aliases.get(binding.name);
|
|
2912
|
+
if (!alias) {
|
|
2913
|
+
do
|
|
2914
|
+
alias = `__stillwindCheckerHelper${next++}`;
|
|
2915
|
+
while (names.has(alias));
|
|
2916
|
+
aliases.set(binding.name, alias);
|
|
2917
|
+
}
|
|
2918
|
+
this.#edits.push({
|
|
2919
|
+
at: binding.projectedStart,
|
|
2920
|
+
end: binding.projectedEnd,
|
|
2921
|
+
text: alias,
|
|
2922
|
+
original: {
|
|
2923
|
+
start: 0,
|
|
2924
|
+
end: 0
|
|
2925
|
+
}
|
|
2926
|
+
});
|
|
2927
|
+
}
|
|
2928
|
+
const id = `${this.id}.stillwind-helpers.ts`;
|
|
2929
|
+
this.helperModule = {
|
|
2930
|
+
id,
|
|
2931
|
+
text: [...aliases.keys()].map((name) => `export declare const ${name}: typeof globalThis.${name};`).join("\n")
|
|
2932
|
+
};
|
|
2933
|
+
this.#edits.push({
|
|
2934
|
+
at: buffer.source.length,
|
|
2935
|
+
end: buffer.source.length,
|
|
2936
|
+
text: `\nimport {${[...aliases].map(([name, alias]) => `${name} as ${alias}`).join(",")}} from ${JSON.stringify(path.resolve(id).replaceAll("\\", "/"))};\n`,
|
|
2937
|
+
original: {
|
|
2938
|
+
start: 0,
|
|
2939
|
+
end: 0
|
|
2940
|
+
}
|
|
2941
|
+
});
|
|
2942
|
+
}
|
|
2943
|
+
this.#edits.sort((a, b) => a.at - b.at || a.end - b.end);
|
|
2944
|
+
for (let i = 1; i < this.#edits.length; i++) if (this.#edits[i].at < this.#edits[i - 1].end || this.#edits[i].at === this.#edits[i - 1].at) throw new Error(`Overlapping checker edits in ${source.moduleId}`);
|
|
2945
|
+
for (const probe of source.probes) for (const [start, end] of this.targets(probe.start, probe.end, "expression")) this.probes.set(`${this.toShadow(start)}:${this.toShadow(end, true)}`, probe);
|
|
2946
|
+
for (const specifier of specifiers) this.moduleSpecifiers.set(`${this.toShadow(specifier.start)}:${this.toShadow(specifier.end, true)}`, specifier.text);
|
|
2947
|
+
for (const producer of producerRanges) this.producers.set(`${this.toShadow(producer.start)}:${this.toShadow(producer.end, true)}`, producer.kind);
|
|
2948
|
+
let text = buffer.source;
|
|
2949
|
+
for (const edit of [...this.#edits].reverse()) text = text.slice(0, edit.at) + edit.text + text.slice(edit.end);
|
|
2950
|
+
this.text = text;
|
|
2937
2951
|
}
|
|
2938
2952
|
/**
|
|
2939
|
-
*
|
|
2953
|
+
* Finds input-buffer intervals corresponding to one original range and role.
|
|
2940
2954
|
*
|
|
2941
|
-
* @param
|
|
2942
|
-
* @
|
|
2943
|
-
*
|
|
2955
|
+
* @param start - Inclusive original UTF-8 byte boundary.
|
|
2956
|
+
* @param end - Exclusive original UTF-8 byte boundary.
|
|
2957
|
+
* @param role - Frontend correspondence role, matched exactly for projected input.
|
|
2958
|
+
* @returns Deduplicated UTF-16 intervals. Independent scripts receive only contained ranges.
|
|
2959
|
+
* Their ranges are excluded from the main buffer. Unmapped projected ranges produce an empty
|
|
2960
|
+
* list.
|
|
2961
|
+
* @throws If either original byte boundary is invalid.
|
|
2944
2962
|
*/
|
|
2945
|
-
|
|
2946
|
-
|
|
2963
|
+
targets(start, end, role) {
|
|
2964
|
+
const a = byteToUtf16(this.#original, start);
|
|
2965
|
+
const b = byteToUtf16(this.#original, end);
|
|
2966
|
+
if (this.buffer.region) return a >= this.buffer.region.start && b <= this.buffer.region.end ? [[a - this.buffer.region.start, b - this.buffer.region.start]] : [];
|
|
2967
|
+
if (this.source.checkerBuffers?.some((buffer) => buffer.region && a >= buffer.region.start && b <= buffer.region.end)) return [];
|
|
2968
|
+
if (this.source.kind === "direct") return [[a, b]];
|
|
2969
|
+
return [...new Map((this.source.checkerSpans ?? []).filter((span) => span.start === start && span.end === end && span.role === role).map((span) => [`${span.projectedStart}:${span.projectedEnd}`, [span.projectedStart, span.projectedEnd]])).values()];
|
|
2947
2970
|
}
|
|
2948
2971
|
/**
|
|
2949
|
-
*
|
|
2950
|
-
* Exact native producer records take precedence over checks on alias bindings.
|
|
2972
|
+
* Translates an input-buffer boundary after checker edits are applied.
|
|
2951
2973
|
*
|
|
2952
|
-
* @param
|
|
2953
|
-
* @param
|
|
2954
|
-
*
|
|
2955
|
-
*
|
|
2956
|
-
*
|
|
2957
|
-
* missing, or unsupported bindings.
|
|
2974
|
+
* @param position - UTF-16 boundary in the unedited checker buffer.
|
|
2975
|
+
* @param end - True for an interval end, which stays before an insertion at the same position.
|
|
2976
|
+
* False includes such an insertion.
|
|
2977
|
+
* @returns The UTF-16 boundary in final checker text. Positions should delimit preserved input
|
|
2978
|
+
* intervals rather than lie inside replaced text.
|
|
2958
2979
|
*/
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
}
|
|
2965
|
-
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) return this.#resolveAsset(source, node.expression, seen);
|
|
2966
|
-
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression)) {
|
|
2967
|
-
if (!this.#checker.getSymbolAtLocation(node.expression)?.declarations?.some(ts.isNamespaceImport)) return;
|
|
2968
|
-
} else if (!ts.isIdentifier(node)) return;
|
|
2969
|
-
let symbol = this.#checker.getSymbolAtLocation(node);
|
|
2970
|
-
if (!symbol || this.#mutated.has(symbol) || seen.has(symbol)) return;
|
|
2971
|
-
seen.add(symbol);
|
|
2972
|
-
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
2973
|
-
symbol = this.#checker.getAliasedSymbol(symbol);
|
|
2974
|
-
if (this.#mutated.has(symbol) || seen.has(symbol)) return;
|
|
2975
|
-
seen.add(symbol);
|
|
2976
|
-
}
|
|
2977
|
-
const declaration = symbol.valueDeclaration;
|
|
2978
|
-
if (declaration && ts.isVariableDeclaration(declaration) && declaration.initializer) {
|
|
2979
|
-
const owner = this.files.get(declaration.getSourceFile().fileName)?.source;
|
|
2980
|
-
return owner ? this.#resolveAsset(owner, declaration.initializer, seen) : void 0;
|
|
2980
|
+
toShadow(position, end = false) {
|
|
2981
|
+
let delta = 0;
|
|
2982
|
+
for (const edit of this.#edits) {
|
|
2983
|
+
if (edit.at > position || edit.at === position && (end || edit.end > edit.at)) break;
|
|
2984
|
+
delta += edit.text.length - (edit.end - edit.at);
|
|
2981
2985
|
}
|
|
2986
|
+
return position + delta;
|
|
2982
2987
|
}
|
|
2983
2988
|
/**
|
|
2984
|
-
*
|
|
2989
|
+
* Maps a checker position back to an original document byte boundary.
|
|
2985
2990
|
*
|
|
2986
|
-
* @param
|
|
2987
|
-
* @param
|
|
2988
|
-
*
|
|
2989
|
-
*
|
|
2991
|
+
* @param position - UTF-16 position in final checker text.
|
|
2992
|
+
* @param end - True selects the owning original range's end for positions inside generated edits.
|
|
2993
|
+
* False selects its start.
|
|
2994
|
+
* @returns An original UTF-8 byte offset, or null when a projected position has no unique known
|
|
2995
|
+
* correspondence.
|
|
2990
2996
|
*/
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
if (
|
|
2997
|
-
|
|
2998
|
-
return best;
|
|
2999
|
-
}
|
|
3000
|
-
};
|
|
3001
|
-
/**
|
|
3002
|
-
* Converts native producer call ranges with one traversal of the original source.
|
|
3003
|
-
*
|
|
3004
|
-
* @param text - Original document text that owns every producer range.
|
|
3005
|
-
* @param assets - Native records whose call ranges use original UTF-8 byte offsets.
|
|
3006
|
-
* @returns Records keyed by `start:end`, where both offsets use original UTF-16 coordinates.
|
|
3007
|
-
* @throws If a call boundary falls inside a UTF-8 sequence or outside the source.
|
|
3008
|
-
*/
|
|
3009
|
-
function assetRanges(text, assets) {
|
|
3010
|
-
const offsets = [...new Set(assets.flatMap((asset) => [asset.callStart, asset.callEnd]))].sort((a, b) => a - b);
|
|
3011
|
-
const positions = /* @__PURE__ */ new Map();
|
|
3012
|
-
let byte = 0;
|
|
3013
|
-
let utf16 = 0;
|
|
3014
|
-
for (const offset of offsets) {
|
|
3015
|
-
while (byte < offset && utf16 < text.length) {
|
|
3016
|
-
const point = text.codePointAt(utf16);
|
|
3017
|
-
byte += point <= 127 ? 1 : point <= 2047 ? 2 : point <= 65535 ? 3 : 4;
|
|
3018
|
-
utf16 += point > 65535 ? 2 : 1;
|
|
2997
|
+
originalOffset(position, end = false) {
|
|
2998
|
+
let delta = 0;
|
|
2999
|
+
for (const edit of this.#edits) {
|
|
3000
|
+
const start = edit.at + delta;
|
|
3001
|
+
if (position < start) break;
|
|
3002
|
+
if (position < start + edit.text.length) return end ? edit.original.end : edit.original.start;
|
|
3003
|
+
delta += edit.text.length - (edit.end - edit.at);
|
|
3019
3004
|
}
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
}
|
|
3025
|
-
/**
|
|
3026
|
-
* Provides the separator spelling used by the in-memory checker file map.
|
|
3027
|
-
*
|
|
3028
|
-
* @param name - Checker filename with either slash or backslash separators.
|
|
3029
|
-
* @returns The filename with slash separators. Casing and path segments remain unchanged.
|
|
3030
|
-
*/
|
|
3031
|
-
function checkerName(name) {
|
|
3032
|
-
return name.replaceAll("\\", "/");
|
|
3033
|
-
}
|
|
3005
|
+
const generated = position - delta;
|
|
3006
|
+
const original = this.buffer.region ? generated + this.buffer.region.start : this.source.kind === "direct" ? generated : this.#points.get(generated);
|
|
3007
|
+
return original === void 0 || original === null ? null : new TextEncoder().encode(this.#original.slice(0, original)).length;
|
|
3008
|
+
}
|
|
3009
|
+
};
|
|
3034
3010
|
/**
|
|
3035
|
-
*
|
|
3011
|
+
* Collects exact projection points for mapping checker diagnostics back to a document.
|
|
3036
3012
|
*
|
|
3037
|
-
* @param
|
|
3038
|
-
* @
|
|
3039
|
-
*
|
|
3040
|
-
* @throws If
|
|
3013
|
+
* @param source - Prepared source with its original text and any Volar or version 3 projection map.
|
|
3014
|
+
* @returns Generated UTF-16 positions mapped to original UTF-16 positions. Conflicting points map
|
|
3015
|
+
* to null. Direct sources produce an empty map because their offsets are translated directly.
|
|
3016
|
+
* @throws If a version 3 projection map cannot be decoded.
|
|
3041
3017
|
*/
|
|
3042
|
-
function
|
|
3043
|
-
|
|
3044
|
-
|
|
3018
|
+
function projectionPoints(source) {
|
|
3019
|
+
const points = /* @__PURE__ */ new Map();
|
|
3020
|
+
/**
|
|
3021
|
+
* Records a projection point and keeps conflicting mappings ambiguous.
|
|
3022
|
+
* @param generated - UTF-16 offset in the generated checker source.
|
|
3023
|
+
* @param original - Corresponding UTF-16 offset in the original document.
|
|
3024
|
+
*/
|
|
3025
|
+
const add = (generated, original) => {
|
|
3026
|
+
if (points.has(generated) && points.get(generated) !== original) points.set(generated, null);
|
|
3027
|
+
else points.set(generated, original);
|
|
3028
|
+
};
|
|
3029
|
+
if (source.kind === "direct") return points;
|
|
3030
|
+
const map = source.projection.map;
|
|
3031
|
+
if (map.kind === "volar") for (const entry of map.mappings) for (let i = 0; i < entry.sourceOffsets.length; i++) {
|
|
3032
|
+
const length = entry.generatedLengths?.[i] ?? entry.lengths[i];
|
|
3033
|
+
add(entry.generatedOffsets[i], entry.sourceOffsets[i]);
|
|
3034
|
+
if (length === entry.lengths[i]) for (let j = 1; j < length; j++) add(entry.generatedOffsets[i] + j, entry.sourceOffsets[i] + j);
|
|
3035
|
+
}
|
|
3036
|
+
else {
|
|
3037
|
+
/**
|
|
3038
|
+
* Builds line starts for converting source-map columns to offsets.
|
|
3039
|
+
* @param text - Original or generated text whose LF characters delimit lines.
|
|
3040
|
+
* @returns Ordered UTF-16 offsets, beginning with zero and including the position after every LF.
|
|
3041
|
+
*/
|
|
3042
|
+
const lines = (text) => [0, ...[...text.matchAll(/\n/g)].map((match) => match.index + 1)];
|
|
3043
|
+
const generated = lines(source.projection.code);
|
|
3044
|
+
const original = lines(source.document.code);
|
|
3045
|
+
const trace = new TraceMap(map.json);
|
|
3046
|
+
const selected = trace.sources.indexOf(map.source);
|
|
3047
|
+
for (const [line, segments] of decodedMappings(trace).entries()) for (const segment of segments) if (segment.length >= 4 && segment[1] === selected) add(generated[line] + segment[0], original[segment[2]] + segment[3]);
|
|
3048
|
+
}
|
|
3049
|
+
return points;
|
|
3045
3050
|
}
|
|
3046
|
-
// src/editor/source/literal.ts
|
|
3047
3051
|
/**
|
|
3048
|
-
*
|
|
3049
|
-
*
|
|
3050
|
-
*
|
|
3051
|
-
*
|
|
3052
|
-
* @param raw - Literal content without its opening delimiter or closing delimiter/interpolation.
|
|
3053
|
-
* @param start - Original UTF-16 offset of the first code unit in `raw`.
|
|
3054
|
-
* @returns Decoded text and `text.length + 1` source boundaries, or undefined for a trailing
|
|
3055
|
-
* backslash, invalid hex digits, a missing Unicode brace, or a code point above U+10FFFF.
|
|
3052
|
+
* Encodes identity JSON as an unpadded base64url key.
|
|
3053
|
+
* @param text - Serialized identity text encoded as UTF-8 before base64 conversion.
|
|
3054
|
+
* @returns Base64 with URL-safe characters and no trailing padding.
|
|
3056
3055
|
*/
|
|
3057
|
-
function
|
|
3058
|
-
|
|
3059
|
-
const boundaries = [start];
|
|
3060
|
-
for (let index = 0; index < raw.length;) {
|
|
3061
|
-
const begin = index;
|
|
3062
|
-
let value = raw[index++];
|
|
3063
|
-
if (value === "\\") {
|
|
3064
|
-
const escape = raw[index++];
|
|
3065
|
-
if (escape === void 0) return;
|
|
3066
|
-
if (escape === "\n" || escape === "\r") {
|
|
3067
|
-
if (escape === "\r" && raw[index] === "\n") index++;
|
|
3068
|
-
boundaries[boundaries.length - 1] = start + index;
|
|
3069
|
-
continue;
|
|
3070
|
-
}
|
|
3071
|
-
if (escape === "x" || escape === "u") {
|
|
3072
|
-
const brace = escape === "u" && raw[index] === "{";
|
|
3073
|
-
const end = brace ? raw.indexOf("}", index + 1) : index + (escape === "x" ? 2 : 4);
|
|
3074
|
-
const digits = raw.slice(index + (brace ? 1 : 0), end);
|
|
3075
|
-
if (end < index || !/^[\da-f]+$/i.test(digits)) return;
|
|
3076
|
-
const point = Number.parseInt(digits, 16);
|
|
3077
|
-
if (point > 1114111) return;
|
|
3078
|
-
value = String.fromCodePoint(point);
|
|
3079
|
-
index = end + (brace ? 1 : 0);
|
|
3080
|
-
} else value = {
|
|
3081
|
-
n: "\n",
|
|
3082
|
-
r: "\r",
|
|
3083
|
-
t: " ",
|
|
3084
|
-
b: "\b",
|
|
3085
|
-
f: "\f",
|
|
3086
|
-
v: "\v",
|
|
3087
|
-
"0": "\0"
|
|
3088
|
-
}[escape] ?? escape;
|
|
3089
|
-
} else if (value === "\r") {
|
|
3090
|
-
if (raw[index] === "\n") index++;
|
|
3091
|
-
value = "\n";
|
|
3092
|
-
}
|
|
3093
|
-
text += value;
|
|
3094
|
-
for (let unit = 1; unit <= value.length; unit++) boundaries.push(start + (unit === value.length ? index : begin));
|
|
3095
|
-
}
|
|
3096
|
-
return {
|
|
3097
|
-
text,
|
|
3098
|
-
boundaries
|
|
3099
|
-
};
|
|
3056
|
+
function base64url(text) {
|
|
3057
|
+
return btoa(String.fromCharCode(...new TextEncoder().encode(text))).replaceAll("+", "-").replaceAll("/", "_").replace(/=+/, "");
|
|
3100
3058
|
}
|
|
3101
|
-
// src/editor/source/inputs.ts
|
|
3102
3059
|
/**
|
|
3103
|
-
*
|
|
3060
|
+
* Finds the next token start after whitespace and comments.
|
|
3061
|
+
* @param text - Checker call text to scan.
|
|
3062
|
+
* @param offset - UTF-16 position at which scanning starts.
|
|
3063
|
+
* @returns UTF-16 start of the next token, or text.length at end of input.
|
|
3104
3064
|
*/
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3065
|
+
function skipTrivia(text, offset) {
|
|
3066
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true);
|
|
3067
|
+
scanner.setText(text);
|
|
3068
|
+
scanner.resetTokenState(offset);
|
|
3069
|
+
scanner.scan();
|
|
3070
|
+
return scanner.getTokenStart();
|
|
3071
|
+
}
|
|
3072
|
+
// src/editor/previews.ts
|
|
3073
|
+
/** Captures compiler input plans and assembles their required declaration previews. */
|
|
3074
|
+
var GeneratedPreviews = class {
|
|
3075
|
+
/** Fingerprint of original buffers, capture revisions, and resolved graph dependencies. */
|
|
3076
|
+
revision;
|
|
3077
|
+
/** Captures and original UTF-16 sites, keyed by full compiler module identity. */
|
|
3078
|
+
#modules = /* @__PURE__ */ new Map();
|
|
3079
|
+
/** Declaration and style-call sites keyed by semantic identity, including imported values. */
|
|
3080
|
+
#declarations = /* @__PURE__ */ new Map();
|
|
3116
3081
|
/**
|
|
3117
|
-
* Captures
|
|
3118
|
-
*
|
|
3119
|
-
*
|
|
3082
|
+
* Captures the graph's known modules together so their display names share one table.
|
|
3083
|
+
* Unknown modules are skipped.
|
|
3084
|
+
* Dependency previews require their declaration modules to be present in the graph.
|
|
3120
3085
|
*
|
|
3121
|
-
* @param graph - Original buffers and
|
|
3122
|
-
*
|
|
3123
|
-
*
|
|
3124
|
-
*
|
|
3125
|
-
* @
|
|
3126
|
-
* @throws If source transport, a projection map, or an original UTF-8 boundary is invalid.
|
|
3086
|
+
* @param graph - Original buffers and dependencies for the source revisions to capture.
|
|
3087
|
+
* @param session - Session whose analyses for modules match the supplied buffers. Captures remain
|
|
3088
|
+
* usable after later session updates. Published sites are readonly.
|
|
3089
|
+
* @param modules - Module IDs successfully compiled from these buffers. Other retained analyses are skipped.
|
|
3090
|
+
* @throws If a captured UTF-8 range is invalid for its original buffer.
|
|
3127
3091
|
*/
|
|
3128
3092
|
constructor(graph, session, modules) {
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
for (const
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
if (asset?.identity.kind === "keyframes" || this.#bindings.producer(node.expression, "keyframes")) {
|
|
3144
|
-
this.#keyframes(source, node.arguments[0]);
|
|
3145
|
-
return;
|
|
3146
|
-
}
|
|
3147
|
-
if (asset?.identity.kind === "viewTransition" || this.#bindings.producer(node.expression, "viewTransition")) {
|
|
3148
|
-
const body = node.arguments[0];
|
|
3149
|
-
if (body && ts.isObjectLiteralExpression(body)) for (const property of body.properties) {
|
|
3150
|
-
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3151
|
-
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3152
|
-
if ([
|
|
3153
|
-
"group",
|
|
3154
|
-
"old",
|
|
3155
|
-
"new",
|
|
3156
|
-
"imagePair"
|
|
3157
|
-
].includes(name) && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "viewTransitionUtilities");
|
|
3158
|
-
}
|
|
3159
|
-
return;
|
|
3160
|
-
}
|
|
3161
|
-
if (this.#bindings.producer(node.expression, "css")) {
|
|
3162
|
-
const inputs = [...node.arguments];
|
|
3163
|
-
const options = inputs.at(-1);
|
|
3164
|
-
const anchored = options !== void 0 && isCssOptions(options) && options.properties.some((property) => ts.isPropertyAssignment(property) && propertyName(property) === "selector");
|
|
3165
|
-
if (options !== void 0 && isCssOptions(options)) {
|
|
3166
|
-
inputs.pop();
|
|
3167
|
-
for (const property of options.properties) if (ts.isPropertyAssignment(property) && propertyName(property) === "template" && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "cssTemplate");
|
|
3168
|
-
}
|
|
3169
|
-
for (const input of inputs) this.#cssInput(source, input, "", anchored ? "anchoredDocument" : "document");
|
|
3170
|
-
return;
|
|
3171
|
-
}
|
|
3172
|
-
if (this.#bindings.producer(node.expression, "media")) {
|
|
3173
|
-
for (const argument of node.arguments) if (ts.isStringLiteralLike(argument)) this.#literal(source, argument, "", "mediaVariants");
|
|
3174
|
-
return;
|
|
3175
|
-
}
|
|
3176
|
-
if (this.asset(source, node.expression)?.identity.kind === "keyframes") {
|
|
3177
|
-
const options = node.arguments[0];
|
|
3178
|
-
if (options && ts.isStringLiteralLike(options)) this.#literal(source, options, "", "animationOptions");
|
|
3179
|
-
}
|
|
3180
|
-
});
|
|
3181
|
-
walk(ast, (node) => {
|
|
3182
|
-
if (!ts.isStringLiteralLike(node) && !ts.isTemplateLiteralToken(node)) return;
|
|
3183
|
-
const range = this.range(source, node);
|
|
3184
|
-
const site = range && this.previews.input(source, ...range);
|
|
3185
|
-
if (site?.native.context) this.#literal(source, node, "", site.native.context, site);
|
|
3093
|
+
const revisions = [];
|
|
3094
|
+
const captures = new Map(session.editorModules([...modules]).map((editor) => [editor.moduleId(), editor]));
|
|
3095
|
+
for (const source of graph.sources) {
|
|
3096
|
+
const editor = captures.get(source.moduleId);
|
|
3097
|
+
revisions.push(source.moduleId, fingerprint(source.document.code), editor?.revision() ?? "");
|
|
3098
|
+
if (!editor) continue;
|
|
3099
|
+
const sites = editor.sites().map((native) => ({
|
|
3100
|
+
native,
|
|
3101
|
+
start: byteToUtf16(source.document.code, native.start),
|
|
3102
|
+
end: byteToUtf16(source.document.code, native.end)
|
|
3103
|
+
}));
|
|
3104
|
+
this.#modules.set(source.moduleId, {
|
|
3105
|
+
editor,
|
|
3106
|
+
sites
|
|
3186
3107
|
});
|
|
3108
|
+
for (const { native } of sites) if (native.identity) this.#declarations.set(identityKey(native.identity), native);
|
|
3187
3109
|
}
|
|
3188
|
-
this.
|
|
3189
|
-
this.containers = this.#containers;
|
|
3110
|
+
this.revision = fingerprint(JSON.stringify([revisions, graph.dependencies]));
|
|
3190
3111
|
}
|
|
3191
3112
|
/**
|
|
3192
|
-
*
|
|
3193
|
-
*
|
|
3194
|
-
*
|
|
3113
|
+
* Finds the occurrences captured for one compiler module.
|
|
3114
|
+
*
|
|
3115
|
+
* @param source - Prepared source whose full module identity selects the capture.
|
|
3116
|
+
* @returns Occurrences in compiler traversal order, or an empty list for an unknown module.
|
|
3117
|
+
* The array and its entries are readonly and shared with this capture.
|
|
3118
|
+
*/
|
|
3119
|
+
sites(source) {
|
|
3120
|
+
return this.#modules.get(source.moduleId)?.sites ?? [];
|
|
3121
|
+
}
|
|
3122
|
+
/**
|
|
3123
|
+
* Finds a literal at the half-open original UTF-16 range `[start, end)`.
|
|
3124
|
+
* The range may address the literal content or include its one-character delimiters.
|
|
3125
|
+
*
|
|
3126
|
+
* @param source - Prepared source whose full module identity selects the capture.
|
|
3127
|
+
* @param start - Inclusive original UTF-16 offset of the content or opening delimiter.
|
|
3128
|
+
* @param end - Exclusive original UTF-16 offset of the content or closing delimiter.
|
|
3129
|
+
* @returns A shared occurrence with an input context, or undefined when no range matches exactly.
|
|
3130
|
+
*/
|
|
3131
|
+
input(source, start, end) {
|
|
3132
|
+
for (const site of this.sites(source)) if (site.native.context && (site.start === start && site.end === end || site.start === start + 1 && site.end === end - 1)) return site;
|
|
3133
|
+
}
|
|
3134
|
+
/**
|
|
3135
|
+
* Renders a replacement for the whole captured input while retaining its compiler conditions.
|
|
3136
|
+
* The result contains only that plan's CSS. Dependency assembly is handled by `preview`.
|
|
3195
3137
|
*
|
|
3196
|
-
* @param source - Prepared source
|
|
3197
|
-
* @param
|
|
3198
|
-
*
|
|
3138
|
+
* @param source - Prepared source whose full module identity selects the capture.
|
|
3139
|
+
* @param start - Inclusive original UTF-8 byte offset of the captured input, taken from
|
|
3140
|
+
* `PreviewSite.native.start`.
|
|
3141
|
+
* @param candidate - Decoded replacement text without enclosing group variants.
|
|
3142
|
+
* @returns CSS for that plan, or undefined for an unknown module or input, failed emission,
|
|
3143
|
+
* or output with no CSS. Captured plans and shared generated names remain unchanged.
|
|
3199
3144
|
*/
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
for (const property of node.properties) {
|
|
3203
|
-
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3204
|
-
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3205
|
-
if (name.endsWith(":") || ts.isObjectLiteralExpression(property.initializer)) {
|
|
3206
|
-
if (ts.isStringLiteralLike(property.name)) this.#literal(source, property.name, "", "definitionVariants");
|
|
3207
|
-
this.#keyframes(source, property.initializer);
|
|
3208
|
-
} else if (/^(?:from|to|(?:\d+(?:\.\d+)?%)(?:\s*,\s*\d+(?:\.\d+)?%)*)$/.test(name) && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "keyframesFrame");
|
|
3209
|
-
}
|
|
3145
|
+
previewInput(source, start, candidate) {
|
|
3146
|
+
return this.#modules.get(source.moduleId)?.editor.previewInput(start, candidate) ?? void 0;
|
|
3210
3147
|
}
|
|
3211
3148
|
/**
|
|
3212
|
-
*
|
|
3213
|
-
*
|
|
3149
|
+
* Assembles a captured declaration or style call addressed by its semantic identity.
|
|
3150
|
+
* Imported aliases reuse that identity.
|
|
3214
3151
|
*
|
|
3215
|
-
* @param
|
|
3216
|
-
* @
|
|
3217
|
-
*
|
|
3218
|
-
* Ref markers and supported projection calls share the declaration's opaque identity.
|
|
3152
|
+
* @param identity - Full native declaration identity, including original UTF-8 ranges.
|
|
3153
|
+
* @returns The preview assembled by `preview`, or undefined when the identity is absent
|
|
3154
|
+
* or its declaration and dependencies cannot supply valid, nonempty CSS.
|
|
3219
3155
|
*/
|
|
3220
|
-
|
|
3221
|
-
|
|
3156
|
+
previewIdentity(identity) {
|
|
3157
|
+
const site = this.#declarations.get(identityKey(identity));
|
|
3158
|
+
return site ? this.preview(site) : void 0;
|
|
3222
3159
|
}
|
|
3223
3160
|
/**
|
|
3224
|
-
*
|
|
3225
|
-
*
|
|
3226
|
-
*
|
|
3161
|
+
* Assembles `site` and its dependencies from this capture batch. Composed styles precede
|
|
3162
|
+
* the site's CSS, and global definitions follow it. Each dependency identity is visited once.
|
|
3163
|
+
* Identical CSS parts and runtime expression text are deduplicated in traversal order.
|
|
3227
3164
|
*
|
|
3228
|
-
* @param
|
|
3229
|
-
*
|
|
3230
|
-
* @returns
|
|
3231
|
-
*
|
|
3165
|
+
* @param site - Occurrence from this instance's capture batch. Its generated names must share
|
|
3166
|
+
* the declaration captures' name table.
|
|
3167
|
+
* @returns CSS and runtime key expressions, or undefined if any required site is missing or
|
|
3168
|
+
* invalid, or if the assembled CSS is empty. The site and declaration captures remain unchanged.
|
|
3232
3169
|
*/
|
|
3233
|
-
|
|
3234
|
-
|
|
3170
|
+
preview(site) {
|
|
3171
|
+
const preview = this.assemble(site);
|
|
3172
|
+
return preview?.css ? preview : void 0;
|
|
3235
3173
|
}
|
|
3236
3174
|
/**
|
|
3237
|
-
*
|
|
3238
|
-
* Both successful resolutions and misses are cached for the AST node.
|
|
3175
|
+
* Assembles `site` as {@link preview} does and keeps a successful empty result.
|
|
3239
3176
|
*
|
|
3240
|
-
* @param
|
|
3241
|
-
* @
|
|
3242
|
-
*
|
|
3243
|
-
* producer is found. Mutated bindings and cyclic aliases stop alias resolution.
|
|
3177
|
+
* @param site - Occurrence from this instance's capture batch.
|
|
3178
|
+
* @returns CSS and runtime key expressions, with empty CSS when the occurrence contributes none,
|
|
3179
|
+
* or undefined if any required site is missing or invalid.
|
|
3244
3180
|
*/
|
|
3245
|
-
|
|
3246
|
-
|
|
3181
|
+
assemble(site) {
|
|
3182
|
+
const parts = [];
|
|
3183
|
+
const runtimeNames = /* @__PURE__ */ new Set();
|
|
3184
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3185
|
+
/**
|
|
3186
|
+
* Appends valid output and dependency expressions to the shared accumulators.
|
|
3187
|
+
* A false result can leave partial entries, which the outer query discards on failure.
|
|
3188
|
+
*
|
|
3189
|
+
* @param site - Occurrence from the same capture batch as its dependency declarations.
|
|
3190
|
+
* @returns Whether the occurrence and every newly visited dependency are valid and present.
|
|
3191
|
+
*/
|
|
3192
|
+
const collect = (site) => {
|
|
3193
|
+
if (!site.valid) return false;
|
|
3194
|
+
const dependencies = site.dependencies.flatMap((identity) => {
|
|
3195
|
+
const key = identityKey(identity);
|
|
3196
|
+
if (seen.has(key)) return [];
|
|
3197
|
+
seen.add(key);
|
|
3198
|
+
return [{
|
|
3199
|
+
identity,
|
|
3200
|
+
site: this.#declarations.get(key)
|
|
3201
|
+
}];
|
|
3202
|
+
});
|
|
3203
|
+
for (const dependency of dependencies.filter((item) => item.identity.kind === "style")) if (!dependency.site || !collect(dependency.site)) return false;
|
|
3204
|
+
if (site.css) parts.push(site.css);
|
|
3205
|
+
for (const name of site.runtimeNames) runtimeNames.add(name);
|
|
3206
|
+
for (const dependency of dependencies.filter((item) => item.identity.kind !== "style")) if (!dependency.site || !collect(dependency.site)) return false;
|
|
3207
|
+
return true;
|
|
3208
|
+
};
|
|
3209
|
+
if (!collect(site)) return;
|
|
3210
|
+
return {
|
|
3211
|
+
css: [...new Set(parts)].join("\n"),
|
|
3212
|
+
runtimeNames: [...runtimeNames]
|
|
3213
|
+
};
|
|
3247
3214
|
}
|
|
3215
|
+
};
|
|
3216
|
+
// src/editor/source/bindings.ts
|
|
3217
|
+
/**
|
|
3218
|
+
* Resolves declaration identities and original ranges in a fixed source graph.
|
|
3219
|
+
*/
|
|
3220
|
+
var SourceBindings = class {
|
|
3221
|
+
/** In-memory files keyed by checker filename with slash separators and preserved casing. */
|
|
3222
|
+
files = /* @__PURE__ */ new Map();
|
|
3223
|
+
/** Producers keyed first by module identity, then by `start:end` in original UTF-16 coordinates. */
|
|
3224
|
+
#assets = /* @__PURE__ */ new Map();
|
|
3225
|
+
/** Comments with `swlint-*` directives keyed by module identity, from each source's scan. */
|
|
3226
|
+
#lintComments = /* @__PURE__ */ new Map();
|
|
3227
|
+
/** Resolved producers and cached misses for nodes in the captured checker ASTs. */
|
|
3228
|
+
#nodeAssets = /* @__PURE__ */ new WeakMap();
|
|
3229
|
+
/** Generated UTF-16 positions mapped to original positions. Null marks conflicting origins. */
|
|
3230
|
+
#points = /* @__PURE__ */ new Map();
|
|
3231
|
+
/** Binder over the in-memory files and the graph's resolved import dependencies. */
|
|
3232
|
+
#checker;
|
|
3233
|
+
/** Symbols found in assignment or increment/decrement targets, excluded from stable aliases. */
|
|
3234
|
+
#mutated = /* @__PURE__ */ new Set();
|
|
3248
3235
|
/**
|
|
3249
|
-
*
|
|
3236
|
+
* Parses sources, scans declaration records, and binds imports without reading disk.
|
|
3250
3237
|
*
|
|
3251
|
-
* @param
|
|
3252
|
-
* @
|
|
3253
|
-
*
|
|
3238
|
+
* @param graph - Fixed sources and resolved dependencies from one analysis. Do not mutate its sources.
|
|
3239
|
+
* @param session - Session used to scan those sources. Capture previews before constructing this binder.
|
|
3240
|
+
* @throws If source scanning or projection mapping fails.
|
|
3254
3241
|
*/
|
|
3255
|
-
|
|
3256
|
-
|
|
3242
|
+
constructor(graph, session) {
|
|
3243
|
+
for (const source of graph.sources) {
|
|
3244
|
+
const text = source.kind === "direct" ? source.document.code : source.projection.code;
|
|
3245
|
+
const fileName = checkerName(source.checkerFileName);
|
|
3246
|
+
const ast = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
3247
|
+
this.files.set(fileName, {
|
|
3248
|
+
source,
|
|
3249
|
+
ast
|
|
3250
|
+
});
|
|
3251
|
+
this.#points.set(source.moduleId, projectionPoints(source));
|
|
3252
|
+
const scan = session.scanSource(source);
|
|
3253
|
+
this.#assets.set(source.moduleId, assetRanges(source.document.code, scan.kind === "ready" ? scan.summary.assets : []));
|
|
3254
|
+
this.#lintComments.set(source.moduleId, scan.kind === "ready" ? scan.summary.lintComments : []);
|
|
3255
|
+
}
|
|
3256
|
+
const host = {
|
|
3257
|
+
getSourceFile: (name) => this.files.get(checkerName(name))?.ast,
|
|
3258
|
+
getDefaultLibFileName: () => "lib.d.ts",
|
|
3259
|
+
writeFile: () => {},
|
|
3260
|
+
getCurrentDirectory: () => "/",
|
|
3261
|
+
getDirectories: () => [],
|
|
3262
|
+
fileExists: (name) => this.files.has(checkerName(name)),
|
|
3263
|
+
readFile: (name) => this.files.get(checkerName(name))?.ast.text,
|
|
3264
|
+
getCanonicalFileName: checkerName,
|
|
3265
|
+
useCaseSensitiveFileNames: () => true,
|
|
3266
|
+
getNewLine: () => "\n",
|
|
3267
|
+
resolveModuleNames: (names, containingFile) => names.map((name) => {
|
|
3268
|
+
const source = this.files.get(checkerName(containingFile))?.source;
|
|
3269
|
+
const dependency = graph.dependencies.find((item) => item.importer === source?.resolverId && item.specifier === name);
|
|
3270
|
+
const target = graph.sources.find((item) => item.resolverId === dependency?.id);
|
|
3271
|
+
return target ? {
|
|
3272
|
+
resolvedFileName: checkerName(target.checkerFileName),
|
|
3273
|
+
extension: ts.Extension.Tsx
|
|
3274
|
+
} : void 0;
|
|
3275
|
+
})
|
|
3276
|
+
};
|
|
3277
|
+
const program = ts.createProgram([...this.files.keys()], {
|
|
3278
|
+
noLib: true,
|
|
3279
|
+
allowJs: true
|
|
3280
|
+
}, host);
|
|
3281
|
+
this.#checker = program.getTypeChecker();
|
|
3282
|
+
for (const { ast } of this.files.values()) walk(ast, (node) => {
|
|
3283
|
+
const target = ts.isBinaryExpression(node) && node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && node.operatorToken.kind <= ts.SyntaxKind.LastAssignment ? node.left : (ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) && [ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken].includes(node.operator) ? node.operand : void 0;
|
|
3284
|
+
if (target) {
|
|
3285
|
+
/**
|
|
3286
|
+
* Records symbols in assignment targets that can contain identifier bindings.
|
|
3287
|
+
* Property accesses do not mark their receiver as a reassigned binding.
|
|
3288
|
+
*
|
|
3289
|
+
* @param node - Identifier or supported destructuring subtree of the target.
|
|
3290
|
+
* Recognized symbols are added to `#mutated`, including nested pattern members.
|
|
3291
|
+
*/
|
|
3292
|
+
const mark = (node) => {
|
|
3293
|
+
if (ts.isIdentifier(node)) {
|
|
3294
|
+
const symbol = this.#checker.getSymbolAtLocation(node);
|
|
3295
|
+
if (symbol) this.#mutated.add(symbol);
|
|
3296
|
+
} else if (ts.isArrayLiteralExpression(node) || ts.isObjectLiteralExpression(node) || ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node) || ts.isSpreadAssignment(node) || ts.isSpreadElement(node) || ts.isParenthesizedExpression(node)) ts.forEachChild(node, mark);
|
|
3297
|
+
};
|
|
3298
|
+
mark(target);
|
|
3299
|
+
}
|
|
3300
|
+
});
|
|
3257
3301
|
}
|
|
3258
3302
|
/**
|
|
3259
|
-
*
|
|
3260
|
-
*
|
|
3261
|
-
* Invalid sites remain identifiable.
|
|
3303
|
+
* Recognizes a direct method of an imported Stillwind style producer.
|
|
3304
|
+
* Other property accesses with the same spelling do not establish producer authority.
|
|
3262
3305
|
*
|
|
3263
|
-
* @param
|
|
3264
|
-
* @param
|
|
3265
|
-
* @returns
|
|
3266
|
-
* Callers must keep the returned site unchanged.
|
|
3306
|
+
* @param node - Expression in one of this index's checker ASTs.
|
|
3307
|
+
* @param name - Exact method spelling, such as `keyframes` or `viewTransition`.
|
|
3308
|
+
* @returns Whether the receiver is a supported value import of `style` and the method matches.
|
|
3267
3309
|
*/
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
if (!range) return;
|
|
3271
|
-
const sites = this.previews.sites(source);
|
|
3272
|
-
const exact = sites.find((site) => site.native.kind !== "input" && site.start === range[0] && site.end === range[1]);
|
|
3273
|
-
if (exact || !ts.isCallExpression(node)) return exact;
|
|
3274
|
-
const receiver = ts.isPropertyAccessExpression(node.expression) ? node.expression.expression : node.expression;
|
|
3275
|
-
const receiverRange = this.range(source, receiver);
|
|
3276
|
-
return receiverRange ? sites.find((site) => site.native.kind === "use" && site.start === receiverRange[0] && site.end === receiverRange[1]) : void 0;
|
|
3310
|
+
producer(node, name) {
|
|
3311
|
+
return ts.isPropertyAccessExpression(node) && node.name.text === name && this.styleImport(node.expression);
|
|
3277
3312
|
}
|
|
3278
3313
|
/**
|
|
3279
|
-
*
|
|
3314
|
+
* Checks direct named imports of `style` and namespace accesses to that export.
|
|
3280
3315
|
*
|
|
3281
|
-
* @param
|
|
3282
|
-
*
|
|
3283
|
-
* @returns
|
|
3284
|
-
*
|
|
3316
|
+
* @param node - Expression in this index's checker ASTs. Supported imports come from
|
|
3317
|
+
* Stillwind's framework and vanilla entrypoints.
|
|
3318
|
+
* @returns Whether a value import establishes that producer. Type-only imports and symbols
|
|
3319
|
+
* marked as mutated fail this check.
|
|
3285
3320
|
*/
|
|
3286
|
-
|
|
3287
|
-
|
|
3321
|
+
styleImport(node) {
|
|
3322
|
+
const namespace = ts.isPropertyAccessExpression(node) && node.name.text === "style";
|
|
3323
|
+
if (!ts.isIdentifier(node) && !(namespace && ts.isIdentifier(node.expression))) return false;
|
|
3324
|
+
const symbol = this.#checker.getSymbolAtLocation(namespace ? node.expression : node);
|
|
3325
|
+
if (!symbol || this.#mutated.has(symbol)) return false;
|
|
3326
|
+
return !!symbol.declarations?.some((declaration) => {
|
|
3327
|
+
if (namespace ? !ts.isNamespaceImport(declaration) : !ts.isImportSpecifier(declaration) || (declaration.propertyName ?? declaration.name).text !== "style" || declaration.isTypeOnly) return false;
|
|
3328
|
+
const owner = ts.isNamespaceImport(declaration) ? declaration.parent.parent : declaration.parent.parent.parent;
|
|
3329
|
+
return ts.isImportDeclaration(owner) && owner.importClause?.phaseModifier !== ts.SyntaxKind.TypeKeyword && ts.isStringLiteral(owner.moduleSpecifier) && /^stillwind\/(?:alpine|astro|lit|preact|qwik|react|solid|svelte|vanilla|vue)$/.test(owner.moduleSpecifier.text);
|
|
3330
|
+
});
|
|
3288
3331
|
}
|
|
3289
3332
|
/**
|
|
3290
|
-
*
|
|
3291
|
-
*
|
|
3333
|
+
* Maps a checker node to an exact range in its original document.
|
|
3334
|
+
* For projections, an anchor is accepted only when the intervening and selected text match
|
|
3335
|
+
* the original buffer. A conflicting mapping at the node's start prevents fallback to an anchor.
|
|
3292
3336
|
*
|
|
3293
|
-
* @param source - Prepared source
|
|
3294
|
-
* @param node -
|
|
3295
|
-
* @
|
|
3296
|
-
*
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
return;
|
|
3311
|
-
}
|
|
3312
|
-
if (ts.isObjectLiteralExpression(node)) for (const property of node.properties) {
|
|
3313
|
-
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3314
|
-
const name = propertyName(property);
|
|
3315
|
-
if (name === "" || name.endsWith(":")) this.#cssInput(source, property.initializer, name === ":" ? prefix : prefix + name, context);
|
|
3337
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3338
|
+
* @param node - Node from this index, with leading trivia excluded from its selected range.
|
|
3339
|
+
* @returns Inclusive start and exclusive end in original UTF-16 coordinates, or undefined
|
|
3340
|
+
* when a projection cannot map the text exactly.
|
|
3341
|
+
*/
|
|
3342
|
+
range(source, node) {
|
|
3343
|
+
const ast = node.getSourceFile();
|
|
3344
|
+
const start = node.getStart(ast);
|
|
3345
|
+
const end = node.end;
|
|
3346
|
+
if (source.kind === "direct") return [start, end];
|
|
3347
|
+
const points = this.#points.get(source.moduleId);
|
|
3348
|
+
const exact = points.get(start);
|
|
3349
|
+
const anchors = exact === void 0 ? [...points].filter(([position, original]) => position <= start && original !== null).sort((a, b) => b[0] - a[0]) : [[start, exact]];
|
|
3350
|
+
for (const [position, original] of anchors) {
|
|
3351
|
+
if (original === null || original === void 0) continue;
|
|
3352
|
+
const offset = original + start - position;
|
|
3353
|
+
if (source.document.code.slice(original, offset + end - start) === ast.text.slice(position, end)) return [offset, offset + end - start];
|
|
3316
3354
|
}
|
|
3317
3355
|
}
|
|
3318
3356
|
/**
|
|
3319
|
-
*
|
|
3320
|
-
*
|
|
3321
|
-
* expressions contribute only their right operand, and unrecognized syntax adds nothing.
|
|
3357
|
+
* Resolves a native producer or an alias through stable variable initializers and imports.
|
|
3358
|
+
* Both successful resolutions and misses are cached for the AST node.
|
|
3322
3359
|
*
|
|
3323
3360
|
* @param source - Prepared source whose checker AST contains `node`.
|
|
3324
|
-
* @param node -
|
|
3325
|
-
*
|
|
3326
|
-
*
|
|
3327
|
-
* names are appended before their literals are recorded.
|
|
3361
|
+
* @param node - Node from this index. Alias chains may reach another source in the same graph.
|
|
3362
|
+
* @returns A shared native record that callers must keep unchanged, or undefined when no
|
|
3363
|
+
* producer is found. Mutated bindings and cyclic aliases stop alias resolution.
|
|
3328
3364
|
*/
|
|
3329
|
-
|
|
3330
|
-
if (
|
|
3331
|
-
|
|
3332
|
-
return;
|
|
3333
|
-
}
|
|
3334
|
-
if (ts.isTemplateExpression(node)) {
|
|
3335
|
-
this.#literal(source, node.head, prefix);
|
|
3336
|
-
for (const span of node.templateSpans) this.#literal(source, span.literal, prefix);
|
|
3337
|
-
return;
|
|
3338
|
-
}
|
|
3339
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
3340
|
-
this.#containers.push({
|
|
3341
|
-
source,
|
|
3342
|
-
node
|
|
3343
|
-
});
|
|
3344
|
-
for (const element of node.elements) this.#input(source, element, prefix);
|
|
3345
|
-
return;
|
|
3346
|
-
}
|
|
3347
|
-
if (ts.isConditionalExpression(node)) {
|
|
3348
|
-
this.#input(source, node.whenTrue, prefix);
|
|
3349
|
-
this.#input(source, node.whenFalse, prefix);
|
|
3350
|
-
return;
|
|
3351
|
-
}
|
|
3352
|
-
if (ts.isBinaryExpression(node) && [
|
|
3353
|
-
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
3354
|
-
ts.SyntaxKind.BarBarToken,
|
|
3355
|
-
ts.SyntaxKind.QuestionQuestionToken
|
|
3356
|
-
].includes(node.operatorToken.kind)) {
|
|
3357
|
-
this.#input(source, node.right, prefix);
|
|
3358
|
-
return;
|
|
3359
|
-
}
|
|
3360
|
-
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
3361
|
-
this.#input(source, node.expression, prefix);
|
|
3362
|
-
return;
|
|
3363
|
-
}
|
|
3364
|
-
if (ts.isElementAccessExpression(node) || ts.isPropertyAccessExpression(node)) {
|
|
3365
|
-
this.#table(source, node.expression, prefix);
|
|
3366
|
-
return;
|
|
3367
|
-
}
|
|
3368
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
3369
|
-
this.#containers.push({
|
|
3370
|
-
source,
|
|
3371
|
-
node
|
|
3372
|
-
});
|
|
3373
|
-
for (const property of node.properties) {
|
|
3374
|
-
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3375
|
-
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3376
|
-
if (name !== "" && !name.endsWith(":")) continue;
|
|
3377
|
-
this.#input(source, property.initializer, name === ":" ? prefix : prefix + name);
|
|
3378
|
-
}
|
|
3379
|
-
}
|
|
3365
|
+
asset(source, node) {
|
|
3366
|
+
if (!this.#nodeAssets.has(node)) this.#nodeAssets.set(node, this.#resolveAsset(source, node, /* @__PURE__ */ new Set()));
|
|
3367
|
+
return this.#nodeAssets.get(node);
|
|
3380
3368
|
}
|
|
3381
3369
|
/**
|
|
3382
|
-
*
|
|
3383
|
-
* Literal spreads are traversed, and keys are not utilities. A table is not a sortable
|
|
3384
|
-
* container, because reordering its entries would change which entry an index selects.
|
|
3370
|
+
* Returns the comments that contain `swlint-*` directives in `source`'s original buffer.
|
|
3385
3371
|
*
|
|
3386
|
-
* @param source - Prepared source
|
|
3387
|
-
* @
|
|
3388
|
-
*
|
|
3372
|
+
* @param source - Prepared source of this index.
|
|
3373
|
+
* @returns Comment bodies in original UTF-8 byte offsets, or an empty list when the scan
|
|
3374
|
+
* failed. Callers must keep the returned list unchanged.
|
|
3389
3375
|
*/
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
const values = ts.isArrayLiteralExpression(node) ? [...node.elements] : [];
|
|
3393
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
3394
|
-
for (const property of node.properties) if (ts.isPropertyAssignment(property)) values.push(property.initializer);
|
|
3395
|
-
else if (ts.isSpreadAssignment(property)) this.#table(source, property.expression, prefix);
|
|
3396
|
-
}
|
|
3397
|
-
for (const value of values) if (ts.isSpreadElement(value)) this.#table(source, value.expression, prefix);
|
|
3398
|
-
else if (ts.isStringLiteralLike(value)) this.#literal(source, value, prefix);
|
|
3376
|
+
lintComments(source) {
|
|
3377
|
+
return this.#lintComments.get(source.moduleId) ?? [];
|
|
3399
3378
|
}
|
|
3400
3379
|
/**
|
|
3401
|
-
*
|
|
3402
|
-
*
|
|
3403
|
-
* Unmapped ranges or decoding that disagrees with the AST add nothing.
|
|
3380
|
+
* Follows a producer range or a stable alias chain without caching intermediate nodes.
|
|
3381
|
+
* Exact native producer records take precedence over checks on alias bindings.
|
|
3404
3382
|
*
|
|
3405
3383
|
* @param source - Prepared source whose checker AST contains `node`.
|
|
3406
|
-
* @param node -
|
|
3407
|
-
* @param
|
|
3408
|
-
*
|
|
3409
|
-
* @
|
|
3410
|
-
*
|
|
3384
|
+
* @param node - Expression to resolve, including supported TypeScript wrappers.
|
|
3385
|
+
* @param seen - Symbols already visited in the current chain. Visited symbols are added
|
|
3386
|
+
* even when resolution fails, so a separate chain needs a fresh set.
|
|
3387
|
+
* @returns The native record reached by the chain, or undefined for mutated, cyclic,
|
|
3388
|
+
* missing, or unsupported bindings.
|
|
3411
3389
|
*/
|
|
3412
|
-
#
|
|
3390
|
+
#resolveAsset(source, node, seen) {
|
|
3413
3391
|
const range = this.range(source, node);
|
|
3414
|
-
if (
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
if (
|
|
3419
|
-
|
|
3420
|
-
if (
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
this.#
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
});
|
|
3392
|
+
if (range) {
|
|
3393
|
+
const found = this.#assets.get(source.moduleId)?.get(`${range[0]}:${range[1]}`);
|
|
3394
|
+
if (found) return found;
|
|
3395
|
+
}
|
|
3396
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) return this.#resolveAsset(source, node.expression, seen);
|
|
3397
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression)) {
|
|
3398
|
+
if (!this.#checker.getSymbolAtLocation(node.expression)?.declarations?.some(ts.isNamespaceImport)) return;
|
|
3399
|
+
} else if (!ts.isIdentifier(node)) return;
|
|
3400
|
+
let symbol = this.#checker.getSymbolAtLocation(node);
|
|
3401
|
+
if (!symbol || this.#mutated.has(symbol) || seen.has(symbol)) return;
|
|
3402
|
+
seen.add(symbol);
|
|
3403
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
3404
|
+
symbol = this.#checker.getAliasedSymbol(symbol);
|
|
3405
|
+
if (this.#mutated.has(symbol) || seen.has(symbol)) return;
|
|
3406
|
+
seen.add(symbol);
|
|
3407
|
+
}
|
|
3408
|
+
const declaration = symbol.valueDeclaration;
|
|
3409
|
+
if (declaration && ts.isVariableDeclaration(declaration) && declaration.initializer) {
|
|
3410
|
+
const owner = this.files.get(declaration.getSourceFile().fileName)?.source;
|
|
3411
|
+
return owner ? this.#resolveAsset(owner, declaration.initializer, seen) : void 0;
|
|
3435
3412
|
}
|
|
3436
3413
|
}
|
|
3414
|
+
/**
|
|
3415
|
+
* Finds the smallest mapped AST node that contains an original document position.
|
|
3416
|
+
*
|
|
3417
|
+
* @param source - Prepared source whose checker filename selects a file in this index.
|
|
3418
|
+
* @param offset - Original UTF-16 cursor offset. Containment uses half-open ranges.
|
|
3419
|
+
* @returns The containing node, or undefined when the file is absent or no mapped node
|
|
3420
|
+
* contains the position. A position at a node's exclusive end does not select that node.
|
|
3421
|
+
*/
|
|
3422
|
+
nodeAt(source, offset) {
|
|
3423
|
+
const file = this.files.get(checkerName(source.checkerFileName));
|
|
3424
|
+
let best;
|
|
3425
|
+
if (file) walk(file.ast, (node) => {
|
|
3426
|
+
const range = this.range(source, node);
|
|
3427
|
+
if (range && range[0] <= offset && offset < range[1] && (!best || node.end - node.pos < best.end - best.pos)) best = node;
|
|
3428
|
+
});
|
|
3429
|
+
return best;
|
|
3430
|
+
}
|
|
3437
3431
|
};
|
|
3438
3432
|
/**
|
|
3439
|
-
*
|
|
3440
|
-
*
|
|
3441
|
-
* @param property - Property assignment whose key is not computed.
|
|
3442
|
-
* @returns The decoded key text.
|
|
3443
|
-
*/
|
|
3444
|
-
function propertyName(property) {
|
|
3445
|
-
return ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3446
|
-
}
|
|
3447
|
-
/**
|
|
3448
|
-
* Recognizes the options object of `style.css()`: an object literal whose properties
|
|
3449
|
-
* all have the name `template` or `selector`.
|
|
3450
|
-
*
|
|
3451
|
-
* @param node - Last argument of a `style.css()` call.
|
|
3452
|
-
* @returns Whether the compiler reads the argument as options rather than an input.
|
|
3453
|
-
*/
|
|
3454
|
-
function isCssOptions(node) {
|
|
3455
|
-
return ts.isObjectLiteralExpression(node) && node.properties.length > 0 && node.properties.every((property) => ts.isPropertyAssignment(property) && !ts.isComputedPropertyName(property.name) && ["template", "selector"].includes(propertyName(property)));
|
|
3456
|
-
}
|
|
3457
|
-
// src/editor/colors.ts
|
|
3458
|
-
/**
|
|
3459
|
-
* Finds a spelling with an arbitrary color that preserves the candidate's variants, importance,
|
|
3460
|
-
* and modifier.
|
|
3461
|
-
* Existing opacity modifiers remain present, so their maximum opacity can prevent a replacement.
|
|
3462
|
-
* The emitted color must agree with every requested component within 2/255.
|
|
3433
|
+
* Converts native producer call ranges with one traversal of the original source.
|
|
3463
3434
|
*
|
|
3464
|
-
* @param text -
|
|
3465
|
-
* @param
|
|
3466
|
-
* @
|
|
3467
|
-
* @
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
*/
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
const importance = utility.startsWith("!") || utility.endsWith("!") ? "!" : "";
|
|
3483
|
-
const [base, modifier] = segments(utility.replace(/^!|!$/g, ""), "/");
|
|
3484
|
-
const spellings = [];
|
|
3485
|
-
if (base.startsWith("[") && base.endsWith("]")) {
|
|
3486
|
-
const property = base.slice(1).split(":", 1)[0];
|
|
3487
|
-
spellings.push((value) => `[${property}:${value}]`);
|
|
3488
|
-
} else for (let index = base.length - 1; index > 0; index--) if (base[index] === "-") {
|
|
3489
|
-
const root = base.slice(0, index);
|
|
3490
|
-
spellings.push((value) => `${root}-[${value}]`);
|
|
3491
|
-
}
|
|
3492
|
-
const rgba = [
|
|
3493
|
-
color.red,
|
|
3494
|
-
color.green,
|
|
3495
|
-
color.blue,
|
|
3496
|
-
color.alpha
|
|
3497
|
-
].map((value) => Math.min(1, Math.max(0, value)));
|
|
3498
|
-
const rgb = rgba.slice(0, 3).map((value) => Math.round(value * 255));
|
|
3499
|
-
for (const spelling of spellings) {
|
|
3500
|
-
/**
|
|
3501
|
-
* Rebuilds a color candidate with its variants, importance, and opacity modifier preserved.
|
|
3502
|
-
*
|
|
3503
|
-
* @param value - CSS color encoded for an arbitrary utility value.
|
|
3504
|
-
* @returns A candidate using the current spelling, without the separately supplied outer variants.
|
|
3505
|
-
*/
|
|
3506
|
-
const build = (value) => `${prefix}${importance}${spelling(value)}${modifier ? `/${modifier}` : ""}`;
|
|
3507
|
-
const probe = candidates.color(outer + build("#ffffff"));
|
|
3508
|
-
if (!probe || rgba[3] > probe[3] + 1 / 255) continue;
|
|
3509
|
-
const alpha = probe[3] ? rgba[3] / probe[3] : 1;
|
|
3510
|
-
const candidate = build(alpha >= 1 ? `#${rgb.map((value) => value.toString(16).padStart(2, "0")).join("")}` : `rgb(${rgb.join("_")}_/_${Number(alpha.toFixed(4))})`);
|
|
3511
|
-
if (candidates.color(outer + candidate)?.every((component, index) => Math.abs(component - rgba[index]) <= 2 / 255)) return candidate;
|
|
3435
|
+
* @param text - Original document text that owns every producer range.
|
|
3436
|
+
* @param assets - Native records whose call ranges use original UTF-8 byte offsets.
|
|
3437
|
+
* @returns Records keyed by `start:end`, where both offsets use original UTF-16 coordinates.
|
|
3438
|
+
* @throws If a call boundary falls inside a UTF-8 sequence or outside the source.
|
|
3439
|
+
*/
|
|
3440
|
+
function assetRanges(text, assets) {
|
|
3441
|
+
const offsets = [...new Set(assets.flatMap((asset) => [asset.callStart, asset.callEnd]))].sort((a, b) => a - b);
|
|
3442
|
+
const positions = /* @__PURE__ */ new Map();
|
|
3443
|
+
let byte = 0;
|
|
3444
|
+
let utf16 = 0;
|
|
3445
|
+
for (const offset of offsets) {
|
|
3446
|
+
while (byte < offset && utf16 < text.length) {
|
|
3447
|
+
const point = text.codePointAt(utf16);
|
|
3448
|
+
byte += point <= 127 ? 1 : point <= 2047 ? 2 : point <= 65535 ? 3 : 4;
|
|
3449
|
+
utf16 += point > 65535 ? 2 : 1;
|
|
3450
|
+
}
|
|
3451
|
+
if (byte !== offset) throw new Error("Invalid original UTF-8 boundary");
|
|
3452
|
+
positions.set(offset, utf16);
|
|
3512
3453
|
}
|
|
3454
|
+
return new Map(assets.map((asset) => [`${positions.get(asset.callStart)}:${positions.get(asset.callEnd)}`, asset]));
|
|
3513
3455
|
}
|
|
3514
3456
|
/**
|
|
3515
|
-
*
|
|
3457
|
+
* Provides the separator spelling used by the in-memory checker file map.
|
|
3516
3458
|
*
|
|
3517
|
-
* @param
|
|
3518
|
-
* @returns
|
|
3459
|
+
* @param name - Checker filename with either slash or backslash separators.
|
|
3460
|
+
* @returns The filename with slash separators. Casing and path segments remain unchanged.
|
|
3519
3461
|
*/
|
|
3520
|
-
function
|
|
3521
|
-
|
|
3522
|
-
color.red,
|
|
3523
|
-
color.green,
|
|
3524
|
-
color.blue,
|
|
3525
|
-
color.alpha
|
|
3526
|
-
];
|
|
3527
|
-
if (!components.every(Number.isFinite)) return;
|
|
3528
|
-
const rgba = components.map((value) => Math.min(1, Math.max(0, value)));
|
|
3529
|
-
const rgb = rgba.slice(0, 3).map((value) => Math.round(value * 255));
|
|
3530
|
-
return rgba[3] === 1 ? `#${rgb.map((value) => value.toString(16).padStart(2, "0")).join("")}` : `rgb(${rgb.join(" ")} / ${Number(rgba[3].toFixed(4))})`;
|
|
3462
|
+
function checkerName(name) {
|
|
3463
|
+
return name.replaceAll("\\", "/");
|
|
3531
3464
|
}
|
|
3532
|
-
// src/editor/completion.ts
|
|
3533
3465
|
/**
|
|
3534
|
-
*
|
|
3535
|
-
*
|
|
3536
|
-
* @param
|
|
3537
|
-
* @
|
|
3466
|
+
* Visits an AST node before its descendants in TypeScript's child order.
|
|
3467
|
+
*
|
|
3468
|
+
* @param node - Root of the subtree to traverse.
|
|
3469
|
+
* @param visit - Callback for each node. It must preserve the tree during traversal.
|
|
3470
|
+
* Returning from the callback does not skip descendants.
|
|
3471
|
+
* @throws If the callback throws, traversal stops and propagates that error.
|
|
3538
3472
|
*/
|
|
3539
|
-
function
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
const placeholder = variant.lastIndexOf("[]");
|
|
3543
|
-
if (placeholder < 0) return;
|
|
3544
|
-
const index = text.length - variant.length + placeholder;
|
|
3545
|
-
return `${escape(source(candidate.slice(0, index + 1)))}\${1}${escape(source(candidate.slice(index + 1)))}\${0}`;
|
|
3473
|
+
function walk(node, visit) {
|
|
3474
|
+
visit(node);
|
|
3475
|
+
ts.forEachChild(node, (child) => walk(child, visit));
|
|
3546
3476
|
}
|
|
3477
|
+
// src/editor/source/literal.ts
|
|
3547
3478
|
/**
|
|
3548
|
-
*
|
|
3549
|
-
*
|
|
3550
|
-
*
|
|
3551
|
-
*
|
|
3479
|
+
* Decodes JS literal content while mapping every decoded position to its source spelling.
|
|
3480
|
+
* CR and CRLF become LF. Escaped line continuations emit no text and move the preceding
|
|
3481
|
+
* boundary past the continuation. The caller must compare the result with the AST's decoded text.
|
|
3482
|
+
*
|
|
3483
|
+
* @param raw - Literal content without its opening delimiter or closing delimiter/interpolation.
|
|
3484
|
+
* @param start - Original UTF-16 offset of the first code unit in `raw`.
|
|
3485
|
+
* @returns Decoded text and `text.length + 1` source boundaries, or undefined for a trailing
|
|
3486
|
+
* backslash, invalid hex digits, a missing Unicode brace, or a code point above U+10FFFF.
|
|
3552
3487
|
*/
|
|
3553
|
-
function
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3488
|
+
function decodeLiteral(raw, start) {
|
|
3489
|
+
let text = "";
|
|
3490
|
+
const boundaries = [start];
|
|
3491
|
+
for (let index = 0; index < raw.length;) {
|
|
3492
|
+
const begin = index;
|
|
3493
|
+
let value = raw[index++];
|
|
3494
|
+
if (value === "\\") {
|
|
3495
|
+
const escape = raw[index++];
|
|
3496
|
+
if (escape === void 0) return;
|
|
3497
|
+
if (escape === "\n" || escape === "\r") {
|
|
3498
|
+
if (escape === "\r" && raw[index] === "\n") index++;
|
|
3499
|
+
boundaries[boundaries.length - 1] = start + index;
|
|
3500
|
+
continue;
|
|
3501
|
+
}
|
|
3502
|
+
if (escape === "x" || escape === "u") {
|
|
3503
|
+
const brace = escape === "u" && raw[index] === "{";
|
|
3504
|
+
const end = brace ? raw.indexOf("}", index + 1) : index + (escape === "x" ? 2 : 4);
|
|
3505
|
+
const digits = raw.slice(index + (brace ? 1 : 0), end);
|
|
3506
|
+
if (end < index || !/^[\da-f]+$/i.test(digits)) return;
|
|
3507
|
+
const point = Number.parseInt(digits, 16);
|
|
3508
|
+
if (point > 1114111) return;
|
|
3509
|
+
value = String.fromCodePoint(point);
|
|
3510
|
+
index = end + (brace ? 1 : 0);
|
|
3511
|
+
} else value = {
|
|
3512
|
+
n: "\n",
|
|
3513
|
+
r: "\r",
|
|
3514
|
+
t: " ",
|
|
3515
|
+
b: "\b",
|
|
3516
|
+
f: "\f",
|
|
3517
|
+
v: "\v",
|
|
3518
|
+
"0": "\0"
|
|
3519
|
+
}[escape] ?? escape;
|
|
3520
|
+
} else if (value === "\r") {
|
|
3521
|
+
if (raw[index] === "\n") index++;
|
|
3522
|
+
value = "\n";
|
|
3523
|
+
}
|
|
3524
|
+
text += value;
|
|
3525
|
+
for (let unit = 1; unit <= value.length; unit++) boundaries.push(start + (unit === value.length ? index : begin));
|
|
3526
|
+
}
|
|
3527
|
+
return {
|
|
3528
|
+
text,
|
|
3529
|
+
boundaries
|
|
3530
|
+
};
|
|
3558
3531
|
}
|
|
3559
|
-
// src/editor/source/
|
|
3560
|
-
/** Most catalog candidates that one completion request adds. */
|
|
3561
|
-
const CATALOG_COMPLETIONS = 200;
|
|
3532
|
+
// src/editor/source/inputs.ts
|
|
3562
3533
|
/**
|
|
3563
|
-
*
|
|
3534
|
+
* Indexes utility literals and sortable inputs in a fixed source graph.
|
|
3564
3535
|
*/
|
|
3565
|
-
var
|
|
3566
|
-
/**
|
|
3567
|
-
|
|
3568
|
-
/**
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
#
|
|
3574
|
-
/**
|
|
3575
|
-
#
|
|
3576
|
-
/** Fingerprint of captures, graph dependencies, and configuration used for lazy resolution. */
|
|
3577
|
-
#revision;
|
|
3578
|
-
/** Comments added to formatted CSS previews. Omitted fields use native defaults. */
|
|
3579
|
-
#annotations;
|
|
3580
|
-
/** Queries that include callback CSS for selected completions in this snapshot. */
|
|
3581
|
-
#resolved = /* @__PURE__ */ new Map();
|
|
3582
|
-
/** Resolves completion CSS separately from the captured compiler session. */
|
|
3583
|
-
#plugins;
|
|
3536
|
+
var SourceIndex = class {
|
|
3537
|
+
/** Generated occurrences and replacement plans captured from the same source graph. */
|
|
3538
|
+
previews;
|
|
3539
|
+
/** Indexed literals in discovery order, with fixed source boundaries. */
|
|
3540
|
+
literals;
|
|
3541
|
+
#literals = [];
|
|
3542
|
+
/** Fixed direct inputs eligible for grouping, including nested containers. */
|
|
3543
|
+
containers;
|
|
3544
|
+
#containers = [];
|
|
3545
|
+
/** Binder and source ranges shared by all indexed inputs. */
|
|
3546
|
+
#bindings;
|
|
3584
3547
|
/**
|
|
3585
|
-
*
|
|
3548
|
+
* Captures generated previews, rescans graph sources for producer records, and binds aliases.
|
|
3549
|
+
* Checker ASTs use projected text when present. Editor ranges address original buffers.
|
|
3550
|
+
* Source scanning updates the session's source registry and can replace reports for a failed scan.
|
|
3586
3551
|
*
|
|
3587
|
-
* @param
|
|
3588
|
-
*
|
|
3589
|
-
* @param
|
|
3590
|
-
*
|
|
3591
|
-
* @param
|
|
3592
|
-
* @
|
|
3593
|
-
* @param plugins - Optional plugin runtime used for lazy suggestions and selected candidates.
|
|
3552
|
+
* @param graph - Original buffers and resolved dependencies for the analyzed source revisions.
|
|
3553
|
+
* Its prepared sources must remain unchanged while the index is used.
|
|
3554
|
+
* @param session - Session whose analyses for modules match these revisions. Captures are taken
|
|
3555
|
+
* before rescanning, and later queries use the captured plans and the in-memory ASTs.
|
|
3556
|
+
* @param modules - Module IDs successfully compiled from these buffers. Failed scans have no capture.
|
|
3557
|
+
* @throws If source transport, a projection map, or an original UTF-8 boundary is invalid.
|
|
3594
3558
|
*/
|
|
3595
|
-
constructor(
|
|
3596
|
-
this
|
|
3597
|
-
this.#
|
|
3598
|
-
this.#
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3559
|
+
constructor(graph, session, modules) {
|
|
3560
|
+
this.previews = new GeneratedPreviews(graph, session, modules);
|
|
3561
|
+
this.#bindings = new SourceBindings(graph, session);
|
|
3562
|
+
for (const { source, ast } of this.#bindings.files.values()) {
|
|
3563
|
+
walk(ast, (node) => {
|
|
3564
|
+
if (!ts.isCallExpression(node)) return;
|
|
3565
|
+
const asset = this.asset(source, node);
|
|
3566
|
+
if (asset?.identity.kind === "style" || this.#bindings.styleImport(node.expression)) {
|
|
3567
|
+
if (node.arguments.length > 1) this.#containers.push({
|
|
3568
|
+
source,
|
|
3569
|
+
node
|
|
3570
|
+
});
|
|
3571
|
+
for (const argument of node.arguments) this.#input(source, argument);
|
|
3572
|
+
return;
|
|
3573
|
+
}
|
|
3574
|
+
if (asset?.identity.kind === "keyframes" || this.#bindings.producer(node.expression, "keyframes")) {
|
|
3575
|
+
this.#keyframes(source, node.arguments[0]);
|
|
3576
|
+
return;
|
|
3577
|
+
}
|
|
3578
|
+
if (asset?.identity.kind === "viewTransition" || this.#bindings.producer(node.expression, "viewTransition")) {
|
|
3579
|
+
const body = node.arguments[0];
|
|
3580
|
+
if (body && ts.isObjectLiteralExpression(body)) for (const property of body.properties) {
|
|
3581
|
+
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3582
|
+
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3583
|
+
if ([
|
|
3584
|
+
"group",
|
|
3585
|
+
"old",
|
|
3586
|
+
"new",
|
|
3587
|
+
"imagePair"
|
|
3588
|
+
].includes(name) && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "viewTransitionUtilities");
|
|
3589
|
+
}
|
|
3590
|
+
return;
|
|
3591
|
+
}
|
|
3592
|
+
if (this.#bindings.producer(node.expression, "css")) {
|
|
3593
|
+
const inputs = [...node.arguments];
|
|
3594
|
+
const options = inputs.at(-1);
|
|
3595
|
+
const anchored = options !== void 0 && isCssOptions(options) && options.properties.some((property) => ts.isPropertyAssignment(property) && propertyName(property) === "selector");
|
|
3596
|
+
if (options !== void 0 && isCssOptions(options)) {
|
|
3597
|
+
inputs.pop();
|
|
3598
|
+
for (const property of options.properties) if (ts.isPropertyAssignment(property) && propertyName(property) === "template" && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "cssTemplate");
|
|
3599
|
+
}
|
|
3600
|
+
for (const input of inputs) this.#cssInput(source, input, "", anchored ? "anchoredDocument" : "document");
|
|
3601
|
+
return;
|
|
3602
|
+
}
|
|
3603
|
+
if (this.#bindings.producer(node.expression, "media")) {
|
|
3604
|
+
for (const argument of node.arguments) if (ts.isStringLiteralLike(argument)) this.#literal(source, argument, "", "mediaVariants");
|
|
3605
|
+
return;
|
|
3606
|
+
}
|
|
3607
|
+
if (this.asset(source, node.expression)?.identity.kind === "keyframes") {
|
|
3608
|
+
const options = node.arguments[0];
|
|
3609
|
+
if (options && ts.isStringLiteralLike(options)) this.#literal(source, options, "", "animationOptions");
|
|
3610
|
+
}
|
|
3611
|
+
});
|
|
3612
|
+
walk(ast, (node) => {
|
|
3613
|
+
if (!ts.isStringLiteralLike(node) && !ts.isTemplateLiteralToken(node)) return;
|
|
3614
|
+
const range = this.range(source, node);
|
|
3615
|
+
const site = range && this.previews.input(source, ...range);
|
|
3616
|
+
if (site?.native.context) this.#literal(source, node, "", site.native.context, site);
|
|
3617
|
+
});
|
|
3618
|
+
}
|
|
3619
|
+
this.literals = this.#literals;
|
|
3620
|
+
this.containers = this.#containers;
|
|
3621
|
+
}
|
|
3622
|
+
/**
|
|
3623
|
+
* Indexes literal frame utilities and quoted condition keys in an authored keyframes body.
|
|
3624
|
+
* Nested object bodies retain definition contexts. Computed properties, nonliteral frame
|
|
3625
|
+
* values, and missing bodies add no literals.
|
|
3626
|
+
*
|
|
3627
|
+
* @param source - Prepared source stored in this index.
|
|
3628
|
+
* @param node - Body in that source's checker AST, or undefined for a missing argument.
|
|
3629
|
+
* Recognized quoted conditions and frame utilities are appended to `literals`.
|
|
3630
|
+
*/
|
|
3631
|
+
#keyframes(source, node) {
|
|
3632
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return;
|
|
3633
|
+
for (const property of node.properties) {
|
|
3634
|
+
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3635
|
+
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3636
|
+
if (name.endsWith(":") || ts.isObjectLiteralExpression(property.initializer)) {
|
|
3637
|
+
if (ts.isStringLiteralLike(property.name)) this.#literal(source, property.name, "", "definitionVariants");
|
|
3638
|
+
this.#keyframes(source, property.initializer);
|
|
3639
|
+
} else if (/^(?:from|to|(?:\d+(?:\.\d+)?%)(?:\s*,\s*\d+(?:\.\d+)?%)*)$/.test(name) && ts.isStringLiteralLike(property.initializer)) this.#literal(source, property.initializer, "", "keyframesFrame");
|
|
3640
|
+
}
|
|
3603
3641
|
}
|
|
3604
3642
|
/**
|
|
3605
|
-
*
|
|
3606
|
-
*
|
|
3607
|
-
* use them. Other requests are ignored.
|
|
3643
|
+
* Classifies direct input syntax and bound identities without reading base bodies or CSS.
|
|
3644
|
+
* Arrays and objects receive a category only when all children share one non-ref category.
|
|
3608
3645
|
*
|
|
3609
|
-
* @param source -
|
|
3610
|
-
* @param
|
|
3611
|
-
*
|
|
3612
|
-
*
|
|
3613
|
-
* @throws If a plugin fails or the native loader rejects the generated CSS.
|
|
3646
|
+
* @param source - Prepared source stored in this index.
|
|
3647
|
+
* @param node - Input or property in that source's checker AST.
|
|
3648
|
+
* @returns The grouping category, or a barrier for unknown, mixed, empty, or computed input.
|
|
3649
|
+
* Ref markers and supported projection calls share the declaration's opaque identity.
|
|
3614
3650
|
*/
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
const input = this.#completionInput(source, request.offset);
|
|
3618
|
-
const partial = input && this.#catalogContext(input.literal.context) ? segments(input.literal.prefix + input.prefix, ":").pop() : "";
|
|
3619
|
-
if (partial && !this.#inputCatalogs.has(partial)) this.#inputCatalogs.set(partial, await this.#plugins.catalog(partial));
|
|
3620
|
-
}
|
|
3621
|
-
if (request.kind === "resolve" && request.snapshot === this.#revision && this.#plugins && !this.#resolved.has(request.candidate)) this.#resolved.set(request.candidate, new CandidateQueries(await this.#plugins.query(request.candidate)));
|
|
3651
|
+
inputOrder(source, node) {
|
|
3652
|
+
return classifyInput(source, node, (source, node) => this.asset(source, node));
|
|
3622
3653
|
}
|
|
3623
3654
|
/**
|
|
3624
|
-
*
|
|
3655
|
+
* Maps a checker node to an exact range in its original document.
|
|
3656
|
+
* For projections, an anchor is accepted only when the intervening and selected text match
|
|
3657
|
+
* the original buffer. A conflicting mapping at the node's start prevents fallback to an anchor.
|
|
3625
3658
|
*
|
|
3626
|
-
* @param source -
|
|
3627
|
-
* @param
|
|
3628
|
-
* @
|
|
3629
|
-
*
|
|
3630
|
-
* @throws If candidate resolution, CSS formatting, or native color queries fail.
|
|
3659
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3660
|
+
* @param node - Node from this index, with leading trivia excluded from its selected range.
|
|
3661
|
+
* @returns Inclusive start and exclusive end in original UTF-16 coordinates, or undefined
|
|
3662
|
+
* when a projection cannot map the text exactly.
|
|
3631
3663
|
*/
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
const literal = this.#index.literals.find((literal) => literal.source === source && literal.boundaries[0] === request.input);
|
|
3635
|
-
const candidates = this.#resolved.get(request.candidate) ?? this.#candidates;
|
|
3636
|
-
const current = literal && request.snapshot === this.#revision;
|
|
3637
|
-
const css = current ? request.candidate.endsWith(":") ? candidates.variant(request.candidate, literal.context) : this.#preview(literal, request.candidate.slice(literal.prefix.length)) ?? candidates.preview(request.candidate, literal.context) : void 0;
|
|
3638
|
-
const marker = current ? candidates.marker(request.candidate, literal.context) : null;
|
|
3639
|
-
if (css || marker) {
|
|
3640
|
-
const formatted = css ? this.#query.formatCss(css, this.#annotations) : "";
|
|
3641
|
-
result.hover = {
|
|
3642
|
-
start: 0,
|
|
3643
|
-
end: 0,
|
|
3644
|
-
css: formatted,
|
|
3645
|
-
detail: css ? this.#candidates.detail(formatted) : "marker class",
|
|
3646
|
-
...marker ? { documentation: markerDocumentation(marker) } : {}
|
|
3647
|
-
};
|
|
3648
|
-
}
|
|
3649
|
-
return result;
|
|
3650
|
-
}
|
|
3651
|
-
const tokens = this.#sourceTokens(source);
|
|
3652
|
-
if (request.kind === "colors") {
|
|
3653
|
-
result.colors = tokens.flatMap((token) => {
|
|
3654
|
-
if (!this.#colorInput(token.literal) || !this.#preview(token.literal, token.text)) return [];
|
|
3655
|
-
const color = this.#candidates.color(token.literal.prefix + token.text);
|
|
3656
|
-
return color ? [{
|
|
3657
|
-
start: token.start,
|
|
3658
|
-
end: token.end,
|
|
3659
|
-
red: color[0],
|
|
3660
|
-
green: color[1],
|
|
3661
|
-
blue: color[2],
|
|
3662
|
-
alpha: color[3]
|
|
3663
|
-
}] : [];
|
|
3664
|
-
});
|
|
3665
|
-
return result;
|
|
3666
|
-
}
|
|
3667
|
-
if (request.kind === "colorPresentation") {
|
|
3668
|
-
const token = tokens.find((token) => token.start === request.start && token.end === request.end);
|
|
3669
|
-
if (!token || !this.#colorInput(token.literal)) return result;
|
|
3670
|
-
const candidate = colorReplacement(token.text, token.literal.prefix, request.color, this.#candidates);
|
|
3671
|
-
if (candidate && this.#preview(token.literal, candidate)) result.edits = [{
|
|
3672
|
-
start: token.start,
|
|
3673
|
-
end: token.end,
|
|
3674
|
-
newText: escapeContent(candidate, token.literal)
|
|
3675
|
-
}];
|
|
3676
|
-
return result;
|
|
3677
|
-
}
|
|
3678
|
-
const token = tokens.find((token) => token.start <= request.offset && request.offset <= token.end);
|
|
3679
|
-
if (request.kind === "hover") {
|
|
3680
|
-
if (token) {
|
|
3681
|
-
const css = this.#preview(token.literal, token.text);
|
|
3682
|
-
const marker = this.#candidates.marker(token.literal.prefix + token.text, token.literal.context);
|
|
3683
|
-
if (css || marker) result.hover = {
|
|
3684
|
-
start: token.start,
|
|
3685
|
-
end: token.end,
|
|
3686
|
-
css: css ? this.#query.formatCss(css, this.#annotations) : "",
|
|
3687
|
-
...marker ? { documentation: markerDocumentation(marker) } : {}
|
|
3688
|
-
};
|
|
3689
|
-
} else result.hover = this.#expressionPreview(source, request.offset);
|
|
3690
|
-
return result;
|
|
3691
|
-
}
|
|
3692
|
-
const input = this.#completionInput(source, request.offset);
|
|
3693
|
-
if (!input) return result;
|
|
3694
|
-
const { literal, start, end, prefix } = input;
|
|
3695
|
-
const native = this.#candidates.complete(literal.prefix + prefix, literal.context, request.snippets === true).filter(({ label, marker }) => marker || label.endsWith(":") || !literal.site || this.#preview(literal, label.slice(literal.prefix.length)));
|
|
3696
|
-
const known = new Set(native.map(({ label }) => label));
|
|
3697
|
-
result.completions = [...native, ...this.#catalogCompletions(literal.prefix + prefix, literal.context).filter((candidate) => !known.has(candidate)).map((label) => ({
|
|
3698
|
-
label,
|
|
3699
|
-
marker: false
|
|
3700
|
-
}))].map(({ label: candidate, marker }) => {
|
|
3701
|
-
const label = candidate.slice(literal.prefix.length);
|
|
3702
|
-
const source = (text) => escapeContent(text, literal);
|
|
3703
|
-
return {
|
|
3704
|
-
label,
|
|
3705
|
-
candidate,
|
|
3706
|
-
input: literal.boundaries[0],
|
|
3707
|
-
snapshot: this.#revision,
|
|
3708
|
-
start,
|
|
3709
|
-
end,
|
|
3710
|
-
newText: escapeContent(label, literal),
|
|
3711
|
-
...marker ? { marker } : {},
|
|
3712
|
-
snippet: !request.snippets ? void 0 : marker && candidate.endsWith("/") ? markerModifierSnippet(label, source) : candidate.endsWith(":") ? completionSnippet(label, source) : void 0,
|
|
3713
|
-
retrigger: candidate.endsWith(":") || candidate.endsWith("-")
|
|
3714
|
-
};
|
|
3715
|
-
});
|
|
3716
|
-
return result;
|
|
3664
|
+
range(source, node) {
|
|
3665
|
+
return this.#bindings.range(source, node);
|
|
3717
3666
|
}
|
|
3718
3667
|
/**
|
|
3719
|
-
*
|
|
3720
|
-
*
|
|
3721
|
-
* missing or invalid output ends the search, preventing an enclosing partial preview.
|
|
3722
|
-
* Without a captured occurrence, the nearest expression with a resolvable declaration supplies
|
|
3723
|
-
* the fallback preview and range.
|
|
3668
|
+
* Resolves a native producer or an alias through stable variable initializers and imports.
|
|
3669
|
+
* Both successful resolutions and misses are cached for the AST node.
|
|
3724
3670
|
*
|
|
3725
|
-
* @param source - Prepared source
|
|
3726
|
-
* @param
|
|
3727
|
-
* @returns
|
|
3728
|
-
*
|
|
3671
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3672
|
+
* @param node - Node from this index. Alias chains may reach another source in the same graph.
|
|
3673
|
+
* @returns A shared native record that callers must keep unchanged, or undefined when no
|
|
3674
|
+
* producer is found. Mutated bindings and cyclic aliases stop alias resolution.
|
|
3729
3675
|
*/
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
let reference;
|
|
3733
|
-
while (node) {
|
|
3734
|
-
const site = this.#index.previewSite(source, node);
|
|
3735
|
-
if (site) {
|
|
3736
|
-
const preview = this.#index.previews.preview(site.native);
|
|
3737
|
-
const range = this.#index.range(source, node);
|
|
3738
|
-
return preview && range ? {
|
|
3739
|
-
start: range[0],
|
|
3740
|
-
end: range[1],
|
|
3741
|
-
css: this.#query.formatCss(preview.css, this.#annotations),
|
|
3742
|
-
...preview.runtimeNames.length ? { runtimeNames: preview.runtimeNames } : {}
|
|
3743
|
-
} : void 0;
|
|
3744
|
-
}
|
|
3745
|
-
if (!reference) {
|
|
3746
|
-
const asset = this.#index.asset(source, node);
|
|
3747
|
-
const preview = asset && this.#index.previews.previewIdentity(asset.identity);
|
|
3748
|
-
const range = this.#index.range(source, node);
|
|
3749
|
-
if (preview && range) reference = {
|
|
3750
|
-
preview,
|
|
3751
|
-
range
|
|
3752
|
-
};
|
|
3753
|
-
}
|
|
3754
|
-
if (ts.isCallExpression(node)) break;
|
|
3755
|
-
node = node.parent;
|
|
3756
|
-
}
|
|
3757
|
-
return reference ? {
|
|
3758
|
-
start: reference.range[0],
|
|
3759
|
-
end: reference.range[1],
|
|
3760
|
-
css: this.#query.formatCss(reference.preview.css, this.#annotations),
|
|
3761
|
-
...reference.preview.runtimeNames.length ? { runtimeNames: reference.preview.runtimeNames } : {}
|
|
3762
|
-
} : void 0;
|
|
3676
|
+
asset(source, node) {
|
|
3677
|
+
return this.#bindings.asset(source, node);
|
|
3763
3678
|
}
|
|
3764
3679
|
/**
|
|
3765
|
-
*
|
|
3766
|
-
* A captured plan already retains those variants; a standalone query receives the prefix
|
|
3767
|
-
* explicitly. The original source remains unchanged.
|
|
3680
|
+
* Returns the comments that contain `swlint-*` directives in `source`'s original buffer.
|
|
3768
3681
|
*
|
|
3769
|
-
* @param
|
|
3770
|
-
* @
|
|
3771
|
-
*
|
|
3772
|
-
* @throws If a matching ordinary utility requirement is malformed in a standalone query.
|
|
3682
|
+
* @param source - Prepared source of this index.
|
|
3683
|
+
* @returns Comment bodies in original UTF-8 byte offsets, or an empty list when the scan
|
|
3684
|
+
* failed. Callers must keep the returned list unchanged.
|
|
3773
3685
|
*/
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
return this.#candidates.preview(literal.prefix + candidate, literal.context) ?? void 0;
|
|
3686
|
+
lintComments(source) {
|
|
3687
|
+
return this.#bindings.lintComments(source);
|
|
3777
3688
|
}
|
|
3778
3689
|
/**
|
|
3779
|
-
*
|
|
3690
|
+
* Finds a captured expression contribution at `node` in `source`'s checker AST.
|
|
3691
|
+
* Exact non-input ranges take precedence; calls then try their receiver's use site.
|
|
3692
|
+
* Invalid sites remain identifiable.
|
|
3780
3693
|
*
|
|
3781
|
-
* @param
|
|
3782
|
-
* @param
|
|
3783
|
-
* @returns
|
|
3784
|
-
*
|
|
3694
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3695
|
+
* @param node - Expression or call in this index.
|
|
3696
|
+
* @returns A shared captured occurrence, or undefined for unmapped or unrecognized expressions.
|
|
3697
|
+
* Callers must keep the returned site unchanged.
|
|
3785
3698
|
*/
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
const
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
|
|
3699
|
+
previewSite(source, node) {
|
|
3700
|
+
const range = this.range(source, node);
|
|
3701
|
+
if (!range) return;
|
|
3702
|
+
const sites = this.previews.sites(source);
|
|
3703
|
+
const exact = sites.find((site) => site.native.kind !== "input" && site.start === range[0] && site.end === range[1]);
|
|
3704
|
+
if (exact || !ts.isCallExpression(node)) return exact;
|
|
3705
|
+
const receiver = ts.isPropertyAccessExpression(node.expression) ? node.expression.expression : node.expression;
|
|
3706
|
+
const receiverRange = this.range(source, receiver);
|
|
3707
|
+
return receiverRange ? sites.find((site) => site.native.kind === "use" && site.start === receiverRange[0] && site.end === receiverRange[1]) : void 0;
|
|
3793
3708
|
}
|
|
3794
3709
|
/**
|
|
3795
|
-
*
|
|
3710
|
+
* Finds the smallest mapped AST node that contains an original document position.
|
|
3796
3711
|
*
|
|
3797
|
-
* @param
|
|
3798
|
-
* @
|
|
3712
|
+
* @param source - Prepared source whose checker filename selects a file in this index.
|
|
3713
|
+
* @param offset - Original UTF-16 cursor offset. Containment uses half-open ranges.
|
|
3714
|
+
* @returns The containing node, or undefined when the file is absent or no mapped node
|
|
3715
|
+
* contains the position. A position at a node's exclusive end does not select that node.
|
|
3799
3716
|
*/
|
|
3800
|
-
|
|
3801
|
-
return
|
|
3802
|
-
"utility",
|
|
3803
|
-
"document",
|
|
3804
|
-
"anchoredDocument"
|
|
3805
|
-
].includes(context);
|
|
3717
|
+
nodeAt(source, offset) {
|
|
3718
|
+
return this.#bindings.nodeAt(source, offset);
|
|
3806
3719
|
}
|
|
3807
3720
|
/**
|
|
3808
|
-
*
|
|
3721
|
+
* Indexes the static utility literals of one `style.css()` input with their group keys.
|
|
3722
|
+
* Runtime values and computed keys add no literals.
|
|
3809
3723
|
*
|
|
3810
|
-
* @param source -
|
|
3811
|
-
* @
|
|
3724
|
+
* @param source - Prepared source stored in this index.
|
|
3725
|
+
* @param node - Input in that source's checker AST.
|
|
3726
|
+
* @param prefix - Variants of the enclosing groups, each ending in `:`.
|
|
3727
|
+
* @param context - Input role of the call: `anchoredDocument` when it has a `selector`
|
|
3728
|
+
* option, and `document` otherwise.
|
|
3812
3729
|
*/
|
|
3813
|
-
#
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
}
|
|
3730
|
+
#cssInput(source, node, prefix, context) {
|
|
3731
|
+
if (ts.isStringLiteralLike(node)) {
|
|
3732
|
+
this.#literal(source, node, prefix, context);
|
|
3733
|
+
return;
|
|
3734
|
+
}
|
|
3735
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
3736
|
+
for (const element of node.elements) this.#cssInput(source, element, prefix, context);
|
|
3737
|
+
return;
|
|
3738
|
+
}
|
|
3739
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
3740
|
+
this.#cssInput(source, node.expression, prefix, context);
|
|
3741
|
+
return;
|
|
3742
|
+
}
|
|
3743
|
+
if (ts.isObjectLiteralExpression(node)) for (const property of node.properties) {
|
|
3744
|
+
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3745
|
+
const name = propertyName(property);
|
|
3746
|
+
if (name === "" || name.endsWith(":")) this.#cssInput(source, property.initializer, name === ":" ? prefix : prefix + name, context);
|
|
3747
|
+
}
|
|
3818
3748
|
}
|
|
3819
3749
|
/**
|
|
3820
|
-
*
|
|
3821
|
-
*
|
|
3750
|
+
* Records literals and sortable containers in recognized direct style input syntax.
|
|
3751
|
+
* Arrays and both conditional branches are traversed. Logical `&&`, `||`, and `??`
|
|
3752
|
+
* expressions contribute only their right operand, and unrecognized syntax adds nothing.
|
|
3822
3753
|
*
|
|
3823
|
-
* @param source -
|
|
3824
|
-
* @param
|
|
3825
|
-
*
|
|
3826
|
-
*
|
|
3754
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3755
|
+
* @param node - Input expression to inspect. Object properties ending in `:` contribute
|
|
3756
|
+
* their values under the accumulated group variants. The keys `""` and `":"` add no variants.
|
|
3757
|
+
* @param prefix - Decoded enclosing variants, including trailing colons. Nested group
|
|
3758
|
+
* names are appended before their literals are recorded.
|
|
3827
3759
|
*/
|
|
3828
|
-
#
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3835
|
-
|
|
3836
|
-
|
|
3837
|
-
|
|
3838
|
-
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3760
|
+
#input(source, node, prefix = "") {
|
|
3761
|
+
if (ts.isStringLiteralLike(node)) {
|
|
3762
|
+
this.#literal(source, node, prefix);
|
|
3763
|
+
return;
|
|
3764
|
+
}
|
|
3765
|
+
if (ts.isTemplateExpression(node)) {
|
|
3766
|
+
this.#literal(source, node.head, prefix);
|
|
3767
|
+
for (const span of node.templateSpans) this.#literal(source, span.literal, prefix);
|
|
3768
|
+
return;
|
|
3769
|
+
}
|
|
3770
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
3771
|
+
this.#containers.push({
|
|
3772
|
+
source,
|
|
3773
|
+
node
|
|
3774
|
+
});
|
|
3775
|
+
for (const element of node.elements) this.#input(source, element, prefix);
|
|
3776
|
+
return;
|
|
3777
|
+
}
|
|
3778
|
+
if (ts.isConditionalExpression(node)) {
|
|
3779
|
+
this.#input(source, node.whenTrue, prefix);
|
|
3780
|
+
this.#input(source, node.whenFalse, prefix);
|
|
3781
|
+
return;
|
|
3782
|
+
}
|
|
3783
|
+
if (ts.isBinaryExpression(node) && [
|
|
3784
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
3785
|
+
ts.SyntaxKind.BarBarToken,
|
|
3786
|
+
ts.SyntaxKind.QuestionQuestionToken
|
|
3787
|
+
].includes(node.operatorToken.kind)) {
|
|
3788
|
+
this.#input(source, node.right, prefix);
|
|
3789
|
+
return;
|
|
3790
|
+
}
|
|
3791
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
3792
|
+
this.#input(source, node.expression, prefix);
|
|
3793
|
+
return;
|
|
3794
|
+
}
|
|
3795
|
+
if (ts.isElementAccessExpression(node) || ts.isPropertyAccessExpression(node)) {
|
|
3796
|
+
this.#table(source, node.expression, prefix);
|
|
3797
|
+
return;
|
|
3798
|
+
}
|
|
3799
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
3800
|
+
this.#containers.push({
|
|
3801
|
+
source,
|
|
3802
|
+
node
|
|
3803
|
+
});
|
|
3804
|
+
for (const property of node.properties) {
|
|
3805
|
+
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name)) continue;
|
|
3806
|
+
const name = ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3807
|
+
if (name !== "" && !name.endsWith(":")) continue;
|
|
3808
|
+
this.#input(source, property.initializer, name === ":" ? prefix : prefix + name);
|
|
3809
|
+
}
|
|
3810
|
+
}
|
|
3842
3811
|
}
|
|
3843
3812
|
/**
|
|
3844
|
-
*
|
|
3845
|
-
*
|
|
3813
|
+
* Records the literal values of an array or object literal that a style input indexes.
|
|
3814
|
+
* Literal spreads are traversed, and keys are not utilities. A table is not a sortable
|
|
3815
|
+
* container, because reordering its entries would change which entry an index selects.
|
|
3846
3816
|
*
|
|
3847
|
-
* @param
|
|
3848
|
-
* @
|
|
3817
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3818
|
+
* @param node - Receiver of the lookup. Other receivers add nothing.
|
|
3819
|
+
* @param prefix - Decoded enclosing group variants, including trailing colons.
|
|
3849
3820
|
*/
|
|
3850
|
-
#
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3821
|
+
#table(source, node, prefix) {
|
|
3822
|
+
while (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) node = node.expression;
|
|
3823
|
+
const values = ts.isArrayLiteralExpression(node) ? [...node.elements] : [];
|
|
3824
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
3825
|
+
for (const property of node.properties) if (ts.isPropertyAssignment(property)) values.push(property.initializer);
|
|
3826
|
+
else if (ts.isSpreadAssignment(property)) this.#table(source, property.expression, prefix);
|
|
3827
|
+
}
|
|
3828
|
+
for (const value of values) if (ts.isSpreadElement(value)) this.#table(source, value.expression, prefix);
|
|
3829
|
+
else if (ts.isStringLiteralLike(value)) this.#literal(source, value, prefix);
|
|
3856
3830
|
}
|
|
3857
3831
|
/**
|
|
3858
|
-
*
|
|
3832
|
+
* Records decoded content and original UTF-16 boundaries for a literal in `source`'s AST.
|
|
3833
|
+
* A captured `site` overrides an existing entry's context and plan at the same content start.
|
|
3834
|
+
* Unmapped ranges or decoding that disagrees with the AST add nothing.
|
|
3859
3835
|
*
|
|
3860
|
-
* @param
|
|
3861
|
-
*
|
|
3862
|
-
* @
|
|
3836
|
+
* @param source - Prepared source whose checker AST contains `node`.
|
|
3837
|
+
* @param node - String or template token whose raw spelling supplies the source boundaries.
|
|
3838
|
+
* @param prefix - Decoded enclosing group variants, including trailing colons.
|
|
3839
|
+
* @param context - Input role stored with a new entry. Defaults to ordinary utilities.
|
|
3840
|
+
* @param site - Optional captured input. An existing entry keeps its prefix and boundaries
|
|
3841
|
+
* when this site replaces its context and plan.
|
|
3863
3842
|
*/
|
|
3864
|
-
#
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3843
|
+
#literal(source, node, prefix, context = "utility", site) {
|
|
3844
|
+
const range = this.range(source, node);
|
|
3845
|
+
if (!range) return;
|
|
3846
|
+
const raw = source.document.code.slice(...range);
|
|
3847
|
+
const suffix = ts.isTemplateHead(node) || ts.isTemplateMiddle(node) ? 2 : node.isUnterminated ? 0 : 1;
|
|
3848
|
+
const decoded = decodeLiteral(raw.slice(1, suffix ? -suffix : void 0), range[0] + 1);
|
|
3849
|
+
if (decoded?.text === node.text) {
|
|
3850
|
+
const previous = this.#literals.find((literal) => literal.source === source && literal.boundaries[0] === decoded.boundaries[0]);
|
|
3851
|
+
if (previous) {
|
|
3852
|
+
if (site) {
|
|
3853
|
+
previous.context = context;
|
|
3854
|
+
previous.site = site;
|
|
3855
|
+
}
|
|
3856
|
+
return;
|
|
3857
|
+
}
|
|
3858
|
+
this.#literals.push({
|
|
3859
|
+
source,
|
|
3860
|
+
node,
|
|
3861
|
+
prefix,
|
|
3862
|
+
context,
|
|
3863
|
+
site,
|
|
3864
|
+
...decoded
|
|
3865
|
+
});
|
|
3866
|
+
}
|
|
3870
3867
|
}
|
|
3871
3868
|
};
|
|
3872
|
-
// src/editor/snapshot.ts
|
|
3873
3869
|
/**
|
|
3874
|
-
*
|
|
3870
|
+
* Returns the written name of a property with a literal or identifier key.
|
|
3871
|
+
*
|
|
3872
|
+
* @param property - Property assignment whose key is not computed.
|
|
3873
|
+
* @returns The decoded key text.
|
|
3874
|
+
*/
|
|
3875
|
+
function propertyName(property) {
|
|
3876
|
+
return ts.isStringLiteralLike(property.name) ? property.name.text : property.name.getText();
|
|
3877
|
+
}
|
|
3878
|
+
/**
|
|
3879
|
+
* Recognizes the options object of `style.css()`: an object literal whose properties
|
|
3880
|
+
* all have the name `template` or `selector`.
|
|
3881
|
+
*
|
|
3882
|
+
* @param node - Last argument of a `style.css()` call.
|
|
3883
|
+
* @returns Whether the compiler reads the argument as options rather than an input.
|
|
3884
|
+
*/
|
|
3885
|
+
function isCssOptions(node) {
|
|
3886
|
+
return ts.isObjectLiteralExpression(node) && node.properties.length > 0 && node.properties.every((property) => ts.isPropertyAssignment(property) && !ts.isComputedPropertyName(property.name) && ["template", "selector"].includes(propertyName(property)));
|
|
3887
|
+
}
|
|
3888
|
+
// src/editor/colors.ts
|
|
3889
|
+
/**
|
|
3890
|
+
* Finds a spelling with an arbitrary color that preserves the candidate's variants, importance,
|
|
3891
|
+
* and modifier.
|
|
3892
|
+
* Existing opacity modifiers remain present, so their maximum opacity can prevent a replacement.
|
|
3893
|
+
* The emitted color must agree with every requested component within 2/255.
|
|
3894
|
+
*
|
|
3895
|
+
* @param text - Decoded candidate without enclosing group variants. Its current color must resolve.
|
|
3896
|
+
* @param outer - Enclosing decoded variants, including trailing colons, used when probing colors.
|
|
3897
|
+
* @param color - Finite sRGB components and effective alpha. Values are clamped to 0..1.
|
|
3898
|
+
* @param candidates - Query for the snapshot's design system, used to validate each proposed spelling.
|
|
3899
|
+
* @returns Decoded replacement text without `outer`, or undefined when the original color cannot
|
|
3900
|
+
* resolve or no supported spelling with an arbitrary color reproduces the requested components.
|
|
3901
|
+
* @throws If a matching utility definition has a malformed requirement.
|
|
3902
|
+
*/
|
|
3903
|
+
function colorReplacement(text, outer, color, candidates) {
|
|
3904
|
+
if (![
|
|
3905
|
+
color.red,
|
|
3906
|
+
color.green,
|
|
3907
|
+
color.blue,
|
|
3908
|
+
color.alpha
|
|
3909
|
+
].every(Number.isFinite) || !candidates.color(outer + text)) return;
|
|
3910
|
+
const parts = segments(text, ":");
|
|
3911
|
+
const utility = parts.pop();
|
|
3912
|
+
const prefix = parts.length ? `${parts.join(":")}:` : "";
|
|
3913
|
+
const importance = utility.startsWith("!") || utility.endsWith("!") ? "!" : "";
|
|
3914
|
+
const [base, modifier] = segments(utility.replace(/^!|!$/g, ""), "/");
|
|
3915
|
+
const spellings = [];
|
|
3916
|
+
if (base.startsWith("[") && base.endsWith("]")) {
|
|
3917
|
+
const property = base.slice(1).split(":", 1)[0];
|
|
3918
|
+
spellings.push((value) => `[${property}:${value}]`);
|
|
3919
|
+
} else for (let index = base.length - 1; index > 0; index--) if (base[index] === "-") {
|
|
3920
|
+
const root = base.slice(0, index);
|
|
3921
|
+
spellings.push((value) => `${root}-[${value}]`);
|
|
3922
|
+
}
|
|
3923
|
+
const rgba = [
|
|
3924
|
+
color.red,
|
|
3925
|
+
color.green,
|
|
3926
|
+
color.blue,
|
|
3927
|
+
color.alpha
|
|
3928
|
+
].map((value) => Math.min(1, Math.max(0, value)));
|
|
3929
|
+
const rgb = rgba.slice(0, 3).map((value) => Math.round(value * 255));
|
|
3930
|
+
for (const spelling of spellings) {
|
|
3931
|
+
/**
|
|
3932
|
+
* Rebuilds a color candidate with its variants, importance, and opacity modifier preserved.
|
|
3933
|
+
*
|
|
3934
|
+
* @param value - CSS color encoded for an arbitrary utility value.
|
|
3935
|
+
* @returns A candidate using the current spelling, without the separately supplied outer variants.
|
|
3936
|
+
*/
|
|
3937
|
+
const build = (value) => `${prefix}${importance}${spelling(value)}${modifier ? `/${modifier}` : ""}`;
|
|
3938
|
+
const probe = candidates.color(outer + build("#ffffff"));
|
|
3939
|
+
if (!probe || rgba[3] > probe[3] + 1 / 255) continue;
|
|
3940
|
+
const alpha = probe[3] ? rgba[3] / probe[3] : 1;
|
|
3941
|
+
const candidate = build(alpha >= 1 ? `#${rgb.map((value) => value.toString(16).padStart(2, "0")).join("")}` : `rgb(${rgb.join("_")}_/_${Number(alpha.toFixed(4))})`);
|
|
3942
|
+
if (candidates.color(outer + candidate)?.every((component, index) => Math.abs(component - rgba[index]) <= 2 / 255)) return candidate;
|
|
3943
|
+
}
|
|
3944
|
+
}
|
|
3945
|
+
/**
|
|
3946
|
+
* Serializes a picker color as a CSS literal for one local reference replacement.
|
|
3947
|
+
*
|
|
3948
|
+
* @param color - Finite sRGB components and effective alpha, clamped to 0..1.
|
|
3949
|
+
* @returns A hex or rgb() literal, or undefined for nonfinite components.
|
|
3950
|
+
*/
|
|
3951
|
+
function cssColor(color) {
|
|
3952
|
+
const components = [
|
|
3953
|
+
color.red,
|
|
3954
|
+
color.green,
|
|
3955
|
+
color.blue,
|
|
3956
|
+
color.alpha
|
|
3957
|
+
];
|
|
3958
|
+
if (!components.every(Number.isFinite)) return;
|
|
3959
|
+
const rgba = components.map((value) => Math.min(1, Math.max(0, value)));
|
|
3960
|
+
const rgb = rgba.slice(0, 3).map((value) => Math.round(value * 255));
|
|
3961
|
+
return rgba[3] === 1 ? `#${rgb.map((value) => value.toString(16).padStart(2, "0")).join("")}` : `rgb(${rgb.join(" ")} / ${Number(rgba[3].toFixed(4))})`;
|
|
3962
|
+
}
|
|
3963
|
+
// src/editor/completion.ts
|
|
3964
|
+
/**
|
|
3965
|
+
* Converts one empty arbitrary-value placeholder into an LSP snippet.
|
|
3966
|
+
* @param candidate - Decoded replacement text, including enclosing variants.
|
|
3967
|
+
* @param source - Encodes decoded text for its source string. CSS uses the unchanged text.
|
|
3968
|
+
* @returns A snippet with its first tab stop inside `[]`, or undefined without a placeholder.
|
|
3875
3969
|
*/
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3970
|
+
function completionSnippet(candidate, source = (text) => text) {
|
|
3971
|
+
const text = candidate.endsWith(":") ? candidate.slice(0, -1) : candidate;
|
|
3972
|
+
const variant = segments(text, ":").at(-1);
|
|
3973
|
+
const placeholder = variant.lastIndexOf("[]");
|
|
3974
|
+
if (placeholder < 0) return;
|
|
3975
|
+
const index = text.length - variant.length + placeholder;
|
|
3976
|
+
return `${escape(source(candidate.slice(0, index + 1)))}\${1}${escape(source(candidate.slice(index + 1)))}\${0}`;
|
|
3977
|
+
}
|
|
3978
|
+
/**
|
|
3979
|
+
* Converts the start of a modifiable marker's modifier into an LSP snippet.
|
|
3980
|
+
* @param label - Decoded marker name followed by `/`.
|
|
3981
|
+
* @param source - Encodes decoded text for its source string.
|
|
3982
|
+
* @returns A snippet that inserts `label` and selects a `name` placeholder after it.
|
|
3983
|
+
*/
|
|
3984
|
+
function markerModifierSnippet(label, source = (text) => text) {
|
|
3985
|
+
return `${escape(source(label))}\${1:name}`;
|
|
3986
|
+
}
|
|
3987
|
+
function escape(part) {
|
|
3988
|
+
return part.replace(/[\\$}]/g, "\\$&");
|
|
3989
|
+
}
|
|
3990
|
+
// src/editor/source/query.ts
|
|
3991
|
+
/** Most catalog candidates that one completion request adds. */
|
|
3992
|
+
const CATALOG_COMPLETIONS = 200;
|
|
3993
|
+
/**
|
|
3994
|
+
* Answers source completion, hover, and color queries using a captured editor snapshot.
|
|
3995
|
+
*/
|
|
3996
|
+
var SourceQueries = class {
|
|
3881
3997
|
/** Checker ASTs, stable aliases, and captured emission plans for the graph's sources. */
|
|
3882
3998
|
#index;
|
|
3883
3999
|
/** Native design system and effective lint policy captured during construction. */
|
|
3884
4000
|
#query;
|
|
3885
4001
|
/** Candidate caches shared by all queries against this snapshot. */
|
|
3886
4002
|
#candidates;
|
|
3887
|
-
/**
|
|
3888
|
-
#
|
|
3889
|
-
/**
|
|
3890
|
-
#
|
|
3891
|
-
/**
|
|
3892
|
-
#
|
|
3893
|
-
/**
|
|
3894
|
-
#
|
|
4003
|
+
/** Dynamic-family candidates that plugins list for completion, sorted. */
|
|
4004
|
+
#catalog;
|
|
4005
|
+
/** Plugin suggestions for editor inputs that {@link SourceQueries.prepare} loaded, keyed by input. */
|
|
4006
|
+
#inputCatalogs = /* @__PURE__ */ new Map();
|
|
4007
|
+
/** Fingerprint of captures, graph dependencies, and configuration used for lazy resolution. */
|
|
4008
|
+
#revision;
|
|
4009
|
+
/** Comments added to formatted CSS previews. Omitted fields use native defaults. */
|
|
4010
|
+
#annotations;
|
|
4011
|
+
/** Queries that include callback CSS for selected completions in this snapshot. */
|
|
4012
|
+
#resolved = /* @__PURE__ */ new Map();
|
|
4013
|
+
/** Resolves completion CSS separately from the captured compiler session. */
|
|
4014
|
+
#plugins;
|
|
3895
4015
|
/**
|
|
3896
|
-
*
|
|
3897
|
-
* Copies source metadata, dependencies, fix actions, and sorting settings before indexing them.
|
|
3898
|
-
* Building the source index also scans graph sources in the session.
|
|
4016
|
+
* Shares the snapshot's source index and candidate caches for completion, hover, and color queries.
|
|
3899
4017
|
*
|
|
3900
|
-
* @param
|
|
3901
|
-
* @param
|
|
3902
|
-
* @param
|
|
3903
|
-
* @param
|
|
3904
|
-
*
|
|
3905
|
-
* @param catalog -
|
|
3906
|
-
* @param plugins -
|
|
3907
|
-
* @throws If source transport, a projection map, or an original UTF-8 boundary is invalid.
|
|
4018
|
+
* @param index - Fixed source graph and captured preview plans.
|
|
4019
|
+
* @param query - Native query for the same CSS definitions.
|
|
4020
|
+
* @param candidates - Candidate caches also used by sorting.
|
|
4021
|
+
* @param revision - Snapshot fingerprint copied into lazy completion requests.
|
|
4022
|
+
* @param annotations - Comments to add to CSS previews.
|
|
4023
|
+
* @param catalog - Plugin candidates available without callback execution.
|
|
4024
|
+
* @param plugins - Optional plugin runtime used for lazy suggestions and selected candidates.
|
|
3908
4025
|
*/
|
|
3909
|
-
constructor(
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
this.#
|
|
3915
|
-
this.#
|
|
3916
|
-
this.#
|
|
3917
|
-
this.#query = (pluginLoader(session) ?? loader).editorQuery();
|
|
3918
|
-
this.#candidates = new CandidateQueries(this.#query);
|
|
3919
|
-
const css = this.#query.lintSettings();
|
|
3920
|
-
this.#sort = config.sort === false ? false : {
|
|
3921
|
-
stringLiterals: config.sort?.stringLiterals ?? true,
|
|
3922
|
-
arrays: config.sort?.arrays ?? true
|
|
3923
|
-
};
|
|
3924
|
-
this.#sortSeverity = css["prefer-sorted-utilities"] ?? "off";
|
|
3925
|
-
const annotations = { ...config.preview };
|
|
3926
|
-
const revision = fingerprint(JSON.stringify([this.#index.previews.revision, config]));
|
|
3927
|
-
this.#queries = new SourceQueries(this.#index, this.#query, this.#candidates, revision, annotations, catalog, plugins);
|
|
4026
|
+
constructor(index, query, candidates, revision, annotations, catalog, plugins) {
|
|
4027
|
+
this.#index = index;
|
|
4028
|
+
this.#query = query;
|
|
4029
|
+
this.#candidates = candidates;
|
|
4030
|
+
this.#revision = revision;
|
|
4031
|
+
this.#annotations = annotations;
|
|
4032
|
+
this.#catalog = catalog;
|
|
4033
|
+
this.#plugins = plugins;
|
|
3928
4034
|
}
|
|
3929
4035
|
/**
|
|
3930
4036
|
* Loads plugin suggestions for a completion input, and callback-generated CSS for a selected
|
|
3931
|
-
* completion from this captured snapshot, so that a following {@link
|
|
4037
|
+
* completion from this captured snapshot, so that a following {@link SourceQueries.query} can
|
|
3932
4038
|
* use them. Other requests are ignored.
|
|
3933
4039
|
*
|
|
3934
|
-
* @param
|
|
4040
|
+
* @param source - Fixed source for this request, or undefined for an unknown document.
|
|
3935
4041
|
* @param request - Editor request. A `completion` request loads the plugin suggestions for the
|
|
3936
4042
|
* candidate at its cursor. A `resolve` request for this snapshot's revision loads CSS, once for
|
|
3937
4043
|
* each candidate.
|
|
3938
4044
|
* @throws If a plugin fails or the native loader rejects the generated CSS.
|
|
3939
4045
|
*/
|
|
3940
|
-
async prepare(
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
* Compilation reports already present in the graph remain separate.
|
|
3948
|
-
*
|
|
3949
|
-
* @returns Independent copies of sorting reports for the captured sources.
|
|
3950
|
-
* @throws If candidate ranking fails or native report construction rejects a source URI or range.
|
|
3951
|
-
*/
|
|
3952
|
-
reports() {
|
|
3953
|
-
for (const source of this.#sources) this.#lint(source);
|
|
3954
|
-
return structuredClone([...this.#reports.values()].flat());
|
|
4046
|
+
async prepare(source, request) {
|
|
4047
|
+
if (request.kind === "completion" && source && this.#plugins) {
|
|
4048
|
+
const input = this.#completionInput(source, request.offset);
|
|
4049
|
+
const partial = input && this.#catalogContext(input.literal.context) ? segments(input.literal.prefix + input.prefix, ":").pop() : "";
|
|
4050
|
+
if (partial && !this.#inputCatalogs.has(partial)) this.#inputCatalogs.set(partial, await this.#plugins.catalog(partial));
|
|
4051
|
+
}
|
|
4052
|
+
if (request.kind === "resolve" && request.snapshot === this.#revision && this.#plugins && !this.#resolved.has(request.candidate)) this.#resolved.set(request.candidate, new CandidateQueries(await this.#plugins.query(request.candidate)));
|
|
3955
4053
|
}
|
|
3956
4054
|
/**
|
|
3957
|
-
*
|
|
3958
|
-
* Queries may populate caches, and returned edits leave the original document unchanged.
|
|
4055
|
+
* Answers a source completion, hover, resolution, or color request.
|
|
3959
4056
|
*
|
|
3960
|
-
* @param
|
|
3961
|
-
* @param request -
|
|
3962
|
-
*
|
|
3963
|
-
* @returns
|
|
3964
|
-
*
|
|
3965
|
-
* @throws If native candidate resolution, CSS formatting, or lint suppression checks fail.
|
|
4057
|
+
* @param source - Fixed source from this query's index.
|
|
4058
|
+
* @param request - Request with offsets in the original UTF-16 document.
|
|
4059
|
+
* @param result - Empty answer for the same source. This query fills its data fields.
|
|
4060
|
+
* @returns The supplied answer. Queries may fill caches but do not edit source text.
|
|
4061
|
+
* @throws If candidate resolution, CSS formatting, or native color queries fail.
|
|
3966
4062
|
*/
|
|
3967
|
-
query(
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
}))) : [];
|
|
3985
|
-
result.edits = request.kind === "sort" || this.#sortSeverity !== "off" ? sortSource(source, this.#index, this.#candidates, this.#sort, seed, request.kind === "sort" ? request.protectedRanges?.length ? (start, end) => !request.protectedRanges.some((range) => start < range.end && range.start < end) : void 0 : (start, end) => !this.#query.lintSuppressed(source.document.code, bytes(source.document.code, start), bytes(source.document.code, end), "prefer-sorted-utilities", this.#index.lintComments(source))) : seed;
|
|
4063
|
+
query(source, request, result) {
|
|
4064
|
+
if (request.kind === "resolve") {
|
|
4065
|
+
const literal = this.#index.literals.find((literal) => literal.source === source && literal.boundaries[0] === request.input);
|
|
4066
|
+
const candidates = this.#resolved.get(request.candidate) ?? this.#candidates;
|
|
4067
|
+
const current = literal && request.snapshot === this.#revision;
|
|
4068
|
+
const css = current ? request.candidate.endsWith(":") ? candidates.variant(request.candidate, literal.context) : this.#preview(literal, request.candidate.slice(literal.prefix.length)) ?? candidates.preview(request.candidate, literal.context) : void 0;
|
|
4069
|
+
const marker = current ? candidates.marker(request.candidate, literal.context) : null;
|
|
4070
|
+
if (css || marker) {
|
|
4071
|
+
const formatted = css ? this.#query.formatCss(css, this.#annotations) : "";
|
|
4072
|
+
result.hover = {
|
|
4073
|
+
start: 0,
|
|
4074
|
+
end: 0,
|
|
4075
|
+
css: formatted,
|
|
4076
|
+
detail: css ? this.#candidates.detail(formatted) : "marker class",
|
|
4077
|
+
...marker ? { documentation: markerDocumentation(marker) } : {}
|
|
4078
|
+
};
|
|
4079
|
+
}
|
|
3986
4080
|
return result;
|
|
3987
4081
|
}
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
if (
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4082
|
+
if (request.kind === "previews") {
|
|
4083
|
+
result.previews = this.#index.previews.sites(source).filter((site) => site.native.kind !== "input").flatMap((site) => {
|
|
4084
|
+
const preview = this.#index.previews.assemble(site.native);
|
|
4085
|
+
const range = {
|
|
4086
|
+
start: site.start,
|
|
4087
|
+
end: site.end
|
|
4088
|
+
};
|
|
4089
|
+
if (!preview) return [{
|
|
4090
|
+
...range,
|
|
4091
|
+
css: "",
|
|
4092
|
+
valid: false
|
|
4093
|
+
}];
|
|
4094
|
+
return [{
|
|
4095
|
+
...range,
|
|
4096
|
+
css: preview.css ? this.#query.formatCss(preview.css, this.#annotations) : "",
|
|
4097
|
+
valid: true,
|
|
4098
|
+
...preview.runtimeNames.length ? { runtimeNames: preview.runtimeNames } : {}
|
|
4099
|
+
}];
|
|
4100
|
+
}).sort((a, b) => a.start - b.start || b.end - a.end);
|
|
4101
|
+
let end = -1;
|
|
4102
|
+
result.previews = result.previews.filter((preview) => {
|
|
4103
|
+
const outer = preview.end > end;
|
|
4104
|
+
end = Math.max(end, preview.end);
|
|
4105
|
+
return outer;
|
|
4106
|
+
});
|
|
4107
|
+
return result;
|
|
4108
|
+
}
|
|
4109
|
+
const tokens = this.#sourceTokens(source);
|
|
4110
|
+
if (request.kind === "colors") {
|
|
4111
|
+
result.colors = tokens.flatMap((token) => {
|
|
4112
|
+
if (!this.#colorInput(token.literal) || !this.#preview(token.literal, token.text)) return [];
|
|
4113
|
+
const color = this.#candidates.color(token.literal.prefix + token.text);
|
|
4114
|
+
return color ? [{
|
|
4115
|
+
start: token.start,
|
|
4116
|
+
end: token.end,
|
|
4117
|
+
red: color[0],
|
|
4118
|
+
green: color[1],
|
|
4119
|
+
blue: color[2],
|
|
4120
|
+
alpha: color[3]
|
|
4121
|
+
}] : [];
|
|
4122
|
+
});
|
|
4123
|
+
return result;
|
|
4124
|
+
}
|
|
4125
|
+
if (request.kind === "colorPresentation") {
|
|
4126
|
+
const token = tokens.find((token) => token.start === request.start && token.end === request.end);
|
|
4127
|
+
if (!token || !this.#colorInput(token.literal)) return result;
|
|
4128
|
+
const candidate = colorReplacement(token.text, token.literal.prefix, request.color, this.#candidates);
|
|
4129
|
+
if (candidate && this.#preview(token.literal, candidate)) result.edits = [{
|
|
4130
|
+
start: token.start,
|
|
4131
|
+
end: token.end,
|
|
4132
|
+
newText: escapeContent(candidate, token.literal)
|
|
4133
|
+
}];
|
|
4134
|
+
return result;
|
|
4135
|
+
}
|
|
4136
|
+
const token = tokens.find((token) => token.start <= request.offset && request.offset <= token.end);
|
|
4137
|
+
if (request.kind === "hover") {
|
|
4138
|
+
if (token) {
|
|
4139
|
+
const css = this.#preview(token.literal, token.text);
|
|
4140
|
+
const marker = this.#candidates.marker(token.literal.prefix + token.text, token.literal.context);
|
|
4141
|
+
if (css || marker) result.hover = {
|
|
4142
|
+
start: token.start,
|
|
4143
|
+
end: token.end,
|
|
4144
|
+
css: css ? this.#query.formatCss(css, this.#annotations) : "",
|
|
4145
|
+
...marker ? { documentation: markerDocumentation(marker) } : {}
|
|
4146
|
+
};
|
|
4147
|
+
} else result.hover = this.#expressionPreview(source, request.offset);
|
|
4148
|
+
return result;
|
|
4149
|
+
}
|
|
4150
|
+
const input = this.#completionInput(source, request.offset);
|
|
4151
|
+
if (!input) return result;
|
|
4152
|
+
const { literal, start, end, prefix } = input;
|
|
4153
|
+
const native = this.#candidates.complete(literal.prefix + prefix, literal.context, request.snippets === true).filter(({ label, marker }) => marker || label.endsWith(":") || !literal.site || this.#preview(literal, label.slice(literal.prefix.length)));
|
|
4154
|
+
const known = new Set(native.map(({ label }) => label));
|
|
4155
|
+
result.completions = [...native, ...this.#catalogCompletions(literal.prefix + prefix, literal.context).filter((candidate) => !known.has(candidate)).map((label) => ({
|
|
4156
|
+
label,
|
|
4157
|
+
marker: false
|
|
4158
|
+
}))].map(({ label: candidate, marker }) => {
|
|
4159
|
+
const label = candidate.slice(literal.prefix.length);
|
|
4160
|
+
const source = (text) => escapeContent(text, literal);
|
|
4161
|
+
return {
|
|
4162
|
+
label,
|
|
4163
|
+
candidate,
|
|
4164
|
+
input: literal.boundaries[0],
|
|
4165
|
+
snapshot: this.#revision,
|
|
4166
|
+
start,
|
|
4167
|
+
end,
|
|
4168
|
+
newText: escapeContent(label, literal),
|
|
4169
|
+
...marker ? { marker } : {},
|
|
4170
|
+
snippet: !request.snippets ? void 0 : marker && candidate.endsWith("/") ? markerModifierSnippet(label, source) : candidate.endsWith(":") ? completionSnippet(label, source) : void 0,
|
|
4171
|
+
retrigger: candidate.endsWith(":") || candidate.endsWith("-")
|
|
4172
|
+
};
|
|
4173
|
+
});
|
|
4174
|
+
return result;
|
|
4065
4175
|
}
|
|
4066
4176
|
/**
|
|
4067
|
-
* Finds
|
|
4177
|
+
* Finds formatted CSS at an original UTF-16 `offset`, walking ancestors through the first call.
|
|
4178
|
+
* A captured occurrence takes precedence over a declaration reached through an alias. Its
|
|
4179
|
+
* missing or invalid output ends the search, preventing an enclosing partial preview.
|
|
4180
|
+
* Without a captured occurrence, the nearest expression with a resolvable declaration supplies
|
|
4181
|
+
* the fallback preview and range.
|
|
4068
4182
|
*
|
|
4069
|
-
* @param source -
|
|
4070
|
-
* @
|
|
4183
|
+
* @param source - Prepared source stored in this snapshot's index.
|
|
4184
|
+
* @param offset - Cursor offset in the original document's UTF-16 coordinates.
|
|
4185
|
+
* @returns Formatted CSS and its original range, or undefined when no complete preview is available.
|
|
4186
|
+
* @throws If the native formatter rejects the assembled CSS.
|
|
4071
4187
|
*/
|
|
4072
|
-
|
|
4073
|
-
|
|
4188
|
+
#expressionPreview(source, offset) {
|
|
4189
|
+
let node = this.#index.nodeAt(source, offset);
|
|
4190
|
+
let reference;
|
|
4191
|
+
while (node) {
|
|
4192
|
+
const site = this.#index.previewSite(source, node);
|
|
4193
|
+
if (site) {
|
|
4194
|
+
const preview = this.#index.previews.preview(site.native);
|
|
4195
|
+
const range = this.#index.range(source, node);
|
|
4196
|
+
return preview && range ? {
|
|
4197
|
+
start: range[0],
|
|
4198
|
+
end: range[1],
|
|
4199
|
+
css: this.#query.formatCss(preview.css, this.#annotations),
|
|
4200
|
+
...preview.runtimeNames.length ? { runtimeNames: preview.runtimeNames } : {}
|
|
4201
|
+
} : void 0;
|
|
4202
|
+
}
|
|
4203
|
+
if (!reference) {
|
|
4204
|
+
const asset = this.#index.asset(source, node);
|
|
4205
|
+
const preview = asset && this.#index.previews.previewIdentity(asset.identity);
|
|
4206
|
+
const range = this.#index.range(source, node);
|
|
4207
|
+
if (preview && range) reference = {
|
|
4208
|
+
preview,
|
|
4209
|
+
range
|
|
4210
|
+
};
|
|
4211
|
+
}
|
|
4212
|
+
if (ts.isCallExpression(node)) break;
|
|
4213
|
+
node = node.parent;
|
|
4214
|
+
}
|
|
4215
|
+
return reference ? {
|
|
4216
|
+
start: reference.range[0],
|
|
4217
|
+
end: reference.range[1],
|
|
4218
|
+
css: this.#query.formatCss(reference.preview.css, this.#annotations),
|
|
4219
|
+
...reference.preview.runtimeNames.length ? { runtimeNames: reference.preview.runtimeNames } : {}
|
|
4220
|
+
} : void 0;
|
|
4074
4221
|
}
|
|
4075
4222
|
/**
|
|
4076
|
-
*
|
|
4223
|
+
* Renders candidate text in a captured input plan or the standalone input registry.
|
|
4224
|
+
* A captured plan already retains those variants; a standalone query receives the prefix
|
|
4225
|
+
* explicitly. The original source remains unchanged.
|
|
4077
4226
|
*
|
|
4078
|
-
* @param
|
|
4079
|
-
* @param
|
|
4080
|
-
* @
|
|
4081
|
-
*
|
|
4227
|
+
* @param literal - Indexed literal that supplies the input role, enclosing variants, and plan.
|
|
4228
|
+
* @param candidate - Decoded text without the literal's enclosing group prefix.
|
|
4229
|
+
* @returns CSS for the replacement, or undefined for failed or unavailable emission.
|
|
4230
|
+
* @throws If a matching ordinary utility requirement is malformed in a standalone query.
|
|
4082
4231
|
*/
|
|
4083
|
-
|
|
4084
|
-
if (
|
|
4085
|
-
|
|
4086
|
-
this.#sources.set(sourceKey(source.resolverId), {
|
|
4087
|
-
source,
|
|
4088
|
-
summary,
|
|
4089
|
-
linked: false,
|
|
4090
|
-
resolutions: []
|
|
4091
|
-
});
|
|
4092
|
-
this.#changed();
|
|
4232
|
+
#preview(literal, candidate) {
|
|
4233
|
+
if (literal.site) return this.#index.previews.previewInput(literal.source, literal.site.native.start, candidate);
|
|
4234
|
+
return this.#candidates.preview(literal.prefix + candidate, literal.context) ?? void 0;
|
|
4093
4235
|
}
|
|
4094
4236
|
/**
|
|
4095
|
-
*
|
|
4096
|
-
* Analysis retries failed loads and scans so it can report each target's own error.
|
|
4237
|
+
* Completes dynamic-family candidates that plugins list but compilation has not defined yet.
|
|
4097
4238
|
*
|
|
4098
|
-
* @param
|
|
4099
|
-
* @param
|
|
4100
|
-
* @
|
|
4101
|
-
*
|
|
4102
|
-
* @throws If host import resolution fails. Source loading and scan failures are left for analysis.
|
|
4239
|
+
* @param text - Decoded partial candidate, including enclosing and written variants.
|
|
4240
|
+
* @param context - Input role of the literal. Only utility roles complete catalog names.
|
|
4241
|
+
* @returns Candidates with the written variants, for names that start with the partial
|
|
4242
|
+
* utility, at most {@link CATALOG_COMPLETIONS} of them in catalog order.
|
|
4103
4243
|
*/
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
const resolutions = await Promise.all(summary.moduleSpecifiers.map(async (specifier) => {
|
|
4112
|
-
const resolved = await host.resolve(specifier.source, source.resolverId, { skipSelf: true });
|
|
4113
|
-
if (!resolved || resolved.external) return;
|
|
4114
|
-
await this.#load(resolved.id, host, session, revision).catch(() => {});
|
|
4115
|
-
return {
|
|
4116
|
-
id: this.source(resolved.id)?.resolverId ?? resolved.id,
|
|
4117
|
-
start: specifier.start,
|
|
4118
|
-
end: specifier.end
|
|
4119
|
-
};
|
|
4120
|
-
}));
|
|
4121
|
-
if (revision !== this.#revision) return;
|
|
4122
|
-
record.linked = true;
|
|
4123
|
-
this.#setResolutions(record, resolutions.filter((value) => value !== void 0));
|
|
4124
|
-
}
|
|
4244
|
+
#catalogCompletions(text, context) {
|
|
4245
|
+
if (!this.#catalogContext(context)) return [];
|
|
4246
|
+
const parts = segments(text, ":");
|
|
4247
|
+
const partial = parts.pop();
|
|
4248
|
+
if (!partial) return [];
|
|
4249
|
+
const variants = parts.map((part) => `${part}:`).join("");
|
|
4250
|
+
return [.../* @__PURE__ */ new Set([...this.#catalog, ...this.#inputCatalogs.get(partial) ?? []])].filter((name) => name.startsWith(partial)).slice(0, CATALOG_COMPLETIONS).map((name) => variants + name);
|
|
4125
4251
|
}
|
|
4126
4252
|
/**
|
|
4127
|
-
*
|
|
4253
|
+
* Reports whether an input role completes plugin catalog names.
|
|
4128
4254
|
*
|
|
4129
|
-
* @param
|
|
4130
|
-
*
|
|
4255
|
+
* @param context - Input role of a literal.
|
|
4256
|
+
* @returns Whether the role accepts utilities.
|
|
4131
4257
|
*/
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
if (record.checker) record.checker.resolutions = record.resolutions;
|
|
4139
|
-
record.linked = false;
|
|
4140
|
-
}
|
|
4141
|
-
this.#changed();
|
|
4258
|
+
#catalogContext(context) {
|
|
4259
|
+
return [
|
|
4260
|
+
"utility",
|
|
4261
|
+
"document",
|
|
4262
|
+
"anchoredDocument"
|
|
4263
|
+
].includes(context);
|
|
4142
4264
|
}
|
|
4143
4265
|
/**
|
|
4144
|
-
*
|
|
4266
|
+
* Lists the utility tokens of every literal in one source.
|
|
4145
4267
|
*
|
|
4146
|
-
* @param
|
|
4147
|
-
* @
|
|
4148
|
-
* @param inputs - File overlays and ambient declarations for these sources.
|
|
4149
|
-
* @returns Shared checker sources in the requested order. Do not mutate them.
|
|
4150
|
-
* @throws If a requested source's checker environment or backend installation fails.
|
|
4151
|
-
* Other sources with unavailable framework dependencies are left out of the installed inputs.
|
|
4268
|
+
* @param source - Captured source.
|
|
4269
|
+
* @returns Tokens with their literals, in literal order.
|
|
4152
4270
|
*/
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
if (!record.linked) {
|
|
4159
|
-
const resolutions = new Map(record.resolutions.map((item) => [`${item.start}:${item.end}`, item]));
|
|
4160
|
-
for (const item of source.resolutions ?? []) {
|
|
4161
|
-
const target = this.source(item.id);
|
|
4162
|
-
if (target) resolutions.set(`${item.start}:${item.end}`, {
|
|
4163
|
-
...item,
|
|
4164
|
-
id: target.resolverId
|
|
4165
|
-
});
|
|
4166
|
-
}
|
|
4167
|
-
this.#setResolutions(record, [...resolutions.values()]);
|
|
4168
|
-
}
|
|
4169
|
-
return checker;
|
|
4170
|
-
});
|
|
4171
|
-
const all = /* @__PURE__ */ new Map();
|
|
4172
|
-
for (const [id, record] of this.#sources) try {
|
|
4173
|
-
all.set(id, this.#checkerSource(record));
|
|
4174
|
-
} catch {}
|
|
4175
|
-
for (const source of requested) all.set(sourceKey(source.resolverId), source);
|
|
4176
|
-
if (this.#installed?.backend !== backend || this.#installed.generation !== this.#generation || !sameFiles(this.#installed.inputs?.overlayFiles, inputs?.overlayFiles) || !sameFiles(this.#installed.inputs?.ambientDeclarations, inputs?.ambientDeclarations)) {
|
|
4177
|
-
const inventory = [...all.values()].sort((a, b) => {
|
|
4178
|
-
const left = sourceKey(a.resolverId);
|
|
4179
|
-
const right = sourceKey(b.resolverId);
|
|
4180
|
-
return left < right ? -1 : left > right ? 1 : 0;
|
|
4181
|
-
});
|
|
4182
|
-
backend.setProjectSources?.(inventory, this.assets(), inputs);
|
|
4183
|
-
this.#installed = {
|
|
4184
|
-
backend,
|
|
4185
|
-
generation: this.#generation,
|
|
4186
|
-
inputs,
|
|
4187
|
-
sources: inventory
|
|
4188
|
-
};
|
|
4189
|
-
this.#resolutions = void 0;
|
|
4190
|
-
}
|
|
4191
|
-
return requested;
|
|
4271
|
+
#sourceTokens(source) {
|
|
4272
|
+
return this.#index.literals.filter((literal) => literal.source === source).flatMap((literal) => this.#tokens(literal).map((token) => ({
|
|
4273
|
+
...token,
|
|
4274
|
+
literal
|
|
4275
|
+
})));
|
|
4192
4276
|
}
|
|
4193
4277
|
/**
|
|
4194
|
-
*
|
|
4278
|
+
* Finds the completion input at a cursor: the token that contains it, or an empty position in a
|
|
4279
|
+
* literal.
|
|
4280
|
+
*
|
|
4281
|
+
* @param source - Captured source.
|
|
4282
|
+
* @param offset - Original UTF-16 cursor offset.
|
|
4283
|
+
* @returns The literal, the replaced UTF-16 range, and the decoded text before the cursor, or
|
|
4284
|
+
* undefined outside every literal.
|
|
4195
4285
|
*/
|
|
4196
|
-
|
|
4197
|
-
|
|
4286
|
+
#completionInput(source, offset) {
|
|
4287
|
+
const token = this.#sourceTokens(source).find((token) => token.start <= offset && offset <= token.end);
|
|
4288
|
+
const literal = token?.literal ?? this.#index.literals.find((literal) => literal.source === source && literal.boundaries[0] <= offset && offset <= literal.boundaries.at(-1));
|
|
4289
|
+
if (!literal) return;
|
|
4290
|
+
const start = token?.start ?? offset;
|
|
4291
|
+
const end = token?.end ?? offset;
|
|
4292
|
+
const decoded = literal.boundaries.findIndex((position) => position >= offset);
|
|
4293
|
+
const decodedStart = literal.boundaries.indexOf(start);
|
|
4294
|
+
return {
|
|
4295
|
+
literal,
|
|
4296
|
+
start,
|
|
4297
|
+
end,
|
|
4298
|
+
prefix: literal.text.slice(Math.max(0, decodedStart), Math.max(0, decoded))
|
|
4299
|
+
};
|
|
4198
4300
|
}
|
|
4199
4301
|
/**
|
|
4200
|
-
*
|
|
4302
|
+
* Allows color operations only for input roles that emit utility declarations.
|
|
4303
|
+
* Option and definition-prefix inputs use different value contracts.
|
|
4201
4304
|
*
|
|
4202
|
-
* @param
|
|
4203
|
-
* @
|
|
4204
|
-
* @param inputs - File overlays and ambient declarations for this analysis.
|
|
4205
|
-
* @returns Shared maps from resolver ID to authored specifier and resolved ID. Do not mutate them.
|
|
4206
|
-
* @throws If checker input preparation, installation, or import resolution fails.
|
|
4305
|
+
* @param literal - Indexed literal whose context supplies the input role.
|
|
4306
|
+
* @returns Whether its role supports a utility candidate's concrete color and replacement.
|
|
4207
4307
|
*/
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
if (generation === this.#generation) this.#resolutions = resolutions;
|
|
4215
|
-
return resolutions;
|
|
4216
|
-
}
|
|
4217
|
-
return this.#resolutions;
|
|
4308
|
+
#colorInput(literal) {
|
|
4309
|
+
return [
|
|
4310
|
+
"utility",
|
|
4311
|
+
"keyframesFrame",
|
|
4312
|
+
"viewTransitionUtilities"
|
|
4313
|
+
].includes(literal.context);
|
|
4218
4314
|
}
|
|
4219
4315
|
/**
|
|
4220
|
-
*
|
|
4316
|
+
* Maps whitespace-separated decoded tokens to their exact original spellings.
|
|
4221
4317
|
*
|
|
4222
|
-
* @param
|
|
4223
|
-
*
|
|
4224
|
-
* @
|
|
4225
|
-
* @param revision - Revision captured before loading. A newer revision discards this work.
|
|
4226
|
-
* @returns Completion of loading and scanning. Failed scans are not stored.
|
|
4227
|
-
* @throws If loading, projection, or native transport fails.
|
|
4318
|
+
* @param literal - Literal with one source boundary for every decoded UTF-16 position,
|
|
4319
|
+
* including the end. Whitespace is recognized by JavaScript's `\S` regular expression.
|
|
4320
|
+
* @returns Tokens in source order, without trimming or decoding them again.
|
|
4228
4321
|
*/
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
if (scan.kind !== "failed") this.retain(source, scan.summary, revision);
|
|
4322
|
+
#tokens(literal) {
|
|
4323
|
+
return [...literal.text.matchAll(/\S+/gu)].map((match) => ({
|
|
4324
|
+
text: match[0],
|
|
4325
|
+
start: literal.boundaries[match.index],
|
|
4326
|
+
end: literal.boundaries[match.index + match[0].length]
|
|
4327
|
+
}));
|
|
4236
4328
|
}
|
|
4329
|
+
};
|
|
4330
|
+
// src/editor/snapshot.ts
|
|
4331
|
+
/**
|
|
4332
|
+
* Runs source editor requests and sorting diagnostics against captured compiler output.
|
|
4333
|
+
*/
|
|
4334
|
+
var EditorSnapshot = class {
|
|
4335
|
+
/** Owned source buffers and projection metadata captured before publication. */
|
|
4336
|
+
#sources;
|
|
4337
|
+
/** First fix actions from compilation reports, captured in original coordinates. */
|
|
4338
|
+
#fixes;
|
|
4339
|
+
/** Checker ASTs, stable aliases, and captured emission plans for the graph's sources. */
|
|
4340
|
+
#index;
|
|
4341
|
+
/** Native design system and effective lint policy captured during construction. */
|
|
4342
|
+
#query;
|
|
4343
|
+
/** Candidate caches shared by all queries against this snapshot. */
|
|
4344
|
+
#candidates;
|
|
4345
|
+
/** Targets for explicit sorting and sorting fixes. False disables both. */
|
|
4346
|
+
#sort;
|
|
4347
|
+
/** Effective prefer-sorted-utilities severity. Off skips its diagnostics and sorting in fixes. */
|
|
4348
|
+
#sortSeverity;
|
|
4349
|
+
/** Sorting reports cached by full compiler module identity, including empty results. */
|
|
4350
|
+
#reports = /* @__PURE__ */ new Map();
|
|
4351
|
+
/** Completion, hover, and color queries over the same captured sources. */
|
|
4352
|
+
#queries;
|
|
4237
4353
|
/**
|
|
4238
|
-
*
|
|
4354
|
+
* Captures recognized producers and compiler output for one set of source revisions.
|
|
4355
|
+
* Copies source metadata, dependencies, fix actions, and sorting settings before indexing them.
|
|
4356
|
+
* Building the source index also scans graph sources in the session.
|
|
4239
4357
|
*
|
|
4240
|
-
* @param
|
|
4241
|
-
* @
|
|
4242
|
-
* @
|
|
4358
|
+
* @param graph - Original sources, demanded dependencies, emitted CSS, and lint reports.
|
|
4359
|
+
* @param session - Session that compiled the graph and recognizes its producer identities.
|
|
4360
|
+
* @param loader - Completed design system with the same compiler options as the session.
|
|
4361
|
+
* @param config - Sorting targets and preview comments to capture. Effective lint policy comes
|
|
4362
|
+
* from the loader.
|
|
4363
|
+
* @param catalog - Finite plugin candidates offered without running their callbacks.
|
|
4364
|
+
* @param plugins - Runtime for resolving selected plugin completions without changing the session.
|
|
4365
|
+
* @throws If source transport, a projection map, or an original UTF-8 boundary is invalid.
|
|
4243
4366
|
*/
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4367
|
+
constructor(graph, session, loader, config, catalog = [], plugins) {
|
|
4368
|
+
const inputs = structuredClone({
|
|
4369
|
+
sources: graph.sources,
|
|
4370
|
+
dependencies: graph.dependencies
|
|
4371
|
+
});
|
|
4372
|
+
this.#sources = inputs.sources;
|
|
4373
|
+
this.#fixes = structuredClone(graph.fixes);
|
|
4374
|
+
this.#index = new SourceIndex(inputs, session, graph.modules.map((module) => module.moduleId));
|
|
4375
|
+
this.#query = (pluginLoader(session) ?? loader).editorQuery();
|
|
4376
|
+
this.#candidates = new CandidateQueries(this.#query);
|
|
4377
|
+
const css = this.#query.lintSettings();
|
|
4378
|
+
this.#sort = config.sort === false ? false : {
|
|
4379
|
+
stringLiterals: config.sort?.stringLiterals ?? true,
|
|
4380
|
+
arrays: config.sort?.arrays ?? true
|
|
4252
4381
|
};
|
|
4382
|
+
this.#sortSeverity = css["prefer-sorted-utilities"] ?? "off";
|
|
4383
|
+
const annotations = { ...config.preview };
|
|
4384
|
+
const revision = fingerprint(JSON.stringify([this.#index.previews.revision, config]));
|
|
4385
|
+
this.#queries = new SourceQueries(this.#index, this.#query, this.#candidates, revision, annotations, catalog, plugins);
|
|
4253
4386
|
}
|
|
4254
4387
|
/**
|
|
4255
|
-
*
|
|
4388
|
+
* Loads plugin suggestions for a completion input, and callback-generated CSS for a selected
|
|
4389
|
+
* completion from this captured snapshot, so that a following {@link EditorSnapshot.query} can
|
|
4390
|
+
* use them. Other requests are ignored.
|
|
4256
4391
|
*
|
|
4257
|
-
* @param
|
|
4258
|
-
* @param
|
|
4392
|
+
* @param id - Full resolver identity of the requested source.
|
|
4393
|
+
* @param request - Editor request. A `completion` request loads the plugin suggestions for the
|
|
4394
|
+
* candidate at its cursor. A `resolve` request for this snapshot's revision loads CSS, once for
|
|
4395
|
+
* each candidate.
|
|
4396
|
+
* @throws If a plugin fails or the native loader rejects the generated CSS.
|
|
4259
4397
|
*/
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
if (record.checker) record.checker.resolutions = resolutions;
|
|
4264
|
-
this.#changed();
|
|
4398
|
+
async prepare(id, request) {
|
|
4399
|
+
const source = this.#sources.find((source) => sourceKey(source.resolverId) === sourceKey(id));
|
|
4400
|
+
await this.#queries.prepare(source, request);
|
|
4265
4401
|
}
|
|
4266
4402
|
/**
|
|
4267
|
-
*
|
|
4403
|
+
* Computes sorting diagnostics for all graph sources and caches them by module identity.
|
|
4404
|
+
* Effective lint severity and source suppression determine which reports are returned.
|
|
4405
|
+
* Compilation reports already present in the graph remain separate.
|
|
4406
|
+
*
|
|
4407
|
+
* @returns Independent copies of sorting reports for the captured sources.
|
|
4408
|
+
* @throws If candidate ranking fails or native report construction rejects a source URI or range.
|
|
4268
4409
|
*/
|
|
4269
|
-
|
|
4270
|
-
this.#
|
|
4271
|
-
this.#
|
|
4410
|
+
reports() {
|
|
4411
|
+
for (const source of this.#sources) this.#lint(source);
|
|
4412
|
+
return structuredClone([...this.#reports.values()].flat());
|
|
4413
|
+
}
|
|
4414
|
+
/**
|
|
4415
|
+
* Queries a source by resolver identity. Unknown or unmapped positions produce no data.
|
|
4416
|
+
* Queries may populate caches, and returned edits leave the original document unchanged.
|
|
4417
|
+
*
|
|
4418
|
+
* @param id - Full resolver identity captured in the graph, including any query suffix.
|
|
4419
|
+
* @param request - Operation and positions in the original document's UTF-16 coordinate space.
|
|
4420
|
+
* Completion resolution must reuse the candidate, input boundary, and snapshot fingerprint.
|
|
4421
|
+
* @returns Data with the original source fingerprint, or undefined for an unknown source.
|
|
4422
|
+
* A known source with no applicable answer returns a result with the relevant field absent or empty.
|
|
4423
|
+
* @throws If native candidate resolution, CSS formatting, or lint suppression checks fail.
|
|
4424
|
+
*/
|
|
4425
|
+
query(id, request) {
|
|
4426
|
+
const source = this.#sources.find((source) => sourceKey(source.resolverId) === sourceKey(id));
|
|
4427
|
+
if (!source) return;
|
|
4428
|
+
const result = editorResult(source.document.code);
|
|
4429
|
+
if (request.kind === "definition") return {
|
|
4430
|
+
...result,
|
|
4431
|
+
definitions: []
|
|
4432
|
+
};
|
|
4433
|
+
if (request.kind === "links") return {
|
|
4434
|
+
...result,
|
|
4435
|
+
links: []
|
|
4436
|
+
};
|
|
4437
|
+
if (request.kind === "fixes" || request.kind === "sort") {
|
|
4438
|
+
const seed = request.kind === "fixes" ? uniqueEdits(this.#fixes.filter((edits) => edits.every((edit) => edit.location.uri === source.document.uri && edit.location.revision === result.revision)).flat().map((edit) => ({
|
|
4439
|
+
start: edit.location.startUtf16,
|
|
4440
|
+
end: edit.location.endUtf16,
|
|
4441
|
+
newText: edit.replacement
|
|
4442
|
+
}))) : [];
|
|
4443
|
+
result.edits = request.kind === "sort" || this.#sortSeverity !== "off" ? sortSource(source, this.#index, this.#candidates, this.#sort, seed, request.kind === "sort" ? request.protectedRanges?.length ? (start, end) => !request.protectedRanges.some((range) => start < range.end && range.start < end) : void 0 : (start, end) => !this.#query.lintSuppressed(source.document.code, bytes(source.document.code, start), bytes(source.document.code, end), "prefer-sorted-utilities", this.#index.lintComments(source))) : seed;
|
|
4444
|
+
return result;
|
|
4445
|
+
}
|
|
4446
|
+
return this.#queries.query(source, request, result);
|
|
4447
|
+
}
|
|
4448
|
+
/**
|
|
4449
|
+
* Computes sorting edits once and records reports through the native lint policy.
|
|
4450
|
+
* UTF-16 edit boundaries are converted to original UTF-8 byte ranges before reporting.
|
|
4451
|
+
* Empty results are cached too, and a failed computation leaves the module uncached.
|
|
4452
|
+
*
|
|
4453
|
+
* @param source - Unchanged prepared source stored in this snapshot's graph.
|
|
4454
|
+
* @throws If candidate ranking fails or native reporting rejects the source URI or byte ranges.
|
|
4455
|
+
*/
|
|
4456
|
+
#lint(source) {
|
|
4457
|
+
if (this.#reports.has(source.moduleId)) return;
|
|
4458
|
+
const edits = this.#sortSeverity === "off" ? [] : sortSource(source, this.#index, this.#candidates, this.#sort);
|
|
4459
|
+
const reports = this.#query.reportSortEdits(source.document.uri, source.document.code, edits.map((edit) => ({
|
|
4460
|
+
start: bytes(source.document.code, edit.start),
|
|
4461
|
+
end: bytes(source.document.code, edit.end),
|
|
4462
|
+
replacement: edit.newText
|
|
4463
|
+
})), this.#index.lintComments(source));
|
|
4464
|
+
this.#reports.set(source.moduleId, reports);
|
|
4272
4465
|
}
|
|
4273
4466
|
};
|
|
4274
4467
|
/**
|
|
4275
|
-
*
|
|
4468
|
+
* Converts an original document position to the native compiler's byte coordinates.
|
|
4276
4469
|
*
|
|
4277
|
-
* @param
|
|
4278
|
-
* @
|
|
4470
|
+
* @param text - Original source text whose encoding supplies the byte count.
|
|
4471
|
+
* @param offset - UTF-16 integer offset within `text`, at a Unicode scalar boundary.
|
|
4472
|
+
* The caller must ensure that the offset does not split a surrogate pair.
|
|
4473
|
+
* @returns Number of UTF-8 bytes before that position.
|
|
4279
4474
|
*/
|
|
4280
|
-
function
|
|
4281
|
-
|
|
4282
|
-
return /^[a-z]:\//i.test(normalized) ? normalized.toLowerCase() : normalized;
|
|
4475
|
+
function bytes(text, offset) {
|
|
4476
|
+
return new TextEncoder().encode(text.slice(0, offset)).length;
|
|
4283
4477
|
}
|
|
4284
4478
|
/**
|
|
4285
|
-
*
|
|
4479
|
+
* Sorts the supplied array in place and greedily keeps edits that do not overlap earlier ones.
|
|
4286
4480
|
*
|
|
4287
|
-
* @param
|
|
4288
|
-
*
|
|
4289
|
-
* @returns
|
|
4481
|
+
* @param edits - Edits in the same original UTF-16 coordinate space. The array is reordered by
|
|
4482
|
+
* start and then end. Earlier starts win, and a shorter edit wins when starts are equal.
|
|
4483
|
+
* @returns A new array of accepted edit objects. Touching ranges and identical zero-width
|
|
4484
|
+
* insertions are retained. The edit objects themselves remain unchanged.
|
|
4290
4485
|
*/
|
|
4291
|
-
function
|
|
4292
|
-
|
|
4486
|
+
function uniqueEdits(edits) {
|
|
4487
|
+
const result = [];
|
|
4488
|
+
for (const edit of edits.sort((a, b) => a.start - b.start || a.end - b.end)) {
|
|
4489
|
+
if (result.some((other) => other.start < edit.end && edit.start < other.end)) continue;
|
|
4490
|
+
result.push(edit);
|
|
4491
|
+
}
|
|
4492
|
+
return result;
|
|
4293
4493
|
}
|
|
4294
4494
|
// src/css/plugins/paths.ts
|
|
4295
4495
|
/**
|
|
@@ -4765,6 +4965,6 @@ function compilerOptions(config) {
|
|
|
4765
4965
|
rootFontSize: resolved.rootFontSize
|
|
4766
4966
|
};
|
|
4767
4967
|
}
|
|
4768
|
-
export {
|
|
4968
|
+
export { compileWithPlugins as A, compileModule as C, fileSourceKind as D, cleanModuleId as E, prepareSourcePlugins as M, TypeFactsError as O, analyzeModule as S, CompilationFailure as T, loadEditorCssGraph as _, pluginDirectory as a, cssBannerPrefix as b, CheckerSource as c, CompilationProject as d, sourceKey as f, loadCssGraph as g, lintsStylesheet as h, completePluginPaths as i, pluginLoader as j, attachPlugins as k, checkerId as l, isStylesheet as m, cssEntrypoint as n, queryPluginPaths as o, queryDesignSystem as p, StylesheetEditor as r, EditorSnapshot as s, compilerOptions as t, withRequestedProbes as u, PluginRuntime as v, warnRejectedNames as w, finalizeCss as x, defaultPluginExport as y };
|
|
4769
4969
|
|
|
4770
|
-
//# sourceMappingURL=options-
|
|
4970
|
+
//# sourceMappingURL=options-BOutD9j7.js.map
|