@@ -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 ());
0 commit comments