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

RefreshTokenInfo() public method

Runs the tokenizer if it hasn't already been run on the current key press. You can pass a force flag if you want to force the tokenizer to run. You would need to do this only if you've manually changed the buffer within your handler.
public RefreshTokenInfo ( bool force = false ) : void
force bool If true, then the tokenizer is run no matter what. If false, the tokenizer only runs if it hasn't yet run on this keystroke.
return void
        public void RefreshTokenInfo(bool force = false)
        {
            if(hasFreshTokens && force == false)
            {
                return;
            }

            Tokens = Tokenizer.Tokenize(new ConsoleString(Buffer).ToString());

            if (Tokens.Count == 0)
            {
                Tokens.Add(new Token("", 0, 1, 1));
            }

            CurrentToken = null;
            for (int i = 0; i < Tokens.Count; i++)
            {
                var token = Tokens[i];
                if (BufferPosition < token.EndIndex && BufferPosition >= token.StartIndex)
                {
                    // BUFFER---------: a command line string
                    // BUFFER POSITION:   [       ]
                    CurrentToken = token;
                    CurrentTokenIndex = i;
                    break;
                }
            }

            if(CurrentToken == null)
            {
                CurrentToken = Tokens[Tokens.Count - 1];
                CurrentTokenIndex = Tokens.Count - 1;
            }
        }

Usage Example

 /// <summary>
 /// Handles the tab key by calling all registered tab completion handlers.
 /// </summary>
 /// <param name="context">Context that can be used to inspect the current command line to perform tab completion</param>
 public void Handle(RichCommandLineContext context)
 {
     context.Intercept = true;
     context.RefreshTokenInfo();
     try
     {
         foreach (var handler in TabCompletionHandlers)
         {
             if (handler.TryTabComplete(context))
             {
                 break;
             }
         }
     }
     catch(Exception ex)
     {
         if (ThrowOnTabCompletionHandlerException)
         {
             throw;
         }
         else
         {
             PowerLogger.LogLine("Tab completion handler threw exception: " + ex.ToString());
         }
     }
 }
All Usage Examples Of PowerArgs.Cli.RichCommandLineContext::RefreshTokenInfo