hinmil 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +2306 -2
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +338 -0
- package/dist/cli.js.map +1 -0
- package/dist/commentParser.d.ts +384 -0
- package/dist/commentParser.d.ts.map +1 -0
- package/dist/commentParser.js +2487 -0
- package/dist/commentParser.js.map +1 -0
- package/dist/config.d.ts +203 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +375 -0
- package/dist/config.js.map +1 -0
- package/dist/debugLog.d.ts +13 -0
- package/dist/debugLog.d.ts.map +1 -0
- package/dist/debugLog.js +44 -0
- package/dist/debugLog.js.map +1 -0
- package/dist/estree.d.ts +100 -0
- package/dist/estree.d.ts.map +1 -0
- package/dist/estree.js +34 -0
- package/dist/estree.js.map +1 -0
- package/dist/hm/env.d.ts +134 -0
- package/dist/hm/env.d.ts.map +1 -0
- package/dist/hm/env.js +939 -0
- package/dist/hm/env.js.map +1 -0
- package/dist/hm/index.d.ts +7 -0
- package/dist/hm/index.d.ts.map +1 -0
- package/dist/hm/index.js +26 -0
- package/dist/hm/index.js.map +1 -0
- package/dist/hm/substitution.d.ts +57 -0
- package/dist/hm/substitution.d.ts.map +1 -0
- package/dist/hm/substitution.js +546 -0
- package/dist/hm/substitution.js.map +1 -0
- package/dist/hm/typeParser.d.ts +39 -0
- package/dist/hm/typeParser.d.ts.map +1 -0
- package/dist/hm/typeParser.js +1285 -0
- package/dist/hm/typeParser.js.map +1 -0
- package/dist/hm/typeUtils.d.ts +248 -0
- package/dist/hm/typeUtils.d.ts.map +1 -0
- package/dist/hm/typeUtils.js +905 -0
- package/dist/hm/typeUtils.js.map +1 -0
- package/dist/hm/types.d.ts +738 -0
- package/dist/hm/types.d.ts.map +1 -0
- package/dist/hm/types.js +1853 -0
- package/dist/hm/types.js.map +1 -0
- package/dist/hm/unify.d.ts +59 -0
- package/dist/hm/unify.d.ts.map +1 -0
- package/dist/hm/unify.js +1700 -0
- package/dist/hm/unify.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +196 -0
- package/dist/index.js.map +1 -0
- package/dist/infer/annotation-rigid.d.ts +76 -0
- package/dist/infer/annotation-rigid.d.ts.map +1 -0
- package/dist/infer/annotation-rigid.js +542 -0
- package/dist/infer/annotation-rigid.js.map +1 -0
- package/dist/infer/annotation-validation.d.ts +109 -0
- package/dist/infer/annotation-validation.d.ts.map +1 -0
- package/dist/infer/annotation-validation.js +1078 -0
- package/dist/infer/annotation-validation.js.map +1 -0
- package/dist/infer/annotations.d.ts +277 -0
- package/dist/infer/annotations.d.ts.map +1 -0
- package/dist/infer/annotations.js +1343 -0
- package/dist/infer/annotations.js.map +1 -0
- package/dist/infer/call-overloads.d.ts +24 -0
- package/dist/infer/call-overloads.d.ts.map +1 -0
- package/dist/infer/call-overloads.js +900 -0
- package/dist/infer/call-overloads.js.map +1 -0
- package/dist/infer/call-rows.d.ts +154 -0
- package/dist/infer/call-rows.d.ts.map +1 -0
- package/dist/infer/call-rows.js +2162 -0
- package/dist/infer/call-rows.js.map +1 -0
- package/dist/infer/calls.d.ts +20 -0
- package/dist/infer/calls.d.ts.map +1 -0
- package/dist/infer/calls.js +1468 -0
- package/dist/infer/calls.js.map +1 -0
- package/dist/infer/constraints.d.ts +99 -0
- package/dist/infer/constraints.d.ts.map +1 -0
- package/dist/infer/constraints.js +1208 -0
- package/dist/infer/constraints.js.map +1 -0
- package/dist/infer/expr-assignment.d.ts +20 -0
- package/dist/infer/expr-assignment.d.ts.map +1 -0
- package/dist/infer/expr-assignment.js +620 -0
- package/dist/infer/expr-assignment.js.map +1 -0
- package/dist/infer/expr-constraint-lookup.d.ts +44 -0
- package/dist/infer/expr-constraint-lookup.d.ts.map +1 -0
- package/dist/infer/expr-constraint-lookup.js +207 -0
- package/dist/infer/expr-constraint-lookup.js.map +1 -0
- package/dist/infer/expr-functions.d.ts +20 -0
- package/dist/infer/expr-functions.d.ts.map +1 -0
- package/dist/infer/expr-functions.js +1359 -0
- package/dist/infer/expr-functions.js.map +1 -0
- package/dist/infer/expr-jsx.d.ts +19 -0
- package/dist/infer/expr-jsx.d.ts.map +1 -0
- package/dist/infer/expr-jsx.js +310 -0
- package/dist/infer/expr-jsx.js.map +1 -0
- package/dist/infer/expr-member.d.ts +40 -0
- package/dist/infer/expr-member.d.ts.map +1 -0
- package/dist/infer/expr-member.js +1154 -0
- package/dist/infer/expr-member.js.map +1 -0
- package/dist/infer/expr-prototype.d.ts +11 -0
- package/dist/infer/expr-prototype.d.ts.map +1 -0
- package/dist/infer/expr-prototype.js +202 -0
- package/dist/infer/expr-prototype.js.map +1 -0
- package/dist/infer/expressions.d.ts +40 -0
- package/dist/infer/expressions.d.ts.map +1 -0
- package/dist/infer/expressions.js +1497 -0
- package/dist/infer/expressions.js.map +1 -0
- package/dist/infer/functionPatterns.d.ts +229 -0
- package/dist/infer/functionPatterns.d.ts.map +1 -0
- package/dist/infer/functionPatterns.js +1035 -0
- package/dist/infer/functionPatterns.js.map +1 -0
- package/dist/infer/helpers.d.ts +73 -0
- package/dist/infer/helpers.d.ts.map +1 -0
- package/dist/infer/helpers.js +187 -0
- package/dist/infer/helpers.js.map +1 -0
- package/dist/infer/index.d.ts +551 -0
- package/dist/infer/index.d.ts.map +1 -0
- package/dist/infer/index.js +1127 -0
- package/dist/infer/index.js.map +1 -0
- package/dist/infer/jsx-dom.d.ts +26 -0
- package/dist/infer/jsx-dom.d.ts.map +1 -0
- package/dist/infer/jsx-dom.js +303 -0
- package/dist/infer/jsx-dom.js.map +1 -0
- package/dist/infer/moduleReps.d.ts +7 -0
- package/dist/infer/moduleReps.d.ts.map +1 -0
- package/dist/infer/moduleReps.js +48 -0
- package/dist/infer/moduleReps.js.map +1 -0
- package/dist/infer/modules.d.ts +10 -0
- package/dist/infer/modules.d.ts.map +1 -0
- package/dist/infer/modules.js +775 -0
- package/dist/infer/modules.js.map +1 -0
- package/dist/infer/narrowing.d.ts +199 -0
- package/dist/infer/narrowing.d.ts.map +1 -0
- package/dist/infer/narrowing.js +1428 -0
- package/dist/infer/narrowing.js.map +1 -0
- package/dist/infer/results.d.ts +24 -0
- package/dist/infer/results.d.ts.map +1 -0
- package/dist/infer/results.js +611 -0
- package/dist/infer/results.js.map +1 -0
- package/dist/infer/setupPass.d.ts +11 -0
- package/dist/infer/setupPass.d.ts.map +1 -0
- package/dist/infer/setupPass.js +524 -0
- package/dist/infer/setupPass.js.map +1 -0
- package/dist/infer/statements.d.ts +26 -0
- package/dist/infer/statements.d.ts.map +1 -0
- package/dist/infer/statements.js +750 -0
- package/dist/infer/statements.js.map +1 -0
- package/dist/infer/stmt-bindings.d.ts +22 -0
- package/dist/infer/stmt-bindings.d.ts.map +1 -0
- package/dist/infer/stmt-bindings.js +1482 -0
- package/dist/infer/stmt-bindings.js.map +1 -0
- package/dist/infer/stmt-destructure.d.ts +29 -0
- package/dist/infer/stmt-destructure.d.ts.map +1 -0
- package/dist/infer/stmt-destructure.js +490 -0
- package/dist/infer/stmt-destructure.js.map +1 -0
- package/dist/infer/types.d.ts +220 -0
- package/dist/infer/types.d.ts.map +1 -0
- package/dist/infer/types.js +46 -0
- package/dist/infer/types.js.map +1 -0
- package/dist/infer/unifyDiag.d.ts +46 -0
- package/dist/infer/unifyDiag.d.ts.map +1 -0
- package/dist/infer/unifyDiag.js +642 -0
- package/dist/infer/unifyDiag.js.map +1 -0
- package/dist/parsing-babel.d.ts +3 -0
- package/dist/parsing-babel.d.ts.map +1 -0
- package/dist/parsing-babel.js +353 -0
- package/dist/parsing-babel.js.map +1 -0
- package/dist/parsing.d.ts +69 -0
- package/dist/parsing.d.ts.map +1 -0
- package/dist/parsing.js +288 -0
- package/dist/parsing.js.map +1 -0
- package/dist/registries/constraintRegistry.d.ts +233 -0
- package/dist/registries/constraintRegistry.d.ts.map +1 -0
- package/dist/registries/constraintRegistry.js +803 -0
- package/dist/registries/constraintRegistry.js.map +1 -0
- package/dist/registries/dataTypeRegistry.d.ts +63 -0
- package/dist/registries/dataTypeRegistry.d.ts.map +1 -0
- package/dist/registries/dataTypeRegistry.js +180 -0
- package/dist/registries/dataTypeRegistry.js.map +1 -0
- package/dist/registries/domGlobals.d.ts +33 -0
- package/dist/registries/domGlobals.d.ts.map +1 -0
- package/dist/registries/domGlobals.js +309 -0
- package/dist/registries/domGlobals.js.map +1 -0
- package/dist/registries/methodRegistry.d.ts +277 -0
- package/dist/registries/methodRegistry.d.ts.map +1 -0
- package/dist/registries/methodRegistry.js +2074 -0
- package/dist/registries/methodRegistry.js.map +1 -0
- package/dist/registries/session.d.ts +38 -0
- package/dist/registries/session.d.ts.map +1 -0
- package/dist/registries/session.js +75 -0
- package/dist/registries/session.js.map +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1896 -0
- package/dist/server.js.map +1 -0
- package/dist/services/codeActions.d.ts +22 -0
- package/dist/services/codeActions.d.ts.map +1 -0
- package/dist/services/codeActions.js +465 -0
- package/dist/services/codeActions.js.map +1 -0
- package/dist/services/completion.d.ts +259 -0
- package/dist/services/completion.d.ts.map +1 -0
- package/dist/services/completion.js +2794 -0
- package/dist/services/completion.js.map +1 -0
- package/dist/services/definitions.d.ts +37 -0
- package/dist/services/definitions.d.ts.map +1 -0
- package/dist/services/definitions.js +605 -0
- package/dist/services/definitions.js.map +1 -0
- package/dist/services/diagnosticCodes.d.ts +70 -0
- package/dist/services/diagnosticCodes.d.ts.map +1 -0
- package/dist/services/diagnosticCodes.js +239 -0
- package/dist/services/diagnosticCodes.js.map +1 -0
- package/dist/services/diagnostics.d.ts +86 -0
- package/dist/services/diagnostics.d.ts.map +1 -0
- package/dist/services/diagnostics.js +665 -0
- package/dist/services/diagnostics.js.map +1 -0
- package/dist/services/documentCache.d.ts +88 -0
- package/dist/services/documentCache.d.ts.map +1 -0
- package/dist/services/documentCache.js +108 -0
- package/dist/services/documentCache.js.map +1 -0
- package/dist/services/documentSymbols.d.ts +20 -0
- package/dist/services/documentSymbols.d.ts.map +1 -0
- package/dist/services/documentSymbols.js +381 -0
- package/dist/services/documentSymbols.js.map +1 -0
- package/dist/services/fileSelection.d.ts +24 -0
- package/dist/services/fileSelection.d.ts.map +1 -0
- package/dist/services/fileSelection.js +154 -0
- package/dist/services/fileSelection.js.map +1 -0
- package/dist/services/fileSelectionRefresh.d.ts +26 -0
- package/dist/services/fileSelectionRefresh.d.ts.map +1 -0
- package/dist/services/fileSelectionRefresh.js +77 -0
- package/dist/services/fileSelectionRefresh.js.map +1 -0
- package/dist/services/files.d.ts +342 -0
- package/dist/services/files.d.ts.map +1 -0
- package/dist/services/files.js +1712 -0
- package/dist/services/files.js.map +1 -0
- package/dist/services/fsProbe.d.ts +14 -0
- package/dist/services/fsProbe.d.ts.map +1 -0
- package/dist/services/fsProbe.js +112 -0
- package/dist/services/fsProbe.js.map +1 -0
- package/dist/services/globalsLoader.d.ts +20 -0
- package/dist/services/globalsLoader.d.ts.map +1 -0
- package/dist/services/globalsLoader.js +122 -0
- package/dist/services/globalsLoader.js.map +1 -0
- package/dist/services/hmFileServices.d.ts +113 -0
- package/dist/services/hmFileServices.d.ts.map +1 -0
- package/dist/services/hmFileServices.js +686 -0
- package/dist/services/hmFileServices.js.map +1 -0
- package/dist/services/hover.d.ts +280 -0
- package/dist/services/hover.d.ts.map +1 -0
- package/dist/services/hover.js +1687 -0
- package/dist/services/hover.js.map +1 -0
- package/dist/services/importContext.d.ts +396 -0
- package/dist/services/importContext.d.ts.map +1 -0
- package/dist/services/importContext.js +1999 -0
- package/dist/services/importContext.js.map +1 -0
- package/dist/services/projectChecker.d.ts +50 -0
- package/dist/services/projectChecker.d.ts.map +1 -0
- package/dist/services/projectChecker.js +228 -0
- package/dist/services/projectChecker.js.map +1 -0
- package/dist/services/references.d.ts +41 -0
- package/dist/services/references.d.ts.map +1 -0
- package/dist/services/references.js +757 -0
- package/dist/services/references.js.map +1 -0
- package/dist/services/rename.d.ts +22 -0
- package/dist/services/rename.d.ts.map +1 -0
- package/dist/services/rename.js +101 -0
- package/dist/services/rename.js.map +1 -0
- package/dist/services/shortCircuit.d.ts +27 -0
- package/dist/services/shortCircuit.d.ts.map +1 -0
- package/dist/services/shortCircuit.js +28 -0
- package/dist/services/shortCircuit.js.map +1 -0
- package/dist/services/sidecarPerf.d.ts +19 -0
- package/dist/services/sidecarPerf.d.ts.map +1 -0
- package/dist/services/sidecarPerf.js +59 -0
- package/dist/services/sidecarPerf.js.map +1 -0
- package/dist/services/signatureHelp.d.ts +27 -0
- package/dist/services/signatureHelp.d.ts.map +1 -0
- package/dist/services/signatureHelp.js +984 -0
- package/dist/services/signatureHelp.js.map +1 -0
- package/dist/walking.d.ts +25 -0
- package/dist/walking.d.ts.map +1 -0
- package/dist/walking.js +107 -0
- package/dist/walking.js.map +1 -0
- package/hm-types-builtin/assert.hm +20 -0
- package/hm-types-builtin/buffer.hm +8 -0
- package/hm-types-builtin/child_process.hm +16 -0
- package/hm-types-builtin/crypto.hm +17 -0
- package/hm-types-builtin/fs.hm +15 -0
- package/hm-types-builtin/http.hm +14 -0
- package/hm-types-builtin/os.hm +19 -0
- package/hm-types-builtin/path.hm +18 -0
- package/hm-types-builtin/react-dom.hm +27 -0
- package/hm-types-builtin/react.hm +113 -0
- package/hm-types-builtin/url.hm +7 -0
- package/hm-types-builtin/zlib.hm +12 -0
- package/package.json +53 -4
- package/schemas/hinmil.config.schema.json +261 -0
- package/schemas/package-hinmil.schema.json +59 -0
|
@@ -0,0 +1,984 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Signature Help — show parameter info when typing function arguments
|
|
4
|
+
//
|
|
5
|
+
// Plain language: the little hint that appears while typing a function call
|
|
6
|
+
// (`f(…`), showing the parameters and highlighting the one being typed.
|
|
7
|
+
// This file works out which call the cursor is inside, fetches the callee's
|
|
8
|
+
// signature (picking the right overload where applicable), and marks the
|
|
9
|
+
// active parameter — including for curried calls like `f(a)(b)`.
|
|
10
|
+
// ============================================================================
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.provideSignatureHelp = provideSignatureHelp;
|
|
13
|
+
const index_1 = require("../index");
|
|
14
|
+
const types_1 = require("../hm/types");
|
|
15
|
+
const env_1 = require("../hm/env");
|
|
16
|
+
const typeUtils_1 = require("../hm/typeUtils");
|
|
17
|
+
const types_2 = require("../hm/types");
|
|
18
|
+
const unify_1 = require("../hm/unify");
|
|
19
|
+
const substitution_1 = require("../hm/substitution");
|
|
20
|
+
const files_1 = require("./files");
|
|
21
|
+
/**
|
|
22
|
+
* Walk backwards from `offset` to find the enclosing `(`, then compute the
|
|
23
|
+
* active parameter index by a FORWARD scan from just after that `(` up to the
|
|
24
|
+
* cursor, counting only depth-0 commas.
|
|
25
|
+
*
|
|
26
|
+
* The forward scan is essential: a backward comma count is wrong when the
|
|
27
|
+
* cursor sits inside an *unclosed* nested literal (e.g. `f({ a: 1, b: 2`),
|
|
28
|
+
* because going backwards the commas inside the `{ … }` are seen at depth 0
|
|
29
|
+
* (the opening brace has not been matched yet) and wrongly advance the active
|
|
30
|
+
* parameter. Scanning forward, the nested `{`/`[`/`(` raise the depth first,
|
|
31
|
+
* so their inner commas are correctly ignored.
|
|
32
|
+
* Returns the offset of '(' or -1 if not found.
|
|
33
|
+
*/
|
|
34
|
+
function findOpenParen(source, offset) {
|
|
35
|
+
let depth = 0;
|
|
36
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
37
|
+
const ch = source[i];
|
|
38
|
+
if (ch === ')' || ch === ']' || ch === '}') {
|
|
39
|
+
depth++;
|
|
40
|
+
}
|
|
41
|
+
else if (ch === '(' || ch === '[' || ch === '{') {
|
|
42
|
+
if (depth > 0) {
|
|
43
|
+
depth--;
|
|
44
|
+
}
|
|
45
|
+
else if (ch === '(') {
|
|
46
|
+
// A depth-0 '(' is only an argument list if it belongs to a CALL —
|
|
47
|
+
// i.e. it is immediately preceded by a callee expression. When the
|
|
48
|
+
// user starts building a callback argument (e.g. `sort((` while typing
|
|
49
|
+
// `sort((a, b) => …)`) the innermost '(' is a *grouping* / arrow
|
|
50
|
+
// parameter paren with no callee before it. Skip past it and keep
|
|
51
|
+
// scanning outward for the enclosing call's '(' so the popup keeps
|
|
52
|
+
// showing that call's signature instead of vanishing.
|
|
53
|
+
if (isCallParen(source, i)) {
|
|
54
|
+
return { parenOffset: i, activeParam: countActiveParam(source, i, offset) };
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Unmatched '[' or '{' at depth 0 — the user is typing inside
|
|
60
|
+
// a nested array/object literal that is an argument. Keep searching
|
|
61
|
+
// backwards to find the enclosing '('.
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { parenOffset: -1, activeParam: 0 };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Is the '(' at `parenOffset` a *call* argument list (as opposed to a grouping
|
|
70
|
+
* or arrow-parameter paren)? A call paren is immediately preceded — ignoring
|
|
71
|
+
* whitespace — by a callee expression, which ends in an identifier character,
|
|
72
|
+
* a ')' (chained/curried call) or a ']' (computed member access). Grouping and
|
|
73
|
+
* arrow-parameter parens are preceded by '(', ',', operators, '=>', '{', '[',
|
|
74
|
+
* ':', ';' or the start of input.
|
|
75
|
+
*/
|
|
76
|
+
function isCallParen(source, parenOffset) {
|
|
77
|
+
let i = parenOffset - 1;
|
|
78
|
+
while (i >= 0 && /\s/.test(source[i]))
|
|
79
|
+
i--;
|
|
80
|
+
if (i < 0)
|
|
81
|
+
return false;
|
|
82
|
+
return /[A-Za-z0-9_$)\]]/.test(source[i]);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Offset of the INNERMOST depth-0 open '(' enclosing the cursor — whether it is
|
|
86
|
+
* a call argument list OR a grouping / arrow-parameter paren. Unlike
|
|
87
|
+
* `findOpenParen` (which skips grouping parens to reach the enclosing call),
|
|
88
|
+
* this returns the closest '(' so we can detect when the cursor sits inside a
|
|
89
|
+
* callback argument's own parentheses. Returns -1 when none is open.
|
|
90
|
+
*/
|
|
91
|
+
function innermostOpenParen(source, offset) {
|
|
92
|
+
let depth = 0;
|
|
93
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
94
|
+
const ch = source[i];
|
|
95
|
+
if (ch === ')' || ch === ']' || ch === '}') {
|
|
96
|
+
depth++;
|
|
97
|
+
}
|
|
98
|
+
else if (ch === '(' || ch === '[' || ch === '{') {
|
|
99
|
+
if (depth > 0)
|
|
100
|
+
depth--;
|
|
101
|
+
else if (ch === '(')
|
|
102
|
+
return i;
|
|
103
|
+
else
|
|
104
|
+
continue; // unmatched '[' or '{' at depth 0 — keep scanning
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Narrow a type to its callable branch(es): a `TFun` is returned unchanged; a
|
|
111
|
+
* union (`opt()`-style `fn | Undefined`, or a set of overloaded callbacks)
|
|
112
|
+
* yields its function branches — the sole `TFun` when there is exactly one, or
|
|
113
|
+
* a `Union` of the function branches otherwise. Non-callable members (e.g. the
|
|
114
|
+
* `Undefined` that marks an optional callback) are dropped. Returns `null`
|
|
115
|
+
* when the type carries no function branch at all.
|
|
116
|
+
*/
|
|
117
|
+
function functionBranchesOf(t) {
|
|
118
|
+
if (t.tag === 'TFun')
|
|
119
|
+
return t;
|
|
120
|
+
if (t.tag === 'TCon' && t.name === 'Union') {
|
|
121
|
+
const fns = t.args.filter(a => a.tag === 'TFun');
|
|
122
|
+
if (fns.length === 1)
|
|
123
|
+
return fns[0];
|
|
124
|
+
if (fns.length > 1)
|
|
125
|
+
return (0, types_2.tCon)('Union', fns);
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/** The type of the `index`-th parameter slot of a (tuple or curried) function. */
|
|
130
|
+
function activeParamSlot(t, index) {
|
|
131
|
+
if (t.tag !== 'TFun')
|
|
132
|
+
return null;
|
|
133
|
+
const params = (0, typeUtils_1.extractParamList)(t);
|
|
134
|
+
return params[index] ?? null;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Count depth-0 commas in `source` between the argument list opened at
|
|
138
|
+
* `parenOffset` and `cursor` — i.e. the 0-indexed active parameter.
|
|
139
|
+
*/
|
|
140
|
+
function countActiveParam(source, parenOffset, cursor) {
|
|
141
|
+
let depth = 0;
|
|
142
|
+
let activeParam = 0;
|
|
143
|
+
for (let i = parenOffset + 1; i < cursor && i < source.length; i++) {
|
|
144
|
+
const ch = source[i];
|
|
145
|
+
if (ch === '(' || ch === '[' || ch === '{')
|
|
146
|
+
depth++;
|
|
147
|
+
else if (ch === ')' || ch === ']' || ch === '}') {
|
|
148
|
+
if (depth === 0)
|
|
149
|
+
break; // closing paren of this call's argument list
|
|
150
|
+
depth--;
|
|
151
|
+
}
|
|
152
|
+
else if (ch === ',' && depth === 0) {
|
|
153
|
+
activeParam++;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return activeParam;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Decompose a function type into a list of parameter types and return type.
|
|
160
|
+
* For curried: a -> b -> c => params=[a], returnType=b -> c
|
|
161
|
+
* For tuple: (a, b) -> c => params=[a, b], returnType=c
|
|
162
|
+
* Only decomposes ONE call level (single param or multi-param via Tuple).
|
|
163
|
+
*/
|
|
164
|
+
function decomposeFunctionType(t) {
|
|
165
|
+
if (t.tag !== 'TFun')
|
|
166
|
+
return null;
|
|
167
|
+
const params = (0, typeUtils_1.extractParamList)(t).map(p => ({ label: (0, index_1.prettyTypeToString)(p), type: p }));
|
|
168
|
+
return { params, returnType: t.result };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Find the inferred type of the outermost expression spanning a source range,
|
|
172
|
+
* looked up from the inference result's nodeTypes table.
|
|
173
|
+
*/
|
|
174
|
+
function argTypeInRange(start, end, result) {
|
|
175
|
+
// Prefer the OUTERMOST expression that ends exactly at the range boundary.
|
|
176
|
+
// For a call argument like `Left("2")` this is the CallExpression — whose
|
|
177
|
+
// type is the CONSTRUCTED value (`Either String a`) — not the callee
|
|
178
|
+
// Identifier `Left` (`String -> Either String a`). `nodeTypes` is keyed by
|
|
179
|
+
// START and drops a CallExpression that shares its start offset with the
|
|
180
|
+
// callee identifier, so a by-start scan would wrongly surface the callee's
|
|
181
|
+
// function type; `nodeTypesByEnd` (keyed by END, smallest span) still holds
|
|
182
|
+
// the call. Used for overload narrowing, where the callee's TFun type would
|
|
183
|
+
// unify with the wrong branches (or, against a polymorphic `a`, none).
|
|
184
|
+
// Require the candidate to span the WHOLE range (start AND end), so it is the
|
|
185
|
+
// outermost expression — not an inner node that merely happens to end at the
|
|
186
|
+
// boundary. E.g. for an incomplete active record `{ a: 1` the `1` literal
|
|
187
|
+
// ends at the cursor too, but starts later, so it is rejected and the by-start
|
|
188
|
+
// scan below recovers the record type.
|
|
189
|
+
const byEnd = result.nodeTypesByEnd.get(end);
|
|
190
|
+
if (byEnd && byEnd.start === start) {
|
|
191
|
+
return result.substitution.apply(byEnd.type);
|
|
192
|
+
}
|
|
193
|
+
let best = null;
|
|
194
|
+
for (const [s, info] of result.nodeTypes) {
|
|
195
|
+
if (s >= start && s < end && info.end <= end) {
|
|
196
|
+
if (!best || s < best.start || (s === best.start && info.end > best.end)) {
|
|
197
|
+
best = { type: info.type, end: info.end, start: s };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return best ? result.substitution.apply(best.type) : null;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Collect the inferred types of the top-level arguments up to (and including)
|
|
205
|
+
* the one the cursor currently sits in. Each argument is measured to its real
|
|
206
|
+
* end — the next depth-0 comma or the closing paren — so an argument the cursor
|
|
207
|
+
* is *inside* (e.g. `concat("|")`) is still resolved to its full type.
|
|
208
|
+
* Arguments that lie entirely after the cursor are ignored.
|
|
209
|
+
*/
|
|
210
|
+
function collectTypedArgTypes(src, parenOffset, cursor, result) {
|
|
211
|
+
// Split the whole argument list into depth-0 comma-separated segments,
|
|
212
|
+
// scanning past the cursor so the current argument is measured to its end.
|
|
213
|
+
const segments = [];
|
|
214
|
+
let depth = 0;
|
|
215
|
+
let segStart = parenOffset + 1;
|
|
216
|
+
let i = parenOffset + 1;
|
|
217
|
+
for (; i < src.length; i++) {
|
|
218
|
+
const ch = src[i];
|
|
219
|
+
if (ch === '(' || ch === '[' || ch === '{')
|
|
220
|
+
depth++;
|
|
221
|
+
else if (ch === ')' || ch === ']' || ch === '}') {
|
|
222
|
+
if (depth === 0)
|
|
223
|
+
break; // closing paren of this call's argument list
|
|
224
|
+
depth--;
|
|
225
|
+
}
|
|
226
|
+
else if (ch === ',' && depth === 0) {
|
|
227
|
+
segments.push({ start: segStart, end: i });
|
|
228
|
+
segStart = i + 1;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
segments.push({ start: segStart, end: i });
|
|
232
|
+
const types = [];
|
|
233
|
+
for (const seg of segments) {
|
|
234
|
+
// Stop once we reach an argument that begins after the cursor — the user
|
|
235
|
+
// has not started typing it yet, so it must not constrain the overloads.
|
|
236
|
+
if (seg.start > cursor)
|
|
237
|
+
break;
|
|
238
|
+
// An argument that is (or begins with) an unbound identifier is still
|
|
239
|
+
// being typed (e.g. `te` on its way to `testje`). Inference may have
|
|
240
|
+
// provisionally bound its fresh type variable to the FIRST overload's
|
|
241
|
+
// parameter, so its recorded type is unreliable — treat it as unresolved
|
|
242
|
+
// so every overload stays visible until the identifier resolves.
|
|
243
|
+
let head = seg.start;
|
|
244
|
+
while (head < seg.end && /\s/.test(src[head]))
|
|
245
|
+
head++;
|
|
246
|
+
if (result.unboundIdentifiers?.has(head)) {
|
|
247
|
+
types.push(null);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
types.push(argTypeInRange(seg.start, seg.end, result));
|
|
251
|
+
}
|
|
252
|
+
return types;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Does an overload branch accept the argument types typed so far? A branch is
|
|
256
|
+
* rejected only when a concrete already-typed argument cannot unify with the
|
|
257
|
+
* corresponding parameter (e.g. a `String` argument against an `[a]` param).
|
|
258
|
+
*/
|
|
259
|
+
function branchAcceptsArgs(branch, argTypes) {
|
|
260
|
+
if (branch.tag !== 'TFun')
|
|
261
|
+
return true;
|
|
262
|
+
const decomposed = decomposeFunctionType(branch);
|
|
263
|
+
if (!decomposed)
|
|
264
|
+
return true;
|
|
265
|
+
const params = decomposed.params;
|
|
266
|
+
for (let k = 0; k < argTypes.length; k++) {
|
|
267
|
+
const at = argTypes[k];
|
|
268
|
+
const pt = params[k]?.type;
|
|
269
|
+
if (!at || !pt)
|
|
270
|
+
continue;
|
|
271
|
+
// Record discrimination first: closed records whose required fields the
|
|
272
|
+
// argument lacks identify the wrong branch even though lenient record
|
|
273
|
+
// unification would merge the differing field names and wrongly succeed.
|
|
274
|
+
if ((0, typeUtils_1.discriminateRecordArg)(at, pt) === 'reject')
|
|
275
|
+
return false;
|
|
276
|
+
try {
|
|
277
|
+
(0, unify_1.unify)(at, pt);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Collect the inferred argument types of a CURRIED call chain, one per applied
|
|
287
|
+
* frame. For `reduce(fn)(init)({…})` the result is `[typeof fn, typeof init,
|
|
288
|
+
* typeof {…}]`, aligned with the branch's flattened parameters so a later
|
|
289
|
+
* application can discriminate the overloads. `cursor` bounds the active
|
|
290
|
+
* (cursor-containing) frame: narrowing passes the caret, display passes the
|
|
291
|
+
* end of source. `activeText` is the raw text typed into the active frame
|
|
292
|
+
* (e.g. `"{"`), used for the syntactic narrowing hint.
|
|
293
|
+
*/
|
|
294
|
+
function collectCurriedArgTypes(src, idEnd, parenOffset, cursor, result) {
|
|
295
|
+
const types = [];
|
|
296
|
+
let i = idEnd;
|
|
297
|
+
while (i < parenOffset && /\s/.test(src[i]))
|
|
298
|
+
i++;
|
|
299
|
+
// Walk every completed curried application `( … )` up to the active paren.
|
|
300
|
+
while (i < parenOffset && src[i] === '(') {
|
|
301
|
+
let depth = 0;
|
|
302
|
+
let j = i;
|
|
303
|
+
for (; j < src.length; j++) {
|
|
304
|
+
const ch = src[j];
|
|
305
|
+
if (ch === '(')
|
|
306
|
+
depth++;
|
|
307
|
+
else if (ch === ')') {
|
|
308
|
+
depth--;
|
|
309
|
+
if (depth === 0)
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (j >= src.length || j > parenOffset)
|
|
314
|
+
break;
|
|
315
|
+
types.push(argTypeInRange(i + 1, j, result));
|
|
316
|
+
i = j + 1;
|
|
317
|
+
while (i < parenOffset && /\s/.test(src[i]))
|
|
318
|
+
i++;
|
|
319
|
+
}
|
|
320
|
+
// Active frame: the paren the cursor sits in. A curried frame has a single
|
|
321
|
+
// argument, so take the first of whatever `collectTypedArgTypes` resolves.
|
|
322
|
+
const activeArgs = collectTypedArgTypes(src, parenOffset, cursor, result);
|
|
323
|
+
types.push(activeArgs.length > 0 ? activeArgs[0] : null);
|
|
324
|
+
let a = parenOffset + 1;
|
|
325
|
+
while (a < cursor && /\s/.test(src[a]))
|
|
326
|
+
a++;
|
|
327
|
+
return { types, activeText: src.slice(a, cursor) };
|
|
328
|
+
}
|
|
329
|
+
/** Does a (substitution-applied) parameter type admit an OBJECT/record
|
|
330
|
+
* argument? Drives the `({` syntactic narrowing hint: a concrete non-object
|
|
331
|
+
* parameter (e.g. `Array a`, `Number`) rejects an object argument, while a
|
|
332
|
+
* record, `Object`, a bare variable, or a higher-kinded `f a` admits it. */
|
|
333
|
+
function paramAdmitsObject(pt) {
|
|
334
|
+
if (pt.tag === 'TVar' || pt.tag === 'TRow' || pt.tag === 'TRecord')
|
|
335
|
+
return true;
|
|
336
|
+
if (pt.tag === 'TCon') {
|
|
337
|
+
if ((0, types_2.isKindVariable)(pt.name))
|
|
338
|
+
return true; // `f a` — `f` could be an object
|
|
339
|
+
return pt.name === 'Object';
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
/** Mirror of {@link paramAdmitsObject} for array/list arguments (`[`). */
|
|
344
|
+
function paramAdmitsList(pt) {
|
|
345
|
+
if (pt.tag === 'TVar' || pt.tag === 'TRow')
|
|
346
|
+
return true;
|
|
347
|
+
if (pt.tag === 'TCon') {
|
|
348
|
+
if ((0, types_2.isKindVariable)(pt.name))
|
|
349
|
+
return true; // `f a` — `f` could be a list
|
|
350
|
+
return pt.name === 'Array' || pt.name === 'List';
|
|
351
|
+
}
|
|
352
|
+
return false; // TRecord, TFun, … reject a list argument
|
|
353
|
+
}
|
|
354
|
+
/** A *concrete* record/Object parameter (as opposed to a bare variable or a
|
|
355
|
+
* higher-kinded `f a`). Such a parameter accepts any object literal that is
|
|
356
|
+
* still being typed, even before its required fields are present. */
|
|
357
|
+
function isConcreteObjectParam(pt) {
|
|
358
|
+
return pt.tag === 'TRecord' || (pt.tag === 'TCon' && pt.name === 'Object');
|
|
359
|
+
}
|
|
360
|
+
/** A *concrete* list parameter (`Array a` / `List a`). */
|
|
361
|
+
function isConcreteListParam(pt) {
|
|
362
|
+
return pt.tag === 'TCon' && (pt.name === 'Array' || pt.name === 'List');
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Cross-frame variant of {@link branchAcceptsArgs} for curried callees: match
|
|
366
|
+
* the i-th already-applied curried argument against the branch's i-th
|
|
367
|
+
* FLATTENED parameter so a discriminating argument in a LATER application
|
|
368
|
+
* narrows the overload set. The active (last) frame additionally honours a
|
|
369
|
+
* syntactic hint — `{` rejects non-object parameters, `[` rejects non-list
|
|
370
|
+
* parameters — so an empty `({` immediately drops the incompatible overloads.
|
|
371
|
+
*/
|
|
372
|
+
function branchAcceptsCurriedArgs(branch, args, activeText) {
|
|
373
|
+
const { params } = (0, types_1.flattenCurriedType)(branch);
|
|
374
|
+
for (let k = 0; k < args.length; k++) {
|
|
375
|
+
const pt = params[k];
|
|
376
|
+
if (!pt)
|
|
377
|
+
return false; // branch has fewer curried params than supplied
|
|
378
|
+
if (k === args.length - 1 && activeText) {
|
|
379
|
+
const hint = activeText[0];
|
|
380
|
+
// The active frame is an object/array literal STILL BEING TYPED. Apply
|
|
381
|
+
// the syntactic kind hint, then — for a parameter that is a concrete
|
|
382
|
+
// container of that kind — accept it without unifying the (necessarily
|
|
383
|
+
// incomplete) literal against it. This keeps every matching object
|
|
384
|
+
// overload (`{ [k] :: a }`, `{ k :: a }`, `Object`) live while the user
|
|
385
|
+
// is still inside `({`, while higher-kinded `f a` (which can't be a
|
|
386
|
+
// record) and the wrong container kind are still dropped.
|
|
387
|
+
if (hint === '{') {
|
|
388
|
+
if (!paramAdmitsObject(pt))
|
|
389
|
+
return false;
|
|
390
|
+
if (isConcreteObjectParam(pt))
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
else if (hint === '[') {
|
|
394
|
+
if (!paramAdmitsList(pt))
|
|
395
|
+
return false;
|
|
396
|
+
if (isConcreteListParam(pt))
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const at = args[k];
|
|
401
|
+
if (!at)
|
|
402
|
+
continue;
|
|
403
|
+
if ((0, typeUtils_1.discriminateRecordArg)(at, pt) === 'reject')
|
|
404
|
+
return false;
|
|
405
|
+
try {
|
|
406
|
+
(0, unify_1.unify)(at, pt);
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
/** Under a `{` / `[` container hint, drop higher-kinded `f a` branches when at
|
|
415
|
+
* least one surviving branch has a CONCRETE container parameter of the same
|
|
416
|
+
* kind in the active slot. Unification now lets `f a` accept an object (via a
|
|
417
|
+
* `::rep Foldable Object#reduce` bridge) or an array, so the abstract branch
|
|
418
|
+
* would otherwise linger in the menu beside the strictly more informative
|
|
419
|
+
* record / `Object` / `Array a` overloads. When NO concrete container branch
|
|
420
|
+
* survives (e.g. a lone `Foldable f => f a` overload), the abstract branch is
|
|
421
|
+
* kept so signature help still appears. */
|
|
422
|
+
function preferConcreteContainer(branches, activeSlot, activeText) {
|
|
423
|
+
const hint = activeText[0];
|
|
424
|
+
if (hint !== '{' && hint !== '[')
|
|
425
|
+
return branches;
|
|
426
|
+
const activeParam = (b) => (0, types_1.flattenCurriedType)(b).params[activeSlot];
|
|
427
|
+
const isConcrete = hint === '{' ? isConcreteObjectParam : isConcreteListParam;
|
|
428
|
+
const isAbstract = (pt) => !!pt && pt.tag === 'TCon' && (0, types_2.isKindVariable)(pt.name);
|
|
429
|
+
const hasConcrete = branches.some(b => { const p = activeParam(b); return p ? isConcrete(p) : false; });
|
|
430
|
+
if (!hasConcrete)
|
|
431
|
+
return branches;
|
|
432
|
+
const filtered = branches.filter(b => !isAbstract(activeParam(b)));
|
|
433
|
+
return filtered.length > 0 ? filtered : branches;
|
|
434
|
+
}
|
|
435
|
+
/** A genuinely curried function: ≥2 nested single-argument arrows whose
|
|
436
|
+
* parameters are NOT tuples. Tuple parameters (`(a, b) -> c`) indicate an
|
|
437
|
+
* uncurried/multi-arg call shape (e.g. the `concat` overloads) and keep the
|
|
438
|
+
* comma-separated rendering. Purely shape-based, so it also covers ANNOTATED
|
|
439
|
+
* curried functions/overloads like `reduce` (which `curriedNodes` does not
|
|
440
|
+
* flag, as inference only populates that for AUTO-detected curried bindings). */
|
|
441
|
+
function isPureCurriedType(t) {
|
|
442
|
+
const { params } = (0, types_1.flattenCurriedType)(t);
|
|
443
|
+
return params.length >= 2 && params.every(p => !(0, types_2.isTuple)(p));
|
|
444
|
+
}
|
|
445
|
+
function provideSignatureHelp(ctx) {
|
|
446
|
+
const { source, offset, engine, resolver, typeResolver, importTypeAliasResolver, reinferWithArrayASI } = ctx;
|
|
447
|
+
// Normalize ASI for array literals
|
|
448
|
+
const { patched: normSource, insertions: sigInsertions } = (0, files_1.normalizeArrayLiteralASI)(source);
|
|
449
|
+
const normOffset = (0, files_1.mapOffset)(offset, sigInsertions);
|
|
450
|
+
// Find the opening paren and active parameter index
|
|
451
|
+
const { parenOffset, activeParam } = findOpenParen(normSource, normOffset);
|
|
452
|
+
if (parenOffset < 0)
|
|
453
|
+
return null;
|
|
454
|
+
// Infer the source to get types (via the request-scoped cache when the
|
|
455
|
+
// server provides one — signature help re-fires on every keystroke).
|
|
456
|
+
let result = ctx.inferSource
|
|
457
|
+
? ctx.inferSource(normSource)
|
|
458
|
+
: engine.infer(normSource, undefined, resolver, typeResolver, importTypeAliasResolver);
|
|
459
|
+
// Find the callee: the expression immediately before the '('
|
|
460
|
+
// Walk back from parenOffset to find the callee's start offset
|
|
461
|
+
const calleeEnd = parenOffset;
|
|
462
|
+
let calleeStart = calleeEnd - 1;
|
|
463
|
+
// Skip whitespace before '('
|
|
464
|
+
while (calleeStart >= 0 && /\s/.test(normSource[calleeStart]))
|
|
465
|
+
calleeStart--;
|
|
466
|
+
// Handle chained calls: f(x)(y) — the char before '(' is ')'.
|
|
467
|
+
// Skip balanced parenthesized sub-expressions to find the root callee name,
|
|
468
|
+
// and use nodeTypesByEnd to get the return type of the inner call.
|
|
469
|
+
let isChainedCall = false;
|
|
470
|
+
let chainEndPos = -1;
|
|
471
|
+
// How many complete curried applications precede the active paren.
|
|
472
|
+
// For `f(` it is 0, `f(x)(` it is 1, `f(x)(y)(` it is 2 — this is the
|
|
473
|
+
// curry-frame depth used to advance the highlighted parameter for curried
|
|
474
|
+
// callees (each curried application consumes exactly one parameter).
|
|
475
|
+
let curryDepth = 0;
|
|
476
|
+
if (calleeStart >= 0 && normSource[calleeStart] === ')') {
|
|
477
|
+
isChainedCall = true;
|
|
478
|
+
chainEndPos = calleeStart + 1; // exclusive end of the inner call expression
|
|
479
|
+
// Skip balanced parens (possibly multiple levels: f(x)(y)(z))
|
|
480
|
+
while (calleeStart >= 0 && normSource[calleeStart] === ')') {
|
|
481
|
+
let depth = 1;
|
|
482
|
+
calleeStart--;
|
|
483
|
+
while (calleeStart >= 0 && depth > 0) {
|
|
484
|
+
if (normSource[calleeStart] === ')')
|
|
485
|
+
depth++;
|
|
486
|
+
else if (normSource[calleeStart] === '(')
|
|
487
|
+
depth--;
|
|
488
|
+
calleeStart--;
|
|
489
|
+
}
|
|
490
|
+
curryDepth++;
|
|
491
|
+
// Skip whitespace between chained calls
|
|
492
|
+
while (calleeStart >= 0 && /\s/.test(normSource[calleeStart]))
|
|
493
|
+
calleeStart--;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
// Walk back through identifier chars (including '.' for member access)
|
|
497
|
+
const idEnd = calleeStart + 1;
|
|
498
|
+
while (calleeStart >= 0 && /[A-Za-z0-9_$.]/.test(normSource[calleeStart]))
|
|
499
|
+
calleeStart--;
|
|
500
|
+
calleeStart++;
|
|
501
|
+
if (calleeStart >= idEnd)
|
|
502
|
+
return null;
|
|
503
|
+
// Find the best type for the callee in nodeTypes
|
|
504
|
+
let bestType = null;
|
|
505
|
+
let bestRawType = null;
|
|
506
|
+
let bestStart = -1;
|
|
507
|
+
if (isChainedCall) {
|
|
508
|
+
// For chained calls, look up the return type of the inner call via nodeTypesByEnd.
|
|
509
|
+
// The inner call expression's node.end equals the position of the outer '('.
|
|
510
|
+
const chainEntry = result.nodeTypesByEnd.get(chainEndPos);
|
|
511
|
+
if (chainEntry) {
|
|
512
|
+
bestType = result.substitution.apply(chainEntry.type);
|
|
513
|
+
bestRawType = chainEntry.type;
|
|
514
|
+
bestStart = chainEntry.start;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (!bestType) {
|
|
518
|
+
for (const [start, { type }] of result.nodeTypes) {
|
|
519
|
+
if (start >= calleeStart && start < parenOffset && start > bestStart) {
|
|
520
|
+
bestType = result.substitution.apply(type);
|
|
521
|
+
bestRawType = type;
|
|
522
|
+
bestStart = start;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
// Fallback: if the callee starts with '.' and is preceded by ']', the parser
|
|
527
|
+
// may have treated `[...].method(` as a computed member access on the previous
|
|
528
|
+
// expression. Re-infer with ';' inserted before the matching '[' so it parses
|
|
529
|
+
// as an array literal.
|
|
530
|
+
if ((!bestType || bestType.tag === 'TVar') && calleeStart > 0 && normSource[calleeStart - 1] === ']') {
|
|
531
|
+
const origCalleeStart = (0, files_1.unmapOffset)(calleeStart, sigInsertions);
|
|
532
|
+
const fix = reinferWithArrayASI(source, origCalleeStart - 1, resolver, typeResolver);
|
|
533
|
+
if (fix) {
|
|
534
|
+
let bestType2 = null;
|
|
535
|
+
let bestStart2 = -1;
|
|
536
|
+
for (const [start, { type }] of fix.result.nodeTypes) {
|
|
537
|
+
if (start >= calleeStart + fix.shift && start < parenOffset + fix.shift && start > bestStart2) {
|
|
538
|
+
bestType2 = fix.result.substitution.apply(type);
|
|
539
|
+
bestStart2 = start;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (bestType2 && bestType2.tag === 'TFun') {
|
|
543
|
+
bestType = bestType2;
|
|
544
|
+
bestStart = bestStart2 - fix.shift;
|
|
545
|
+
result = fix.result;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (!bestType)
|
|
550
|
+
return null;
|
|
551
|
+
// The resolved type can carry rigid skolem constants (`§N`) when the call
|
|
552
|
+
// appears inside an annotated method body whose params/`this` were
|
|
553
|
+
// skolemized to keep the signature authoritative. Scrub them to display
|
|
554
|
+
// letters so parameter hints never leak `§N` (e.g. `this.reduce(b -> a ->
|
|
555
|
+
// b) -> b -> b`).
|
|
556
|
+
if (result.skolemDisplayNames) {
|
|
557
|
+
if ((0, typeUtils_1.someType)(bestType, types_2.isSkolem))
|
|
558
|
+
bestType = (0, typeUtils_1.scrubSkolems)(bestType, result.skolemDisplayNames);
|
|
559
|
+
if (bestRawType && (0, typeUtils_1.someType)(bestRawType, types_2.isSkolem))
|
|
560
|
+
bestRawType = (0, typeUtils_1.scrubSkolems)(bestRawType, result.skolemDisplayNames);
|
|
561
|
+
}
|
|
562
|
+
// displayType is just bestType now — factory produces the full signature
|
|
563
|
+
let displayType = bestType;
|
|
564
|
+
const calleeName = normSource.slice(calleeStart, idEnd);
|
|
565
|
+
// Chained curried call (`f(x)(y)(`): the `bestType` resolved above is the
|
|
566
|
+
// REMAINDER type of the inner call (from `nodeTypesByEnd`), which has already
|
|
567
|
+
// consumed the applied arguments and therefore HIDES them — signature help
|
|
568
|
+
// would collapse `equals(Left("2"))(` to `equals(Either String _) -> Boolean`,
|
|
569
|
+
// losing the first, already-supplied argument. Recover the ROOT callee's
|
|
570
|
+
// full curried type (recorded at its identifier start offset) so the curried
|
|
571
|
+
// renderer can show the COMPLETE form `f(p0)(p1)... -> ret` with the
|
|
572
|
+
// already-applied arguments substituted in and the active (current) frame
|
|
573
|
+
// highlighted — matching annotated curried overloads (e.g. `reduce`). This
|
|
574
|
+
// runs BEFORE the annotation-Union block below so an annotated overload still
|
|
575
|
+
// wins and narrows its menu; it only rescues UN-annotated / single curried
|
|
576
|
+
// callees whose remainder would otherwise drop the applied frames.
|
|
577
|
+
if (isChainedCall && curryDepth > 0) {
|
|
578
|
+
const rootEntry = result.nodeTypes.get(calleeStart);
|
|
579
|
+
if (rootEntry) {
|
|
580
|
+
let rootType = result.substitution.apply(rootEntry.type);
|
|
581
|
+
if (result.skolemDisplayNames && (0, typeUtils_1.someType)(rootType, types_2.isSkolem)) {
|
|
582
|
+
rootType = (0, typeUtils_1.scrubSkolems)(rootType, result.skolemDisplayNames);
|
|
583
|
+
}
|
|
584
|
+
const rootIsCurriedFun = rootType.tag === 'TFun' && isPureCurriedType(rootType);
|
|
585
|
+
const rootIsCurriedUnion = rootType.tag === 'TCon' && rootType.name === 'Union'
|
|
586
|
+
&& rootType.args.length > 1
|
|
587
|
+
&& rootType.args.every(a => a.tag === 'TFun' && isPureCurriedType(a));
|
|
588
|
+
if (rootIsCurriedFun || rootIsCurriedUnion) {
|
|
589
|
+
// Arity sanity: the root must still carry MORE curried frames than the
|
|
590
|
+
// ones already applied, i.e. it genuinely retains the consumed frames.
|
|
591
|
+
const rootArity = rootType.tag === 'TCon'
|
|
592
|
+
? Math.max(...rootType.args.map(a => (0, types_1.flattenCurriedType)(a).params.length))
|
|
593
|
+
: (0, types_1.flattenCurriedType)(rootType).params.length;
|
|
594
|
+
if (rootArity > curryDepth) {
|
|
595
|
+
if (rootType.tag === 'TCon') {
|
|
596
|
+
// Overloaded callee (Union of curried branches) reached WITHOUT a
|
|
597
|
+
// local annotation (e.g. imported / global / aliased `equals`):
|
|
598
|
+
// the annotation-Union block below is keyed on
|
|
599
|
+
// `result.annotations` and would be skipped, so narrow the menu
|
|
600
|
+
// HERE by the already-applied curried arguments — otherwise a
|
|
601
|
+
// chained call would list every overload (and prefer an unrelated
|
|
602
|
+
// first branch) even after the first argument already discriminates
|
|
603
|
+
// them. Mirrors the annotation block's curried narrowing.
|
|
604
|
+
const branches = rootType.args;
|
|
605
|
+
const { types: curriedArgs, activeText } = collectCurriedArgTypes(normSource, idEnd, parenOffset, normOffset, result);
|
|
606
|
+
const compatible = preferConcreteContainer(branches.filter(b => branchAcceptsCurriedArgs(b, curriedArgs, activeText)), curriedArgs.length - 1, activeText);
|
|
607
|
+
if (compatible.length === 1) {
|
|
608
|
+
displayType = compatible[0];
|
|
609
|
+
}
|
|
610
|
+
else if (compatible.length > 1 && compatible.length < branches.length) {
|
|
611
|
+
displayType = (0, types_2.tCon)('Union', compatible);
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
displayType = rootType;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
displayType = rootType;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
// When the call has no actual arguments yet (user just opened the paren),
|
|
625
|
+
// polymorphic functions like `identity :: a -> a` would show as
|
|
626
|
+
// `identity(Undefined) -> Undefined` because the inference fills in
|
|
627
|
+
// undefined for missing args. Use the raw (pre-substitution) type from
|
|
628
|
+
// nodeTypes which still has the original type variables intact.
|
|
629
|
+
const textBeforeCursor = normSource.substring(parenOffset + 1, normOffset).trim();
|
|
630
|
+
if (textBeforeCursor === '' && !isChainedCall && bestRawType && bestRawType.tag === 'TFun') {
|
|
631
|
+
displayType = bestRawType;
|
|
632
|
+
}
|
|
633
|
+
// Overloaded callee: when the binding's annotation is a Union of TFun
|
|
634
|
+
// branches (e.g. `add :: (Number, Number) -> Number` plus
|
|
635
|
+
// `add :: (String, String) -> String`), surface every overload that is
|
|
636
|
+
// still compatible with the arguments typed so far. We deliberately do
|
|
637
|
+
// NOT trust inference's recorded `callSiteOverloadIndex` here: lenient
|
|
638
|
+
// record unification can make it commit to the first structurally-loose
|
|
639
|
+
// branch (e.g. `{ nam }` accepting a `{ name }` argument), which would hide
|
|
640
|
+
// the genuinely-matching overload. Filtering with `branchAcceptsArgs`
|
|
641
|
+
// (which rejects record arguments that lack a branch's required fields)
|
|
642
|
+
// selects the right branch when the user has committed to one and keeps all
|
|
643
|
+
// overloads visible while the argument is still ambiguous or unresolved.
|
|
644
|
+
if (calleeName) {
|
|
645
|
+
const ann = result.annotations.get(calleeName);
|
|
646
|
+
if (ann?.scheme) {
|
|
647
|
+
const inst = (0, env_1.instantiate)(ann.scheme);
|
|
648
|
+
if (inst.tag === 'TCon' && inst.name === 'Union'
|
|
649
|
+
&& inst.args.length > 1
|
|
650
|
+
&& inst.args.every(a => a.tag === 'TFun')) {
|
|
651
|
+
// Curried overloaded callee (`reduce(fn)(init)({…})`): discriminate
|
|
652
|
+
// across ALL applied frames so a later argument narrows the menu.
|
|
653
|
+
// Each frame's argument is matched against the branch's flattened
|
|
654
|
+
// parameter at the same index, plus a syntactic hint on the active
|
|
655
|
+
// frame (`{`/`[`) for an as-yet-untyped literal.
|
|
656
|
+
if (inst.args.every(isPureCurriedType)) {
|
|
657
|
+
const { types: curriedArgs, activeText } = collectCurriedArgTypes(normSource, idEnd, parenOffset, normOffset, result);
|
|
658
|
+
const compatible = preferConcreteContainer(inst.args.filter(b => branchAcceptsCurriedArgs(b, curriedArgs, activeText)), curriedArgs.length - 1, activeText);
|
|
659
|
+
if (compatible.length === 1) {
|
|
660
|
+
displayType = compatible[0];
|
|
661
|
+
}
|
|
662
|
+
else if (compatible.length > 1 && compatible.length < inst.args.length) {
|
|
663
|
+
displayType = (0, types_2.tCon)('Union', compatible);
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
displayType = inst;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
const argTypes = collectTypedArgTypes(normSource, parenOffset, normOffset, result);
|
|
671
|
+
const compatible = inst.args.filter(branch => branchAcceptsArgs(branch, argTypes));
|
|
672
|
+
if (compatible.length === 1) {
|
|
673
|
+
displayType = compatible[0];
|
|
674
|
+
}
|
|
675
|
+
else if (compatible.length > 1 && compatible.length < inst.args.length) {
|
|
676
|
+
displayType = (0, types_2.tCon)('Union', compatible);
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
// None compatible (still typing) or all compatible (ambiguous /
|
|
680
|
+
// no arguments yet) → show every overload.
|
|
681
|
+
displayType = inst;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
// Level 2 — nested callback signature help. When the cursor sits inside a
|
|
688
|
+
// callback argument's OWN parentheses (a grouping / arrow-parameter paren
|
|
689
|
+
// nested inside the call, e.g. `sort((a, b|))`), show the EXPECTED CALLBACK
|
|
690
|
+
// signature — `(String, String) -> Number` — with the active callback
|
|
691
|
+
// parameter highlighted, instead of the enclosing call's signature. This is
|
|
692
|
+
// both narrower (drops the optional `| Undefined`) and points the user at the
|
|
693
|
+
// parameter they are actually typing.
|
|
694
|
+
if (displayType.tag === 'TFun') {
|
|
695
|
+
const innerParen = innermostOpenParen(normSource, normOffset);
|
|
696
|
+
if (innerParen > parenOffset && !isCallParen(normSource, innerParen)) {
|
|
697
|
+
const slot = activeParamSlot(displayType, activeParam);
|
|
698
|
+
const fn = slot ? functionBranchesOf(result.substitution.apply(slot)) : null;
|
|
699
|
+
const decomposed = fn ? decomposeFunctionType(fn) : null;
|
|
700
|
+
if (decomposed && decomposed.params.length > 0) {
|
|
701
|
+
const innerActive = countActiveParam(normSource, innerParen, normOffset);
|
|
702
|
+
let cbLabel = '(';
|
|
703
|
+
const cbParams = [];
|
|
704
|
+
for (let i = 0; i < decomposed.params.length; i++) {
|
|
705
|
+
if (i > 0)
|
|
706
|
+
cbLabel += ', ';
|
|
707
|
+
const start = cbLabel.length;
|
|
708
|
+
cbLabel += decomposed.params[i].label;
|
|
709
|
+
cbParams.push({ label: [start, cbLabel.length] });
|
|
710
|
+
}
|
|
711
|
+
cbLabel += `) -> ${(0, index_1.prettyTypeToString)(decomposed.returnType)}`;
|
|
712
|
+
return {
|
|
713
|
+
signatures: [{ label: cbLabel, parameters: cbParams }],
|
|
714
|
+
activeSignature: 0,
|
|
715
|
+
activeParameter: Math.min(innerActive, decomposed.params.length - 1),
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
// Collect all overload types to present as signatures.
|
|
721
|
+
// For auto-detected curried functions, generate all curried overloads; otherwise just the type itself.
|
|
722
|
+
// Check both bestStart (the node whose type we resolved) and calleeStart (the root
|
|
723
|
+
// identifier, relevant for chained calls like curriedAdd(1)(2) where the root callee
|
|
724
|
+
// is the one marked in curriedNodes).
|
|
725
|
+
let overloadTypes = [];
|
|
726
|
+
// When set, each overloadType is rendered as a single curried signature
|
|
727
|
+
// (`name(p0)(p1)(p2) -> ret`) with EVERY curried parameter as its own
|
|
728
|
+
// highlightable segment, and the active parameter advances with the curry
|
|
729
|
+
// depth. This is clearer than the old `generateCurriedOverloads` expansion
|
|
730
|
+
// (which produced 2^(n-1) tuple/curry partitions and only ever highlighted
|
|
731
|
+
// the first parameter for a chained call).
|
|
732
|
+
let curriedDisplay = false;
|
|
733
|
+
const isPureCurried = isPureCurriedType;
|
|
734
|
+
const autoCurried = result.curriedNodes.has(bestStart) || result.curriedNodes.has(calleeStart);
|
|
735
|
+
if (displayType.tag === 'TFun') {
|
|
736
|
+
if (isPureCurried(displayType)) {
|
|
737
|
+
overloadTypes = [displayType];
|
|
738
|
+
curriedDisplay = true;
|
|
739
|
+
}
|
|
740
|
+
else if (autoCurried) {
|
|
741
|
+
const { params: flatParams, result: resultType } = (0, types_1.flattenCurriedType)(displayType);
|
|
742
|
+
overloadTypes = flatParams.length > 1
|
|
743
|
+
? (0, types_1.generateCurriedOverloads)(flatParams, resultType)
|
|
744
|
+
: [displayType];
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
overloadTypes = [displayType];
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
else if (displayType.tag === 'TCon' && displayType.name === 'Union'
|
|
751
|
+
&& displayType.args.some(a => a.tag === 'TFun')) {
|
|
752
|
+
// Overloaded function: surface every TFun branch as its own
|
|
753
|
+
// signature so the user can see all available shapes (e.g.
|
|
754
|
+
// `add(Number, Number) -> Number` AND `add(String, String) -> String`).
|
|
755
|
+
// For a curried overloaded callee (e.g. `reduce`), render each branch as
|
|
756
|
+
// a single fully-curried signature so every parameter is highlightable.
|
|
757
|
+
const branches = displayType.args.filter(a => a.tag === 'TFun');
|
|
758
|
+
if (branches.every(isPureCurried)) {
|
|
759
|
+
overloadTypes = branches;
|
|
760
|
+
curriedDisplay = true;
|
|
761
|
+
}
|
|
762
|
+
else if (autoCurried) {
|
|
763
|
+
// Mixed/tuple arity under an auto-detected curried binding: fall back to
|
|
764
|
+
// the per-branch curry-partition expansion.
|
|
765
|
+
overloadTypes = branches.flatMap(branch => {
|
|
766
|
+
const { params: flatParams, result: resultType } = (0, types_1.flattenCurriedType)(branch);
|
|
767
|
+
return flatParams.length > 1
|
|
768
|
+
? (0, types_1.generateCurriedOverloads)(flatParams, resultType)
|
|
769
|
+
: [branch];
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
overloadTypes = branches;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
else if (displayType.tag === 'TCon' && /^[A-Z]/.test(displayType.name)) {
|
|
777
|
+
// Constructor-as-callable: when the callee resolves to an uppercase
|
|
778
|
+
// TCon (e.g. `QueryTag b`), only an instance-callable annotation
|
|
779
|
+
// (`QueryTag() :: a -> RTask r e [b]`) provides callable signatures.
|
|
780
|
+
// The bare constructor scheme (`QueryTag :: ...`) describes how to
|
|
781
|
+
// construct an `QueryTag`, never how to call one, so it is not
|
|
782
|
+
// surfaced here as a signature of an instance.
|
|
783
|
+
const tconName = displayType.name;
|
|
784
|
+
const callables = result.instanceCallables?.get(tconName);
|
|
785
|
+
let rawBranches;
|
|
786
|
+
if (callables && callables.length > 0) {
|
|
787
|
+
rawBranches = callables.map(s => (0, env_1.instantiate)(s)).filter(t => t.tag === 'TFun');
|
|
788
|
+
}
|
|
789
|
+
else {
|
|
790
|
+
rawBranches = [];
|
|
791
|
+
}
|
|
792
|
+
if (rawBranches.length > 0) {
|
|
793
|
+
// Specialise each branch's carrier TVar with the host TCon's row
|
|
794
|
+
// arg so e.g. `QueryTag {projected}` shown in signature help renders
|
|
795
|
+
// as `({}) -> RTask r e [{projected}]` instead of leaving a bare
|
|
796
|
+
// `[b]`. When the host is a multi-arg container (`QueryTag p row`),
|
|
797
|
+
// also substitute the parameter TVar in the overload's parameter
|
|
798
|
+
// slot. Mirrors the substitution applied in
|
|
799
|
+
// `tryConstructorCallable` (`src/infer/index.ts`) and
|
|
800
|
+
// `collectConstructorCallableOverloads` (`src/services/hover.ts`).
|
|
801
|
+
const slots = (0, typeUtils_1.findHostSlots)(displayType);
|
|
802
|
+
// Display-only fallback: if the host has no parameter slot in its
|
|
803
|
+
// declared arity, fall back to a `paramHint` attached during
|
|
804
|
+
// call-site post-processing (see `bindParamSlot` in
|
|
805
|
+
// `src/infer/calls.ts`).
|
|
806
|
+
const paramArg = slots.paramArg
|
|
807
|
+
?? (displayType.tag === 'TCon' ? displayType.paramHint ?? null : null);
|
|
808
|
+
const branches = rawBranches.map(b => {
|
|
809
|
+
if (b.tag !== 'TFun')
|
|
810
|
+
return b;
|
|
811
|
+
let out = b;
|
|
812
|
+
const { paramTVarId, carrierTVarId } = (0, typeUtils_1.findOverloadSlots)(out);
|
|
813
|
+
if (slots.rowArg && carrierTVarId !== null) {
|
|
814
|
+
out = (0, typeUtils_1.substTVar)(out, carrierTVarId, slots.rowArg);
|
|
815
|
+
}
|
|
816
|
+
if (paramArg && paramTVarId !== null) {
|
|
817
|
+
out = (0, typeUtils_1.substTVar)(out, paramTVarId, paramArg);
|
|
818
|
+
}
|
|
819
|
+
return out;
|
|
820
|
+
});
|
|
821
|
+
overloadTypes = branches;
|
|
822
|
+
}
|
|
823
|
+
else {
|
|
824
|
+
overloadTypes = [displayType];
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
overloadTypes = [displayType];
|
|
829
|
+
}
|
|
830
|
+
const signatures = [];
|
|
831
|
+
// Types of the arguments the user has actually typed, for DISPLAY: used to
|
|
832
|
+
// (a) show each typed param with the user's concrete argument type instead
|
|
833
|
+
// of the abstract one, and (b) specialise the return type by unifying the
|
|
834
|
+
// typed args against the overload's abstract parameters. Unlike the
|
|
835
|
+
// overload-filtering scan above, this collects EVERY already-typed argument
|
|
836
|
+
// (passing the end of the source as the cursor), not just those before the
|
|
837
|
+
// caret — so editing the first argument still shows the later, already-typed
|
|
838
|
+
// arguments specialised rather than reverting them to their abstract form.
|
|
839
|
+
const typedArgs = collectTypedArgTypes(normSource, parenOffset, normSource.length, result);
|
|
840
|
+
// For a curried callee, also collect one argument per applied frame so each
|
|
841
|
+
// curried parameter (and the return type) can be specialised to what the
|
|
842
|
+
// user has supplied so far (P4).
|
|
843
|
+
const curriedArgs = curriedDisplay
|
|
844
|
+
? collectCurriedArgTypes(normSource, idEnd, parenOffset, normSource.length, result).types
|
|
845
|
+
: [];
|
|
846
|
+
for (const overloadType of overloadTypes) {
|
|
847
|
+
if (curriedDisplay) {
|
|
848
|
+
// Render the whole curried signature `name(p0)(p1)(p2) -> ret` with each
|
|
849
|
+
// curried parameter as its own highlightable segment. Parameters and the
|
|
850
|
+
// return type are SPECIALISED by unifying each already-applied argument
|
|
851
|
+
// against the corresponding flattened parameter — so `reduce(fn)(1)` shows
|
|
852
|
+
// the seed/return as `Number` — while still rendering the parameter SHAPE
|
|
853
|
+
// (`sub.apply(param)`), never the raw argument, so a partial lambda never
|
|
854
|
+
// collapses the `(b -> a -> b)` callback to a bare variable.
|
|
855
|
+
const { params: flatParams, result: resultType } = (0, types_1.flattenCurriedType)(overloadType);
|
|
856
|
+
if (flatParams.length === 0)
|
|
857
|
+
continue;
|
|
858
|
+
let sub = new substitution_1.Substitution();
|
|
859
|
+
for (let i = 0; i < flatParams.length; i++) {
|
|
860
|
+
const at = curriedArgs[i];
|
|
861
|
+
if (!at)
|
|
862
|
+
continue;
|
|
863
|
+
try {
|
|
864
|
+
const s = (0, unify_1.unify)(sub.apply(flatParams[i]), sub.apply(at));
|
|
865
|
+
sub = s.compose(sub);
|
|
866
|
+
}
|
|
867
|
+
catch {
|
|
868
|
+
// Incompatible (still typing / wrong branch) — keep what we have.
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
let label = calleeName;
|
|
872
|
+
const paramInfos = [];
|
|
873
|
+
for (let i = 0; i < flatParams.length; i++) {
|
|
874
|
+
label += '(';
|
|
875
|
+
const start = label.length;
|
|
876
|
+
label += (0, index_1.prettyTypeToString)(sub.apply(flatParams[i]));
|
|
877
|
+
const end = label.length;
|
|
878
|
+
label += ')';
|
|
879
|
+
paramInfos.push({ label: [start, end] });
|
|
880
|
+
}
|
|
881
|
+
label += ` -> ${(0, index_1.prettyTypeToString)(sub.apply(resultType))}`;
|
|
882
|
+
signatures.push({ label, parameters: paramInfos });
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
const decomposed = decomposeFunctionType(overloadType);
|
|
886
|
+
if (!decomposed)
|
|
887
|
+
continue;
|
|
888
|
+
const { params: paramTypes, returnType } = decomposed;
|
|
889
|
+
if (paramTypes.length === 0)
|
|
890
|
+
continue;
|
|
891
|
+
// Specialise the signature to the user's input: unify each typed argument
|
|
892
|
+
// against the corresponding abstract parameter so the accumulated
|
|
893
|
+
// substitution can both refine later params and the return type.
|
|
894
|
+
let sub = new substitution_1.Substitution();
|
|
895
|
+
for (let i = 0; i < paramTypes.length; i++) {
|
|
896
|
+
const at = typedArgs[i];
|
|
897
|
+
if (!at)
|
|
898
|
+
continue;
|
|
899
|
+
try {
|
|
900
|
+
const s = (0, unify_1.unify)(sub.apply(paramTypes[i].type), sub.apply(at));
|
|
901
|
+
sub = s.compose(sub);
|
|
902
|
+
}
|
|
903
|
+
catch {
|
|
904
|
+
// Incompatible (still typing / wrong branch) — keep what we have.
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
// Param labels: show the user's concrete argument type for slots already
|
|
908
|
+
// typed, the (substitution-refined) abstract type otherwise.
|
|
909
|
+
const paramLabels = paramTypes.map((p, i) => {
|
|
910
|
+
const at = typedArgs[i];
|
|
911
|
+
// Callback / function-typed slots: once the user has begun an argument in
|
|
912
|
+
// the slot but it does NOT yet resolve to a function (an in-progress
|
|
913
|
+
// `(a, b)` arrow still parses as a sequence expression, so `at` is a bare
|
|
914
|
+
// variable — rendering `.sort(a) -> [String]`), show the expected
|
|
915
|
+
// FUNCTION SHAPE instead — narrowed to its callable branch, dropping the
|
|
916
|
+
// optional `| Undefined`. A completed function argument (`at` is a
|
|
917
|
+
// `TFun`) keeps its concrete inferred type; an empty slot keeps the full
|
|
918
|
+
// union so the optional callback stays visible.
|
|
919
|
+
if (at != null && at.tag !== 'TFun') {
|
|
920
|
+
const fn = functionBranchesOf(sub.apply(p.type));
|
|
921
|
+
if (fn)
|
|
922
|
+
return (0, index_1.prettyTypeToString)(fn);
|
|
923
|
+
}
|
|
924
|
+
return (0, index_1.prettyTypeToString)(at ?? sub.apply(p.type));
|
|
925
|
+
});
|
|
926
|
+
const sigLabel = `${calleeName}(${paramLabels.join(', ')}) -> ${(0, index_1.prettyTypeToString)(sub.apply(returnType))}`;
|
|
927
|
+
// Build parameter information with offsets for highlighting
|
|
928
|
+
const paramInfos = [];
|
|
929
|
+
let labelOffset = calleeName.length + 1; // after 'name('
|
|
930
|
+
for (let i = 0; i < paramLabels.length; i++) {
|
|
931
|
+
const start = labelOffset;
|
|
932
|
+
const end = start + paramLabels[i].length;
|
|
933
|
+
paramInfos.push({
|
|
934
|
+
label: [start, end],
|
|
935
|
+
});
|
|
936
|
+
labelOffset = end + 2; // skip ', '
|
|
937
|
+
}
|
|
938
|
+
signatures.push({
|
|
939
|
+
label: sigLabel,
|
|
940
|
+
parameters: paramInfos,
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
if (signatures.length === 0)
|
|
944
|
+
return null;
|
|
945
|
+
// Two overloads can specialise to the SAME rendered signature once the
|
|
946
|
+
// already-applied curried arguments are substituted in (e.g. `Array a` and a
|
|
947
|
+
// `Foldable f => f a` branch both collapse to `[a]` when the user opens an
|
|
948
|
+
// array literal). Collapse exact-label duplicates so the menu never shows
|
|
949
|
+
// the same signature twice.
|
|
950
|
+
const dedupedSignatures = [];
|
|
951
|
+
const seenLabels = new Set();
|
|
952
|
+
for (const sig of signatures) {
|
|
953
|
+
if (seenLabels.has(sig.label))
|
|
954
|
+
continue;
|
|
955
|
+
seenLabels.add(sig.label);
|
|
956
|
+
dedupedSignatures.push(sig);
|
|
957
|
+
}
|
|
958
|
+
signatures.length = 0;
|
|
959
|
+
signatures.push(...dedupedSignatures);
|
|
960
|
+
// Pick the best active signature: prefer one whose param count matches the
|
|
961
|
+
// number of arguments being typed (activeParam is 0-indexed, so activeParam+1
|
|
962
|
+
// is the number of args so far). Fall back to the first signature.
|
|
963
|
+
//
|
|
964
|
+
// For a curried callee the active parameter is the curry-frame depth (each
|
|
965
|
+
// curried application consumes exactly one parameter), not the comma index
|
|
966
|
+
// within a single paren; the best signature is the first overload that still
|
|
967
|
+
// has a parameter at that depth.
|
|
968
|
+
const effectiveActiveParam = curriedDisplay ? curryDepth : activeParam;
|
|
969
|
+
let bestSig = 0;
|
|
970
|
+
const argCount = effectiveActiveParam + 1;
|
|
971
|
+
for (let i = 0; i < signatures.length; i++) {
|
|
972
|
+
const paramCount = signatures[i].parameters?.length ?? 0;
|
|
973
|
+
if (paramCount >= argCount) {
|
|
974
|
+
bestSig = i;
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
return {
|
|
979
|
+
signatures,
|
|
980
|
+
activeSignature: bestSig,
|
|
981
|
+
activeParameter: Math.min(effectiveActiveParam, (signatures[bestSig].parameters?.length ?? 1) - 1),
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
//# sourceMappingURL=signatureHelp.js.map
|