Exemple #1
0
 protected override void OnContextMenuOpening(ContextMenuEventArgs e)
 {
     if (ContextMenu == null)
     {
         var m = new CSharpSymbolContextMenu(_Bar._SemanticContext)
         {
             SyntaxNode = Node
         };
         m.AddNodeCommands();
         var s = Symbol;
         if (s != null)
         {
             m.Symbol = s;
             m.Items.Add(new Separator());
             m.AddAnalysisCommands();
             m.AddSymbolCommands();
             m.AddTitleItem(Node.GetDeclarationSignature());
         }
         m.PlacementTarget = this;
         m.Placement       = System.Windows.Controls.Primitives.PlacementMode.Bottom;
         ContextMenu       = m;
     }
     base.OnContextMenuOpening(e);
 }
Exemple #2
0
        static void ApplyClickAndGo(ISymbol symbol, ITextBuffer textBuffer, TextBlock description, IAsyncQuickInfoSession quickInfoSession)
        {
            if (symbol.Kind == SymbolKind.Namespace)
            {
                description.ToolTip     = R.T_Locations + symbol.DeclaringSyntaxReferences.Length;
                description.MouseEnter += HookEvents;
                return;
            }
            if (symbol.Kind == SymbolKind.Method)
            {
                if (((IMethodSymbol)symbol).MethodKind == MethodKind.LambdaMethod)
                {
                    using (var sbr = Microsoft.VisualStudio.Utilities.ReusableStringBuilder.AcquireDefault(30)) {
                        var sb = sbr.Resource;
                        sb.Append('(');
                        foreach (var item in ((IMethodSymbol)symbol).Parameters)
                        {
                            if (item.Ordinal > 0)
                            {
                                sb.Append(", ");
                            }
                            sb.Append(item.Type.ToDisplayString(CodeAnalysisHelper.QuickInfoSymbolDisplayFormat))
                            .Append(item.Type.GetParameterString())
                            .Append(' ')
                            .Append(item.Name);
                        }
                        sb.Append(')');
                        description.Append(sb.ToString(), ThemeHelper.DocumentTextBrush);
                    }
                }
            }
            description.UseDummyToolTip();
            if (symbol.HasSource() == false && symbol.ContainingType?.HasSource() == true)
            {
                // if the symbol is implicitly declared but its containing type is in source,
                // navigate to the containing type
                symbol = symbol.ContainingType;
            }
            description.MouseEnter += HookEvents;

            void HookEvents(object sender, MouseEventArgs e)
            {
                var s = sender as FrameworkElement;

                s.MouseEnter -= HookEvents;
                HighlightSymbol(sender, e);
                s.Cursor = Cursors.Hand;
                if (symbol.Kind != SymbolKind.Namespace)
                {
                    s.ToolTipOpening += ShowToolTip;
                    s.UseDummyToolTip();
                }
                s.MouseEnter         += HighlightSymbol;
                s.MouseLeave         += RemoveSymbolHighlight;
                s.MouseLeftButtonUp  += GoToSource;
                s.ContextMenuOpening += ShowContextMenu;
                s.ContextMenuClosing += ReleaseQuickInfo;
            }

            async void GoToSource(object sender, MouseButtonEventArgs e)
            {
                await quickInfoSession.DismissAsync();

                symbol.GoToDefinition();
            }

            void ShowToolTip(object sender, ToolTipEventArgs e)
            {
                var t = sender as TextBlock;

                t.ToolTip         = ShowSymbolLocation(symbol, symbol.HasSource() ? System.IO.Path.GetFileName(symbol.Locations[0].SourceTree.FilePath) : symbol.GetAssemblyModuleName());
                t.ToolTipOpening -= ShowToolTip;
            }

            void HighlightSymbol(object sender, EventArgs e)
            {
                ((TextBlock)sender).Background = (symbol.HasSource() ? SystemColors.HighlightBrush : SystemColors.GrayTextBrush).Alpha(0.3);
            }

            void RemoveSymbolHighlight(object sender, MouseEventArgs e)
            {
                ((TextBlock)sender).Background = Brushes.Transparent;
            }

            void ShowContextMenu(object sender, ContextMenuEventArgs e)
            {
                var s = sender as FrameworkElement;

                if (s.ContextMenu == null)
                {
                    var ctx = SemanticContext.GetOrCreateSingetonInstance(quickInfoSession.TextView as IWpfTextView);
                    SyncHelper.RunSync(() => ctx.UpdateAsync(textBuffer, default));
                    var m = new CSharpSymbolContextMenu(ctx)
                    {
                        Symbol     = symbol,
                        SyntaxNode = symbol.GetSyntaxNode()
                    };
                    m.AddAnalysisCommands();
                    if (m.HasItems)
                    {
                        m.Items.Add(new Separator());
                    }
                    m.AddSymbolNodeCommands();
                    m.AddTitleItem(symbol.GetOriginalName());
                    m.ItemClicked += HideQuickInfo;
                    s.ContextMenu  = m;
                }
                HoldQuickInfo(s, true);
                s.ContextMenu.IsOpen = true;
            }

            void ReleaseQuickInfo(object sender, ContextMenuEventArgs e)
            {
                HoldQuickInfo(sender as DependencyObject, false);
            }

            void HideQuickInfo(object sender, RoutedEventArgs e)
            {
                DismissQuickInfo(description);
            }
        }