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

IsCursorOnToken() private method

private IsCursorOnToken ( Token t ) : bool
t Token
return bool
        internal bool IsCursorOnToken(Token t)
        {
            if(Buffer.Count == 0 && t.StartIndex == 0 && t.EndIndex == 0)
            {
                // the buffer is empty, return true if we have an empty token
                return true;
            }
            else if(Buffer.Count == 0)
            {
                // the buffer is empty and the given token is not, throw
                throw new ArgumentException("The given token does not appear to be a part of the buffer");
            }

            // never say that the cursor is on a whitespace token - not sure about this :(
            if (string.IsNullOrWhiteSpace(t.Value)) return false;

            // the cursor is at a point in the buffer before the current token
            if(BufferPosition < t.StartIndex) return false;
            // the cursor is pointing at a specific character within the current token
            if (BufferPosition <= t.EndIndex) return true;

            return false;
        }

Usage Example

        /// <summary>
        /// Creates a result that replaces the current token with the given selection.
        /// </summary>
        /// <param name="context">Context from the parent reader</param>
        /// <param name="selection">The selection string to insert</param>
        /// <returns>a result that replaces the current token with the given selection</returns>
        public static ContextAssistResult CreateInsertResult(RichCommandLineContext context, ConsoleString selection)
        {
            context.RefreshTokenInfo();
            var ret = new ContextAssistResult();

            bool hasInserted = false;
            var  newBuffer   = new List <ConsoleCharacter>();

            foreach (var token in context.Tokens)
            {
                if (context.IsCursorOnToken(token))
                {
                    if (string.IsNullOrWhiteSpace(token.Value))
                    {
                        newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                        ret.ConsoleRefreshLeftOffset = selection.Length;
                    }
                    else
                    {
                        var tokenOffset = context.BufferPosition - token.StartIndex;
                        ret.ConsoleRefreshLeftOffset = selection.Length - tokenOffset;
                    }

                    if (hasInserted == false)
                    {
                        hasInserted = true;
                        // cursor is on the current token
                        newBuffer.AddRange(selection);
                    }
                }
                else
                {
                    // this token not be modified
                    newBuffer.AddRange(context.GetBufferSubstringFromToken(token));
                }
            }

            if (hasInserted == false)
            {
                hasInserted = true;
                // cursor is on the current token
                newBuffer.AddRange(selection);
                ret.ConsoleRefreshLeftOffset = selection.Length;
            }

            ret.StatusCode = ContextAssistResultStatusCode.Success;
            ret.NewBuffer  = newBuffer;
            return(ret);
        }
All Usage Examples Of PowerArgs.Cli.RichCommandLineContext::IsCursorOnToken