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;
006    
007    import bsh.Interpreter;
008    import bsh.util.NameCompletion;
009    import graphlab.plugins.commandline.util.codecompletionutils.CodeCompletionUtils;
010    
011    import java.lang.reflect.Method;
012    import java.util.HashMap;
013    import java.util.Vector;
014    
015    
016    /**
017     * @author Mohammad Ali Rostami
018     * @email ma.rostami@yahoo.com
019     */
020    
021    public class ShellCodeCompletion implements NameCompletion {
022        public HashMap<String, Method> commands;
023        public HashMap<String, String> abbrs;
024        Interpreter interpreter;
025        private HashMap<String, Class> ext_commands;
026    
027        public ShellCodeCompletion(Interpreter interpreter
028                , HashMap<String, Method> commands,
029                  HashMap<String, String> abbrs,
030                  HashMap<String, Class> ext_commands) {
031            this.commands = commands;
032            this.abbrs = abbrs;
033            this.ext_commands = ext_commands;
034            this.interpreter = interpreter;
035        }
036    
037        public String[] completeName(String part) {
038            Vector<String> ret = new Vector<String>();
039            if (part.startsWith("_")) {
040                ret = CodeCompletionUtils.complete(abbrs, part);
041            } else if (part.endsWith("(")) {
042                ret = CodeCompletionUtils.complete(part, interpreter, commands, ext_commands);
043            } else if (part.contains(".")) {
044                ret = CodeCompletionUtils.complete(part, interpreter);
045            } else {
046                for (String temp : commands.keySet())
047                    if (temp.startsWith(part))
048                        ret.add(temp);
049                for (String temp : ext_commands.keySet())
050                    if (temp.startsWith(part))
051                        ret.add(temp);
052            }
053    
054            String[] res = new String[ret.size()];
055            for (int i = 0; i < ret.size(); i++)
056                res[i] = ret.get(i);
057            return res;
058        }
059    }