@telorun/ide-support 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/completions/import-source.js +2 -2
- package/dist/completions/resolve-node.d.ts +5 -0
- package/dist/completions/resolve-node.d.ts.map +1 -1
- package/dist/completions/resolve-node.js +4 -0
- package/dist/definition/alias-qualified-value.d.ts +23 -0
- package/dist/definition/alias-qualified-value.d.ts.map +1 -0
- package/dist/definition/alias-qualified-value.js +24 -0
- package/dist/definition/build-definition.d.ts +15 -8
- package/dist/definition/build-definition.d.ts.map +1 -1
- package/dist/definition/build-definition.js +35 -89
- package/dist/definition/manifest-navigation.d.ts +36 -0
- package/dist/definition/manifest-navigation.d.ts.map +1 -0
- package/dist/definition/manifest-navigation.js +92 -0
- package/dist/definition/resolve-cel-target.d.ts +17 -0
- package/dist/definition/resolve-cel-target.d.ts.map +1 -0
- package/dist/definition/resolve-cel-target.js +125 -0
- package/dist/definition/resolve-export-chain.d.ts +23 -0
- package/dist/definition/resolve-export-chain.d.ts.map +1 -0
- package/dist/definition/resolve-export-chain.js +89 -0
- package/dist/definition/resolve-kind-target.d.ts +26 -0
- package/dist/definition/resolve-kind-target.d.ts.map +1 -0
- package/dist/definition/resolve-kind-target.js +51 -0
- package/dist/definition/resolve-ref-target.d.ts +14 -0
- package/dist/definition/resolve-ref-target.d.ts.map +1 -0
- package/dist/definition/resolve-ref-target.js +25 -0
- package/dist/import-upgrades/build-import-upgrades.d.ts +46 -9
- package/dist/import-upgrades/build-import-upgrades.d.ts.map +1 -1
- package/dist/import-upgrades/build-import-upgrades.js +92 -27
- package/dist/import-upgrades/find-import-entries.d.ts +17 -11
- package/dist/import-upgrades/find-import-entries.d.ts.map +1 -1
- package/dist/import-upgrades/find-import-entries.js +4 -5
- package/dist/import-upgrades/index.d.ts +3 -2
- package/dist/import-upgrades/index.d.ts.map +1 -1
- package/dist/import-upgrades/index.js +1 -0
- package/dist/import-upgrades/parse-module-versions.d.ts +19 -0
- package/dist/import-upgrades/parse-module-versions.d.ts.map +1 -0
- package/dist/import-upgrades/parse-module-versions.js +29 -0
- package/package.json +2 -2
- package/src/completions/import-source.ts +2 -2
- package/src/completions/resolve-node.ts +9 -0
- package/src/definition/alias-qualified-value.ts +42 -0
- package/src/definition/build-definition.ts +34 -104
- package/src/definition/manifest-navigation.ts +124 -0
- package/src/definition/resolve-cel-target.ts +151 -0
- package/src/definition/resolve-export-chain.ts +119 -0
- package/src/definition/resolve-kind-target.ts +61 -0
- package/src/definition/resolve-ref-target.ts +34 -0
- package/src/import-upgrades/build-import-upgrades.ts +150 -36
- package/src/import-upgrades/find-import-entries.ts +27 -19
- package/src/import-upgrades/index.ts +8 -1
- package/src/import-upgrades/parse-module-versions.ts +30 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isCanonicalIntegrity } from "@telorun/analyzer";
|
|
2
|
+
/**
|
|
3
|
+
* Read the hub's `GET /module/versions` payload into {@link ModuleVersion}s,
|
|
4
|
+
* newest first (the route's own ordering, preserved).
|
|
5
|
+
*
|
|
6
|
+
* Pure — the host owns the `fetch`, as everything else in this package does.
|
|
7
|
+
* It lives here rather than in each host because the shape is this package's:
|
|
8
|
+
* three hosts parsing the same route by hand is how one of them was left
|
|
9
|
+
* filtering for strings after the route started returning objects, which
|
|
10
|
+
* TypeScript could not catch behind the response cast and which surfaced only
|
|
11
|
+
* as a silently empty version list.
|
|
12
|
+
*
|
|
13
|
+
* Tolerant by design: an entry with no usable `version` is dropped rather than
|
|
14
|
+
* throwing, and an `integrity` that is not a canonical `sha256-<base64url>` is
|
|
15
|
+
* discarded rather than carried — a pin is spliced into the author's YAML, so a
|
|
16
|
+
* malformed one has to become "no pin", never corrupt text.
|
|
17
|
+
*/
|
|
18
|
+
export function parseModuleVersions(payload) {
|
|
19
|
+
const versions = payload?.versions;
|
|
20
|
+
if (!Array.isArray(versions))
|
|
21
|
+
return [];
|
|
22
|
+
return versions.flatMap((entry) => {
|
|
23
|
+
const version = entry?.version;
|
|
24
|
+
if (typeof version !== "string" || version === "")
|
|
25
|
+
return [];
|
|
26
|
+
const integrity = entry.integrity;
|
|
27
|
+
return [isCanonicalIntegrity(integrity) ? { version, integrity } : { version }];
|
|
28
|
+
});
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/ide-support",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Editor-host-agnostic IDE support (completions, diagnostic normalization) for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"src/**"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@telorun/analyzer": "0.
|
|
39
|
+
"@telorun/analyzer": "0.52.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^20.0.0",
|
|
@@ -161,8 +161,8 @@ async function refSearchCompletions(
|
|
|
161
161
|
|
|
162
162
|
/** The `org/name` tail of a location ref: its last two path segments, with the
|
|
163
163
|
* transport scheme (`oci://`, `https://`, …) and registry host dropped.
|
|
164
|
-
* `oci://ghcr.io/telorun/telo-console` → `telorun/telo-console`; `
|
|
165
|
-
* → `
|
|
164
|
+
* `oci://ghcr.io/telorun/telo-console` → `telorun/telo-console`; `acme/console`
|
|
165
|
+
* → `acme/console`. Falls back to fewer segments (or the whole ref) when there
|
|
166
166
|
* aren't two. */
|
|
167
167
|
function refDisplayName(ref: string): string {
|
|
168
168
|
const withoutScheme = ref.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "");
|
|
@@ -16,6 +16,11 @@ import {
|
|
|
16
16
|
* "which container am I typing into". */
|
|
17
17
|
export interface ResolvedCursor {
|
|
18
18
|
docIndex: number;
|
|
19
|
+
/** The cursor as a document offset. Carried so a consumer hit-testing inside a
|
|
20
|
+
* node (a CEL chain, the halves of an `Alias.Name` value) reuses the one this
|
|
21
|
+
* resolution was performed with, rather than recomputing the line table and
|
|
22
|
+
* risking a different answer. */
|
|
23
|
+
offset: number;
|
|
19
24
|
/** Top-level `kind:` value of the cursor's document, when present. */
|
|
20
25
|
docKind?: string;
|
|
21
26
|
slot: "key" | "value";
|
|
@@ -332,6 +337,7 @@ export function resolveNodeAtPosition(
|
|
|
332
337
|
}
|
|
333
338
|
return {
|
|
334
339
|
docIndex,
|
|
340
|
+
offset,
|
|
335
341
|
docKind,
|
|
336
342
|
slot: "key",
|
|
337
343
|
path: found.path,
|
|
@@ -362,6 +368,7 @@ export function resolveNodeAtPosition(
|
|
|
362
368
|
const { path, existingKeys, scope } = columnSearch(doc.root, col, offset, lineOffsets);
|
|
363
369
|
return {
|
|
364
370
|
docIndex,
|
|
371
|
+
offset,
|
|
365
372
|
docKind,
|
|
366
373
|
slot: "key",
|
|
367
374
|
path,
|
|
@@ -375,6 +382,7 @@ export function resolveNodeAtPosition(
|
|
|
375
382
|
const clampedEnd = Math.min(offset, value.range[1]);
|
|
376
383
|
return {
|
|
377
384
|
docIndex,
|
|
385
|
+
offset,
|
|
378
386
|
docKind,
|
|
379
387
|
slot: "value",
|
|
380
388
|
path: found.keyName != null ? [...found.path, found.keyName] : found.path,
|
|
@@ -395,6 +403,7 @@ export function resolveNodeAtPosition(
|
|
|
395
403
|
: { path: [], existingKeys: new Set<string>(), scope: { depth: 0 } };
|
|
396
404
|
return {
|
|
397
405
|
docIndex,
|
|
406
|
+
offset,
|
|
398
407
|
docKind,
|
|
399
408
|
slot: "key",
|
|
400
409
|
path: resolution.path,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AstScalar } from "@telorun/analyzer";
|
|
2
|
+
|
|
3
|
+
/** An `<Alias>.<Name>` scalar split at its first dot — the grammar `kind:`,
|
|
4
|
+
* `extends:`, `x-telo-ref` and `!ref` all share — plus which half the cursor
|
|
5
|
+
* sits on, so the alias navigates to its import and the suffix to the thing
|
|
6
|
+
* the alias qualifies. */
|
|
7
|
+
export interface AliasQualifiedValue {
|
|
8
|
+
/** Undefined for an unqualified value (a bare local name). */
|
|
9
|
+
alias?: string;
|
|
10
|
+
/** Everything after the first dot, or the whole value when unqualified. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** True when the cursor sits on the alias half (dot inclusive). */
|
|
13
|
+
onAlias: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Split the scalar at `node` around the cursor at `offset` (a document offset).
|
|
17
|
+
*
|
|
18
|
+
* The parsed value is authoritative where there is one — the source range spans
|
|
19
|
+
* the whole scalar token, so a quoted `x-telo-ref: "Alias.Kind"` would otherwise
|
|
20
|
+
* carry its quotes into the alias and miss every lookup. A `!ref` / `!cel`
|
|
21
|
+
* scalar resolves to a tagged sentinel rather than a string, so those fall back
|
|
22
|
+
* to the raw slice. The range is still what locates the value inside the token,
|
|
23
|
+
* which is what the cursor is hit-tested against. */
|
|
24
|
+
export function splitAliasQualified(
|
|
25
|
+
text: string,
|
|
26
|
+
node: AstScalar,
|
|
27
|
+
offset: number,
|
|
28
|
+
): AliasQualifiedValue | undefined {
|
|
29
|
+
const raw = text.slice(node.range[0], node.range[1]);
|
|
30
|
+
const value = typeof node.value === "string" ? node.value : raw.trim();
|
|
31
|
+
if (!value) return undefined;
|
|
32
|
+
const at = raw.indexOf(value);
|
|
33
|
+
const start = node.range[0] + (at >= 0 ? at : 0);
|
|
34
|
+
|
|
35
|
+
const dot = value.indexOf(".");
|
|
36
|
+
if (dot === -1) return { name: value, onAlias: false };
|
|
37
|
+
return {
|
|
38
|
+
alias: value.slice(0, dot),
|
|
39
|
+
name: value.slice(dot + 1),
|
|
40
|
+
onAlias: offset <= start + dot,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -1,97 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseToAst,
|
|
3
|
-
type AstDocument,
|
|
4
|
-
type LoadedFile,
|
|
5
|
-
type LoadedGraph,
|
|
6
|
-
type LoadedModule,
|
|
7
|
-
type Range,
|
|
8
|
-
} from "@telorun/analyzer";
|
|
1
|
+
import { parseToAst, type AstDocument, type AstScalar, type LoadedGraph } from "@telorun/analyzer";
|
|
9
2
|
import type { DefinitionResult } from "../types.js";
|
|
10
3
|
import { resolveNodeAtPosition } from "../completions/resolve-node.js";
|
|
4
|
+
import { splitAliasQualified } from "./alias-qualified-value.js";
|
|
5
|
+
import { moduleForFile } from "./manifest-navigation.js";
|
|
6
|
+
import { resolveCelTarget } from "./resolve-cel-target.js";
|
|
7
|
+
import { isKindSlot, resolveKindTarget } from "./resolve-kind-target.js";
|
|
8
|
+
import { resolveRefTarget } from "./resolve-ref-target.js";
|
|
11
9
|
|
|
12
|
-
/**
|
|
13
|
-
function moduleForFile(graph: LoadedGraph, filePath: string): LoadedModule | undefined {
|
|
14
|
-
for (const mod of graph.modules.values()) {
|
|
15
|
-
if (mod.owner.source === filePath) return mod;
|
|
16
|
-
if (mod.partials.some((p) => p.source === filePath)) return mod;
|
|
17
|
-
}
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** First resource named `name` across `files`, located at its `metadata.name`
|
|
22
|
-
* (or its first line as a fallback). Names are unique within a module scope, so
|
|
23
|
-
* the first hit is the definition. */
|
|
24
|
-
function locateResource(files: LoadedFile[], name: string): DefinitionResult | undefined {
|
|
25
|
-
for (const file of files) {
|
|
26
|
-
for (let i = 0; i < file.manifests.length; i++) {
|
|
27
|
-
const manifest = file.manifests[i];
|
|
28
|
-
if (!manifest || manifest.metadata?.name !== name) continue;
|
|
29
|
-
const pos = file.positions[i];
|
|
30
|
-
const range: Range | undefined =
|
|
31
|
-
pos?.positionIndex.get("metadata.name") ??
|
|
32
|
-
pos?.positionIndex.get("@key:metadata.name") ??
|
|
33
|
-
(pos ? { start: { line: pos.sourceLine, character: 0 }, end: { line: pos.sourceLine, character: 0 } } : undefined);
|
|
34
|
-
if (range) return { uri: file.source, range };
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** The `exports.resources` list of a module's owner doc (empty when absent). */
|
|
41
|
-
function exportedResources(mod: LoadedModule): string[] {
|
|
42
|
-
const doc = mod.owner.manifests.find(
|
|
43
|
-
(m) => m?.kind === "Telo.Library" || m?.kind === "Telo.Application",
|
|
44
|
-
) as { exports?: { resources?: unknown } } | undefined;
|
|
45
|
-
const list = doc?.exports?.resources;
|
|
46
|
-
return Array.isArray(list) ? list.filter((e): e is string => typeof e === "string") : [];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Follow `name` into `moduleSource` across the export boundary, honoring the
|
|
50
|
-
* `exports.resources` gate and re-export chains (`app → api → domain → …`). A
|
|
51
|
-
* terminal match is a locally-owned instance the module actually exports; a
|
|
52
|
-
* re-export entry `InnerAlias.name` hops through that module's own import edge.
|
|
53
|
-
* `seen` bounds cyclic import graphs. */
|
|
54
|
-
function resolveExported(
|
|
55
|
-
graph: LoadedGraph,
|
|
56
|
-
moduleSource: string,
|
|
57
|
-
name: string,
|
|
58
|
-
seen: Set<string> = new Set(),
|
|
59
|
-
): DefinitionResult | undefined {
|
|
60
|
-
if (seen.has(moduleSource)) return undefined;
|
|
61
|
-
seen.add(moduleSource);
|
|
62
|
-
const mod = graph.modules.get(moduleSource);
|
|
63
|
-
if (!mod) return undefined;
|
|
64
|
-
|
|
65
|
-
const exports = exportedResources(mod);
|
|
66
|
-
|
|
67
|
-
// No `exports.resources` block → ungated (the module hasn't opted into the
|
|
68
|
-
// gate, same as `exports.kinds`): a plain name match keeps navigation working
|
|
69
|
-
// for modules that predate explicit exports.
|
|
70
|
-
if (exports.length === 0) {
|
|
71
|
-
return locateResource([mod.owner, ...mod.partials], name);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (exports.includes(name)) {
|
|
75
|
-
const local = locateResource([mod.owner, ...mod.partials], name);
|
|
76
|
-
if (local) return local;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const reexport = exports.find((e) => e.endsWith(`.${name}`));
|
|
80
|
-
if (!reexport) return undefined;
|
|
81
|
-
const innerAlias = reexport.slice(0, reexport.length - name.length - 1);
|
|
82
|
-
const edge = graph.importEdges.get(mod.owner.source)?.get(innerAlias);
|
|
83
|
-
return edge ? resolveExported(graph, edge.targetSource, name, seen) : undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Resolve the `!ref` under the cursor to its target resource's definition.
|
|
10
|
+
/** Resolve the symbol under the cursor to where it is declared.
|
|
87
11
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
12
|
+
* Three navigable symbol classes, each dispatched on what the cursor sits in
|
|
13
|
+
* rather than on the field it happens to be under:
|
|
14
|
+
*
|
|
15
|
+
* - a CEL identifier (`variables.port`, `resources.Store.conn`) → its
|
|
16
|
+
* declaration on the module doc, or the resource it names;
|
|
17
|
+
* - an alias-qualified kind (`kind: Http.Server`, `extends:`, `x-telo-ref`) →
|
|
18
|
+
* the `Telo.Definition` that registers it;
|
|
19
|
+
* - a `!ref` target → the resource instance it names.
|
|
20
|
+
*
|
|
21
|
+
* In the last two the alias half (`Http`) navigates to the import that declares
|
|
22
|
+
* it, and the suffix to what the alias qualifies. Returns `undefined` when the
|
|
23
|
+
* cursor is on nothing navigable, or the target can't be found (a kernel
|
|
24
|
+
* built-in, a scope-local name, an unexported entry, or an import that failed
|
|
25
|
+
* to load). */
|
|
95
26
|
export function buildDefinition(
|
|
96
27
|
text: string,
|
|
97
28
|
line: number,
|
|
@@ -101,23 +32,22 @@ export function buildDefinition(
|
|
|
101
32
|
docs?: AstDocument[],
|
|
102
33
|
): DefinitionResult | undefined {
|
|
103
34
|
const astDocs = docs ?? parseToAst(text);
|
|
104
|
-
const
|
|
105
|
-
if (!
|
|
106
|
-
|
|
107
|
-
// The scalar's range covers the ref target text (the value after `!ref`).
|
|
108
|
-
const source = text.slice(node.range[0], node.range[1]).trim();
|
|
109
|
-
if (!source) return undefined;
|
|
35
|
+
const resolved = resolveNodeAtPosition(text, astDocs, line, character);
|
|
36
|
+
if (!resolved) return undefined;
|
|
110
37
|
|
|
111
38
|
const currentModule = moduleForFile(graph, currentFilePath) ?? graph.entry;
|
|
112
|
-
const dot = source.indexOf(".");
|
|
113
|
-
const alias = dot === -1 ? undefined : source.slice(0, dot);
|
|
114
|
-
const name = dot === -1 ? source : source.slice(dot + 1);
|
|
115
39
|
|
|
116
|
-
if (
|
|
117
|
-
return
|
|
40
|
+
if (resolved.cel) {
|
|
41
|
+
return resolveCelTarget(graph, currentModule, resolved.cel.segment, resolved.cel.offset);
|
|
118
42
|
}
|
|
119
43
|
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
122
|
-
|
|
44
|
+
const node = resolved.node;
|
|
45
|
+
if (resolved.slot !== "value" || node?.kind !== "scalar") return undefined;
|
|
46
|
+
|
|
47
|
+
const split = splitAliasQualified(text, node as AstScalar, resolved.offset);
|
|
48
|
+
if (!split) return undefined;
|
|
49
|
+
|
|
50
|
+
if (node.tag === "!ref") return resolveRefTarget(graph, currentModule, split);
|
|
51
|
+
if (isKindSlot(resolved)) return resolveKindTarget(graph, currentModule, split);
|
|
52
|
+
return undefined;
|
|
123
53
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { LoadedFile, LoadedGraph, LoadedModule, Range } from "@telorun/analyzer";
|
|
2
|
+
import type { DefinitionResult } from "../types.js";
|
|
3
|
+
|
|
4
|
+
/** The shape every navigation lookup reads off a `LoadedFile.manifests` entry.
|
|
5
|
+
* Narrower than `ResourceManifest` on purpose — navigation only ever asks what
|
|
6
|
+
* kind a doc is and what it is named. */
|
|
7
|
+
export interface NavigableManifest {
|
|
8
|
+
kind?: string;
|
|
9
|
+
metadata?: { name?: string };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const MODULE_DOC_KINDS = new Set(["Telo.Application", "Telo.Library"]);
|
|
13
|
+
const KIND_DOC_KINDS = new Set(["Telo.Definition", "Telo.Abstract"]);
|
|
14
|
+
|
|
15
|
+
/** The module whose owner or partials includes `filePath`. */
|
|
16
|
+
export function moduleForFile(graph: LoadedGraph, filePath: string): LoadedModule | undefined {
|
|
17
|
+
for (const mod of graph.modules.values()) {
|
|
18
|
+
if (mod.owner.source === filePath) return mod;
|
|
19
|
+
if (mod.partials.some((p) => p.source === filePath)) return mod;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Every file in a module's scope, owner first. */
|
|
25
|
+
export function moduleFiles(mod: LoadedModule): LoadedFile[] {
|
|
26
|
+
return [mod.owner, ...mod.partials];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function rangeAt(file: LoadedFile, docIndex: number, key: string): Range | undefined {
|
|
30
|
+
return file.positions[docIndex]?.positionIndex.get(key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The value span of `path`, falling back to its key when the value has none. */
|
|
34
|
+
function valueRange(file: LoadedFile, docIndex: number, path: string): Range | undefined {
|
|
35
|
+
return rangeAt(file, docIndex, path) ?? rangeAt(file, docIndex, `@key:${path}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The key span of `path` — what a declaration jump underlines, so the target
|
|
39
|
+
* reads as `variables:` / `imports.Http`, not the whole block that follows. */
|
|
40
|
+
function keyRange(file: LoadedFile, docIndex: number, path: string): Range | undefined {
|
|
41
|
+
return rangeAt(file, docIndex, `@key:${path}`) ?? rangeAt(file, docIndex, path);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function docStartRange(file: LoadedFile, docIndex: number): Range | undefined {
|
|
45
|
+
const pos = file.positions[docIndex];
|
|
46
|
+
if (!pos) return undefined;
|
|
47
|
+
return {
|
|
48
|
+
start: { line: pos.sourceLine, character: 0 },
|
|
49
|
+
end: { line: pos.sourceLine, character: 0 },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** First manifest across `files` satisfying `match`, located at its
|
|
54
|
+
* `metadata.name` (or its first line as a fallback). Names are unique within a
|
|
55
|
+
* module scope, so the first hit is the definition. */
|
|
56
|
+
export function locateManifest(
|
|
57
|
+
files: LoadedFile[],
|
|
58
|
+
match: (manifest: NavigableManifest) => boolean,
|
|
59
|
+
): DefinitionResult | undefined {
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
for (let i = 0; i < file.manifests.length; i++) {
|
|
62
|
+
const manifest = file.manifests[i] as NavigableManifest | null;
|
|
63
|
+
if (!manifest || !match(manifest)) continue;
|
|
64
|
+
const range = valueRange(file, i, "metadata.name") ?? docStartRange(file, i);
|
|
65
|
+
if (range) return { uri: file.source, range };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The resource instance named `name` in a module scope. */
|
|
72
|
+
export function locateResource(files: LoadedFile[], name: string): DefinitionResult | undefined {
|
|
73
|
+
return locateManifest(files, (m) => m.metadata?.name === name);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The `Telo.Definition` / `Telo.Abstract` doc registering kind suffix `name`.
|
|
77
|
+
* Filtered by doc kind so an instance sharing the name never shadows it. */
|
|
78
|
+
export function locateKindDefinition(
|
|
79
|
+
files: LoadedFile[],
|
|
80
|
+
name: string,
|
|
81
|
+
): DefinitionResult | undefined {
|
|
82
|
+
return locateManifest(files, (m) => KIND_DOC_KINDS.has(m.kind ?? "") && m.metadata?.name === name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Index of the module's `Telo.Application` / `Telo.Library` doc in its owner
|
|
86
|
+
* file — where `imports`, `variables`, `secrets` and `ports` are declared. */
|
|
87
|
+
function moduleDocIndex(mod: LoadedModule): number {
|
|
88
|
+
return mod.owner.manifests.findIndex((m) =>
|
|
89
|
+
MODULE_DOC_KINDS.has((m as NavigableManifest | null)?.kind ?? ""),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The module doc's JSON projection, for reading its `exports` block. */
|
|
94
|
+
export function moduleDoc(mod: LoadedModule): Record<string, unknown> | undefined {
|
|
95
|
+
const index = moduleDocIndex(mod);
|
|
96
|
+
return index < 0 ? undefined : (mod.owner.manifests[index] as Record<string, unknown> | null) ?? undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The module's `metadata.name` — the identity a canonical kind (`<module>.<Kind>`)
|
|
100
|
+
* and an `ImportEdge.targetModuleName` are expressed in. */
|
|
101
|
+
export function moduleName(mod: LoadedModule): string | undefined {
|
|
102
|
+
return (moduleDoc(mod) as NavigableManifest | undefined)?.metadata?.name;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A dotted key path on the module doc (`imports.Http`, `variables.port`),
|
|
106
|
+
* located at the key itself. */
|
|
107
|
+
export function locateModuleDocKey(
|
|
108
|
+
mod: LoadedModule,
|
|
109
|
+
path: string,
|
|
110
|
+
): DefinitionResult | undefined {
|
|
111
|
+
const index = moduleDocIndex(mod);
|
|
112
|
+
if (index < 0) return undefined;
|
|
113
|
+
const range = keyRange(mod.owner, index, path);
|
|
114
|
+
return range ? { uri: mod.owner.source, range } : undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Where the import bound to `alias` is declared: the `imports:` map entry, or
|
|
118
|
+
* the legacy standalone `Telo.Import` doc named after the alias. */
|
|
119
|
+
export function locateImport(mod: LoadedModule, alias: string): DefinitionResult | undefined {
|
|
120
|
+
return (
|
|
121
|
+
locateModuleDocKey(mod, `imports.${alias}`) ??
|
|
122
|
+
locateManifest(moduleFiles(mod), (m) => m.kind === "Telo.Import" && m.metadata?.name === alias)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CelParseError,
|
|
3
|
+
type CelNode,
|
|
4
|
+
type CelSegment,
|
|
5
|
+
type LoadedGraph,
|
|
6
|
+
type LoadedModule,
|
|
7
|
+
} from "@telorun/analyzer";
|
|
8
|
+
import type { DefinitionResult } from "../types.js";
|
|
9
|
+
import {
|
|
10
|
+
locateImport,
|
|
11
|
+
locateModuleDocKey,
|
|
12
|
+
locateResource,
|
|
13
|
+
moduleFiles,
|
|
14
|
+
} from "./manifest-navigation.js";
|
|
15
|
+
import { resolveExportedResource } from "./resolve-export-chain.js";
|
|
16
|
+
|
|
17
|
+
/** Root CEL scopes whose members are declared as a block on the module doc, so
|
|
18
|
+
* `variables.port` navigates to `variables:` and then to its `port:` entry. */
|
|
19
|
+
const DECLARATION_SCOPES = new Set(["variables", "secrets", "ports"]);
|
|
20
|
+
|
|
21
|
+
/** One identifier of a dotted CEL chain, with the span the cursor hit-tests
|
|
22
|
+
* against. */
|
|
23
|
+
interface ChainPart {
|
|
24
|
+
name: string;
|
|
25
|
+
range: [number, number];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Flatten `a.b.c` into its identifiers. Returns undefined as soon as the chain
|
|
29
|
+
* is rooted in something other than a plain identifier (a call, an index), so a
|
|
30
|
+
* navigable prefix is never invented out of a computed expression. */
|
|
31
|
+
function flattenChain(node: CelNode): ChainPart[] | undefined {
|
|
32
|
+
if (node.kind === "ident") return [{ name: node.name, range: node.range }];
|
|
33
|
+
if (node.kind !== "member") return undefined;
|
|
34
|
+
const head = flattenChain(node.target);
|
|
35
|
+
return head ? [...head, { name: node.property, range: node.propertyRange }] : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Exhaustive by construction: a new `CelNode` variant fails the build here
|
|
39
|
+
* rather than silently going unwalked, so the analyzer's node model and this
|
|
40
|
+
* walk cannot drift apart unnoticed. */
|
|
41
|
+
function celChildren(node: CelNode): CelNode[] {
|
|
42
|
+
switch (node.kind) {
|
|
43
|
+
case "literal":
|
|
44
|
+
case "ident":
|
|
45
|
+
return [];
|
|
46
|
+
case "member":
|
|
47
|
+
return [node.target];
|
|
48
|
+
case "index":
|
|
49
|
+
return [node.target, node.index];
|
|
50
|
+
case "call":
|
|
51
|
+
return node.args;
|
|
52
|
+
case "methodCall":
|
|
53
|
+
return [node.receiver, ...node.args];
|
|
54
|
+
case "list":
|
|
55
|
+
return node.items;
|
|
56
|
+
case "map":
|
|
57
|
+
return node.entries.flatMap((e) => [e.key, e.value]);
|
|
58
|
+
case "ternary":
|
|
59
|
+
return [node.cond, node.then, node.else];
|
|
60
|
+
case "unary":
|
|
61
|
+
return [node.operand];
|
|
62
|
+
case "binary":
|
|
63
|
+
return [node.left, node.right];
|
|
64
|
+
}
|
|
65
|
+
const unhandled: never = node;
|
|
66
|
+
throw new Error(`Unhandled CEL node: ${JSON.stringify(unhandled)}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The dotted chain under `offset`, and which of its identifiers was hit. The
|
|
70
|
+
* walk is outermost-first so the longest chain wins — `resources.Store.conn`
|
|
71
|
+
* resolves as one chain rather than as its `resources.Store` prefix. */
|
|
72
|
+
function chainAt(
|
|
73
|
+
node: CelNode,
|
|
74
|
+
offset: number,
|
|
75
|
+
): { parts: ChainPart[]; index: number } | undefined {
|
|
76
|
+
if (offset < node.range[0] || offset > node.range[1]) return undefined;
|
|
77
|
+
const parts = flattenChain(node);
|
|
78
|
+
if (parts) {
|
|
79
|
+
const index = parts.findIndex((p) => offset >= p.range[0] && offset <= p.range[1]);
|
|
80
|
+
if (index >= 0) return { parts, index };
|
|
81
|
+
}
|
|
82
|
+
for (const child of celChildren(node)) {
|
|
83
|
+
const hit = chainAt(child, offset);
|
|
84
|
+
if (hit) return hit;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** `resources.<name>` is a local instance; `resources.<Alias>.<name>` is an
|
|
90
|
+
* imported module's exported one. A local name wins over an import alias, so a
|
|
91
|
+
* deeper access on a local instance (`resources.db.url`) reads as a field of
|
|
92
|
+
* that instance rather than as a cross-module lookup. */
|
|
93
|
+
function resolveResourceChain(
|
|
94
|
+
graph: LoadedGraph,
|
|
95
|
+
currentModule: LoadedModule,
|
|
96
|
+
parts: ChainPart[],
|
|
97
|
+
index: number,
|
|
98
|
+
): DefinitionResult | undefined {
|
|
99
|
+
if (index === 0) return undefined;
|
|
100
|
+
const first = parts[1].name;
|
|
101
|
+
|
|
102
|
+
const local = locateResource(moduleFiles(currentModule), first);
|
|
103
|
+
if (local) return index === 1 ? local : undefined;
|
|
104
|
+
|
|
105
|
+
const edge = graph.importEdges.get(currentModule.owner.source)?.get(first);
|
|
106
|
+
if (!edge) return undefined;
|
|
107
|
+
if (index === 1) return locateImport(currentModule, first);
|
|
108
|
+
if (index === 2) return resolveExportedResource(graph, edge.targetSource, parts[2].name);
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Resolve the CEL identifier under the cursor to where it is declared.
|
|
113
|
+
*
|
|
114
|
+
* `variables` / `secrets` / `ports` navigate to their block on the module doc,
|
|
115
|
+
* and their member to that block's entry. `resources` navigates through the
|
|
116
|
+
* same instance lookup a `!ref` uses. Anything else (a step result, a handler
|
|
117
|
+
* scope like `request`, a member of a resolved value) has no manifest
|
|
118
|
+
* declaration to jump to and resolves to nothing.
|
|
119
|
+
*
|
|
120
|
+
* A body the author is still writing may not parse; that means there is no
|
|
121
|
+
* chain to hit-test, not an error to surface from a navigation request — the
|
|
122
|
+
* analyzer reports the syntax error itself. Only that failure is tolerated: a
|
|
123
|
+
* defect in the CEL wrapper propagates rather than reading as "nothing to
|
|
124
|
+
* navigate to". */
|
|
125
|
+
export function resolveCelTarget(
|
|
126
|
+
graph: LoadedGraph,
|
|
127
|
+
currentModule: LoadedModule,
|
|
128
|
+
segment: CelSegment,
|
|
129
|
+
offset: number,
|
|
130
|
+
): DefinitionResult | undefined {
|
|
131
|
+
let ast: CelNode;
|
|
132
|
+
try {
|
|
133
|
+
ast = segment.ast();
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (!(error instanceof CelParseError)) throw error;
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const hit = chainAt(ast, offset);
|
|
140
|
+
if (!hit) return undefined;
|
|
141
|
+
const { parts, index } = hit;
|
|
142
|
+
const root = parts[0].name;
|
|
143
|
+
|
|
144
|
+
if (DECLARATION_SCOPES.has(root)) {
|
|
145
|
+
if (index === 0) return locateModuleDocKey(currentModule, root);
|
|
146
|
+
if (index === 1) return locateModuleDocKey(currentModule, `${root}.${parts[1].name}`);
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
if (root === "resources") return resolveResourceChain(graph, currentModule, parts, index);
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|