1 46 package groovy.ui; 47 48 import groovy.lang.GroovyShell; 49 import groovy.lang.MetaMethod; 50 51 import java.util.ArrayList ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Set ; 55 56 import org.codehaus.groovy.sandbox.ui.Completer; 57 58 64 public class ShellCompleter implements Completer { 65 private GroovyShell shell; 67 private ArrayList completions; 68 69 public ShellCompleter(GroovyShell shell) { 70 this.shell = shell; 71 } 72 73 public List findCompletions(String token) { 75 completions.clear(); 76 77 if (token.length() == 0) 78 return completions; 79 80 findLocalVariables(token); 82 83 85 findShellMethods(token); 87 88 91 93 return completions; 94 } 95 96 private void findShellMethods(String complete) { 97 List methods = shell.getMetaClass().getMetaMethods(); 98 for (Iterator i = methods.iterator(); i.hasNext();) { 99 MetaMethod method = (MetaMethod) i.next(); 100 if (method.getName().startsWith(complete)) { 101 if (method.getParameterTypes().length > 0) 102 completions.add(method.getName() + "("); 103 else 104 completions.add(method.getName() + "()"); 105 } 106 } 107 } 108 109 private void findLocalVariables(String complete) { 110 Set names = shell.getContext().getVariables().keySet(); 111 112 for (Iterator i = names.iterator(); i.hasNext();) { 113 String name = (String ) i.next(); 114 if (name.startsWith(complete)) { 115 completions.add(name); 116 } 117 } 118 } 119 } 120 | Popular Tags |