Skip to content

Commit e399750

Browse files
gselzerclaude
andcommitted
code-lsp: split CompletionItems.members by how members are reached
instanceMembers lists what an instance reaches (e.g. "s."), and staticMembers what its class does (e.g. "Math."), in place of members and its boolean. So completing on an instance no longer offers its class's statics, as IDEs do; Groovy picks one by its receiver. CompletionItemsTest covers method snippets and both member lists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c30a25d commit e399750

5 files changed

Lines changed: 180 additions & 60 deletions

File tree

‎scijava-code-lsp-groovy/src/main/java/org/scijava/code/lsp/groovy/GroovyLanguageServer.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ private List<CompletionItem> memberCompletions(
437437
}
438438
final Class<?> c = Types.box(ast.toClass(type));
439439
if (c == null) return new ArrayList<>();
440-
final List<CompletionItem> out = sorted(CompletionItems.members(c, prefix,
441-
staticOnly));
440+
final List<CompletionItem> out = sorted(members(c, prefix, staticOnly));
442441
if (!staticOnly) {
443442
for (final Overload m : gdkMethods(c, prefix)) out.add(m.toItem());
444443
}
@@ -470,12 +469,19 @@ private List<CompletionItem> scriptClassMembers(
470469
// Plus what it inherits from (compiled) superclasses.
471470
final Class<?> superclass = ast.toClass(type.getSuperClass());
472471
if (superclass != null && superclass != Object.class) {
473-
sorted.addAll(sorted(CompletionItems.members(superclass, prefix,
474-
staticOnly)));
472+
sorted.addAll(sorted(members(superclass, prefix, staticOnly)));
475473
}
476474
return sorted;
477475
}
478476

477+
/** A compiled class's members, as reached through it or an instance. */
478+
private static List<CompletionItem> members(final Class<?> c,
479+
final String prefix, final boolean staticOnly)
480+
{
481+
return staticOnly ? CompletionItems.staticMembers(c, prefix)
482+
: CompletionItems.instanceMembers(c, prefix);
483+
}
484+
479485
/** Methods of a class defined in the script: by prefix, or exact name. */
480486
private static List<Overload> scriptMethods(final ClassNode type,
481487
final String name, final boolean staticOnly, final boolean exact)

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

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,28 @@ private CompletionItems() {
5353
}
5454

5555
/**
56-
* Builds a completion that inserts a call to a method (or function, or
57-
* constructor), from its name and its parameters.
58-
* <p>
59-
* LSP offers the parts, but leaves how to put them together to each
60-
* server: its snippet syntax, for inserting a call with tab stops through
61-
* its arguments; and its label details, whose {@code detail} it suggests
62-
* for function signatures. Here, they are put together in one way, which
63-
* clients can rely on:
64-
* </p>
65-
* <ul>
66-
* <li>The insert text is a snippet calling the method, with a tab stop per
67-
* parameter, holding the parameter's name as a placeholder, e.g.
68-
* {@code max(${1:a}, ${2:b})}.</li>
69-
* <li>The label details' {@code detail} is the parameter list: one entry
70-
* per tab stop, in order, e.g. {@code (double a, double b)} or
71-
* {@code (a: float, b: float = 0.0)}.</li>
72-
* </ul>
73-
* <p>
74-
* The filter text is the method's name. (By LSP, clients filter on the
75-
* label, which is the name, anyway; this guards against those filtering on
76-
* the insert text.) The kind is {@link CompletionItemKind#Method}. Callers
77-
* may change the kind, and add e.g. a detail (such as the full signature,
78-
* with the return type), documentation, or the label details'
79-
* {@code description} (such as a qualified name).
80-
* </p>
56+
* Builds a method completion from name and parameters.
8157
*
82-
* @param name The name, e.g. {@code max}.
83-
* @param paramNames The parameters' names, as the tab stops show them.
84-
* @param paramList The parameter list, as the language writes it: one
85-
* entry per name, in the same order, e.g.
86-
* {@code (double a, double b)}.
58+
* @param name The name of the method
59+
* @param paramNames The names of the method's parameters, in order
60+
* @param paramList An enclosed list of parameter names and types as
61+
* they'd appear in this method's programming language. In Java,
62+
* for example, {@code (double a, double b)}. In Python,
63+
* {@code (a: double, b: double)}
64+
* @return a {@link CompletionItem} describing a method.
8765
*/
8866
public static CompletionItem method(final String name,
8967
final List<String> paramNames, final String paramList)
9068
{
9169
final CompletionItem item = new CompletionItem(name);
70+
// Kind
9271
item.setKind(CompletionItemKind.Method);
72+
// Label
73+
item.setLabel(name); // Should just be the name since we're adding details
74+
final CompletionItemLabelDetails details = new CompletionItemLabelDetails();
75+
details.setDetail(paramList);
76+
item.setLabelDetails(details);
77+
// InsertText
9378
final StringBuilder snippet = new StringBuilder(escape(name)).append('(');
9479
for (int i = 0; i < paramNames.size(); i++) {
9580
if (i > 0) snippet.append(", ");
@@ -98,41 +83,40 @@ public static CompletionItem method(final String name,
9883
}
9984
item.setInsertText(snippet.append(')').toString());
10085
item.setInsertTextFormat(InsertTextFormat.Snippet);
101-
item.setFilterText(name);
102-
final CompletionItemLabelDetails details = new CompletionItemLabelDetails();
103-
details.setDetail(paramList);
104-
item.setLabelDetails(details);
86+
// FilterText
87+
item.setFilterText(item.getLabel());
10588
return item;
10689
}
10790

10891
/**
109-
* Completions for the public fields and methods of a Java class whose
110-
* names start with a (case-insensitive) prefix, by reflection; methods as
92+
* Completions for the public instance fields and methods of a Java class,
93+
* as reached through an instance (e.g. {@code s.}), whose names start with
94+
* a (case-insensitive) prefix, by reflection; methods as
11195
* {@link #method(String, List, String) method snippets}, with Java-style
11296
* parameter lists (e.g. {@code (double a, double b)}), and their
11397
* signatures as their details.
11498
*
11599
* @param c The class.
116100
* @param prefix The prefix; empty matches everything.
117-
* @param staticOnly Whether to include only static members.
118101
*/
119-
public static List<CompletionItem> members(final Class<?> c,
120-
final String prefix, final boolean staticOnly)
102+
public static List<CompletionItem> instanceMembers(final Class<?> c,
103+
final String prefix)
121104
{
122-
final List<CompletionItem> out = new ArrayList<>();
123-
if (c == null) return out;
124-
final String lp = prefix == null ? "" : prefix.toLowerCase(Locale.ROOT);
125-
for (final Field f : c.getFields()) {
126-
if (staticOnly && !Modifier.isStatic(f.getModifiers())) continue;
127-
if (!f.getName().toLowerCase(Locale.ROOT).startsWith(lp)) continue;
128-
out.add(field(f));
129-
}
130-
for (final Method m : c.getMethods()) {
131-
if (staticOnly && !Modifier.isStatic(m.getModifiers())) continue;
132-
if (!m.getName().toLowerCase(Locale.ROOT).startsWith(lp)) continue;
133-
out.add(method(m));
134-
}
135-
return out;
105+
return members(c, prefix, false);
106+
}
107+
108+
/**
109+
* Completions for the public static fields and methods of a Java class,
110+
* as reached through the class (e.g. {@code Math.}); otherwise as
111+
* {@link #instanceMembers}.
112+
*
113+
* @param c The class.
114+
* @param prefix The prefix; empty matches everything.
115+
*/
116+
public static List<CompletionItem> staticMembers(final Class<?> c,
117+
final String prefix)
118+
{
119+
return members(c, prefix, true);
136120
}
137121

138122
/**
@@ -148,6 +132,25 @@ public static String typeName(final Class<?> c) {
148132

149133
// -- Helper methods --
150134

135+
private static List<CompletionItem> members(final Class<?> c,
136+
final String prefix, final boolean staticMembers)
137+
{
138+
final List<CompletionItem> out = new ArrayList<>();
139+
if (c == null) return out;
140+
final String lp = prefix == null ? "" : prefix.toLowerCase(Locale.ROOT);
141+
for (final Field f : c.getFields()) {
142+
if (Modifier.isStatic(f.getModifiers()) != staticMembers) continue;
143+
if (!f.getName().toLowerCase(Locale.ROOT).startsWith(lp)) continue;
144+
out.add(field(f));
145+
}
146+
for (final Method m : c.getMethods()) {
147+
if (Modifier.isStatic(m.getModifiers()) != staticMembers) continue;
148+
if (!m.getName().toLowerCase(Locale.ROOT).startsWith(lp)) continue;
149+
out.add(method(m));
150+
}
151+
return out;
152+
}
153+
151154
/** A completion for a field. */
152155
private static CompletionItem field(final Field f) {
153156
final CompletionItem item = new CompletionItem(f.getName());

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/impl/BindingsLanguageServer.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected CompletionList complete(final Document doc, final int offset) {
8585
final Object value = bindings.get(member.group(1));
8686
final String prefix = member.group(2);
8787
return sorted(doc, offset - prefix.length(), offset, value == null
88-
? new ArrayList<>() : CompletionItems.members(value
89-
.getClass(), prefix, false));
88+
? new ArrayList<>() : CompletionItems.instanceMembers(value
89+
.getClass(), prefix));
9090
}
9191
final Matcher word = WORD.matcher(line);
9292
final String prefix = word.find() ? word.group(1) : "";

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/jvm/package-info.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
* about a document and an offset. {@link org.scijava.code.lsp.jvm.ClassIndex}
3737
* helps answer them for Java classes: it knows the classes on the classpath,
3838
* with their javadoc. (Completions for Java classes' members are built by
39-
* {@link org.scijava.code.lsp.CompletionItems#members}.)
39+
* {@link org.scijava.code.lsp.CompletionItems#instanceMembers} and
40+
* {@link org.scijava.code.lsp.CompletionItems#staticMembers}.)
4041
* </p>
4142
*/
4243
package org.scijava.code.lsp.jvm;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*-
2+
* #%L
3+
* Language Server Protocol bridge for SciJava code intelligence.
4+
* %%
5+
* Copyright (C) 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.code.lsp;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
37+
import java.util.Arrays;
38+
import java.util.Collections;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.stream.Collectors;
42+
43+
import org.eclipse.lsp4j.CompletionItem;
44+
import org.eclipse.lsp4j.CompletionItemKind;
45+
import org.eclipse.lsp4j.InsertTextFormat;
46+
import org.junit.jupiter.api.Test;
47+
48+
/**
49+
* Tests {@link CompletionItems}.
50+
*
51+
* @author Gabriel Selzer
52+
*/
53+
public class CompletionItemsTest {
54+
55+
@Test
56+
public void testMethod() {
57+
final CompletionItem item = CompletionItems.method("foo", Arrays.asList(
58+
"bar", "baz"), "(double bar, int baz)");
59+
assertEquals("foo", item.getLabel());
60+
assertEquals(CompletionItemKind.Method, item.getKind());
61+
// A tab stop for each parameter.
62+
assertEquals(InsertTextFormat.Snippet, item.getInsertTextFormat());
63+
assertEquals("foo(${1:bar}, ${2:baz})", item.getInsertText());
64+
// Matched by its name, not by its snippet.
65+
assertEquals("foo", item.getFilterText());
66+
// The parameters shown beside the name, as given.
67+
assertEquals("(double bar, int baz)", item.getLabelDetails().getDetail());
68+
// NB: Only methods known by reflection have a signature (see members).
69+
assertNull(item.getDetail());
70+
}
71+
72+
@Test
73+
public void testMembersInstance() {
74+
// Through an instance: its instance members only.
75+
final List<CompletionItem> items = CompletionItems.instanceMembers(String.class,
76+
"");
77+
assertTrue(labels(items).contains("length"));
78+
assertFalse(labels(items).contains("valueOf"));
79+
assertFalse(labels(items).contains("CASE_INSENSITIVE_ORDER"));
80+
// Now with a prefix
81+
final List<CompletionItem> prefixItems = CompletionItems.instanceMembers(String.class,
82+
"v");
83+
assertFalse(labels(prefixItems).contains("length"));
84+
assertFalse(labels(prefixItems).contains("valueOf"));
85+
assertFalse(labels(prefixItems).contains("CASE_INSENSITIVE_ORDER"));
86+
}
87+
88+
@Test
89+
public void testMembersStatic() {
90+
// Through the class: its static members only.
91+
final List<CompletionItem> items = CompletionItems.staticMembers(String.class,
92+
"");
93+
assertTrue(labels(items).contains("valueOf"));
94+
assertTrue(labels(items).contains("CASE_INSENSITIVE_ORDER"));
95+
assertFalse(labels(items).contains("length"));
96+
// Now with a prefix
97+
final List<CompletionItem> prefixItems = CompletionItems.staticMembers(String.class,
98+
"v");
99+
assertTrue(labels(prefixItems).contains("valueOf"));
100+
assertFalse(labels(prefixItems).contains("CASE_INSENSITIVE_ORDER"));
101+
assertFalse(labels(prefixItems).contains("length"));
102+
}
103+
104+
// -- Helper methods --
105+
106+
private static List<String> labels(final List<CompletionItem> items) {
107+
return items.stream().map(CompletionItem::getLabel).collect(Collectors
108+
.toList());
109+
}
110+
}

0 commit comments

Comments
 (0)