AppKit.NSMenu CreateNSMenu (FixMenuDescriptor entrySet)
		{
			var menu = new AppKit.NSMenu ();
			foreach (var item in entrySet.Items) {
				if (item == FixMenuEntry.Separator) {
					menu.AddItem (AppKit.NSMenuItem.SeparatorItem);
					continue;
				}
				var subMenu = item as FixMenuDescriptor;
				if (subMenu != null) {
					var gtkSubMenu = new AppKit.NSMenuItem (item.Label.Replace ("_", ""));
					gtkSubMenu.Submenu = CreateNSMenu (subMenu);
					menu.AddItem (gtkSubMenu); 
					continue;
				}
				var menuItem = new AppKit.NSMenuItem (item.Label.Replace ("_", ""));
				menuItem.Activated += delegate {
					item.Action ();
				};
				menu.AddItem (menuItem); 
			}
			return menu;
		}
        static Menu CreateGtkMenu(FixMenuDescriptor entrySet)
        {
            var menu = new Menu();

            foreach (var item in entrySet.Items)
            {
                if (item == FixMenuEntry.Separator)
                {
                    menu.Add(new SeparatorMenuItem());
                    continue;
                }
                var subMenu = item as FixMenuDescriptor;
                if (subMenu != null)
                {
                    var gtkSubMenu = new Gtk.MenuItem(item.Label);
                    gtkSubMenu.Submenu = CreateGtkMenu(subMenu);
                    menu.Add(gtkSubMenu);
                    continue;
                }
                var menuItem = new Gtk.MenuItem(item.Label);
                menuItem.Activated += delegate {
                    item.Action();
                };
                menu.Add(menuItem);
            }
            return(menu);
        }
        void PopupQuickFixMenu(Gdk.EventButton evt, Action <FixMenuDescriptor> menuAction)
        {
            FixMenuDescriptor menu = new FixMenuDescriptor();
            var fixMenu            = menu;
            //ResolveResult resolveResult;
            //ICSharpCode.NRefactory.CSharp.AstNode node;
            int items = 0;

            //			if (AddPossibleNamespace != null) {
            //				AddPossibleNamespace (Editor, DocumentContext, menu);
            //				items = menu.Items.Count;
            //			}

            PopulateFixes(fixMenu, ref items);

            if (items == 0)
            {
                return;
            }
            Editor.SuppressTooltips = true;
            if (menuAction != null)
            {
                menuAction(menu);
            }

            var p = Editor.LocationToPoint(Editor.OffsetToLocation(currentSmartTagBegin));

            Gtk.Widget widget = Editor;
            var        rect   = new Gdk.Rectangle(
                (int)p.X + widget.Allocation.X,
                (int)p.Y + widget.Allocation.Y, 0, 0);

            ShowFixesMenu(widget, rect, menu);
        }
        ContextMenu CreateContextMenu(FixMenuDescriptor entrySet)
        {
            var menu = new ContextMenu();

            foreach (var item in entrySet.Items)
            {
                if (item == FixMenuEntry.Separator)
                {
                    menu.Items.Add(new SeparatorContextMenuItem());
                    continue;
                }

                var menuItem = new ContextMenuItem(item.Label);
                menuItem.Context = item.Action;
                var subMenu = item as FixMenuDescriptor;
                if (subMenu != null)
                {
                    menuItem.SubMenu = CreateContextMenu(subMenu);
                }
                else
                {
                    menuItem.Clicked += (object sender, ContextMenuItemClickedEventArgs e) => ((System.Action)((ContextMenuItem)sender).Context)();
                }
                menu.Items.Add(menuItem);
            }

            return(menu);
        }
        AppKit.NSMenu CreateNSMenu(FixMenuDescriptor entrySet)
        {
            var menu = new AppKit.NSMenu();

            foreach (var item in entrySet.Items)
            {
                if (item == FixMenuEntry.Separator)
                {
                    menu.AddItem(AppKit.NSMenuItem.SeparatorItem);
                    continue;
                }
                var subMenu = item as FixMenuDescriptor;
                if (subMenu != null)
                {
                    var gtkSubMenu = new AppKit.NSMenuItem(item.Label.Replace("_", ""));
                    gtkSubMenu.Submenu = CreateNSMenu(subMenu);
                    menu.AddItem(gtkSubMenu);
                    continue;
                }
                var menuItem = new AppKit.NSMenuItem(item.Label.Replace("_", ""));
                menuItem.Activated += delegate {
                    item.Action();
                };
                menu.AddItem(menuItem);
            }
            return(menu);
        }
Exemplo n.º 6
0
        bool ShowFixesMenu(Gtk.Widget parent, Gdk.Rectangle evt, FixMenuDescriptor entrySet)
        {
                        #if MAC
            parent.GrabFocus();
            int x, y;
            x = (int)evt.X;
            y = (int)evt.Y;

            Gtk.Application.Invoke(delegate {
                // Explicitly release the grab because the menu is shown on the mouse position, and the widget doesn't get the mouse release event
                Gdk.Pointer.Ungrab(Gtk.Global.CurrentEventTime);
                var menu      = CreateNSMenu(entrySet);
                menu.Delegate = new ClosingMenuDelegate(document.Editor);
                var nsview    = MonoDevelop.Components.Mac.GtkMacInterop.GetNSView(parent);
                var toplevel  = parent.Toplevel as Gtk.Window;
                int trans_x, trans_y;
                parent.TranslateCoordinates(toplevel, (int)x, (int)y, out trans_x, out trans_y);

                // Window coordinates in gtk are the same for cocoa, with the exception of the Y coordinate, that has to be flipped.
                var pt = new CoreGraphics.CGPoint((float)trans_x, (float)trans_y);
                int w, h;
                toplevel.GetSize(out w, out h);
                pt.Y = h - pt.Y;

                var tmp_event = AppKit.NSEvent.MouseEvent(AppKit.NSEventType.LeftMouseDown,
                                                          pt,
                                                          0, 0,
                                                          MonoDevelop.Components.Mac.GtkMacInterop.GetNSWindow(toplevel).WindowNumber,
                                                          null, 0, 0, 0);

                AppKit.NSMenu.PopUpContextMenu(menu, tmp_event, nsview);
            });
                        #else
            var menu = CreateGtkMenu(entrySet);
            menu.Events |= Gdk.EventMask.AllEventsMask;
            menu.SelectFirst(true);

            menu.Hidden += delegate {
                document.Editor.SuppressTooltips = false;
            };
            menu.ShowAll();
            menu.SelectFirst(true);
            menu.MotionNotifyEvent += (o, args) => {
                if (args.Event.Window == Editor.Parent.TextArea.GdkWindow)
                {
                    StartMenuCloseTimer();
                }
                else
                {
                    CancelMenuCloseTimer();
                }
            };

            GtkWorkarounds.ShowContextMenu(menu, parent, null, evt);
                        #endif
            return(true);
        }
 void CurrentSmartTagPopup()
 {
     CancelSmartTagPopupTimeout();
     smartTagPopupTimeoutId = GLib.Timeout.Add(menuTimeout, delegate {
         PopupQuickFixMenu(null, menu => {
             codeActionMenu = menu;
         });
         smartTagPopupTimeoutId = 0;
         return(false);
     });
 }
Exemplo n.º 8
0
        ContextMenu CreateContextMenu(FixMenuDescriptor entrySet)
        {
            var menu = new ContextMenu();

            foreach (var item in entrySet.Items)
            {
                if (item == FixMenuEntry.Separator)
                {
                    menu.Items.Add(new SeparatorContextMenuItem());
                    continue;
                }

                var menuItem = new ContextMenuItem(item.Label);
                menuItem.Context = item.Action;
                var subMenu = item as FixMenuDescriptor;
                if (subMenu != null)
                {
                    menuItem.SubMenu   = CreateContextMenu(subMenu);
                    menuItem.Selected += delegate(object sender, Xwt.Rectangle e) {
                        HidePreviewTooltip();
                    };
                    menuItem.Deselected += delegate { HidePreviewTooltip(); };
                }
                else
                {
                    menuItem.Clicked  += (object sender, ContextMenuItemClickedEventArgs e) => ((System.Action)((ContextMenuItem)sender).Context)();
                    menuItem.Selected += delegate(object sender, Xwt.Rectangle e) {
                        HidePreviewTooltip();
                        if (item.ShowPreviewTooltip != null)
                        {
                            item.ShowPreviewTooltip(e);
                        }
                    };
                    menuItem.Deselected += delegate { HidePreviewTooltip(); };
                }
                menu.Items.Add(menuItem);
            }
            menu.Closed += delegate { HidePreviewTooltip(); };
            return(menu);
        }
        bool ShowFixesMenu(Gtk.Widget parent, Gdk.Rectangle evt, FixMenuDescriptor entrySet)
        {
            if (parent == null || parent.GdkWindow == null)
            {
                Editor.SuppressTooltips = false;
                return(true);
            }

            try {
                parent.GrabFocus();
                int x, y;
                x = (int)evt.X;
                y = (int)evt.Y;

                // Explicitly release the grab because the menu is shown on the mouse position, and the widget doesn't get the mouse release event
                Gdk.Pointer.Ungrab(Gtk.Global.CurrentEventTime);

                var menu = CreateContextMenu(entrySet);
                menu.Show(parent, x, y, () => Editor.SuppressTooltips = false, true);
            } catch (Exception ex) {
                LoggingService.LogError("Error while context menu popup.", ex);
            }
            return(true);
        }
		bool ShowFixesMenu (Gtk.Widget parent, Gdk.Rectangle evt, FixMenuDescriptor entrySet)
		{
			if (parent == null || parent.GdkWindow == null) {
				Editor.SuppressTooltips = false;
				return true;
			}

			try {
				parent.GrabFocus ();
				int x, y;
				x = (int)evt.X;
				y = (int)evt.Y;

				// Explicitly release the grab because the menu is shown on the mouse position, and the widget doesn't get the mouse release event
				Gdk.Pointer.Ungrab (Gtk.Global.CurrentEventTime);

				var menu = CreateContextMenu (entrySet);
				menu.Show (parent, x, y, () => Editor.SuppressTooltips = false, true);
			} catch (Exception ex) {
				LoggingService.LogError ("Error while context menu popup.", ex);
			}
			return true;
		}
		void PopupQuickFixMenu (Gdk.EventButton evt, Action<FixMenuDescriptor> menuAction)
		{
			FixMenuDescriptor menu = new FixMenuDescriptor ();
			var fixMenu = menu;
			//ResolveResult resolveResult;
			//ICSharpCode.NRefactory.CSharp.AstNode node;
			int items = 0;

//			if (AddPossibleNamespace != null) {
//				AddPossibleNamespace (Editor, DocumentContext, menu);
//				items = menu.Items.Count;
//			}

			PopulateFixes (fixMenu, ref items);

			if (items == 0) {
				return;
			}
			Editor.SuppressTooltips = true;
			if (menuAction != null)
				menuAction (menu);

			var p = Editor.LocationToPoint (Editor.OffsetToLocation (currentSmartTagBegin));
			Gtk.Widget widget = Editor;
			var rect = new Gdk.Rectangle (
				(int)p.X + widget.Allocation.X,
				(int)p.Y + widget.Allocation.Y, 0, 0);

			ShowFixesMenu (widget, rect, menu);
		}
		ContextMenu CreateContextMenu (FixMenuDescriptor entrySet)
		{
			var menu = new ContextMenu ();
			foreach (var item in entrySet.Items) {
				if (item == FixMenuEntry.Separator) {
					menu.Items.Add (new SeparatorContextMenuItem ());
					continue;
				}

				var menuItem = new ContextMenuItem (item.Label);
				menuItem.Context = item.Action;
				var subMenu = item as FixMenuDescriptor;
				if (subMenu != null) {
					menuItem.SubMenu = CreateContextMenu (subMenu);
					menuItem.Selected += delegate (object sender, Xwt.Rectangle e) {
						HidePreviewTooltip ();
					};
					menuItem.Deselected += delegate { HidePreviewTooltip (); };
				} else {
					menuItem.Clicked += (object sender, ContextMenuItemClickedEventArgs e) => ((System.Action)((ContextMenuItem)sender).Context) ();
					menuItem.Selected += delegate (object sender, Xwt.Rectangle e) {
						HidePreviewTooltip ();
						if (item.ShowPreviewTooltip != null) {
							item.ShowPreviewTooltip (e);
						}
					};
					menuItem.Deselected += delegate { HidePreviewTooltip (); };
				}
				menu.Items.Add (menuItem);
			}
			menu.Closed += delegate { HidePreviewTooltip (); };
			return menu;
		}
Exemplo n.º 13
0
        void PopulateFixes(FixMenuDescriptor menu, ref int items)
        {
            int  mnemonic = 1;
            bool gotImportantFix = false, addedSeparator = false;

            foreach (var fix_ in GetCurrentFixes().CodeFixActions.OrderByDescending(i => Tuple.Create(IsAnalysisOrErrorFix(i.CodeAction), (int)0, GetUsage(i.CodeAction.EquivalenceKey))))
            {
                // filter out code actions that are already resolutions of a code issue
                if (IsAnalysisOrErrorFix(fix_.CodeAction))
                {
                    gotImportantFix = true;
                }
                if (!addedSeparator && gotImportantFix && !IsAnalysisOrErrorFix(fix_.CodeAction))
                {
                    menu.Add(FixMenuEntry.Separator);
                    addedSeparator = true;
                }

                var fix   = fix_;
                var label = CreateLabel(fix.CodeAction.Title, ref mnemonic);
                var thisInstanceMenuItem = new FixMenuEntry(label, async delegate {
                    await new ContextActionRunner(fix.CodeAction, Editor, DocumentContext).Run();
                    ConfirmUsage(fix.CodeAction.EquivalenceKey);
                });
                menu.Add(thisInstanceMenuItem);
                items++;
            }

            bool first = true;

            foreach (var fix in GetCurrentFixes().CodeRefactoringActions)
            {
                if (first)
                {
                    if (items > 0)
                    {
                        menu.Add(FixMenuEntry.Separator);
                    }
                    first = false;
                }

                var label = CreateLabel(fix.CodeAction.Title, ref mnemonic);
                var thisInstanceMenuItem = new FixMenuEntry(label, async delegate {
                    await new ContextActionRunner(fix.CodeAction, Editor, DocumentContext).Run();
                    ConfirmUsage(fix.CodeAction.EquivalenceKey);
                });
                menu.Add(thisInstanceMenuItem);
                items++;
            }

            first = false;

            var warningsAtCaret = (DocumentContext.AnalysisDocument.GetSemanticModelAsync().Result)
                                  .GetDiagnostics(new TextSpan(Editor.CaretOffset, 0))
                                  .Where(diag => diag.Severity == DiagnosticSeverity.Warning).ToList();

            foreach (var warning in warningsAtCaret)
            {
                var label   = GettextCatalog.GetString("_Options for \"{0}\"", warning.Descriptor.Title);
                var subMenu = new FixMenuDescriptor(label);
                if (first)
                {
                    menu.Add(FixMenuEntry.Separator);
                    first = false;
                }
                var menuItem = new FixMenuEntry(GettextCatalog.GetString("_Suppress with #pragma"),
                                                async delegate {
                    var fixes = await CSharpSuppressionFixProvider.Instance.GetSuppressionsAsync(DocumentContext.AnalysisDocument, new TextSpan(Editor.CaretOffset, 0), new [] { warning }, default(CancellationToken)).ConfigureAwait(false);
                    foreach (var f in fixes)
                    {
                        CodeDiagnosticDescriptor.RunAction(DocumentContext, f.Action, default(CancellationToken));
                    }
                }
                                                );
                subMenu.Add(menuItem);
                menu.Add(subMenu);
                items++;
            }

            foreach (var fix_ in GetCurrentFixes().DiagnosticsAtCaret)
            {
                var fix     = fix_;
                var label   = GettextCatalog.GetString("_Options for \"{0}\"", fix.GetMessage());
                var subMenu = new FixMenuDescriptor(label);

                CodeDiagnosticDescriptor descriptor = BuiltInCodeDiagnosticProvider.GetCodeDiagnosticDescriptor(fix.Id);
                if (descriptor == null)
                {
                    continue;
                }
                if (first)
                {
                    menu.Add(FixMenuEntry.Separator);
                    first = false;
                }
                //				if (inspector.CanSuppressWithAttribute) {
                //					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with attribute"),
                //						delegate {
                //
                //							inspector.SuppressWithAttribute (Editor, DocumentContext, GetTextSpan (fix.Item2));
                //						});
                //					subMenu.Add (menuItem);
                //				}

                if (descriptor.CanDisableWithPragma)
                {
                    var menuItem = new FixMenuEntry(GettextCatalog.GetString("_Suppress with #pragma"),
                                                    delegate {
                        descriptor.DisableWithPragma(Editor, DocumentContext, fix);
                    });
                    subMenu.Add(menuItem);
                    menuItem = new FixMenuEntry(GettextCatalog.GetString("_Suppress with file"),
                                                delegate {
                        descriptor.DisableWithFile(Editor, DocumentContext, fix);
                    });
                    subMenu.Add(menuItem);
                }
                var optionsMenuItem = new FixMenuEntry(GettextCatalog.GetString("_Configure Rule"),
                                                       delegate {
                    IdeApp.Workbench.ShowGlobalPreferencesDialog(null, "C#", dialog => {
                        var panel = dialog.GetPanel <CodeIssuePanel> ("C#");
                        if (panel == null)
                        {
                            return;
                        }
                        panel.Widget.SelectCodeIssue(descriptor.IdString);
                    });
                });
                subMenu.Add(optionsMenuItem);


                foreach (var fix2 in GetCurrentFixes().CodeFixActions.OrderByDescending(i => Tuple.Create(IsAnalysisOrErrorFix(i.CodeAction), (int)0, GetUsage(i.CodeAction.EquivalenceKey))))
                {
                    var provider = fix2.Diagnostic.GetCodeFixProvider().GetFixAllProvider();
                    if (provider == null)
                    {
                        continue;
                    }
                    if (!provider.GetSupportedFixAllScopes().Contains(FixAllScope.Document))
                    {
                        continue;
                    }
                    var subMenu2           = new FixMenuDescriptor(GettextCatalog.GetString("Fix all"));
                    var diagnosticAnalyzer = fix2.Diagnostic.GetCodeDiagnosticDescriptor(LanguageNames.CSharp).GetProvider();
                    if (!diagnosticAnalyzer.SupportedDiagnostics.Contains(fix.Descriptor))
                    {
                        continue;
                    }

                    var menuItem = new FixMenuEntry(
                        GettextCatalog.GetString("In _Document"),
                        async delegate {
                        var fixAllDiagnosticProvider = new FixAllDiagnosticProvider(diagnosticAnalyzer.SupportedDiagnostics.Select(d => d.Id).ToImmutableHashSet(), async(Microsoft.CodeAnalysis.Document doc, ImmutableHashSet <string> diagnostics, CancellationToken token) => {
                            var model = await doc.GetSemanticModelAsync(token);
                            var compilationWithAnalyzer = model.Compilation.WithAnalyzers(new [] { diagnosticAnalyzer }.ToImmutableArray(), null, token);

                            return(await compilationWithAnalyzer.GetAnalyzerSemanticDiagnosticsAsync(model, null, token));
                        }, (Project arg1, bool arg2, ImmutableHashSet <string> arg3, CancellationToken arg4) => {
                            return(Task.FromResult((IEnumerable <Diagnostic>) new Diagnostic [] { }));
                        });
                        var ctx = new FixAllContext(
                            this.DocumentContext.AnalysisDocument,
                            fix2.Diagnostic.GetCodeFixProvider(),
                            FixAllScope.Document,
                            fix2.CodeAction.EquivalenceKey,
                            diagnosticAnalyzer.SupportedDiagnostics.Select(d => d.Id),
                            fixAllDiagnosticProvider,
                            default(CancellationToken)
                            );
                        var fixAll = await provider.GetFixAsync(ctx);
                        using (var undo = Editor.OpenUndoGroup()) {
                            CodeDiagnosticDescriptor.RunAction(DocumentContext, fixAll, default(CancellationToken));
                        }
                    });
                    subMenu2.Add(menuItem);
                    subMenu.Add(FixMenuEntry.Separator);
                    subMenu.Add(subMenu2);
                }

                menu.Add(subMenu);
                items++;
            }
        }
		bool ShowFixesMenu (Gtk.Widget parent, Gdk.Rectangle evt, FixMenuDescriptor entrySet)
		{
			if (parent == null || parent.GdkWindow == null)
				return true;
			try {
				#if MAC
				parent.GrabFocus ();
				int x, y;
				x = (int)evt.X;
				y = (int)evt.Y;
				// Explicitly release the grab because the menu is shown on the mouse position, and the widget doesn't get the mouse release event
				Gdk.Pointer.Ungrab (Gtk.Global.CurrentEventTime);
				var menu = CreateNSMenu (entrySet);
				menu.Delegate = new ClosingMenuDelegate (document.Editor);
				var nsview = MonoDevelop.Components.Mac.GtkMacInterop.GetNSView (parent);
				var toplevel = parent.Toplevel as Gtk.Window;
				int trans_x, trans_y;
				parent.TranslateCoordinates (toplevel, (int)x, (int)y, out trans_x, out trans_y);

				// Window coordinates in gtk are the same for cocoa, with the exception of the Y coordinate, that has to be flipped.
				var pt = new CoreGraphics.CGPoint ((float)trans_x, (float)trans_y);
				int w,h;
				toplevel.GetSize (out w, out h);
				pt.Y = h - pt.Y;

				var tmp_event = AppKit.NSEvent.MouseEvent (AppKit.NSEventType.LeftMouseDown,
				pt,
				0, 0,
				MonoDevelop.Components.Mac.GtkMacInterop.GetNSWindow (toplevel).WindowNumber,
				null, 0, 0, 0);

				AppKit.NSMenu.PopUpContextMenu (menu, tmp_event, nsview);
				#else
				var menu = CreateGtkMenu (entrySet);
				menu.Events |= Gdk.EventMask.AllEventsMask;
				menu.SelectFirst (true);

				menu.Hidden += delegate {
					document.Editor.SuppressTooltips = false;
				};
				menu.ShowAll ();
				menu.SelectFirst (true);
				menu.MotionNotifyEvent += (o, args) => {
					if (args.Event.Window == Editor.Parent.TextArea.GdkWindow) {
						StartMenuCloseTimer ();
					} else {
						CancelMenuCloseTimer ();
					}
				};

				GtkWorkarounds.ShowContextMenu (menu, parent, null, evt);
				#endif
			} catch (Exception ex) {
				LoggingService.LogError ("Error while context menu popup.", ex);
			}
			return true;
		}
		static Menu CreateGtkMenu (FixMenuDescriptor entrySet)
		{
			var menu = new Menu ();
			foreach (var item in entrySet.Items) {
				if (item == FixMenuEntry.Separator) {
					menu.Add (new SeparatorMenuItem ()); 
					continue;
				}
				var subMenu = item as FixMenuDescriptor;
				if (subMenu != null) {
					var gtkSubMenu = new Gtk.MenuItem (item.Label);
					gtkSubMenu.Submenu = CreateGtkMenu (subMenu);
					menu.Add (gtkSubMenu); 
					continue;
				}
				var menuItem = new Gtk.MenuItem (item.Label);
				menuItem.Activated += delegate {
					item.Action ();
				};
				menu.Add (menuItem); 
			}
			return menu;
		}
        void PopulateFixes(FixMenuDescriptor menu, ref int items)
        {
            if (!RefactoringService.ShowFixes)
            {
                return;
            }
            int  mnemonic = 1;
            bool gotImportantFix = false, addedSeparator = false;
            var  fixesAdded = new List <string> ();

            foreach (var fix_ in Fixes.OrderByDescending(i => Tuple.Create(IsAnalysisOrErrorFix(i), (int)i.Severity, GetUsage(i.IdString))))
            {
                // filter out code actions that are already resolutions of a code issue
                if (fixesAdded.Any(f => fix_.IdString.IndexOf(f, StringComparison.Ordinal) >= 0))
                {
                    continue;
                }
                fixesAdded.Add(fix_.IdString);
                if (IsAnalysisOrErrorFix(fix_))
                {
                    gotImportantFix = true;
                }
                if (!addedSeparator && gotImportantFix && !IsAnalysisOrErrorFix(fix_))
                {
                    menu.Add(FixMenuEntry.Separator);
                    addedSeparator = true;
                }

                var fix          = fix_;
                var escapedLabel = fix.Title.Replace("_", "__");
                var label        = (mnemonic <= 10)
                                        ? "_" + (mnemonic++ % 10).ToString() + " " + escapedLabel
                                        : "  " + escapedLabel;
                var thisInstanceMenuItem = new FixMenuEntry(label, delegate {
                    new ContextActionRunner(fix, document, currentSmartTagBegin).Run(null, EventArgs.Empty);
                    ConfirmUsage(fix.IdString);
                });
                menu.Add(thisInstanceMenuItem);
                items++;
            }

            bool first             = true;
            var  settingsMenuFixes = Fixes
                                     .OfType <AnalysisContextActionProvider.AnalysisCodeAction> ()
                                     .Where(f => f.Result is InspectorResults)
                                     .GroupBy(f => ((InspectorResults)f.Result).Inspector);

            foreach (var analysisFixGroup_ in settingsMenuFixes)
            {
                var analysisFixGroup    = analysisFixGroup_;
                var arbitraryFixInGroup = analysisFixGroup.First();
                var ir = (InspectorResults)arbitraryFixInGroup.Result;

                if (first)
                {
                    menu.Add(FixMenuEntry.Separator);
                    first = false;
                }

                var subMenu = new FixMenuDescriptor();
                foreach (var analysisFix_ in analysisFixGroup)
                {
                    var analysisFix = analysisFix_;
                    if (analysisFix.SupportsBatchRunning)
                    {
                        var batchRunMenuItem = new FixMenuEntry(
                            string.Format(GettextCatalog.GetString("Apply in file: {0}"), analysisFix.Title),
                            delegate {
                            ConfirmUsage(analysisFix.IdString);
                            new ContextActionRunner(analysisFix, document, this.currentSmartTagBegin).BatchRun(null, EventArgs.Empty);
                        }
                            );
                        subMenu.Add(batchRunMenuItem);
                        subMenu.Add(FixMenuEntry.Separator);
                    }
                }

                var inspector = ir.Inspector;
                if (inspector.CanSuppressWithAttribute)
                {
                    var menuItem = new FixMenuEntry(GettextCatalog.GetString("_Suppress with attribute"),
                                                    delegate {
                        inspector.SuppressWithAttribute(document, arbitraryFixInGroup.DocumentRegion);
                    });
                    subMenu.Add(menuItem);
                }

                if (inspector.CanDisableWithPragma)
                {
                    var menuItem = new FixMenuEntry(GettextCatalog.GetString("_Suppress with #pragma"),
                                                    delegate {
                        inspector.DisableWithPragma(document, arbitraryFixInGroup.DocumentRegion);
                    });
                    subMenu.Add(menuItem);
                }

                if (inspector.CanDisableOnce)
                {
                    var menuItem = new FixMenuEntry(GettextCatalog.GetString("_Disable Once"),
                                                    delegate {
                        inspector.DisableOnce(document, arbitraryFixInGroup.DocumentRegion);
                    });
                    subMenu.Add(menuItem);
                }

                if (inspector.CanDisableAndRestore)
                {
                    var menuItem = new FixMenuEntry(GettextCatalog.GetString("Disable _and Restore"),
                                                    delegate {
                        inspector.DisableAndRestore(document, arbitraryFixInGroup.DocumentRegion);
                    });
                    subMenu.Add(menuItem);
                }
                var label       = GettextCatalog.GetString("_Options for \"{0}\"", InspectorResults.GetTitle(ir.Inspector));
                var subMenuItem = new FixMenuDescriptor(label);

                var optionsMenuItem = new FixMenuEntry(GettextCatalog.GetString("_Configure Rule"),
                                                       delegate {
                    arbitraryFixInGroup.ShowOptions(null, EventArgs.Empty);
                });
                subMenuItem.Add(optionsMenuItem);

                menu.Add(subMenuItem);
                items++;
            }
        }
Exemplo n.º 17
0
		void CurrentSmartTagPopup ()
		{
			CancelSmartTagPopupTimeout ();
			smartTagPopupTimeoutId = GLib.Timeout.Add (menuTimeout, delegate {
				PopupQuickFixMenu (null, menu => {
					codeActionMenu = menu;
				});
				smartTagPopupTimeoutId = 0;
				return false;
			});
		}
		ContextMenu CreateContextMenu (FixMenuDescriptor entrySet)
		{
			var menu = new ContextMenu ();

			foreach (var item in entrySet.Items) {
				if (item == FixMenuEntry.Separator) {
					menu.Items.Add (new SeparatorContextMenuItem ());
					continue;
				}

				var menuItem = new ContextMenuItem (item.Label);
				menuItem.Context = item.Action;
				var subMenu = item as FixMenuDescriptor;
				if (subMenu != null) {
					menuItem.SubMenu = CreateContextMenu (subMenu);
				} else {
					menuItem.Clicked += (object sender, ContextMenuItemClickedEventArgs e) => ((System.Action)((ContextMenuItem)sender).Context) ();
				}
				menu.Items.Add (menuItem);
			}

			return menu;
		}
		void PopupQuickFixMenu (Gdk.EventButton evt, Action<FixMenuDescriptor> menuAction)
		{
			FixMenuDescriptor menu = new FixMenuDescriptor ();
			var fixMenu = menu;
			ResolveResult resolveResult;
			ICSharpCode.NRefactory.CSharp.AstNode node;
			int items = 0;
			if (ResolveCommandHandler.ResolveAt (document, out resolveResult, out node)) {
				var possibleNamespaces = MonoDevelop.Refactoring.ResolveCommandHandler.GetPossibleNamespaces (
					document,
					node,
					ref resolveResult
				);

				foreach (var t in possibleNamespaces.Where (tp => tp.OnlyAddReference)) {
					menu.Add (new FixMenuEntry (t.GetImportText (), delegate {
						new ResolveCommandHandler.AddImport (document, resolveResult, null, t.Reference, true, node).Run ();
					}));
					items++;
				}

				bool addUsing = !(resolveResult is AmbiguousTypeResolveResult);
				if (addUsing) {
					foreach (var t in possibleNamespaces.Where (tp => tp.IsAccessibleWithGlobalUsing)) {
						string ns = t.Namespace;
						var reference = t.Reference;
						menu.Add (new FixMenuEntry (t.GetImportText (), 
							delegate {
								new ResolveCommandHandler.AddImport (document, resolveResult, ns, reference, true, node).Run ();
							})
						);
						items++;
					}
				}

				bool resolveDirect = !(resolveResult is UnknownMemberResolveResult);
				if (resolveDirect) {
					foreach (var t in possibleNamespaces) {
						string ns = t.Namespace;
						var reference = t.Reference;
						menu.Add (new FixMenuEntry (t.GetInsertNamespaceText (document.Editor.GetTextBetween (node.StartLocation, node.EndLocation)),
							delegate {
							new ResolveCommandHandler.AddImport (document, resolveResult, ns, reference, false, node).Run ();
						}));
						items++;
					}
				}

				if (menu.Items.Any () && Fixes.Any ()) {
					fixMenu = new FixMenuDescriptor (GettextCatalog.GetString ("Quick Fixes"));
					menu.Add (fixMenu);
					items++;
				}
			}
			PopulateFixes (fixMenu, ref items);
			if (items == 0) {
				return;
			}
			document.Editor.SuppressTooltips = true;
			document.Editor.Parent.HideTooltip ();
			if (menuAction != null)
				menuAction (menu);
			var container = document.Editor.Parent;

			var p = container.LocationToPoint (currentSmartTagBegin);
			var rect = new Gdk.Rectangle (
				p.X + container.Allocation.X , 
				p.Y + (int)document.Editor.LineHeight + container.Allocation.Y, 0, 0);

			ShowFixesMenu (document.Editor.Parent, rect, menu);
		}
		void PopulateFixes (FixMenuDescriptor menu, ref int items)
		{
			if (!RefactoringService.ShowFixes)
				return;
			int mnemonic = 1;
			bool gotImportantFix = false, addedSeparator = false;
			var fixesAdded = new List<string> ();
			foreach (var fix_ in Fixes.OrderByDescending (i => Tuple.Create (IsAnalysisOrErrorFix(i), (int)i.Severity, GetUsage (i.IdString)))) {
				// filter out code actions that are already resolutions of a code issue
				if (fixesAdded.Any (f => fix_.IdString.IndexOf (f, StringComparison.Ordinal) >= 0))
					continue;
				fixesAdded.Add (fix_.IdString);
				if (IsAnalysisOrErrorFix (fix_))
					gotImportantFix = true;
				if (!addedSeparator && gotImportantFix && !IsAnalysisOrErrorFix(fix_)) {
					menu.Add (FixMenuEntry.Separator);
					addedSeparator = true;
				}

				var fix = fix_;
				var escapedLabel = fix.Title.Replace ("_", "__");
				var label = (mnemonic <= 10)
					? "_" + (mnemonic++ % 10).ToString () + " " + escapedLabel
					: "  " + escapedLabel;
				var thisInstanceMenuItem = new FixMenuEntry (label, delegate {
					new ContextActionRunner (fix, document, currentSmartTagBegin).Run (null, EventArgs.Empty);
					ConfirmUsage (fix.IdString);
				});
				menu.Add (thisInstanceMenuItem);
				items++;
			}

			bool first = true;
			var settingsMenuFixes = Fixes
				.OfType<AnalysisContextActionProvider.AnalysisCodeAction> ()
				.Where (f => f.Result is InspectorResults)
				.GroupBy (f => ((InspectorResults)f.Result).Inspector);
			foreach (var analysisFixGroup_ in settingsMenuFixes) {
				var analysisFixGroup = analysisFixGroup_;
				var arbitraryFixInGroup = analysisFixGroup.First ();
				var ir = (InspectorResults)arbitraryFixInGroup.Result;

				if (first) {
					menu.Add (FixMenuEntry.Separator);
					first = false;
				}

				var subMenu = new FixMenuDescriptor ();
				foreach (var analysisFix_ in analysisFixGroup) {
					var analysisFix = analysisFix_;
					if (analysisFix.SupportsBatchRunning) {
						var batchRunMenuItem = new FixMenuEntry (
							string.Format (GettextCatalog.GetString ("Apply in file: {0}"), analysisFix.Title), 
							delegate {
								ConfirmUsage (analysisFix.IdString);
								new ContextActionRunner (analysisFix, document, this.currentSmartTagBegin).BatchRun (null, EventArgs.Empty);
							}
						);
						subMenu.Add (batchRunMenuItem);
						subMenu.Add (FixMenuEntry.Separator);
					}
				}

				var inspector = ir.Inspector;
				if (inspector.CanSuppressWithAttribute) {
					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with attribute"),
						delegate {
							inspector.SuppressWithAttribute (document, arbitraryFixInGroup.DocumentRegion); 
						});
					subMenu.Add (menuItem);
				}

				if (inspector.CanDisableWithPragma) {
					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with #pragma"),
						delegate {
							inspector.DisableWithPragma (document, arbitraryFixInGroup.DocumentRegion); 
						});
					subMenu.Add (menuItem);
				}

				if (inspector.CanDisableOnce) {
					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Disable Once"),
						delegate {
							inspector.DisableOnce (document, arbitraryFixInGroup.DocumentRegion); 
						});
					subMenu.Add (menuItem);
				}

				if (inspector.CanDisableAndRestore) {
					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("Disable _and Restore"),
						delegate {
							inspector.DisableAndRestore (document, arbitraryFixInGroup.DocumentRegion); 
						});
					subMenu.Add (menuItem);
				}
				var label = GettextCatalog.GetString ("_Options for \"{0}\"", InspectorResults.GetTitle (ir.Inspector));
				var subMenuItem = new FixMenuDescriptor (label);

				var optionsMenuItem = new FixMenuEntry (GettextCatalog.GetString ("_Configure Rule"),
					delegate {
						arbitraryFixInGroup.ShowOptions (null, EventArgs.Empty);
					});
				subMenuItem.Add (optionsMenuItem);

				menu.Add (subMenuItem);
				items++;
			}
		}
		void PopulateFixes (FixMenuDescriptor menu, ref int items)
		{
			int mnemonic = 1;
			bool gotImportantFix = false, addedSeparator = false;

			foreach (var fix_ in GetCurrentFixes ().CodeFixActions.OrderByDescending (i => Tuple.Create (IsAnalysisOrErrorFix (i.CodeAction), (int)0, GetUsage (i.CodeAction.EquivalenceKey)))) {
				// filter out code actions that are already resolutions of a code issue
				if (IsAnalysisOrErrorFix (fix_.CodeAction))
					gotImportantFix = true;
				if (!addedSeparator && gotImportantFix && !IsAnalysisOrErrorFix (fix_.CodeAction)) {
					menu.Add (FixMenuEntry.Separator);
					addedSeparator = true;
				}

				var fix = fix_;
				var label = CreateLabel (fix.CodeAction.Title, ref mnemonic);
				var thisInstanceMenuItem = new FixMenuEntry (label, delegate {
					new ContextActionRunner (fix.CodeAction, Editor, DocumentContext).Run (null, EventArgs.Empty);
					ConfirmUsage (fix.CodeAction.EquivalenceKey);
				});
				menu.Add (thisInstanceMenuItem);
				items++;
			}

			bool first = true;
			foreach (var fix in GetCurrentFixes ().CodeRefactoringActions) {
				if (first) {
					if (items > 0)
						menu.Add (FixMenuEntry.Separator);
					first = false;
				}

				var label = CreateLabel (fix.CodeAction.Title, ref mnemonic);
				var thisInstanceMenuItem = new FixMenuEntry (label, delegate {
					new ContextActionRunner (fix.CodeAction, Editor, DocumentContext).Run (null, EventArgs.Empty);
					ConfirmUsage (fix.CodeAction.EquivalenceKey);
				});
				menu.Add (thisInstanceMenuItem);
				items++;
			}

			first = false;
			foreach (var fix_ in GetCurrentFixes ().DiagnosticsAtCaret) {
				var fix = fix_;
				var label = GettextCatalog.GetString ("_Options for \"{0}\"", fix.GetMessage ());
				var subMenu = new FixMenuDescriptor (label);

				CodeDiagnosticDescriptor descriptor = BuiltInCodeDiagnosticProvider.GetCodeDiagnosticDescriptor (fix.Id);
				if (descriptor == null)
					continue;
				if (first) {
					menu.Add (FixMenuEntry.Separator);
					first = false;
				}
				//				if (inspector.CanSuppressWithAttribute) {
				//					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with attribute"),
				//						delegate {
				//							
				//							inspector.SuppressWithAttribute (Editor, DocumentContext, GetTextSpan (fix.Item2)); 
				//						});
				//					subMenu.Add (menuItem);
				//				}

				if (descriptor.CanDisableWithPragma) {
					var menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with #pragma"),
													 delegate {
														 descriptor.DisableWithPragma (Editor, DocumentContext, fix);
													 });
					subMenu.Add (menuItem);
					menuItem = new FixMenuEntry (GettextCatalog.GetString ("_Suppress with file"),
						delegate {
							descriptor.DisableWithFile (Editor, DocumentContext, fix);
						});
					subMenu.Add (menuItem);
				}
				var optionsMenuItem = new FixMenuEntry (GettextCatalog.GetString ("_Configure Rule"),
					delegate {
						IdeApp.Workbench.ShowGlobalPreferencesDialog (null, "C#", dialog => {
							var panel = dialog.GetPanel<CodeIssuePanel> ("C#");
							if (panel == null)
								return;
							panel.Widget.SelectCodeIssue (descriptor.IdString);
						});
					});
				subMenu.Add (optionsMenuItem);


				foreach (var fix2 in GetCurrentFixes ().CodeFixActions.OrderByDescending (i => Tuple.Create (IsAnalysisOrErrorFix (i.CodeAction), (int)0, GetUsage (i.CodeAction.EquivalenceKey)))) {

					var provider = fix2.Diagnostic.GetCodeFixProvider ().GetFixAllProvider ();
					if (provider == null)
						continue;
					if (!provider.GetSupportedFixAllScopes ().Contains (FixAllScope.Document))
						continue;
					var subMenu2 = new FixMenuDescriptor (GettextCatalog.GetString ("Fix all"));
					var diagnosticAnalyzer = fix2.Diagnostic.GetCodeDiagnosticDescriptor (LanguageNames.CSharp).GetProvider ();
					if (!diagnosticAnalyzer.SupportedDiagnostics.Contains (fix.Descriptor))
						continue;

					var menuItem = new FixMenuEntry (
						GettextCatalog.GetString ("In _Document"),
						async delegate {
							var fixAllDiagnosticProvider = new FixAllDiagnosticProvider (diagnosticAnalyzer.SupportedDiagnostics.Select (d => d.Id).ToImmutableHashSet (), async (Microsoft.CodeAnalysis.Document doc, ImmutableHashSet<string> diagnostics, CancellationToken token) => {

								var model = await doc.GetSemanticModelAsync (token);
								var compilationWithAnalyzer = model.Compilation.WithAnalyzers (new [] { diagnosticAnalyzer }.ToImmutableArray (), null, token);

								return await compilationWithAnalyzer.GetAnalyzerSemanticDiagnosticsAsync (model, null, token);
							}, (Project arg1, bool arg2, ImmutableHashSet<string> arg3, CancellationToken arg4) => {
								return Task.FromResult ((IEnumerable<Diagnostic>)new Diagnostic[] { });
							});
							var ctx = new FixAllContext (
								this.DocumentContext.AnalysisDocument,
								fix2.Diagnostic.GetCodeFixProvider (),
								FixAllScope.Document,
								fix2.CodeAction.EquivalenceKey,
								diagnosticAnalyzer.SupportedDiagnostics.Select (d => d.Id),
								fixAllDiagnosticProvider,
								default (CancellationToken)
							);
							var fixAll = await provider.GetFixAsync (ctx);
							using (var undo = Editor.OpenUndoGroup ()) {
								CodeDiagnosticDescriptor.RunAction (DocumentContext, fixAll, default (CancellationToken));
							}
						});
					subMenu2.Add (menuItem);
					subMenu.Add (FixMenuEntry.Separator); 
					subMenu.Add (subMenu2);
				}

				menu.Add (subMenu);
				items++;
			}
		}
        void PopupQuickFixMenu(Gdk.EventButton evt, Action <FixMenuDescriptor> menuAction)
        {
            FixMenuDescriptor menu = new FixMenuDescriptor();
            var           fixMenu  = menu;
            ResolveResult resolveResult;

            ICSharpCode.NRefactory.CSharp.AstNode node;
            int items = 0;

            if (ResolveCommandHandler.ResolveAt(document, out resolveResult, out node))
            {
                var possibleNamespaces = MonoDevelop.Refactoring.ResolveCommandHandler.GetPossibleNamespaces(
                    document,
                    node,
                    ref resolveResult
                    );

                foreach (var t in possibleNamespaces.Where(tp => tp.OnlyAddReference))
                {
                    menu.Add(new FixMenuEntry(t.GetImportText(), delegate {
                        new ResolveCommandHandler.AddImport(document, resolveResult, null, t.Reference, true, node).Run();
                    }));
                    items++;
                }

                bool addUsing = !(resolveResult is AmbiguousTypeResolveResult);
                if (addUsing)
                {
                    foreach (var t in possibleNamespaces.Where(tp => tp.IsAccessibleWithGlobalUsing))
                    {
                        string ns        = t.Namespace;
                        var    reference = t.Reference;
                        menu.Add(new FixMenuEntry(t.GetImportText(),
                                                  delegate {
                            new ResolveCommandHandler.AddImport(document, resolveResult, ns, reference, true, node).Run();
                        })
                                 );
                        items++;
                    }
                }

                bool resolveDirect = !(resolveResult is UnknownMemberResolveResult);
                if (resolveDirect)
                {
                    foreach (var t in possibleNamespaces)
                    {
                        string ns        = t.Namespace;
                        var    reference = t.Reference;
                        menu.Add(new FixMenuEntry(t.GetInsertNamespaceText(document.Editor.GetTextBetween(node.StartLocation, node.EndLocation)),
                                                  delegate {
                            new ResolveCommandHandler.AddImport(document, resolveResult, ns, reference, false, node).Run();
                        }));
                        items++;
                    }
                }

                if (menu.Items.Any() && Fixes.Any())
                {
                    fixMenu = new FixMenuDescriptor(GettextCatalog.GetString("Quick Fixes"));
                    menu.Add(fixMenu);
                    items++;
                }
            }
            PopulateFixes(fixMenu, ref items);
            if (items == 0)
            {
                return;
            }
            document.Editor.SuppressTooltips = true;
            document.Editor.Parent.HideTooltip();
            if (menuAction != null)
            {
                menuAction(menu);
            }
            var container = document.Editor.Parent;

            var p    = container.LocationToPoint(currentSmartTagBegin);
            var rect = new Gdk.Rectangle(
                p.X + container.Allocation.X,
                p.Y + (int)document.Editor.LineHeight + container.Allocation.Y, 0, 0);

            ShowFixesMenu(document.Editor.Parent, rect, menu);
        }