@@ -100,6 +100,13 @@ public class GroovyLanguageServer extends AbstractInProcessLanguageServer {
100100 private static final Pattern IMPORT = Pattern .compile (
101101 "^\\ s*import\\ s+(static\\ s+)?([\\ w$.]*)$" );
102102
103+ /**
104+ * A declaration's keyword, e.g. "class " or "def ": what follows is the
105+ * name of the new type, method or variable.
106+ */
107+ private static final Pattern DECLARATION = Pattern .compile (
108+ "(?<![\\ w$.@])(def|class|interface|@interface|enum|trait|record)\\ s+$" );
109+
103110 /** The identifier (possibly empty) being typed at the caret. */
104111 private static final Pattern IDENTIFIER = Pattern .compile ("[\\ w$]*$" );
105112
@@ -157,6 +164,11 @@ protected CompletionList complete(final Document doc, final int caret) {
157164 .isEmpty () || Character .isDigit (prefix .charAt (0 )));
158165 final boolean member = before .trim ().endsWith ("." ) && !before .trim ()
159166 .endsWith (".." ) && !decimal ;
167+ if (!member && DECLARATION .matcher (before ).find ()) {
168+ // A name being declared, e.g. "class Fo" or "def fo": it's new, so
169+ // there is nothing to complete it with.
170+ return list (doc , caret , caret , none );
171+ }
160172 // If it's not a member, there are two cases with less work to do:
161173 if (!member && prefix .isEmpty ()) {
162174 // Nothing typed yet: only a call's arguments are worth offering, e.g.
@@ -181,7 +193,7 @@ protected CompletionList complete(final Document doc, final int caret) {
181193 if (member ) items .addAll (memberCompletions (ast , before , prefix ));
182194 else {
183195 items .addAll (variableCompletions (ast .variablesAtCaret (), prefix ));
184- items .addAll (functionCompletions (scriptFunctions (ast , text ,
196+ items .addAll (functionCompletions (functionsAtCaret (ast , text ,
185197 replaceStart , caret ), prefix ));
186198 items .addAll (keywordCompletions (prefix ));
187199 items .addAll (classCompletions (doc , prefix ));
@@ -452,28 +464,38 @@ private List<CompletionItem> scriptClassMembers(
452464 final boolean staticOnly )
453465 {
454466 final List <CompletionItem > out = new ArrayList <>();
455- for (final PropertyNode p : type .getProperties ()) {
456- if (staticOnly && !p .isStatic () || !matches (p .getName (), prefix )) {
457- continue ;
467+ // Its properties and public fields, and those it inherits.
468+ final Set <String > names = new HashSet <>();
469+ for (ClassNode t = type ; t != null ; t = t .getSuperClass () == null ? null
470+ : t .getSuperClass ().redirect ())
471+ {
472+ final List <PropertyNode > properties ;
473+ final List <FieldNode > fields ;
474+ try {
475+ properties = t .getProperties ();
476+ fields = t .getFields ();
477+ }
478+ catch (final RuntimeException | LinkageError exc ) {
479+ break ; // NB: An unloadable class; its members are unknown.
480+ }
481+ for (final PropertyNode p : properties ) {
482+ if (staticOnly && !p .isStatic () || !matches (p .getName (), prefix ) ||
483+ !names .add (p .getName ())) continue ;
484+ out .add (field (p .getName (), p .getType ().toString (false )));
485+ }
486+ for (final FieldNode f : fields ) {
487+ if (!f .isPublic () || f .isSynthetic () || staticOnly && !f .isStatic () ||
488+ !matches (f .getName (), prefix ) || !names .add (f .getName ())) continue ;
489+ out .add (field (f .getName (), f .getType ().toString (false )));
458490 }
459- out .add (field (p .getName (), p .getType ().toString (false )));
460- }
461- for (final FieldNode f : type .getFields ()) {
462- if (!f .isPublic () || f .isSynthetic () || staticOnly && !f .isStatic () ||
463- !matches (f .getName (), prefix ) || type .getProperty (f
464- .getName ()) != null ) continue ;
465- out .add (field (f .getName (), f .getType ().toString (false )));
466491 }
467- for (final Overload m : classMethods (type , prefix , staticOnly , false )) {
492+ // Its public methods, and those it inherits.
493+ for (final Overload m : classMethods (ast , type , prefix , staticOnly ,
494+ false ))
495+ {
468496 out .add (m .toItem ());
469497 }
470- final List <CompletionItem > sorted = sorted (out );
471- // Plus what it inherits from (compiled) superclasses.
472- final Class <?> superclass = ast .toClass (type .getSuperClass ());
473- if (superclass != null && superclass != Object .class ) {
474- sorted .addAll (sorted (members (superclass , prefix , staticOnly )));
475- }
476- return sorted ;
498+ return sorted (out );
477499 }
478500
479501 /** A compiled class's members, as reached through it or an instance. */
@@ -484,14 +506,17 @@ private static List<CompletionItem> members(final Class<?> c,
484506 : CompletionItems .instanceMembers (c , prefix );
485507 }
486508
487- /** Methods of a class defined in the script: by prefix, or exact name. */
488- private static List <Overload > classMethods (final ClassNode type ,
489- final String name , final boolean staticOnly , final boolean exact )
509+ /**
510+ * Public methods of a class defined in the script, including those it
511+ * inherits (see {@link TypedAST#methodsOf}): by prefix, or exact name.
512+ */
513+ private static List <Overload > classMethods (final TypedAST ast ,
514+ final ClassNode type , final String name , final boolean staticOnly ,
515+ final boolean exact )
490516 {
491517 final List <Overload > out = new ArrayList <>();
492- for (final MethodNode m : type .getMethods ()) {
493- if (!m .isPublic () || m .isSynthetic () || staticOnly && !m .isStatic () || m
494- .getName ().contains ("$" )) continue ;
518+ for (final MethodNode m : ast .methodsOf (type )) {
519+ if (!m .isPublic () || staticOnly && !m .isStatic ()) continue ;
495520 if (exact ? !m .getName ().equals (name ) : !matches (m .getName (), name )) {
496521 continue ;
497522 }
@@ -560,20 +585,22 @@ private static List<CompletionItem> variableCompletions(
560585 }
561586
562587 /**
563- * {@link MethodNode}s defined in a script: in the whole script, with the
564- * caret marked, if it parses; else before the caret.
588+ * The {@link MethodNode}s callable without a receiver at the caret (see
589+ * {@link TypedAST#methodsAtCaret()}): as the whole script declares them,
590+ * with the caret marked, if it parses (as they may be declared after the
591+ * caret); else as declared before the caret.
565592 */
566- private List <MethodNode > scriptFunctions (final TypedAST before ,
593+ private List <MethodNode > functionsAtCaret (final TypedAST before ,
567594 final String text , final int replaceStart , final int caret )
568595 {
569596 // NB: A newline ends the text, in case it ends with a line comment.
570597 final String code = TypedAST .closeBrackets (TypedAST .markCaret (text
571598 .substring (0 , replaceStart )) + text .substring (caret ) + "\n " );
572599 if (code != null ) {
573600 final TypedAST whole = TypedAST .of (code , loader ());
574- if (whole .isParsed ()) return whole .scriptMethods ();
601+ if (whole .isParsed ()) return whole .methodsAtCaret ();
575602 }
576- return before .scriptMethods ();
603+ return before .methodsAtCaret ();
577604 }
578605
579606 private static List <CompletionItem > functionCompletions (
@@ -713,8 +740,9 @@ else if (type.isPrimaryClassNode()) {
713740 final String name = e .getMethodAsString ();
714741 if (name == null ) return out ;
715742 if (e .isImplicitThis ()) {
716- // A function defined in the script, or a GDK method such as println.
717- for (final MethodNode m : ast .scriptMethods ()) {
743+ // A method callable at the caret, e.g. a function the script defines;
744+ // or a GDK method such as println.
745+ for (final MethodNode m : ast .methodsAtCaret ()) {
718746 if (m .getName ().equals (name )) out .add (methodNode (name , m ));
719747 }
720748 out .addAll (named (gdkMethods (Object .class , name ), name ));
@@ -725,7 +753,7 @@ else if (type.isPrimaryClassNode()) {
725753 if (type == null ) return out ;
726754 final boolean staticOnly = receiver instanceof ClassExpression ;
727755 if (type .redirect ().isPrimaryClassNode ()) {
728- return classMethods (type .redirect (), name , staticOnly , true );
756+ return classMethods (ast , type .redirect (), name , staticOnly , true );
729757 }
730758 final Class <?> c = Types .box (ast .toClass (type ));
731759 if (c == null ) return out ;
0 commit comments