public static void Start(string text, DrawingContext dc, FormattedText ft) { SyntaxTree tree = SyntaxTree.ParseText(text); var compilation = Roslyn.Compilers.CSharp.Compilation.Create( "MyCompilation", syntaxTrees: new[] { tree }, references: References); var walker = new CsharpSyntaxWalker(compilation.GetSemanticModel(tree), dc, ft); walker.Visit(tree.GetRoot()); foreach (var item in tree.GetRoot().DescendantTrivia()) { walker.VisitTrivia(item); } }
//Render the text in the text box, using the specified background color and syntax //color scheme. The original rendering done by the text box is invisible as both //the foreground and background brushes are transparent protected override void OnRender(DrawingContext dc) { var ft = new FormattedText( this.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily.Source), this.FontSize, new SolidColorBrush(this.DefaultForegroundColor)); //We specify the left and top margins to match the original rendering exactly. //that way, the original caret and text selection adornments line up correctly. var left_margin = 2.0 + this.BorderThickness.Left; var top_margin = 2.0 + this.BorderThickness.Top; dc.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight))); dc.DrawRectangle(this.Background, new Pen(), new Rect(0, 0, this.ActualWidth, this.ActualHeight)); CsharpSyntaxWalker.Start(this.Text, dc, ft); dc.DrawText(ft, new Point(left_margin - this.HorizontalOffset, top_margin - this.VerticalOffset)); }