コード例 #1
0
        private void TreeAdd(IShellCommand command)
        {
            var tokens = new Queue <string>(ShellCommandTokenizer.Tokenize(command.Pattern));

            var treeLevel = _commandsTree;

            while (tokens.Any())
            {
                var token = tokens.Dequeue();

                object leaf;
                treeLevel.TryGetValue(token, out leaf);

                if (tokens.Count == 0)
                {
                    if (leaf == null)
                    {
                        leaf = command;
                        treeLevel.Add(token, leaf);
                    }

                    if (leaf != command)
                    {
                        throw new NotSupportedException("Already have handler for command");
                    }
                }
                else
                {
                    if (leaf == null)
                    {
                        leaf = new SortedDictionary <string, object>(StringComparer.OrdinalIgnoreCase);
                        treeLevel.Add(token, leaf);
                    }

                    treeLevel = leaf as SortedDictionary <string, object>;

                    if (treeLevel == null)
                    {
                        throw new InvalidOperationException("Tree leaf must be Dictionary");
                    }
                }
            }
        }
コード例 #2
0
        public IEnumerable <string> CompleteInput(Shell shell, string input)
        {
            var allTokens     = ShellCommandTokenizer.Tokenize(input);
            var tokens        = new Queue <string>(allTokens.Last());
            var endsWithSpace = input.EndsWith(" ");

            var result = FindMatchedCommands(tokens, endsWithSpace);

            if (result.Commands.Count == 1)
            {
                var command = result.Commands.First();

                if (tokens.Any() || (endsWithSpace && !result.IsTreeLevel))
                {
                    var commandValue = command.Value as IShellCommand;

                    if (commandValue != null)
                    {
                        var completeResult = commandValue.Complete(shell, tokens.ToArray()) ?? new string[] { };

                        if (!(completeResult.Length == 1 && completeResult[0] == tokens.LastOrDefault() && endsWithSpace))
                        {
                            return(completeResult);
                        }
                    }

                    return(new string[] { });
                }

                return(new[] { command.Key });
            }

            if (result.Commands.Count > 1 && !tokens.Any())
            {
                return(result.Commands.Keys.OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToArray());
            }

            return(new string[] { });
        }