Exemplo n.º 1
0
        private static IEnumerable <string> GetCommandNameSuggestions(string startsWith)
        {
            foreach (var command in ConsoleCommandsSystem.SharedGetCommandNamesSuggestions(startsWith))
            {
                yield return(command.Name);

                if (command.Alias != null)
                {
                    yield return(command.Alias);
                }
            }
        }
        public void UpdateSuggestionGhostOnly()
        {
            var text = this.inputTextBox.Text;

            if (text.Length == 0)
            {
                this.textBlockSuggestionGhost.Text = string.Empty;
                return;
            }

            var    stringBuilder = new StringBuilder(capacity: 100);
            string commandName;

            if (this.currentCommandVariant == null)
            {
                // get suggested command name
                if (text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient)
                {
                    // cannot suggest server commands on client
                }
                else
                {
                    commandName = ConsoleCommandsSystem.SharedGetCommandNamesSuggestions(text)
                                  .FirstOrDefault()?.GetNameOrAlias(text);
                    stringBuilder.Append(commandName);
                }

                this.textBlockSuggestionGhost.Text = stringBuilder.ToString();
                return;
            }

            var consoleCommand = this.currentCommandVariant.ConsoleCommand;

            {
                var name = text.TrimStart(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient);

                var indexOfSpace = name.IndexOf(' ');
                if (indexOfSpace > 0)
                {
                    name = name.Substring(0, indexOfSpace);
                }

                if (!consoleCommand.Name.StartsWith(name) &&
                    (consoleCommand.Alias == null ||
                     !consoleCommand.Alias.StartsWith(name)))
                {
                    // current command variant is different from what is displayed now
                    // remove ghost
                    this.textBlockSuggestionGhost.Text = string.Empty;
                    return;
                }
            }

            ConsoleCommandParser.ParseCommandNameAndArguments(
                text,
                textPosition: (ushort)this.inputTextBox.CaretIndex,
                commandName: out commandName,
                arguments: out var arguments,
                argumentIndexForSuggestion: out var argumentIndexForSuggestion);

            if (text[0] == ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient)
            {
                // append server command prefix
                stringBuilder.Append(ConsoleCommandsSystem.ServerConsoleCommandPrefixOnClient);
                text = text.Substring(1);
            }

            var isCommandNameParsedSuccessfully = this.currentCommandVariant != null &&
                                                  (commandName.Equals(consoleCommand.Name,
                                                                      StringComparison.OrdinalIgnoreCase) ||
                                                   commandName.Equals(
                                                       consoleCommand.Alias,
                                                       StringComparison.OrdinalIgnoreCase));

            if (!isCommandNameParsedSuccessfully)
            {
                // need to print ghost for the command name
                commandName = consoleCommand.GetNameOrAlias(commandName);
                stringBuilder.Append(commandName)
                .Append(' ');
            }
            else
            {
                // no need to print any ghost before for the current auto-complete argument
                // add padding before this argument
                var paddingCount = text.TrimEnd(' ').Length + 1;
                stringBuilder.Append(' ', repeatCount: paddingCount);
            }

            // skip all parameters before latest entered and then append the "ghost text" for all the remaining parameters
            var parametersToSkipCount = Math.Max(arguments.Count(a => !string.IsNullOrEmpty(a)),
                                                 argumentIndexForSuggestion);
            var parameters = this.currentCommandVariant.Parameters;

            {
                var isFirst = true;
                foreach (var commandParameter in parameters.Skip(parametersToSkipCount))
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        stringBuilder.Append(' ');
                    }

                    stringBuilder.Append(commandParameter.Name);
                }
            }

            this.textBlockSuggestionGhost.Text = stringBuilder.ToString();
        }