Ejemplo n.º 1
0
        /// <summary>
        /// Auto-complete a command name in an input string.
        /// </summary>
        /// <param name="input">Input to auto-complete.</param>
        /// <param name="command">Command to auto-complete.</param>
        /// <returns>Result of the auto-complete. See AutoCompletionResult.</returns>
        public static AutoCompletionResult AutoCompleteCommandBody(string input, ICommand command)
        {
            AutoCompletionResult result = new AutoCompletionResult();
            var parsingResult           = TryAnyCommandBody(input);

            // There must be something to complete to begin with
            if (parsingResult.WasSuccessful && parsingResult.Value.Length > 0)
            {
                string commandStub = parsingResult.Value;
                // Already matching a command, nothing to auto-complete
                if (command.Name() == commandStub)
                {
                    result.WasSuccessful          = AutoCompletionResultType.FailureAlreadyComplete;
                    result.Remainder              = input.Substring(parsingResult.Remainder.Position);
                    result.RemainderStartPosition = parsingResult.Remainder.Position;
                }
                else if (command.Name().StartsWith(commandStub))
                {
                    result.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                    result.Results.Add(command.Name());
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public AutoCompletionResult AutoComplete(string line)
        {
            var result = ParsingHelpers.AutoCompleteCommandBody(line, this);

            if (result.WasSuccessful == AutoCompletionResultType.FailureAlreadyComplete)
            {
                foreach (var command in GConsole.Instance.Commands)
                {
                    var resultSecond = ParsingHelpers.AutoCompleteCommandBody(line.Substring(result.RemainderStartPosition), command);
                    if (resultSecond.WasSuccessful == AutoCompletionResultType.SuccessOneOption)
                    {
                        var finalResult = new AutoCompletionResult();
                        finalResult.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                        finalResult.Results.Add(line.Substring(0, result.RemainderStartPosition - 1) + " " + resultSecond.Results[0]);
                        return(finalResult);
                    }
                }

                foreach (var keyword in GConsole.Instance.Keywords)
                {
                    var resultSecond = ParsingHelpers.AutoCompleteString(line.Substring(result.RemainderStartPosition - 1), keyword.Name);
                    if (resultSecond.WasSuccessful == AutoCompletionResultType.SuccessOneOption)
                    {
                        var finalResult = new AutoCompletionResult();
                        finalResult.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                        finalResult.Results.Add(line.Substring(0, result.RemainderStartPosition - 1) + " " + resultSecond.Results[0]);
                        return(finalResult);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attemptes to auto-complete a string based on a list of strings.
        /// </summary>
        /// <param name="input">Input to auto-complete.</param>
        /// <param name="strings">List of potential auto-complete targets.</param>
        /// <returns>Result of the auto-complete. See AutoCompletionResult.</returns>
        public static AutoCompletionResult AutoCompleteStringList(string input, List <string> strings, bool allowSpaces = false)
        {
            AutoCompletionResult result = new AutoCompletionResult();
            var parsingResult           = allowSpaces ? TryTextParam(input) : TryAnyCommandBody(input);

            if (parsingResult.WasSuccessful && parsingResult.Value.Length > 0)
            {
                string stringStub = parsingResult.Value;
                if (allowSpaces)
                {
                    stringStub = stringStub.Replace(" ", "");
                }
                if (allowSpaces)
                {
                    stringStub = stringStub.Replace("\t", "");
                }
                foreach (var s in strings)
                {
                    string ss = s;
                    if (allowSpaces)
                    {
                        ss = ss.Replace(" ", "");
                    }
                    if (allowSpaces)
                    {
                        ss = ss.Replace("\t", "");
                    }
                    if (ss == stringStub)
                    {
                        result.WasSuccessful          = AutoCompletionResultType.FailureAlreadyComplete;
                        result.RemainderStartPosition = parsingResult.Remainder.Position;
                    }
                    else if (ss.StartsWith(stringStub))
                    {
                        result.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                        result.Results.Add(s);
                    }
                }
            }

            if (result.Results.Count > 1)
            {
                result.WasSuccessful = AutoCompletionResultType.SuccessMultipleOptions;
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attemptes to auto-complete a string.
        /// </summary>
        /// <param name="input">Input to auto-complete.</param>
        /// <param name="wantedString">Wanted target of the auto-complete.</param>
        /// <returns>Result of the auto-complete. See AutoCompletionResult.</returns>
        public static AutoCompletionResult AutoCompleteString(string input, string wantedString)
        {
            AutoCompletionResult result = new AutoCompletionResult();
            var parsingResult           = TryTextParam(input);

            if (parsingResult.WasSuccessful && parsingResult.Value.Length > 0)
            {
                string stringStub = parsingResult.Value;
                if (stringStub == wantedString)
                {
                    result.WasSuccessful          = AutoCompletionResultType.FailureAlreadyComplete;
                    result.RemainderStartPosition = parsingResult.Remainder.Position;
                }
                else if (wantedString.StartsWith(stringStub))
                {
                    result.WasSuccessful = AutoCompletionResultType.SuccessOneOption;
                    result.Results.Add(wantedString);
                }
            }

            return(result);
        }