PowerArgs.Cli.RichCommandLineContext.CompleteCurrentToken C# (CSharp) Method

CompleteCurrentToken() public method

Performs an auto complete of the given token.
public CompleteCurrentToken ( Token currentToken, ConsoleString completion ) : void
currentToken Token the token to complete
completion ConsoleString the completed token. Note that it is not required that the completion string starts with the current token value, though it usually does.
return void
        public void CompleteCurrentToken(Token currentToken, ConsoleString completion)
        {
            var quoteStatus = GetQuoteStatus(this.Buffer, this.BufferPosition - 1);
            bool readyToEnd = quoteStatus != QuoteStatus.ClosedQuote;
            var endTarget = quoteStatus == QuoteStatus.NoQuotes ? ' ' : '"';

            int currentTokenStartIndex;
            for (currentTokenStartIndex = this.BufferPosition - 1; currentTokenStartIndex >= 0; currentTokenStartIndex--)
            {
                if (this.Buffer[currentTokenStartIndex].Value == endTarget && readyToEnd)
                {
                    if (endTarget == ' ')
                    {
                        currentTokenStartIndex++;
                    }

                    break;
                }
                else if (this.Buffer[currentTokenStartIndex].Value == endTarget)
                {
                    readyToEnd = true;
                }
            }

            if (currentTokenStartIndex == -1) currentTokenStartIndex = 0;

            var insertThreshold = currentTokenStartIndex + currentToken.Value.Length;

            List<ConsoleCharacter> newBuffer = new List<ConsoleCharacter>(this.Buffer);
            for (int completionIndex = 0; completionIndex < completion.Length; completionIndex++)
            {
                if (completionIndex + currentTokenStartIndex == newBuffer.Count)
                {
                    newBuffer.Add(completion[completionIndex]);
                }
                else if (completionIndex + currentTokenStartIndex < insertThreshold)
                {
                    newBuffer[completionIndex + currentTokenStartIndex] = completion[completionIndex];
                }
                else
                {
                    newBuffer.Insert(completionIndex + currentTokenStartIndex, completion[completionIndex]);
                }
            }

            while (newBuffer.Count > currentTokenStartIndex + completion.Length)
            {
                newBuffer.RemoveAt(currentTokenStartIndex + completion.Length);
            }

            ReplaceConsole(new ConsoleString(newBuffer));
        }

Usage Example

        /// <summary>
        /// Implementation of tab completion that leverages tab completion sources that are registered with the target definition.
        /// </summary>
        /// <param name="cliContext">cintext used internally</param>
        /// <returns>true if a tab completion was successfully made, false otherwise</returns>
        public bool TryTabComplete(RichCommandLineContext cliContext)
        {
            var powerArgsContext = ConvertContext(this.Definition, cliContext);

            bool   oldHookWon = false;
            string completion = null;

            foreach (var completionSource in oldHooks)
            {
                if (completionSource is ITabCompletionSourceWithContext)
                {
                    if (((ITabCompletionSourceWithContext)completionSource).TryComplete(powerArgsContext.Shift, powerArgsContext.PreviousToken, powerArgsContext.CompletionCandidate, out completion))
                    {
                        oldHookWon = true;
                        break;
                    }
                }
                else
                {
                    if (completionSource.TryComplete(powerArgsContext.Shift, powerArgsContext.CompletionCandidate, out completion))
                    {
                        oldHookWon = true;
                        break;
                    }
                }
            }

            if (oldHookWon == false)
            {
                foreach (var completionSource in newHooks)
                {
                    if (completionSource.TryComplete(powerArgsContext, out completion))
                    {
                        break;
                    }
                }
            }

            if (completion != null)
            {
                cliContext.CompleteCurrentToken(cliContext.CurrentToken, new ConsoleString(completion));
                return(true);
            }
            else
            {
                return(false);
            }
        }
All Usage Examples Of PowerArgs.Cli.RichCommandLineContext::CompleteCurrentToken