Context about a token that helps determine if it should be highlighted
Esempio n. 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);
        }
        /// <summary>
        /// The implementation of ISyntaxHighlighter that uses the configuration you've created to perform syntax highlighting.
        /// </summary>
        /// <param name="readerContext">Context that is used internally</param>
        /// <returns>true if any highlighting changes were made, false otherwise</returns>
        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);
        }
 /// <summary>
 /// Returns true if the token matches the keyword and the given conditional evaluation returns true
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the token matches the keyword and the given conditional evaluation returns true</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     if (conditionEval(readerContext, highlighterContext))
     {
         return base.ShouldBeHighlighted(readerContext, highlighterContext);
     }
     else
     {
         return false;
     }
 }
        /// <summary>
        /// The implementation of ISyntaxHighlighter that uses the configuration you've created to perform syntax highlighting.
        /// </summary>
        /// <param name="readerContext">Context that is used internally</param>
        /// <returns>true if any highlighting changes were made, false otherwise</returns>
        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;
        }
 /// <summary>
 /// Returns true if the regular expression is matched, false otherwise
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the regular expression is matched, false otherwise</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     var matches = regex.Matches(highlighterContext.CurrentToken.Value);
     foreach(Match match in matches)
     {
         if (match.Value == highlighterContext.CurrentToken.Value) return true;
     }
     return false;
 }
 /// <summary>
 /// Returns true if the current token is a numeric value, false otherwise
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the current token is a numeric value, false otherwise</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     double numericValue;
     return double.TryParse(highlighterContext.CurrentToken.Value, out numericValue);
 }
 /// <summary>
 /// Returns true if the keyword is matched, false otherwise
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the keyword matched, false otherwise</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     if (highlighterContext.IsLastToken) return false;
     return highlighterContext.CurrentToken.Value.Equals(keyword, comparison);
 }
 /// <summary>
 /// Determines if this highlighter should highlight the current token with this highlighter's foreground and background
 /// colors.  
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if this highlighter should highlight the current token, false otherwise</returns>
 public abstract bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext);
Esempio n. 9
0
        /// <summary>
        /// Searches the reader's tokens for a non whitespace token that preceeds the current token
        /// </summary>
        /// <param name="readerContext">the reader context to inspect</param>
        /// <param name="highlighterContext">the highlighter context to inspect</param>
        /// <returns>a non whitespace token that preceeds the current token or null if no such token is found</returns>
        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);
        }
        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;
        }
        /// <summary>
        /// Searches the reader's tokens for a non whitespace token that preceeds the current token
        /// </summary>
        /// <param name="readerContext">the reader context to inspect</param>
        /// <param name="highlighterContext">the highlighter context to inspect</param>
        /// <returns>a non whitespace token that preceeds the current token or null if no such token is found</returns>
        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;
        }
        /// <summary>
        /// Returns true if the current token is a numeric value, false otherwise
        /// </summary>
        /// <param name="readerContext">context from the reader</param>
        /// <param name="highlighterContext">context about the current token</param>
        /// <returns>true if the current token is a numeric value, false otherwise</returns>
        public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
        {
            double numericValue;

            return(double.TryParse(highlighterContext.CurrentToken.Value, out numericValue));
        }
        /// <summary>
        /// Returns true if the regular expression is matched, false otherwise
        /// </summary>
        /// <param name="readerContext">context from the reader</param>
        /// <param name="highlighterContext">context about the current token</param>
        /// <returns>true if the regular expression is matched, false otherwise</returns>
        public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
        {
            var matches = regex.Matches(highlighterContext.CurrentToken.Value);

            foreach (Match match in matches)
            {
                if (match.Value == highlighterContext.CurrentToken.Value)
                {
                    return(true);
                }
            }
            return(false);
        }
 /// <summary>
 /// Returns true if the token matches the keyword and the given conditional evaluation returns true
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the token matches the keyword and the given conditional evaluation returns true</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     if (conditionEval(readerContext, highlighterContext))
     {
         return(base.ShouldBeHighlighted(readerContext, highlighterContext));
     }
     else
     {
         return(false);
     }
 }
 /// <summary>
 /// Returns true if the keyword is matched, false otherwise
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if the keyword matched, false otherwise</returns>
 public override bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
 {
     if (highlighterContext.IsLastToken)
     {
         return(false);
     }
     return(highlighterContext.CurrentToken.Value.Equals(keyword, comparison));
 }
 /// <summary>
 /// Determines if this highlighter should highlight the current token with this highlighter's foreground and background
 /// colors.
 /// </summary>
 /// <param name="readerContext">context from the reader</param>
 /// <param name="highlighterContext">context about the current token</param>
 /// <returns>true if this highlighter should highlight the current token, false otherwise</returns>
 public abstract bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext);