Esempio n. 1
0
        public static List <IToken> SyntaxHighlight(FastColoredTextBox textbox)
        {
            List <IToken> tokens;

            // TODO: This is slow on a large DAX expression and may cause flickering...
            //textbox.SuspendDrawing(); // Doesn't work as we might be on a different thread
            try
            {
                var lexer = new DAXLexer(new DAXCharStream(textbox.Text, Preferences.Current.UseSemicolonsAsSeparators));
                lexer.RemoveErrorListeners();
                tokens = lexer.GetAllTokens().ToList();
            }
            catch
            {
                textbox.ClearStyle(StyleIndex.All);
                return(new List <IToken>());
            }

            textbox.BeginInvoke((MethodInvoker)(() =>
            {
                textbox.SuspendDrawing();
                textbox.ClearStyle(StyleIndex.All);
                foreach (var tok in tokens)
                {
                    if (tok.Type == DAXLexer.Eof)
                    {
                        break;
                    }
                    var range = textbox.GetRange(tok.StartIndex, tok.StopIndex + 1);
                    if (tok.Channel == DAXLexer.KEYWORD_CHANNEL)
                    {
                        range.SetStyle(KeywordStyle);
                    }
                    else if (tok.Channel == DAXLexer.COMMENTS_CHANNEL)
                    {
                        range.SetStyle(CommentStyle);
                    }
                    else
                    {
                        switch (tok.Type)
                        {
                        case DAXLexer.INTEGER_LITERAL:
                        case DAXLexer.REAL_LITERAL:
                        case DAXLexer.STRING_LITERAL:
                            range.SetStyle(LiteralStyle); break;

                        case DAXLexer.OPEN_PARENS:
                        case DAXLexer.CLOSE_PARENS:
                            range.SetStyle(ParensStyle); break;

                        default:
                            range.SetStyle(PlainStyle); break;
                        }
                    }
                }
                textbox.ResumeDrawing();
            }));

            return(tokens);
        }
Esempio n. 2
0
        private void ClearHighlighting()
        {
            var selectedRange = FastColoredTextBox.Selection.Clone();

            FastColoredTextBox.ClearStyle(StyleIndex.All); //remove all old highlighting
            FastColoredTextBox.Selection = selectedRange;
            FastColoredTextBox.Invalidate();
        }
        public static void SyntaxHighlight(FastColoredTextBox textbox)
        {
            try
            {
                var lexer = new DAXLexer(new AntlrInputStream(textbox.Text));
                lexer.RemoveErrorListeners();

                textbox.ClearStyle(StyleIndex.All);
                var tok = lexer.NextToken();
                while (tok != null && tok.Type != DAXLexer.Eof)
                {
                    var range = textbox.GetRange(tok.StartIndex, tok.StopIndex + 1);
                    if (tok.Channel == DAXLexer.KEYWORD_CHANNEL)
                    {
                        range.SetStyle(KeywordStyle);
                    }
                    else if (tok.Channel == DAXLexer.COMMENTS_CHANNEL)
                    {
                        range.SetStyle(CommentStyle);
                    }
                    else
                    {
                        switch (tok.Type)
                        {
                        case DAXLexer.INTEGER_LITERAL:
                        case DAXLexer.REAL_LITERAL:
                        case DAXLexer.STRING_LITERAL:
                            range.SetStyle(LiteralStyle); break;

                        case DAXLexer.OPEN_PARENS:
                        case DAXLexer.CLOSE_PARENS:
                            range.SetStyle(ParensStyle); break;

                        default:
                            range.SetStyle(PlainStyle); break;
                        }
                    }

                    tok = lexer.NextToken();
                }
            }
            catch
            {
                // Ignore all errors encountered while doing async syntax highlighting
            }
        }
Esempio n. 4
0
        private void HilightTab()
        {
            if (tabFiles.TabPages.Count == 0)
            {
                return;
            }

            FastColoredTextBox fctb = (FastColoredTextBox)tabFiles.SelectedTab.Controls[0];

            fctb.ClearStyle(StyleIndex.All);

            foreach (DataRow row in Hilighters.Rows)
            {
                string Search    = row[HilightSearch.Attributes.Search.ToString()].ToString();
                bool   isRegex   = (bool)row[HilightSearch.Attributes.isRegex.ToString()];
                Color  foreColor = (Color)row[HilightSearch.Attributes.foreColor.ToString()];
                Color  backColor = (Color)row[HilightSearch.Attributes.backColor.ToString()];

                TextStyle a = new TextStyle(new SolidBrush(foreColor), new SolidBrush(backColor), FontStyle.Regular);
                fctb.Range.SetStyle(a, Search);
                fctb.Range.SetFoldingMarkers("{", "}");
            }
        }