001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.commandline.util.codecompletionutils;
006    
007    import bsh.EvalError;
008    import bsh.Interpreter;
009    import graphlab.platform.parameter.Parameter;
010    
011    import java.lang.annotation.Annotation;
012    import java.lang.reflect.Field;
013    import java.lang.reflect.Method;
014    import java.util.HashMap;
015    import java.util.Vector;
016    
017    /**
018     * @author Mohammad Ali Rostami
019     * @email ma.rostami@yahoo.com
020     */
021    public class CodeCompletionUtils {
022        public static Vector<String> complete(HashMap<String, String> abbrs, String part) {
023            Vector<String> ret = new Vector<String>();
024            if (abbrs.get(part) != null)
025                ret.add(abbrs.get(part) + "(");
026            else {
027                for (String t : abbrs.keySet())
028                    if (t.startsWith(part))
029                        ret.add(t + " : " + abbrs.get(t));
030                if (ret.size() == 1)
031                    ret.set(0, ret.get(0).substring(ret.get(0).indexOf(":") + 2));
032            }
033            return ret;
034        }
035    
036        /**
037         * point completion
038         */
039        public static Vector<String> complete(String part, Interpreter interpreter) {
040            int pointCount = 0;
041            Vector<String> ret = new Vector<String>();
042            for (int i = 0; i < part.length(); i++)
043                if (part.charAt(i) == '.') pointCount++;
044    
045            if (pointCount == 1) {
046                try {
047                    Class c = interpreter.get(part.substring(0, part.lastIndexOf("."))).getClass();
048                    String t = part.substring(part.indexOf(".") + 1);
049                    if (c.getMethods() != null)
050                        for (Method m : c.getMethods())
051                            if (m.getName().startsWith(t))
052                                ret.add(part.substring(0, part.lastIndexOf(".")) + "." + m.getName());
053                } catch (Exception e) {
054                    //evalError.printStackTrace();
055                }
056            } else {
057                try {
058                    Class c = interpreter.eval(part.substring(0, part.lastIndexOf("."))).getClass();
059                    String t = part.substring(part.lastIndexOf(".") + 1);
060                    for (Method m : c.getMethods())
061                        if (m.getName().startsWith(t))
062                            ret.add(part.substring(0, part.lastIndexOf(".")) + "." + m.getName());
063                } catch (Exception e) {
064                    //evalError.printStackTrace();
065                }
066            }
067            return ret;
068        }
069    
070        //argumentCompletion
071        public static Vector<String> complete(String part
072                , Interpreter interpreter, HashMap<String, Method> commands
073                , HashMap<String, Class> ext_commands) {
074            Vector<String> ret = new Vector<String>();
075            if (part.contains(".")) {
076                Method[] ms = new Method[0];
077                try {
078                    ms = interpreter.eval(part.substring(0, part.lastIndexOf("."))).getClass().getMethods();
079                } catch (EvalError evalError) {
080                    //evalError.printStackTrace();
081                }
082    
083                for (Method m : ms) {
084                    String result = "";
085                    result += part;
086                    if (m.getName().equals(part.substring(part.lastIndexOf(".") + 1, part.length() - 1))) {
087                        for (Class c : m.getParameterTypes())
088                            result += (c.getSimpleName() + ",");
089                        if (!result.equals(part))
090                            result = result.substring(0, result.length() - 1) + ");";
091                        else result += ");";
092                        ret.add(result);
093                    }
094                }
095            } else {
096                if (commands.containsKey(part.substring(0, part.length() - 1))) {
097                    for (String t : commands.keySet()) {
098                        String result = "";
099                        result += part;
100                        if (t.equals(part.substring(0, part.length() - 1))) {
101                            Method method = commands.get(t);
102                            Annotation[][] pA = method.getParameterAnnotations();
103                            int index = 0;
104                            for (Annotation[] v : pA) {
105                                for (Annotation a : v) {
106                                    if (a.annotationType().equals(Parameter.class)) {
107                                        Parameter pn = (Parameter) a;
108    
109                                        String type = method.getParameterTypes()[index].getSimpleName();
110                                        result += (pn.name() + "(" + type + "), ");
111                                    }
112                                }
113                                index++;
114                            }
115    
116                            if (!result.equals(part)) {
117                                result = result.substring(0, result.length() - 1);
118                                result = result.substring(0, result.length() - 1) + ");";
119                            } else result += ");";
120                            ret.add(result);
121                        }
122                    }
123    
124                } else {
125                    Vector<String> ret1 = new Vector<String>();
126                    for (String t : ext_commands.keySet()) {
127                        String result = part;
128    
129                        if (ext_commands.get(t) == null) {
130                            if (t.startsWith(part))
131                                ret.add(t);
132                        } else if (t.equals(part.substring(0, part.length() - 1))) {
133                            Class clazz = ext_commands.get(t);
134                            //Method method = commands.get(t);
135                            for (Field f : clazz.getFields()) {
136                                Parameter p = f.getAnnotation(Parameter.class);
137                                if (p != null) {
138                                    String type = f.getType().getSimpleName();
139                                    result += (p.name() + "(" + type + "), ");
140                                }
141                            }
142    
143                            if (!result.equals(part)) {
144                                result = result.substring(0, result.length() - 1);
145                                result = result.substring(0, result.length() - 1) + ");";
146                            } else result += ");";
147                            ret1.add(result);
148                        }
149                    }
150                    ret.addAll(ret1);
151                }
152            }
153            return ret;
154        }
155    }