コード例 #1
0
ファイル: FullRestyler.cs プロジェクト: seanofw/joy
        public void Restyle()
        {
            // Get the current text from the editor.
            string text     = null;
            string filename = null;

            RunOnWinFormsThread(() =>
            {
                if (ShouldCancel)
                {
                    return;
                }
                filename = _owner.Filename;
                text     = _editor.Text;
            });

            if (ShouldCancel)
            {
                return;
            }

            // First, do a fast easy pass over the input to find its maximum width,
            // so that we can keep the horizontal scrollbar up-to-date.
            MaximumInfo maximumInfo = CalculateMaximumInfo(text, _configurator.TextEditorConfig.Indentation);

            RunOnWinFormsThread(() =>
            {
                if (ShouldCancel)
                {
                    return;
                }
                _owner.SetMaximumInfo(maximumInfo);
                _owner.ClearLineMarks();
            });

            // Create a Smile Lexer to begin lexical analysis on it.
            SmileLibInterop.Lexer lexer = new SmileLibInterop.Lexer(text, 0, text.Length, filename, 1, 1, true);

            // Spin over the input, collect tokens in approximately ~64K chunks (or ~1024 tokens),
            // and pass them to the LexicalStyler class to style.
            int          currentChunkSize = 0;
            const int    MaximumChunkSize = 64 * 1024;
            const int    TokenWeight      = 8 * 8;      // Approximate guess for how much memory a Token object uses.
            List <Token> tokens           = new List <Token>();
            Token        token;

            while ((token = lexer.Next()).Kind != TokenKind.EOI)
            {
                tokens.Add(token);

                currentChunkSize += token.Position.Length + TokenWeight;
                if (currentChunkSize >= MaximumChunkSize)
                {
                    if (ShouldCancel)
                    {
                        break;
                    }

                    RunOnWinFormsThread(() =>
                    {
                        if (ShouldCancel)
                        {
                            return;
                        }
                        LexicalStyler.StyleTokens(_editor, _owner, tokens);
                    });

                    tokens.Clear();
                    currentChunkSize = 0;
                    if (ShouldCancel)
                    {
                        break;
                    }
                }
            }

            if (tokens.Any())
            {
                RunOnWinFormsThread(() =>
                {
                    if (ShouldCancel)
                    {
                        return;
                    }
                    LexicalStyler.StyleTokens(_editor, _owner, tokens);
                });
            }
        }