claude-db 0.11.1 → 0.12.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.
Files changed (170) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +5 -4
  3. package/dist/cli/commands/graph.js +104 -80
  4. package/dist/cli/commands/languages.d.ts +1 -0
  5. package/dist/cli/commands/languages.js +22 -0
  6. package/dist/cli/index.js +4 -0
  7. package/dist/cli/usage.js +3 -1
  8. package/dist/graph/ast.d.ts +4 -0
  9. package/dist/graph/ast.js +11 -0
  10. package/dist/graph/grammars.d.ts +14 -0
  11. package/dist/graph/grammars.js +58 -0
  12. package/dist/graph/index.d.ts +2 -2
  13. package/dist/graph/index.js +2 -2
  14. package/dist/graph/languages/basic.js +5 -1
  15. package/dist/graph/languages/ecmascript.js +7 -0
  16. package/dist/graph/languages/go.js +0 -1
  17. package/dist/graph/languages/index.d.ts +1 -1
  18. package/dist/graph/languages/index.js +34 -9
  19. package/dist/graph/languages/java.d.ts +2 -0
  20. package/dist/graph/languages/java.js +38 -0
  21. package/dist/graph/languages/kotlin.d.ts +2 -0
  22. package/dist/graph/languages/kotlin.js +19 -0
  23. package/dist/graph/languages/python.js +10 -2
  24. package/dist/graph/languages/rules.d.ts +11 -0
  25. package/dist/graph/languages/rust.js +2 -1
  26. package/dist/graph/modules/binding.d.ts +61 -0
  27. package/dist/graph/modules/binding.js +43 -0
  28. package/dist/graph/modules/ecmascript/bind.d.ts +4 -0
  29. package/dist/graph/modules/ecmascript/bind.js +17 -0
  30. package/dist/graph/modules/ecmascript/contexts.d.ts +6 -0
  31. package/dist/graph/modules/ecmascript/contexts.js +86 -0
  32. package/dist/graph/modules/ecmascript/exports.d.ts +9 -0
  33. package/dist/graph/modules/ecmascript/exports.js +91 -0
  34. package/dist/graph/modules/ecmascript/imports.d.ts +4 -0
  35. package/dist/graph/modules/ecmascript/imports.js +73 -0
  36. package/dist/graph/modules/ecmascript/package-resolver.d.ts +4 -0
  37. package/dist/graph/modules/ecmascript/package-resolver.js +73 -0
  38. package/dist/graph/modules/ecmascript/resolver.d.ts +2 -0
  39. package/dist/graph/modules/ecmascript/resolver.js +22 -0
  40. package/dist/graph/modules/ecmascript/specifiers.d.ts +3 -0
  41. package/dist/graph/modules/ecmascript/specifiers.js +15 -0
  42. package/dist/graph/modules/ecmascript/system.d.ts +2 -0
  43. package/dist/graph/modules/ecmascript/system.js +18 -0
  44. package/dist/graph/modules/ecmascript/workspace-resolver.d.ts +5 -0
  45. package/dist/graph/modules/ecmascript/workspace-resolver.js +38 -0
  46. package/dist/graph/modules/go/grammar.d.ts +2 -0
  47. package/dist/graph/modules/go/grammar.js +54 -0
  48. package/dist/graph/modules/go/imports.d.ts +4 -0
  49. package/dist/graph/modules/go/imports.js +32 -0
  50. package/dist/graph/modules/go/resolver.d.ts +2 -0
  51. package/dist/graph/modules/go/resolver.js +54 -0
  52. package/dist/graph/modules/go/system.d.ts +2 -0
  53. package/dist/graph/modules/go/system.js +16 -0
  54. package/dist/graph/modules/java/declarations.d.ts +9 -0
  55. package/dist/graph/modules/java/declarations.js +108 -0
  56. package/dist/graph/modules/java/grammar.d.ts +2 -0
  57. package/dist/graph/modules/java/grammar.js +135 -0
  58. package/dist/graph/modules/java/imports.d.ts +4 -0
  59. package/dist/graph/modules/java/imports.js +13 -0
  60. package/dist/graph/modules/java/receivers.d.ts +3 -0
  61. package/dist/graph/modules/java/receivers.js +139 -0
  62. package/dist/graph/modules/java/system.d.ts +1 -0
  63. package/dist/graph/modules/java/system.js +13 -0
  64. package/dist/graph/modules/jvm/linking.d.ts +20 -0
  65. package/dist/graph/modules/jvm/linking.js +64 -0
  66. package/dist/graph/modules/jvm/packages.d.ts +5 -0
  67. package/dist/graph/modules/jvm/packages.js +58 -0
  68. package/dist/graph/modules/jvm/receivers.d.ts +44 -0
  69. package/dist/graph/modules/jvm/receivers.js +77 -0
  70. package/dist/graph/modules/jvm/system.d.ts +18 -0
  71. package/dist/graph/modules/jvm/system.js +29 -0
  72. package/dist/graph/modules/jvm/types.d.ts +3 -0
  73. package/dist/graph/modules/jvm/types.js +11 -0
  74. package/dist/graph/modules/kotlin/declarations.d.ts +8 -0
  75. package/dist/graph/modules/kotlin/declarations.js +69 -0
  76. package/dist/graph/modules/kotlin/grammar.d.ts +2 -0
  77. package/dist/graph/modules/kotlin/grammar.js +78 -0
  78. package/dist/graph/modules/kotlin/imports.d.ts +4 -0
  79. package/dist/graph/modules/kotlin/imports.js +25 -0
  80. package/dist/graph/modules/kotlin/receivers.d.ts +3 -0
  81. package/dist/graph/modules/kotlin/receivers.js +105 -0
  82. package/dist/graph/modules/kotlin/references.d.ts +8 -0
  83. package/dist/graph/modules/kotlin/references.js +69 -0
  84. package/dist/graph/modules/kotlin/system.d.ts +1 -0
  85. package/dist/graph/modules/kotlin/system.js +15 -0
  86. package/dist/graph/modules/members.d.ts +17 -0
  87. package/dist/graph/modules/members.js +30 -0
  88. package/dist/graph/modules/module-index.d.ts +9 -0
  89. package/dist/graph/modules/module-index.js +62 -0
  90. package/dist/graph/modules/names.d.ts +2 -0
  91. package/dist/graph/modules/names.js +3 -0
  92. package/dist/graph/modules/python/grammar.d.ts +2 -0
  93. package/dist/graph/modules/python/grammar.js +100 -0
  94. package/dist/graph/modules/python/imports.d.ts +3 -0
  95. package/dist/graph/modules/python/imports.js +72 -0
  96. package/dist/graph/modules/python/resolver.d.ts +2 -0
  97. package/dist/graph/modules/python/resolver.js +78 -0
  98. package/dist/graph/modules/python/system.d.ts +2 -0
  99. package/dist/graph/modules/python/system.js +16 -0
  100. package/dist/graph/modules/rebind.d.ts +8 -0
  101. package/dist/graph/modules/rebind.js +51 -0
  102. package/dist/graph/modules/registry.d.ts +5 -0
  103. package/dist/graph/modules/registry.js +47 -0
  104. package/dist/graph/modules/rust/grammar.d.ts +2 -0
  105. package/dist/graph/modules/rust/grammar.js +94 -0
  106. package/dist/graph/modules/rust/imports.d.ts +3 -0
  107. package/dist/graph/modules/rust/imports.js +90 -0
  108. package/dist/graph/modules/rust/resolver.d.ts +3 -0
  109. package/dist/graph/modules/rust/resolver.js +153 -0
  110. package/dist/graph/modules/rust/system.d.ts +2 -0
  111. package/dist/graph/modules/rust/system.js +16 -0
  112. package/dist/graph/modules/shadowing.d.ts +17 -0
  113. package/dist/graph/modules/shadowing.js +65 -0
  114. package/dist/graph/modules/system.d.ts +39 -0
  115. package/dist/graph/modules/system.js +1 -0
  116. package/dist/graph/modules/uses.d.ts +14 -0
  117. package/dist/graph/modules/uses.js +62 -0
  118. package/dist/graph/parser.d.ts +9 -11
  119. package/dist/graph/parser.js +5 -4
  120. package/dist/graph/query/answer.d.ts +15 -0
  121. package/dist/graph/query/answer.js +22 -0
  122. package/dist/graph/query/format.d.ts +8 -1
  123. package/dist/graph/query/format.js +103 -30
  124. package/dist/graph/query/groups.d.ts +14 -0
  125. package/dist/graph/query/groups.js +51 -0
  126. package/dist/graph/query/index.d.ts +2 -1
  127. package/dist/graph/query/index.js +2 -1
  128. package/dist/graph/query/lookup.d.ts +2 -0
  129. package/dist/graph/query/lookup.js +19 -2
  130. package/dist/graph/query/refresh.js +17 -18
  131. package/dist/graph/query/stale.d.ts +2 -0
  132. package/dist/graph/query/stale.js +39 -0
  133. package/dist/graph/query/text.d.ts +12 -0
  134. package/dist/graph/query/text.js +26 -0
  135. package/dist/graph/scan/binder.d.ts +13 -0
  136. package/dist/graph/scan/binder.js +210 -0
  137. package/dist/graph/scan/cache.d.ts +11 -0
  138. package/dist/graph/scan/cache.js +140 -0
  139. package/dist/graph/scan/extract.d.ts +4 -17
  140. package/dist/graph/scan/extract.js +105 -83
  141. package/dist/graph/scan/files.d.ts +3 -9
  142. package/dist/graph/scan/files.js +3 -2
  143. package/dist/graph/scan/index.d.ts +8 -3
  144. package/dist/graph/scan/index.js +96 -36
  145. package/dist/graph/scan/job.d.ts +1 -0
  146. package/dist/graph/scan/job.js +1 -0
  147. package/dist/graph/scan/persist.d.ts +3 -0
  148. package/dist/graph/scan/persist.js +22 -0
  149. package/dist/graph/scan/receivers.d.ts +21 -0
  150. package/dist/graph/scan/receivers.js +87 -0
  151. package/dist/graph/scan/resolve.d.ts +8 -2
  152. package/dist/graph/scan/resolve.js +50 -56
  153. package/dist/graph/scan/spans.d.ts +13 -0
  154. package/dist/graph/scan/spans.js +42 -0
  155. package/dist/graph/symbols.d.ts +10 -0
  156. package/dist/graph/symbols.js +30 -0
  157. package/dist/graph/types.d.ts +56 -0
  158. package/dist/graph/types.js +1 -0
  159. package/dist/hooks/background.js +1 -16
  160. package/dist/hooks/detached.d.ts +3 -0
  161. package/dist/hooks/detached.js +16 -0
  162. package/dist/hooks/graph-refresh.d.ts +1 -0
  163. package/dist/hooks/graph-refresh.js +9 -0
  164. package/dist/hooks/prefer-usages.js +53 -12
  165. package/dist/hooks/session-start.js +8 -8
  166. package/dist/mcp/tools/usages.js +40 -31
  167. package/dist/types.d.ts +1 -1
  168. package/dist/usages/find.js +1 -0
  169. package/docs/how-it-works.md +10 -8
  170. package/package.json +16 -5
@@ -0,0 +1,64 @@
1
+ import { addNamed } from '../binding.js';
2
+ export const CLASS_LIKE = /^[A-Z]/;
3
+ export const here = (own, opens) => ({
4
+ specifier: own,
5
+ strict: true,
6
+ opens,
7
+ });
8
+ export function addImport(facts, path) {
9
+ const { wildcard, member: hasMember, alias, at, span } = path;
10
+ const segments = [...path.segments];
11
+ if (wildcard)
12
+ facts.opens.push(segments.join('.'));
13
+ const member = hasMember && !wildcard ? segments.pop() : undefined;
14
+ const first = segments.findIndex((segment) => CLASS_LIKE.test(segment));
15
+ if (wildcard && first < 0)
16
+ return;
17
+ const cut = first < 0 ? segments.length - 1 : first;
18
+ const specifier = segments.slice(0, cut).join('.');
19
+ const classes = segments.slice(cut);
20
+ const innermost = classes.at(-1);
21
+ if (!innermost)
22
+ return;
23
+ classes.forEach((name, index) => {
24
+ const via = index === 0 ? {} : { via: [classes[index - 1] ?? ''] };
25
+ const imported = { name, link: { specifier, ...via } };
26
+ facts.sites.push({ at, span, imported });
27
+ if (!hasMember && !wildcard && index === classes.length - 1) {
28
+ addNamed(facts, alias ?? name, imported);
29
+ }
30
+ });
31
+ if (!member)
32
+ return;
33
+ const imported = { name: member, link: { specifier, via: [innermost] } };
34
+ addNamed(facts, alias ?? member, imported);
35
+ facts.sites.push({ at, span, imported });
36
+ }
37
+ export function qualifiedTarget(own, chain, facts) {
38
+ const { segments, at } = chain;
39
+ const first = segments.findIndex((segment) => CLASS_LIKE.test(segment));
40
+ const name = segments[at];
41
+ if (!name || first < 0 || at < first)
42
+ return undefined;
43
+ const imported = first === 0 ? facts.named.get(segments[0] ?? '')?.[0] : undefined;
44
+ const specifier = imported?.link.specifier ?? (first === 0 ? own : segments.slice(0, first).join('.'));
45
+ if (at > first)
46
+ return { name, link: { specifier, via: [segments[at - 1] ?? ''], strict: true } };
47
+ if (imported)
48
+ return undefined;
49
+ return { name, link: first === 0 ? here(own, facts.opens) : { specifier, strict: true } };
50
+ }
51
+ export function referenceTarget(own, given, facts) {
52
+ const segments = given.name.split('.');
53
+ const first = segments.findIndex((segment) => CLASS_LIKE.test(segment));
54
+ if (first < 0)
55
+ return undefined;
56
+ const specifier = first === 0 ? own : segments.slice(0, first).join('.');
57
+ const owner = segments.slice(first).at(-2);
58
+ const constructs = given.relation === 'calls' && CLASS_LIKE.test(segments.at(-1) ?? '');
59
+ if (given.relation === 'calls' && !constructs) {
60
+ return owner ? { name: given.name, link: { specifier, via: [owner] } } : undefined;
61
+ }
62
+ const alone = first === 0 ? here(own, facts.opens) : { specifier, strict: true };
63
+ return { name: given.name, link: owner ? { specifier, via: [owner], strict: true } : alone };
64
+ }
@@ -0,0 +1,5 @@
1
+ import type { ModuleResolver } from '../system.js';
2
+ export declare const HEAD_SIZE = 16384;
3
+ export declare const JVM_EXTENSIONS: string[];
4
+ export declare function packageName(source: string): string;
5
+ export declare function jvmResolver(root: string, files: ReadonlySet<string>, extensions: readonly string[]): ModuleResolver;
@@ -0,0 +1,58 @@
1
+ import { closeSync, openSync, readSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ export const HEAD_SIZE = 16384;
4
+ export const JVM_EXTENSIONS = ['.java', '.kt', '.kts'];
5
+ const DEFAULT_PACKAGE = '(default)';
6
+ const BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
7
+ const LINE_COMMENT = /\/\/.*$/gm;
8
+ const PACKAGE_LINE = /\bpackage\s+([\w.]+)/;
9
+ function readHead(path) {
10
+ let handle;
11
+ try {
12
+ handle = openSync(path, 'r');
13
+ const buffer = Buffer.alloc(HEAD_SIZE);
14
+ const read = readSync(handle, buffer, 0, HEAD_SIZE, 0);
15
+ return buffer.toString('utf8', 0, read);
16
+ }
17
+ catch {
18
+ return '';
19
+ }
20
+ finally {
21
+ if (handle !== undefined)
22
+ closeSync(handle);
23
+ }
24
+ }
25
+ export function packageName(source) {
26
+ const code = source.replace(BLOCK_COMMENT, '').replace(LINE_COMMENT, '');
27
+ return PACKAGE_LINE.exec(code)?.[1] ?? DEFAULT_PACKAGE;
28
+ }
29
+ export function jvmResolver(root, files, extensions) {
30
+ const sources = [...files].filter((file) => extensions.some((ext) => file.endsWith(ext)));
31
+ const packages = new Map();
32
+ let declared;
33
+ const unit = (file) => {
34
+ const known = packages.get(file);
35
+ if (known !== undefined)
36
+ return known;
37
+ const name = packageName(readHead(join(root, file)));
38
+ packages.set(file, name);
39
+ return name;
40
+ };
41
+ const known = () => {
42
+ declared ??= new Set(sources.map(unit));
43
+ return declared;
44
+ };
45
+ return {
46
+ resolve(_from, specifier) {
47
+ const parts = specifier.split('.');
48
+ for (let count = parts.length; count > 0; count -= 1) {
49
+ const name = parts.slice(0, count).join('.');
50
+ if (known().has(name))
51
+ return name;
52
+ }
53
+ return known().has(specifier) ? specifier : null;
54
+ },
55
+ unit,
56
+ foreign: () => true,
57
+ };
58
+ }
@@ -0,0 +1,44 @@
1
+ import type { CodeSymbol } from '../../../types.js';
2
+ import type { AstNode } from '../../parser.js';
3
+ import type { Receiver, Reference } from '../../types.js';
4
+ import type { ImportFacts } from '../binding.js';
5
+ export declare const SELF: Receiver;
6
+ export declare const PARENT: Receiver;
7
+ export declare const OUTSIDE: Receiver;
8
+ export declare const UNKNOWN: Receiver;
9
+ export declare const TYPE_NAME: RegExp;
10
+ export interface ReceiverInput {
11
+ nodes: ReadonlyMap<string, AstNode[]>;
12
+ facts: ImportFacts;
13
+ own: string;
14
+ symbols: CodeSymbol[];
15
+ }
16
+ export interface Scoped {
17
+ name: string;
18
+ from: number;
19
+ to: number;
20
+ at: number;
21
+ field: boolean;
22
+ }
23
+ export type FindDeclared<T> = (name: string, position: number, fieldsOnly?: boolean) => T | undefined;
24
+ export declare function scopeLookup<T extends Scoped>(entries: Iterable<T>): FindDeclared<T>;
25
+ export declare function memoize<K, V>(compute: (key: K) => V): (key: K) => V;
26
+ export declare function typeParameterNames(nodes: ReadonlyMap<string, AstNode[]>): Set<string>;
27
+ export interface PathContext {
28
+ own: string;
29
+ facts: ImportFacts;
30
+ generics: ReadonlySet<string>;
31
+ exact: boolean;
32
+ }
33
+ export declare function pathReceivers(context: PathContext): (path: string[]) => Receiver | null;
34
+ export declare const resultOf: (of: string, exact: boolean) => Receiver;
35
+ export declare function symbolAt(symbols: CodeSymbol[]): (name: AstNode) => string | undefined;
36
+ export interface Typing {
37
+ receiverOf: (object: AstNode | null, position: number) => Receiver;
38
+ attach: (references: Reference[]) => Reference[];
39
+ }
40
+ export interface Typed {
41
+ calls?: ReadonlyMap<string, Receiver>;
42
+ returns: ReadonlyMap<string, Receiver>;
43
+ }
44
+ export declare function attachReceivers(references: Reference[], typed: Typed): Reference[];
@@ -0,0 +1,77 @@
1
+ import { typeRef } from './types.js';
2
+ export const SELF = { kind: 'self' };
3
+ export const PARENT = { kind: 'parent' };
4
+ export const OUTSIDE = { kind: 'outside' };
5
+ export const UNKNOWN = { kind: 'unknown' };
6
+ export const TYPE_NAME = /^[A-Z](?![A-Z0-9_]*$)/;
7
+ export function scopeLookup(entries) {
8
+ const byName = new Map();
9
+ for (const entry of entries) {
10
+ const known = byName.get(entry.name);
11
+ if (known)
12
+ known.push(entry);
13
+ else
14
+ byName.set(entry.name, [entry]);
15
+ }
16
+ return (name, position, fieldsOnly = false) => {
17
+ let best;
18
+ for (const entry of byName.get(name) ?? []) {
19
+ if (fieldsOnly && !entry.field)
20
+ continue;
21
+ if (position < entry.from || position >= entry.to || position < entry.at)
22
+ continue;
23
+ const width = entry.to - entry.from;
24
+ const narrower = !best || width < best.to - best.from;
25
+ const later = best !== undefined && width === best.to - best.from && entry.at > best.at;
26
+ if (narrower || later)
27
+ best = entry;
28
+ }
29
+ return best;
30
+ };
31
+ }
32
+ export function memoize(compute) {
33
+ const known = new Map();
34
+ return (key) => {
35
+ if (known.has(key))
36
+ return known.get(key);
37
+ const value = compute(key);
38
+ known.set(key, value);
39
+ return value;
40
+ };
41
+ }
42
+ export function typeParameterNames(nodes) {
43
+ const names = (nodes.get('type_parameter') ?? []).map((parameter) => parameter.children().find((child) => child.kind() === 'type_identifier'));
44
+ return new Set(names.flatMap((name) => (name ? [name.text()] : [])));
45
+ }
46
+ export function pathReceivers(context) {
47
+ const { own, facts, generics, exact } = context;
48
+ const byKey = memoize((key) => {
49
+ const path = key.split('.');
50
+ const [head] = path;
51
+ if (!head || (path.length === 1 && generics.has(head)))
52
+ return null;
53
+ const type = typeRef(path, own, facts);
54
+ if (!type)
55
+ return null;
56
+ return exact ? { kind: 'type', type, exact: true } : { kind: 'type', type };
57
+ });
58
+ return (path) => byKey(path.join('.'));
59
+ }
60
+ export const resultOf = (of, exact) => exact ? { kind: 'result', of, exact: true } : { kind: 'result', of };
61
+ export function symbolAt(symbols) {
62
+ const ids = new Map(symbols.map((symbol) => [`${symbol.name}:${symbol.line}`, symbol.id]));
63
+ return (name) => ids.get(`${name.text()}:${name.range().start.line + 1}`);
64
+ }
65
+ export function attachReceivers(references, typed) {
66
+ return references.map((reference) => {
67
+ const receiver = reference.relation === 'calls' && reference.origin !== undefined
68
+ ? typed.calls?.get(reference.origin)
69
+ : undefined;
70
+ if (receiver)
71
+ return { ...reference, receiver };
72
+ const returned = reference.relation === 'defines' && reference.to
73
+ ? typed.returns.get(reference.to)
74
+ : undefined;
75
+ return returned ? { ...reference, returns: returned } : reference;
76
+ });
77
+ }
@@ -0,0 +1,18 @@
1
+ import type { CodeSymbol } from '../../../types.js';
2
+ import type { AstNode } from '../../parser.js';
3
+ import type { Reference } from '../../types.js';
4
+ import type { Grammar, ImportFacts } from '../binding.js';
5
+ import type { ModuleSystem } from '../system.js';
6
+ import type { ReceiverInput, Typing } from './receivers.js';
7
+ type Nodes = ReadonlyMap<string, AstNode[]>;
8
+ export interface JvmLanguage {
9
+ label: string;
10
+ importKind: string;
11
+ readImport: (facts: ImportFacts, statement: AstNode) => void;
12
+ grammarFor: (own: string) => Grammar;
13
+ typing: (input: ReceiverInput) => Typing;
14
+ nodeKinds: readonly string[];
15
+ readReferences?: (nodes: Nodes, path: string, owner: (line: number) => CodeSymbol | null, typing: Typing) => Reference[];
16
+ }
17
+ export declare function jvmSystem(language: JvmLanguage): ModuleSystem;
18
+ export {};
@@ -0,0 +1,29 @@
1
+ import { bindModule, newFacts } from '../binding.js';
2
+ import { HEAD_SIZE, JVM_EXTENSIONS, jvmResolver, packageName } from './packages.js';
3
+ export function jvmSystem(language) {
4
+ return {
5
+ languages: [language.label],
6
+ read({ root, nodes, file, symbols, references, owner }) {
7
+ const facts = newFacts();
8
+ for (const statement of nodes.get(language.importKind) ?? []) {
9
+ language.readImport(facts, statement);
10
+ }
11
+ const own = packageName(file.source.slice(0, HEAD_SIZE));
12
+ const typing = language.typing({ nodes, facts, own, symbols });
13
+ const extra = language.readReferences?.(nodes, file.path, owner, typing) ?? [];
14
+ const input = {
15
+ root,
16
+ path: file.path,
17
+ references: [...references, ...extra],
18
+ symbols,
19
+ owner,
20
+ };
21
+ const bound = bindModule(input, facts, language.grammarFor(own));
22
+ return { added: [], references: typing.attach(bound) };
23
+ },
24
+ resolver: (root, files) => jvmResolver(root, files, JVM_EXTENSIONS),
25
+ isExternal: () => true,
26
+ importsNames: true,
27
+ nodeKinds: [language.importKind, ...language.nodeKinds],
28
+ };
29
+ }
@@ -0,0 +1,3 @@
1
+ import type { TypeRef } from '../../types.js';
2
+ import type { ImportFacts } from '../binding.js';
3
+ export declare function typeRef(segments: string[], own: string, facts: ImportFacts): TypeRef | undefined;
@@ -0,0 +1,11 @@
1
+ import { targetOf } from '../rebind.js';
2
+ import { here, qualifiedTarget } from './linking.js';
3
+ export function typeRef(segments, own, facts) {
4
+ const [first] = segments;
5
+ if (!first)
6
+ return undefined;
7
+ if (segments.length > 1)
8
+ return qualifiedTarget(own, { segments, at: segments.length - 1 }, facts);
9
+ const imported = facts.named.get(first)?.[0];
10
+ return imported ? targetOf(imported, first) : { name: first, link: here(own, facts.opens) };
11
+ }
@@ -0,0 +1,8 @@
1
+ import type { AstNode } from '../../parser.js';
2
+ import type { FindDeclared, Scoped } from '../jvm/receivers.js';
3
+ export interface Declared extends Scoped {
4
+ type: AstNode | null;
5
+ value: AstNode | null;
6
+ }
7
+ export declare const DECLARATION_KINDS: string[];
8
+ export declare function declarationsIn(nodes: ReadonlyMap<string, AstNode[]>): FindDeclared<Declared>;
@@ -0,0 +1,69 @@
1
+ import { scopeLookup } from '../jvm/receivers.js';
2
+ export const DECLARATION_KINDS = [
3
+ 'variable_declaration',
4
+ 'parameter',
5
+ 'class_parameter',
6
+ 'catch_block',
7
+ ];
8
+ const NAME = 'simple_identifier';
9
+ const TYPES = new Set(['user_type', 'nullable_type']);
10
+ const MEMBER_BODIES = new Set(['class_body', 'enum_class_body']);
11
+ const childOf = (node, kinds) => node.children().find((child) => kinds.has(child.kind())) ?? null;
12
+ const nameOf = (node) => node.children().find((child) => child.kind() === NAME) ?? null;
13
+ function valueOf(property) {
14
+ const parts = property.children();
15
+ const equals = parts.findIndex((part) => part.kind() === '=');
16
+ return equals < 0 ? null : (parts[equals + 1] ?? null);
17
+ }
18
+ const local = (scope) => ({
19
+ scope,
20
+ field: false,
21
+ afterDeclaration: false,
22
+ property: null,
23
+ });
24
+ function variablePlace(node) {
25
+ const holder = node.parent();
26
+ const kind = holder?.kind();
27
+ if (!holder)
28
+ return null;
29
+ if (kind === 'for_statement')
30
+ return local(holder);
31
+ if (kind === 'lambda_parameters') {
32
+ const lambda = holder.parent();
33
+ return lambda ? local(lambda) : null;
34
+ }
35
+ const scope = kind === 'property_declaration' ? holder.parent() : null;
36
+ if (!scope)
37
+ return null;
38
+ const field = MEMBER_BODIES.has(scope.kind());
39
+ return { scope, field, afterDeclaration: scope.kind() === 'statements', property: holder };
40
+ }
41
+ function placeOf(node) {
42
+ const kind = node.kind();
43
+ if (kind === 'variable_declaration')
44
+ return variablePlace(node);
45
+ if (kind === 'catch_block')
46
+ return local(node);
47
+ const scope = node.parent()?.parent();
48
+ if (!scope)
49
+ return null;
50
+ const field = kind === 'class_parameter' &&
51
+ node.children().some((child) => child.kind() === 'binding_pattern_kind');
52
+ return { scope, field, afterDeclaration: false, property: null };
53
+ }
54
+ function declaredBy(node) {
55
+ const name = nameOf(node);
56
+ const place = name ? placeOf(node) : null;
57
+ if (!name || !place)
58
+ return [];
59
+ const { start, end } = place.scope.range();
60
+ const at = place.afterDeclaration ? node.range().start.index : start.index;
61
+ const type = childOf(node, TYPES);
62
+ const value = !type && place.property ? valueOf(place.property) : null;
63
+ return [
64
+ { name: name.text(), from: start.index, to: end.index, at, field: place.field, type, value },
65
+ ];
66
+ }
67
+ export function declarationsIn(nodes) {
68
+ return scopeLookup(DECLARATION_KINDS.flatMap((kind) => nodes.get(kind) ?? []).flatMap(declaredBy));
69
+ }
@@ -0,0 +1,2 @@
1
+ import type { Grammar } from '../binding.js';
2
+ export declare function grammarFor(own: string): Grammar;
@@ -0,0 +1,78 @@
1
+ import { sameNode } from '../../ast.js';
2
+ import { CLASS_LIKE, here, qualifiedTarget, referenceTarget } from '../jvm/linking.js';
3
+ import { memberAccess } from '../members.js';
4
+ const NAME = 'simple_identifier';
5
+ const TYPE = 'type_identifier';
6
+ const members = memberAccess({
7
+ member: 'navigation_expression',
8
+ object: (member) => member.children()[0] ?? null,
9
+ name: (member) => member
10
+ .children()
11
+ .at(-1)
12
+ ?.children()
13
+ .find((child) => child.kind() === NAME)
14
+ ?.text() ?? '',
15
+ call: 'call_expression',
16
+ callee: (call) => call.children()[0] ?? null,
17
+ });
18
+ const NAMING_PARENTS = new Set([
19
+ 'class_declaration',
20
+ 'object_declaration',
21
+ 'type_alias',
22
+ 'type_parameter',
23
+ 'function_declaration',
24
+ 'parameter',
25
+ 'class_parameter',
26
+ 'variable_declaration',
27
+ 'enum_entry',
28
+ 'import_alias',
29
+ 'navigation_suffix',
30
+ 'identifier',
31
+ 'catch_block',
32
+ 'package_header',
33
+ 'import_header',
34
+ ]);
35
+ function isHeritage(parent) {
36
+ if (parent.kind() !== 'user_type')
37
+ return false;
38
+ const holder = parent.parent();
39
+ const specifier = holder?.kind() === 'constructor_invocation' ? holder.parent() : holder;
40
+ return specifier?.kind() === 'delegation_specifier';
41
+ }
42
+ const isUse = (node, parent) => !NAMING_PARENTS.has(parent.kind()) &&
43
+ !isHeritage(parent) &&
44
+ !(parent.kind() === 'call_expression' && sameNode(parent.children()[0] ?? null, node));
45
+ function qualifiedChain(node, parent) {
46
+ if (parent.kind() !== 'user_type')
47
+ return undefined;
48
+ const parts = parent.children().filter((child) => child.kind() === TYPE);
49
+ const at = parts.findIndex((part) => sameNode(part, node));
50
+ return at < 0 ? undefined : { segments: parts.map((part) => part.text()), at };
51
+ }
52
+ export function grammarFor(own) {
53
+ const target = (node, parent, facts) => {
54
+ const text = node.text();
55
+ if (node.kind() === NAME && !CLASS_LIKE.test(text))
56
+ return undefined;
57
+ if (!isUse(node, parent))
58
+ return undefined;
59
+ const chain = qualifiedChain(node, parent);
60
+ if (chain && chain.segments.length > 1)
61
+ return qualifiedTarget(own, chain, facts);
62
+ return { name: text, link: here(own, facts.opens) };
63
+ };
64
+ const reference = (given, facts) => referenceTarget(own, given, facts);
65
+ return {
66
+ kinds: [NAME, TYPE],
67
+ isUse,
68
+ isLocalUse: isUse,
69
+ isMemberObject: members.isMemberObject,
70
+ memberChain: members.memberChain,
71
+ unbound: {
72
+ rules: [{ kind: TYPE }, { kind: NAME, regex: '^[A-Z]' }],
73
+ claims: (node, parent) => (qualifiedChain(node, parent)?.at ?? 0) > 0,
74
+ target,
75
+ reference,
76
+ },
77
+ };
78
+ }
@@ -0,0 +1,4 @@
1
+ import type { AstNode } from '../../parser.js';
2
+ import type { ImportFacts } from '../binding.js';
3
+ export declare const IMPORT_KIND = "import_header";
4
+ export declare function readImport(facts: ImportFacts, header: AstNode): void;
@@ -0,0 +1,25 @@
1
+ import { addImport, CLASS_LIKE } from '../jvm/linking.js';
2
+ export const IMPORT_KIND = 'import_header';
3
+ export function readImport(facts, header) {
4
+ const parts = header.children();
5
+ const at = parts.find((child) => child.kind() === 'identifier');
6
+ if (!at)
7
+ return;
8
+ const segments = at.text().replace(/\s+/g, '').split('.');
9
+ const wildcard = parts.some((child) => child.kind() === 'wildcard_import');
10
+ const alias = parts
11
+ .find((child) => child.kind() === 'import_alias')
12
+ ?.children()
13
+ .find((child) => child.kind() === 'type_identifier')
14
+ ?.text();
15
+ const insideClass = segments.some((segment) => CLASS_LIKE.test(segment));
16
+ const member = !wildcard && insideClass && !CLASS_LIKE.test(segments.at(-1) ?? '');
17
+ addImport(facts, {
18
+ segments,
19
+ at,
20
+ span: header,
21
+ wildcard,
22
+ member,
23
+ ...(alias ? { alias } : {}),
24
+ });
25
+ }
@@ -0,0 +1,3 @@
1
+ import type { ReceiverInput, Typing } from '../jvm/receivers.js';
2
+ export declare const RECEIVER_KINDS: string[];
3
+ export declare function kotlinTyping(input: ReceiverInput): Typing;
@@ -0,0 +1,105 @@
1
+ import { originOf } from '../../ast.js';
2
+ import { PARENT, SELF, TYPE_NAME, UNKNOWN, attachReceivers, memoize, pathReceivers, resultOf, symbolAt, typeParameterNames, } from '../jvm/receivers.js';
3
+ import { DECLARATION_KINDS, declarationsIn } from './declarations.js';
4
+ const EXACT = false;
5
+ const NAME = 'simple_identifier';
6
+ const TYPE = 'type_identifier';
7
+ const CALL = 'call_expression';
8
+ const NAVIGATION = 'navigation_expression';
9
+ const RETURN_TYPES = new Set(['user_type', 'nullable_type']);
10
+ export const RECEIVER_KINDS = [...DECLARATION_KINDS, 'function_declaration', 'type_parameter'];
11
+ function pathOf(node) {
12
+ if (!node)
13
+ return [];
14
+ if (node.kind() === 'nullable_type')
15
+ return pathOf(node.children()[0]);
16
+ if (node.kind() !== 'user_type')
17
+ return [];
18
+ return node
19
+ .children()
20
+ .filter((child) => child.kind() === TYPE)
21
+ .map((child) => child.text());
22
+ }
23
+ function dottedPath(node) {
24
+ if (node.kind() === NAME)
25
+ return [node.text()];
26
+ if (node.kind() !== NAVIGATION)
27
+ return null;
28
+ const [owner, suffix] = node.children();
29
+ const name = suffix?.children().find((child) => child.kind() === NAME);
30
+ const head = owner ? dottedPath(owner) : null;
31
+ return head && name ? [...head, name.text()] : null;
32
+ }
33
+ function returnType(parts, kinds) {
34
+ const after = kinds.indexOf('function_value_parameters');
35
+ const at = kinds.findIndex((kind, index) => index > after && RETURN_TYPES.has(kind));
36
+ return at < 0 ? null : (parts[at] ?? null);
37
+ }
38
+ export function kotlinTyping(input) {
39
+ const { nodes, facts, own, symbols } = input;
40
+ const find = declarationsIn(nodes);
41
+ const generics = typeParameterNames(nodes);
42
+ const ofPath = pathReceivers({ own, facts, generics, exact: EXACT });
43
+ const ofCall = (call) => {
44
+ const callee = call.children()[0];
45
+ const constructed = callee?.kind() === NAME && TYPE_NAME.test(callee.text());
46
+ return (constructed ? ofPath([callee.text()]) : null) ?? resultOf(originOf(call), EXACT);
47
+ };
48
+ const ofDeclared = memoize((declared) => {
49
+ if (declared.type)
50
+ return ofPath(pathOf(declared.type));
51
+ return declared.value?.kind() === CALL ? ofCall(declared.value) : null;
52
+ });
53
+ const ofNavigation = (navigation, position) => {
54
+ const [owner, suffix] = navigation.children();
55
+ const name = suffix?.children().find((child) => child.kind() === NAME);
56
+ if (owner?.kind() === 'this_expression' && name) {
57
+ const declared = find(name.text(), position, true);
58
+ return declared ? ofDeclared(declared) : null;
59
+ }
60
+ const path = dottedPath(navigation);
61
+ return path && TYPE_NAME.test(path.at(-1) ?? '') ? ofPath(path) : null;
62
+ };
63
+ const ofObject = (object, position) => {
64
+ const kind = object.kind();
65
+ if (kind === 'this_expression') {
66
+ const outer = object.children().find((child) => child.kind() === TYPE);
67
+ return outer ? ofPath([outer.text()]) : SELF;
68
+ }
69
+ if (kind === 'super_expression')
70
+ return PARENT;
71
+ if (kind === CALL)
72
+ return ofCall(object);
73
+ if (kind === 'as_expression')
74
+ return ofPath(pathOf(object.children().at(-1)));
75
+ if (kind === 'parenthesized_expression') {
76
+ const inner = object.children().find((child) => !['(', ')'].includes(child.kind()));
77
+ return inner ? ofObject(inner, position) : null;
78
+ }
79
+ if (kind === NAVIGATION)
80
+ return ofNavigation(object, position);
81
+ if (kind !== NAME)
82
+ return null;
83
+ const name = object.text();
84
+ const declared = find(name, position);
85
+ if (declared)
86
+ return ofDeclared(declared);
87
+ return TYPE_NAME.test(name) ? ofPath([name]) : null;
88
+ };
89
+ const receiverOf = (object, position) => object ? (ofObject(object, position) ?? UNKNOWN) : SELF;
90
+ const attach = (references) => {
91
+ const idOf = symbolAt(symbols);
92
+ const returns = new Map();
93
+ for (const declaration of nodes.get('function_declaration') ?? []) {
94
+ const parts = declaration.children();
95
+ const kinds = parts.map((part) => part.kind());
96
+ const name = parts[kinds.indexOf(NAME)];
97
+ const id = name ? idOf(name) : undefined;
98
+ const receiver = ofPath(pathOf(returnType(parts, kinds)));
99
+ if (id && receiver)
100
+ returns.set(id, receiver);
101
+ }
102
+ return attachReceivers(references, { returns });
103
+ };
104
+ return { receiverOf, attach };
105
+ }
@@ -0,0 +1,8 @@
1
+ import type { AstNode } from '../../parser.js';
2
+ import type { CodeSymbol } from '../../../types.js';
3
+ import type { Reference } from '../../types.js';
4
+ import type { Typing } from '../jvm/receivers.js';
5
+ type Owner = (line: number) => CodeSymbol | null;
6
+ export declare const REFERENCE_KINDS: string[];
7
+ export declare function readReferences(nodes: ReadonlyMap<string, AstNode[]>, path: string, owner: Owner, typing: Typing): Reference[];
8
+ export {};