Skip to content

Commit 80c6a81

Browse files
Sort JSDoc @param completions by argument position
Fixes #20183 When triggering completions after `@param` in a JSDoc comment, parameter suggestions are now sorted by their position in the function signature rather than alphabetically. Before: For `function foo(z, a) {}`, completions showed `a`, `z` After: Completions now show `z`, `a` (matching argument order) The fix uses the parameter index to generate a unique sortText value, ensuring the IDE displays parameters in the order they appear in the function definition. This makes it easier to document parameters in the correct order. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 95e3aaa commit 80c6a81

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/services/jsDoc.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export function getJSDocParameterNameCompletions(tag: JSDocParameterTag): Comple
420420
const fn = jsdoc.parent;
421421
if (!isFunctionLike(fn)) return [];
422422

423-
return mapDefined(fn.parameters, param => {
423+
return mapDefined(fn.parameters, (param, index) => {
424424
if (!isIdentifier(param.name)) return undefined;
425425

426426
const name = param.name.text;
@@ -431,7 +431,9 @@ export function getJSDocParameterNameCompletions(tag: JSDocParameterTag): Comple
431431
return undefined;
432432
}
433433

434-
return { name, kind: ScriptElementKind.parameterElement, kindModifiers: "", sortText: Completions.SortText.LocationPriority };
434+
// Use parameter index as sortText suffix to maintain argument order in completions
435+
const sortText = (Completions.SortText.LocationPriority + String(index).padStart(4, "0")) as Completions.SortText;
436+
return { name, kind: ScriptElementKind.parameterElement, kindModifiers: "", sortText };
435437
});
436438
}
437439

tests/cases/fourslash/jsdocParameterNameCompletion.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
///<reference path="fourslash.ts" />
22

3+
// Tests that @param completions are sorted by argument position (not alphabetically).
4+
// See https://github.com/microsoft/TypeScript/issues/20183
5+
36
/////**
47
//// * @param /*0*/
58
//// */
@@ -22,8 +25,27 @@
2225
//// */
2326
////function i(foo, bar) {}
2427

28+
// sortText uses LocationPriority ("11") + zero-padded index to maintain argument order
29+
const locationPriority = completion.SortText.LocationPriority;
2530
verify.completions(
26-
{ marker: ["0", "3", "4"], exact: ["foo", "bar"] },
27-
{ marker: "1", exact: "bar" },
28-
{ marker: "2", exact: ["canary", "canoodle"] },
31+
{
32+
marker: ["0", "3", "4"],
33+
exact: [
34+
{ name: "foo", sortText: locationPriority + "0000" },
35+
{ name: "bar", sortText: locationPriority + "0001" },
36+
],
37+
},
38+
{
39+
marker: "1",
40+
// bar is the second parameter (index 1), already documented foo is filtered out
41+
exact: { name: "bar", sortText: locationPriority + "0001" },
42+
},
43+
{
44+
marker: "2",
45+
// canary is index 1, canoodle is index 2 (cat=0, cantaloupe=3 filtered)
46+
exact: [
47+
{ name: "canary", sortText: locationPriority + "0001" },
48+
{ name: "canoodle", sortText: locationPriority + "0002" },
49+
],
50+
},
2951
);

0 commit comments

Comments
 (0)