private void AddTrivia(TreeViewItem parentItem, CommonSyntaxTrivia trivia, bool isLeadingTrivia)
        {
            var kind = trivia.GetKind(SourceLanguage);
            var tag = new SyntaxTag()
            {
                SyntaxTrivia = trivia,
                IsSyntaxNode = false,
                IsSyntaxToken = false,
                IsSyntaxTrivia = true,
                Span = trivia.Span,
                FullSpan = trivia.FullSpan,
                Kind = kind,
                ParentItem = parentItem
            };

            var item = new TreeViewItem()
            {
                Tag = tag,
                IsExpanded = true,
                Foreground = Brushes.Maroon,
                Background = trivia.HasDiagnostics ? Brushes.Pink : Brushes.White,
                Header = (isLeadingTrivia ? "Lead: " : "Trail: ") + tag.Kind + " " + trivia.Span.ToString()
            };

            if (trivia.HasDiagnostics)
            {
                item.ToolTip = string.Empty;
                foreach (var diagnostic in SyntaxTree.GetDiagnostics(trivia))
                {
                    item.ToolTip += diagnostic.ToString(null) + "\n";
                }

                item.ToolTip = item.ToolTip.ToString().Trim();
            }

            item.Selected += new RoutedEventHandler((sender, e) =>
            {
                if (!e.Handled)
                {
                    isNavigatingFromTreeToSource = true;

                    typeTextLabel.Visibility = Visibility.Visible;
                    kindTextLabel.Visibility = Visibility.Visible;
                    typeValueLabel.Content = trivia.GetType().Name;
                    kindValueLabel.Content = kind;
                    propertyGrid.SelectedObject = trivia;

                    // If IsLazy is true then populate only child items.
                    if (IsLazy && item.Items.Count == 0 && trivia.HasStructure)
                    {
                        AddNode(item, trivia.GetStructure());
                    }

                    item.IsExpanded = true;

                    if (!isNavigatingFromSourceToTree && SyntaxTriviaNavigationToSourceRequested != null)
                    {
                        SyntaxTriviaNavigationToSourceRequested(trivia);
                    }

                    isNavigatingFromTreeToSource = false;
                    e.Handled = true;
                }
            });

            if (parentItem == null)
            {
                treeView.Items.Clear();
                treeView.Items.Add(item);
                typeTextLabel.Visibility = Visibility.Hidden;
                kindTextLabel.Visibility = Visibility.Hidden;
                typeValueLabel.Content = string.Empty;
                kindValueLabel.Content = string.Empty;
            }
            else
            {
                parentItem.Items.Add(item);
            }

            // If IsLazy is false then recursively populate all descendent items.
            if (!IsLazy && trivia.HasStructure)
            {
                AddNode(item, trivia.GetStructure());
            }
        }
        private void AddTrivia(TreeViewItem parentItem, CommonSyntaxTrivia trivia, bool isLeadingTrivia)
        {
            var kind = trivia.GetKind(SourceLanguage);
            var tag  = new SyntaxTag()
            {
                SyntaxTrivia = trivia,
                Category     = SyntaxCategory.SyntaxTrivia,
                Span         = trivia.Span,
                FullSpan     = trivia.FullSpan,
                Kind         = kind,
                ParentItem   = parentItem
            };

            var item = new TreeViewItem()
            {
                Tag        = tag,
                IsExpanded = false,
                Foreground = Brushes.LightPink,
                //Background = trivia.ContainsDiagnostics ? Brushes.Pink : Brushes.White,
                Header = (isLeadingTrivia ? "Lead: " : "Trail: ") + tag.Kind + " " + trivia.Span.ToString()
            };

            if (SyntaxTree != null && trivia.ContainsDiagnostics)
            {
                item.ToolTip = string.Empty;
                foreach (var diagnostic in SyntaxTree.GetDiagnostics(trivia))
                {
                    item.ToolTip += diagnostic.ToString(null) + "\n";
                }

                item.ToolTip = item.ToolTip.ToString().Trim();
            }

            item.Selected += new RoutedEventHandler((sender, e) =>
            {
                isNavigatingFromTreeToSource = true;

                //typeTextLabel.Visibility = Visibility.Visible;
                //kindTextLabel.Visibility = Visibility.Visible;

                //kindValueLabel.Content = kind;
                try
                {
                    propertyGrid.Instance = trivia;
                }
                catch { }
                item.IsExpanded = true;

                if (!isNavigatingFromSourceToTree && SyntaxTriviaNavigationToSourceRequested != null)
                {
                    SyntaxTriviaNavigationToSourceRequested(trivia);
                }

                isNavigatingFromTreeToSource = false;
                e.Handled = true;
            });

            item.Expanded += new RoutedEventHandler((sender, e) =>
            {
                if (item.Items.Count == 1 && item.Items[0] == null)
                {
                    // Remove placeholder child and populate real children.
                    item.Items.RemoveAt(0);
                    AddNode(item, trivia.GetStructure());
                }
            });


            if (parentItem == root)
            {
                parentItem.Items.Clear();
                parentItem.Items.Add(item);
                parentItem.IsExpanded = true;
                //typeTextLabel.Visibility = Visibility.Hidden;
                //kindTextLabel.Visibility = Visibility.Hidden;
                //kindValueLabel.Content = string.Empty;
            }
            else
            {
                parentItem.Items.Add(item);
            }

            if (trivia.HasStructure)
            {
                if (IsLazy)
                {
                    // Add placeholder child to indicate that real children need to be populated on expansion.
                    item.Items.Add(null);
                }
                else
                {
                    // Recursively populate all descendants.
                    AddNode(item, trivia.GetStructure());
                }
            }
        }
Пример #3
0
        private void AddTrivia(TreeViewItem parentItem, CommonSyntaxTrivia trivia, bool isLeadingTrivia)
        {
            var kind = trivia.GetKind(SourceLanguage);
            var tag  = new SyntaxTag()
            {
                SyntaxTrivia   = trivia,
                IsSyntaxNode   = false,
                IsSyntaxToken  = false,
                IsSyntaxTrivia = true,
                Span           = trivia.Span,
                FullSpan       = trivia.FullSpan,
                Kind           = kind,
                ParentItem     = parentItem
            };

            var item = new TreeViewItem()
            {
                Tag        = tag,
                IsExpanded = true,
                Foreground = Brushes.Maroon,
                Background = trivia.HasDiagnostics ? Brushes.Pink : Brushes.White,
                Header     = (isLeadingTrivia ? "Lead: " : "Trail: ") + tag.Kind + " " + trivia.Span.ToString()
            };

            if (trivia.HasDiagnostics)
            {
                item.ToolTip = string.Empty;
                foreach (var diagnostic in SyntaxTree.GetDiagnostics(trivia))
                {
                    item.ToolTip += diagnostic.ToString(null) + "\n";
                }

                item.ToolTip = item.ToolTip.ToString().Trim();
            }

            item.Selected += new RoutedEventHandler((sender, e) =>
            {
                if (!e.Handled)
                {
                    isNavigatingFromTreeToSource = true;

                    typeTextLabel.Visibility    = Visibility.Visible;
                    kindTextLabel.Visibility    = Visibility.Visible;
                    typeValueLabel.Content      = trivia.GetType().Name;
                    kindValueLabel.Content      = kind;
                    propertyGrid.SelectedObject = trivia;

                    // If IsLazy is true then populate only child items.
                    if (IsLazy && item.Items.Count == 0 && trivia.HasStructure)
                    {
                        AddNode(item, trivia.GetStructure());
                    }

                    item.IsExpanded = true;

                    if (!isNavigatingFromSourceToTree && SyntaxTriviaNavigationToSourceRequested != null)
                    {
                        SyntaxTriviaNavigationToSourceRequested(trivia);
                    }

                    isNavigatingFromTreeToSource = false;
                    e.Handled = true;
                }
            });

            if (parentItem == null)
            {
                treeView.Items.Clear();
                treeView.Items.Add(item);
                typeTextLabel.Visibility = Visibility.Hidden;
                kindTextLabel.Visibility = Visibility.Hidden;
                typeValueLabel.Content   = string.Empty;
                kindValueLabel.Content   = string.Empty;
            }
            else
            {
                parentItem.Items.Add(item);
            }

            // If IsLazy is false then recursively populate all descendent items.
            if (!IsLazy && trivia.HasStructure)
            {
                AddNode(item, trivia.GetStructure());
            }
        }