Esempio n. 1
0
        internal IEnumerator <string> Complete(string partialCommand, string lastWord)
        {
            string[] st   = partialCommand.Split(' ');
            String   cmd  = (String)st[0];
            int      argc = st.Length;

            // 'aliases' command gets no names.
            string[] commandNames1 = dispatcher.GetCommandNames(typeof(AliasesCommand));
            if (Array.BinarySearch(commandNames1, cmd) >= 0)
            {
                return(null);
            }

            // some completion within the alias/unalias commands.
            commandNames1 = dispatcher.GetCommandNames(typeof(AliasCommand));
            string[] commandNames2 = dispatcher.GetCommandNames(typeof(UnaliasCommand));
            if (Array.BinarySearch(commandNames1, cmd) >= 0 ||
                Array.BinarySearch(commandNames2, cmd) >= 0)
            {
                List <string> alreadyGiven = new List <string>();

                if (Array.BinarySearch(commandNames1, cmd) >= 0)
                {
                    // do not complete beyond first word.
                    if (argc > ("".Equals(lastWord) ? 0 : 1))
                    {
                        return(null);
                    }
                }
                else
                {
                    /*
                     * remember all aliases, that have already been given on
                     * the commandline and exclude from completion..
                     * cool, isn't it ?
                     */
                    for (int i = 1; i < st.Length; i++)
                    {
                        alreadyGiven.Add(st[i]);
                    }
                }

                // ok, now return the list.
                return(new AliasSortedMatchEnumerator(alreadyGiven, lastWord, aliases));
            }

            /* ok, someone tries to complete something that is a command.
             * try to find the actual command and ask that command to do
             * the completion.
             */
            String toExecute = (String)aliases[cmd];

            if (toExecute != null)
            {
                Command c = dispatcher.GetCommand(toExecute);
                if (c != null)
                {
                    int    i     = 0;
                    String param = partialCommand;
                    while (param.Length < i &&
                           Char.IsWhiteSpace(param[i]))
                    {
                        ++i;
                    }
                    while (param.Length < i &&
                           !Char.IsWhiteSpace(param[i]))
                    {
                        ++i;
                    }
                    return(c.Complete(dispatcher, toExecute + param.Substring(i), lastWord));
                }
            }

            return(null);
        }