Skip to content

Commit e8a9fd2

Browse files
gselzerclaude
andcommitted
code-lsp: keep a server's own duplicate completions
Merging dropped completions inserting the same name with the same parameters even within one server's list, e.g. Groovy's each() from Iterable, Collection, List and Object. Only those duplicating an earlier server's are dropped now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dfbe64a commit e8a9fd2

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

‎code-lsp/src/main/java/org/scijava/code/lsp/MergedLanguageServer.java‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,18 @@ private static CompletionList merge(final List<CompletionList> lists,
515515
items.sort(Comparator.comparing(c -> c.getSortText() != null ? c
516516
.getSortText() : c.getLabel()));
517517
int j = 0;
518+
// NB: A server's own completions are all kept (e.g. the same method
519+
// inherited twice): only those of earlier servers make duplicates.
520+
final Map<String, Set<String>> before = new java.util.HashMap<>();
521+
for (final Map.Entry<String, Set<String>> e : seen.entrySet()) {
522+
before.put(e.getKey(), new HashSet<>(e.getValue()));
523+
}
518524
for (final CompletionItem item : items) {
519-
if (duplicate(seen, insertedLine(item, text), item)) continue;
525+
final String inserted = insertedLine(item, text);
526+
if (duplicate(before, inserted, item)) continue;
527+
final String params = item.getLabelDetails() == null ? null : item
528+
.getLabelDetails().getDetail();
529+
seen.computeIfAbsent(inserted, k -> new HashSet<>()).add(params);
520530
final CompletionItem c = copy(item);
521531
c.setData(new MergedData(uri, i, item.getData(), item.getSortText()));
522532
c.setSortText(String.format("%02d%06d", i, j++));
@@ -527,27 +537,18 @@ private static CompletionList merge(final List<CompletionList> lists,
527537
}
528538

529539
/**
530-
* Whether a completion inserts what an earlier one does: the same name, and
531-
* the same parameters (overloads differ in their label details), or either
532-
* without any (e.g. a server knowing only names). Notes it as seen, if not.
540+
* Whether a completion inserts what an earlier server's does: the same
541+
* name, and the same parameters (overloads differ in their label details),
542+
* or either without any (e.g. a server knowing only names).
533543
*/
534544
private static boolean duplicate(final Map<String, Set<String>> seen,
535545
final String name, final CompletionItem item)
536546
{
547+
final Set<String> known = seen.get(name);
548+
if (known == null) return false;
537549
final String params = item.getLabelDetails() == null ? null : item
538550
.getLabelDetails().getDetail();
539-
final Set<String> known = seen.get(name);
540-
if (known == null) {
541-
final Set<String> s = new HashSet<>();
542-
s.add(params);
543-
seen.put(name, s);
544-
return false;
545-
}
546-
if (params == null || known.contains(null) || known.contains(params)) {
547-
return true;
548-
}
549-
known.add(params);
550-
return false;
551+
return params == null || known.contains(null) || known.contains(params);
551552
}
552553

553554
/**

‎code-lsp/src/test/java/org/scijava/code/lsp/LanguageServersTest.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,23 @@ public void testCompletionsMerged() throws Exception {
144144
// The fast server's at once (by label, having no sortText); the slow
145145
// one's to come.
146146
assertTrue(first instanceof UpdatingCompletionList);
147-
assertEquals(Arrays.asList("mass", "max", "maz", "maz"), labels(first));
147+
assertEquals(Arrays.asList("mass", "max", "maz", "maz", "maz"), labels(
148+
first));
148149
final CompletionList all = ((UpdatingCompletionList) first).update().get(10,
149150
TimeUnit.SECONDS);
150-
// In priority order; "max" once (the same text after inserting); both
151-
// overloads of "maz" (different parameters).
152-
assertEquals(Arrays.asList("mass", "max", "maz", "maz", "map"), labels(
153-
all));
151+
// In priority order; "max" once (the slow server's is the same as the
152+
// fast one's); all of the fast server's "maz" (its own, even the same
153+
// twice, e.g. a method inherited twice).
154+
assertEquals(Arrays.asList("mass", "max", "maz", "maz", "maz", "map"),
155+
labels(all));
154156
final List<String> sorted = all.getItems().stream().map(
155157
CompletionItem::getSortText).collect(Collectors.toList());
156158
final List<String> resorted = new ArrayList<>(sorted);
157159
Collections.sort(resorted);
158160
assertEquals(sorted, resorted);
159161

160162
// Resolving asks the server the completion came from, with its own data.
161-
final CompletionItem map = all.getItems().get(4);
163+
final CompletionItem map = all.getItems().get(5);
162164
final CompletionItem resolved = server.resolveCompletionItem(map).get(10,
163165
TimeUnit.SECONDS);
164166
assertEquals("slow resolved map (data 3)", resolved.getDetail());
@@ -619,7 +621,9 @@ void publish(final String uri, final int version, final String message,
619621
}
620622
if (!slow && name.equals("fast")) {
621623
// Overloads: the same name, different parameters.
622-
for (final String params : new String[] { "(int a)", "(double a)" }) {
624+
for (final String params : new String[] { "(int a)", "(int a)",
625+
"(double a)" })
626+
{
623627
final CompletionItem item = new CompletionItem("maz");
624628
item.setTextEdit(Either.forLeft(new TextEdit(word, "maz")));
625629
final org.eclipse.lsp4j.CompletionItemLabelDetails details =

0 commit comments

Comments
 (0)