PowerArgs.Cli.SimpleSyntaxHighlighter.TryHighlight C# (CSharp) Method

TryHighlight() public method

The implementation of ISyntaxHighlighter that uses the configuration you've created to perform syntax highlighting.
public TryHighlight ( RichCommandLineContext readerContext ) : bool
readerContext RichCommandLineContext Context that is used internally
return bool
        public bool TryHighlight(RichCommandLineContext readerContext)
        {
            readerContext.RefreshTokenInfo();
            bool didWork = false;
            for (int i = 0; i < readerContext.Tokens.Count; i++)
            {
                if(string.IsNullOrWhiteSpace(readerContext.Tokens[i].Value))
                {
                    continue;
                }

                var highlighterContext = new HighlighterContext()
                {
                    CurrentToken = readerContext.Tokens[i],
                    CurrentTokenIndex = i,
                    IsLastToken = i == readerContext.Tokens.Count-1,

                };

                bool didWorkOnThisToken = false;

                bool shouldBeHighlightedByAtLeastOneHighlighter = false;
                foreach (var tokenHighlighter in TokenHighlighters)
                {
                    bool shouldBeHighlightedByThisHighlighter = tokenHighlighter.ShouldBeHighlighted(readerContext, highlighterContext);
                    shouldBeHighlightedByAtLeastOneHighlighter = shouldBeHighlightedByAtLeastOneHighlighter || shouldBeHighlightedByThisHighlighter;
                    if (shouldBeHighlightedByThisHighlighter)
                    {
                        didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, tokenHighlighter.HighlightForegroundColor, tokenHighlighter.HighlightBackgroundColor);
                        break;
                    }
                }

                if(shouldBeHighlightedByAtLeastOneHighlighter == false)
                {
                    didWorkOnThisToken = EnsureHighlighted(highlighterContext.CurrentToken, readerContext, null, null);
                }

                didWork = didWork || didWorkOnThisToken;
            }

            return didWork;
        }