Acquarella.Colorizer.GetColorTypes C# (CSharp) Метод

GetColorTypes() публичный Метод

public GetColorTypes ( string text, Lexer lexer ) : IEnumerable
text string
lexer Acquarella.Lexers.Lexer
Результат IEnumerable
        public IEnumerable<TokenType> GetColorTypes(string text, Lexer lexer)
        {
            int position = 0;

            foreach (var token in lexer.GetTokens(text))
            {
                while (position < token.Start)
                {
                    yield return TokenType.Space;
                    position++;
                }

                while (position < token.Start + token.Length)
                {
                    yield return token.Type;
                    position++;
                }
            }

            int length = text.Length;

            while (position < length)
            {
                yield return TokenType.Space;
                position++;
            }
        }

Usage Example

Пример #1
0
        public string Render(string text)
        {
            Colorizer colorizer = new Colorizer();
            int position = 0;
            TokenType lasttype = TokenType.Space;

            StringBuilder sb = new StringBuilder(text.Length * 2);

            string prologue = this.GetFormat("TextBegin");

            if (prologue != null)
                sb.Append(prologue);

            foreach (var colortype in colorizer.GetColorTypes(text, this.lexer))
            {
                if (colortype != lasttype)
                {
                    if (position > 0)
                    {
                        string endformat = this.GetFormat(System.Enum.GetName(typeof(TokenType), lasttype) + "End");
                        if (!string.IsNullOrEmpty(endformat))
                            sb.Append(endformat);
                    }

                    string beginformat = this.GetFormat(System.Enum.GetName(typeof(TokenType), colortype) + "Begin");
                    if (!string.IsNullOrEmpty(beginformat))
                        sb.Append(beginformat);

                    lasttype = colortype;
                }

                sb.Append(text[position]);
                position++;
            }

            if (position > 0)
            {
                string endformat = this.GetFormat(System.Enum.GetName(typeof(TokenType), lasttype) + "End");
                if (!string.IsNullOrEmpty(endformat))
                    sb.Append(endformat);
            }

            string epilog = this.GetFormat("TextEnd");

            if (epilog != null)
                sb.Append(epilog);

            return sb.ToString();
        }
Colorizer