PowerArgs.Cli.PowerArgsRichCommandLineReader.FindPreviousNonWhitespaceToken C# (CSharp) Method

FindPreviousNonWhitespaceToken() public static method

Searches the reader's tokens for a non whitespace token that preceeds the current token
public static FindPreviousNonWhitespaceToken ( RichCommandLineContext readerContext, HighlighterContext highlighterContext ) : string
readerContext RichCommandLineContext the reader context to inspect
highlighterContext HighlighterContext the highlighter context to inspect
return string
        public static string FindPreviousNonWhitespaceToken(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
        {
            string previousToken = null;

            for (int i = highlighterContext.CurrentTokenIndex - 1; i >= 0; i--)
            {
                if (string.IsNullOrWhiteSpace(readerContext.Tokens[i].Value) == false)
                {
                    previousToken = readerContext.Tokens[i].Value;
                    break;
                }
            }
            return previousToken;
        }

Usage Example

Example #1
0
        public bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
        {
            // don't even try mark tokens as invalid unless the cursor is on it
            if (readerContext.BufferPosition >= highlighterContext.CurrentToken.StartIndex && readerContext.BufferPosition < highlighterContext.CurrentToken.EndIndex)
            {
                return(false);
            }

            var currentToken  = highlighterContext.CurrentToken.Value;
            var previousToken = PowerArgsRichCommandLineReader.FindPreviousNonWhitespaceToken(readerContext, highlighterContext);
            var firstToken    = readerContext.Tokens[0].Value;

            CommandLineAction   contextualAction   = PowerArgsRichCommandLineReader.FindContextualAction(firstToken, definition);
            CommandLineArgument contextualArgument = PowerArgsRichCommandLineReader.FindContextualArgument(previousToken, contextualAction, definition);

            if (contextualArgument != null)
            {
                if (contextualArgument.TestIsValidAndRevivable(currentToken) == false)
                {
                    // the current token either failed validation or could not be revived
                    return(true);
                }
            }

            bool expectMatchingArg;
            CommandLineArgument currentTokenArgument = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(contextualAction, currentToken, out expectMatchingArg, definition);

            if (currentTokenArgument == null && expectMatchingArg)
            {
                // The current token starts with a - or /, but does not match a global or action specific argument, so we'll highlight the token red
                return(true);
            }

            return(false);
        }