Beispiel #1
0
        private void handleAutocomplete()
        {
            if (commandBuffer.Length == 0)
            {
                return;
            }

            List <string> possibleCommands = new List <string>();

            int minMatchingChars = 0;

            foreach (var commandPair in engineCommands)
            {
                var commandName = commandPair.Key;
                if (commandName.StartsWith(commandBuffer))
                {
                    minMatchingChars = Math.Min(minMatchingChars, commandName.Length - commandBuffer.Length);
                    possibleCommands.Add(commandName);
                }
            }

            foreach (var commandPair in gameCommands)
            {
                var commandName = commandPair.Key;
                if (commandName.StartsWith(commandBuffer))
                {
                    minMatchingChars = Math.Min(minMatchingChars, commandName.Length - commandBuffer.Length);
                    possibleCommands.Add(commandName);
                }
            }

            switch (possibleCommands.Count)
            {
            case 0:
                Log(string.Format("no commands starting with '{0}'...", commandBuffer));
                break;

            case 1:
                commandBuffer = possibleCommands[0];
                break;

            default:
                var referenceCommand = possibleCommands[0];

                bool breakLoop = false;
                int  charInd   = 0;
                while (charInd < referenceCommand.Length)
                {
                    // check every other command to see how many of their letters match
                    var character = referenceCommand[charInd];
                    for (int commandInd = 1; commandInd < possibleCommands.Count && !breakLoop; commandInd++)
                    {
                        string command = possibleCommands[commandInd];

                        if (charInd >= command.Length || command[charInd] != character)
                        {
                            charInd--;
                            breakLoop = true;
                        }
                    }

                    if (!breakLoop && charInd < referenceCommand.Length - 1)
                    {
                        charInd++;
                    }
                    else
                    {
                        break;
                    }
                }

                if (charInd + 1 > commandBuffer.Length)
                {
                    commandBuffer = referenceCommand.Substring(0, charInd + 1);
                }
                else
                {
                    Log(string.Format("commands starting with '{0}': ", commandBuffer));
                    Log(ConsoleUtilities.BracketedList(possibleCommands));
                }

                break;
            }

            commandCursorIndex = commandBuffer.Length;
        }