Skip to content

Commit 6ac8dca

Browse files
authored
Add completion sorting by parameter count (#184)
Address #183 Sort method completions by parameter count (fewer parameters first). Previously, JDTLS returned the same sortText value (e.g., 999999179) for all overloads of a method like List.of(), causing Zed to display them in arbitrary order. This change prefixes sortText with a zero-padded parameter count (e.g., 00999999179, 01999999179, 02999999179), ensuring overloads are sorted from fewest to most parameters.
1 parent ec245ac commit 6ac8dca

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/proxy.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ proxy.on("server", (data, passthrough) => {
8888
passthrough();
8989
});
9090

91+
function countParams(detail) {
92+
if (!detail || detail === "()") return 0;
93+
const inner = detail.slice(1, -1).trim();
94+
if (inner === "") return 0;
95+
let count = 1, depth = 0;
96+
for (const ch of inner) {
97+
if (ch === "<") depth++;
98+
else if (ch === ">") depth--;
99+
else if (ch === "," && depth === 0) count++;
100+
}
101+
return count;
102+
}
103+
104+
function sortCompletionsByParamCount(result) {
105+
const items = Array.isArray(result) ? result : result?.items;
106+
if (!Array.isArray(items)) return;
107+
for (const item of items) {
108+
if (item.kind === 2 || item.kind === 3) { // Method or Function
109+
const paramCount = countParams(item.labelDetails?.detail);
110+
item.sortText = String(paramCount).padStart(2, "0") + (item.sortText || "");
111+
}
112+
}
113+
}
114+
91115
const server = createServer(async (req, res) => {
92116
if (req.method !== "POST") {
93117
res.status = 405;
@@ -137,6 +161,13 @@ export function createLspProxy({
137161
return;
138162
}
139163

164+
// Modify completion responses to sort by param count
165+
if (message?.result?.items || Array.isArray(message?.result)) {
166+
sortCompletionsByParamCount(message.result);
167+
proxyStdout.write(stringify(message));
168+
return;
169+
}
170+
140171
events.emit("server", message, () => proxyStdout.write(data));
141172
});
142173

0 commit comments

Comments
 (0)