コード例 #1
0
        public void SetText(string text)
        {
            TextRange range = new TextRange(sourceCodeRichTextBox.Document.ContentStart, sourceCodeRichTextBox.Document.ContentEnd);
            range.Text = text;

            if (sourceCodeRichTextBox.Document == null)
                return;

            TextRange documentRange = new TextRange(sourceCodeRichTextBox.Document.ContentStart, sourceCodeRichTextBox.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = sourceCodeRichTextBox.Document.ContentStart;
            while (navigator.CompareTo(sourceCodeRichTextBox.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    SyntaxHighlight v = new SyntaxHighlight();

                    v.ApplyColors((Run)navigator.Parent);

                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
コード例 #2
0
        public void ApplyColors(Run run)
        {
            string text = run.Text;

            int sIndex = 0;
            int eIndex = 0;
            for (int i = 0; i < text.Length; i++)
            {
                if (Char.IsWhiteSpace(text[i]) || _special.Where(c => c.ToString() == text[i].ToString()).FirstOrDefault() != 0)
                {
                    if (i > 0 && !(Char.IsWhiteSpace(text[i - 1]) || _special.Where(c => c.ToString() == text[i - 1].ToString()).FirstOrDefault() != 0))
                    {
                        eIndex = i - 1;
                        string word = text.Substring(sIndex, eIndex - sIndex + 1);

                        if (_keywords.Where(c => c == word).FirstOrDefault() != null)
                        {
                            SyntaxHighlight t = new SyntaxHighlight();
                            t.StartPosition = run.ContentStart.GetPositionAtOffset(sIndex, LogicalDirection.Forward);
                            t.EndPosition = run.ContentStart.GetPositionAtOffset(eIndex + 1, LogicalDirection.Backward);
                            t.Word = word;
                            _syntax.Add(t);
                        }
                    }
                    sIndex = i + 1;
                }
            }

            string lastWord = text.Substring(sIndex, text.Length - sIndex);
            if (_keywords.Where(c => c == lastWord).FirstOrDefault() != null)
            {
                SyntaxHighlight t = new SyntaxHighlight();
                t.StartPosition = run.ContentStart.GetPositionAtOffset(sIndex, LogicalDirection.Forward);
                t.EndPosition = run.ContentStart.GetPositionAtOffset(eIndex + 1, LogicalDirection.Backward);
                t.Word = lastWord;
                _syntax.Add(t);
            }

            foreach (SyntaxHighlight t in _syntax)
            {
                TextRange range = new TextRange(t.StartPosition, t.EndPosition);
                range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
        }