Esempio n. 1
0
        public bool SyntaxNodeMatchesDeclaration(SyntaxNode syntaxNode)
        {
            if (string.IsNullOrWhiteSpace(_declarationFilter))
            {
                return(true);
            }

            // The semantics of CSharp and Visual Basic appear to be different (I.e., VB puts everything inside a "Block")
            if (syntaxNode.SyntaxTree is Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree)
            {
                if (SyntaxNodeWrapper.Get(syntaxNode).GetKind().EndsWith("Declaration"))
                {
                    return(InternalsHelper.GetIdentifierTokenValueText(syntaxNode) == _declarationFilter);
                }
            }
            else if (syntaxNode.SyntaxTree is Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree)
            {
                if (SyntaxNodeWrapper.Get(syntaxNode).GetKind().EndsWith("Block"))
                {
                    SyntaxNode firstChild = syntaxNode.ChildNodes().FirstOrDefault();
                    if (firstChild != null && SyntaxNodeWrapper.Get(firstChild).GetKind().EndsWith("Statement"))
                    {
                        return(InternalsHelper.GetIdentifierTokenValueText(firstChild) == _declarationFilter);
                    }
                }
            }

            return(false);
        }
Esempio n. 2
0
        private void CreateTreeList(string declarationFilter)
        {
            // Create the tree view
            _treeList = new TreeListView()
            {
                CanExpandGetter     = x => ((SyntaxWrapper)x).CanExpand(),
                ChildrenGetter      = x => ((SyntaxWrapper)x).GetChildren(),
                UseCellFormatEvents = true,
                Dock         = DockStyle.Fill,
                UseFiltering = true
            };
            _treeList.FormatCell    += (x, e) => ((SyntaxWrapper)e.CellValue).FormatCell(e);
            _treeList.BeforeSorting += (x, e) => e.Canceled = true;

            // Select - show the text
            _treeList.SelectedIndexChanged += (x, e) =>
            {
                if (_treeList.SelectedItem != null)
                {
                    SyntaxWrapper wrapper = (SyntaxWrapper)_treeList.SelectedItem.RowObject;
                    _textBox.Text = wrapper.GetSyntaxObject().ToString();
                    PopulateGraph(wrapper);
                }
            };

            // Activate - dump the syntax object
            _treeList.ItemActivate += (x, e) =>
            {
                if (_treeList.SelectedItem != null)
                {
                    SyntaxWrapper wrapper = (SyntaxWrapper)_treeList.SelectedItem.RowObject;
                    wrapper.GetSyntaxObject().Dump(wrapper.GetKind() + " " + wrapper.GetSpan());
                }
            };

            // Context menus - dump semantic nodes
            _treeList.CellRightClick += (x, e) =>
            {
                e.MenuStrip = null;
                if (_treeList.SelectedItem != null)
                {
                    SyntaxNodeWrapper wrapper = _treeList.SelectedItem.RowObject as SyntaxNodeWrapper;
                    if (wrapper != null)
                    {
                        ContextMenuStrip menuStrip  = new ContextMenuStrip();
                        SyntaxNode       syntaxNode = (SyntaxNode)wrapper.GetSyntaxObject();

                        // Dump Symbol
                        ISymbol symbol = GetSymbol(syntaxNode);
                        if (symbol != null)
                        {
                            menuStrip.Items.Add("Dump Symbol").Click +=
                                (x2, e2) => symbol.Dump(symbol.Kind + " " + wrapper.GetSpan());
                        }

                        // Dump TypeSymbol
                        ITypeSymbol typeSymbol = GetTypeSymbol(syntaxNode);
                        if (typeSymbol != null)
                        {
                            menuStrip.Items.Add("Dump TypeSymbol").Click +=
                                (x2, e2) => typeSymbol.Dump(typeSymbol.Kind + " " + wrapper.GetSpan());
                        }

                        // Dump Converted TypeSymbol
                        ITypeSymbol convertedTypeSymbol = GetConvertedTypeSymbol(syntaxNode);
                        if (convertedTypeSymbol != null)
                        {
                            menuStrip.Items.Add("Dump Converted TypeSymbol").Click +=
                                (x2, e2) => convertedTypeSymbol.Dump(convertedTypeSymbol.Kind + " " + wrapper.GetSpan());
                        }

                        // Dump AliasSymbol
                        IAliasSymbol aliasSymbol = GetAliasSymbol(syntaxNode);
                        if (aliasSymbol != null)
                        {
                            menuStrip.Items.Add("Dump AliasSymbol").Click +=
                                (x2, e2) => aliasSymbol.Dump(aliasSymbol.Kind + " " + wrapper.GetSpan());
                        }

                        // Dump ContantValue
                        object constantValue;
                        if (GetConstantValue(syntaxNode, out constantValue))
                        {
                            menuStrip.Items.Add("Show Constant Value").Click +=
                                (x2, e2) => MessageBox.Show(constantValue.ToString(), constantValue.GetType().Name + " " + wrapper.GetSpan(), MessageBoxButtons.OK);
                        }

                        // Add the menu strip if any items were added
                        if (menuStrip.Items.Count > 0)
                        {
                            e.MenuStrip = menuStrip;
                        }
                    }
                }
            };

            // Create columns
            _treeList.Columns.Add(new OLVColumn("Kind", null)
            {
                AspectGetter            = x => x,
                AspectToStringConverter = x => ((SyntaxWrapper)x).GetTreeText()
            });
            _treeList.Columns.Add(new OLVColumn("Span", null)
            {
                AspectGetter            = x => x,
                AspectToStringConverter = x => ((SyntaxWrapper)x).GetSpan()
            });
            _treeList.Columns.Add(new OLVColumn("Text", null)
            {
                AspectGetter            = x => x,
                AspectToStringConverter = x => ((SyntaxWrapper)x).GetText()
            });

            // Set the root
            SyntaxNode root;
            int        depth = 0;

            if (_syntaxTree.TryGetRoot(out root))
            {
                SetRoots(declarationFilter);
                depth = GetDepth(root);
            }

            // Calculate control width
            AutoSizeColumns(_treeList, depth, true);
            _treeList.Layout += (x, e) => AutoSizeColumns(_treeList, depth, false);
        }