bool ShowFixesMenu(Widget parent, Gdk.Rectangle evt, CodeFixMenu entrySet) { if (parent == null || parent.GdkWindow == null) { Editor.SuppressTooltips = false; return(true); } try { parent.GrabFocus(); int x, y; x = evt.X; y = 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); RefactoringPreviewTooltipWindow.HidePreviewTooltip(); menu.Show(parent, x, y, () => { Editor.SuppressTooltips = false; RefactoringPreviewTooltipWindow.HidePreviewTooltip(); FixesMenuClosed?.Invoke(this, EventArgs.Empty); }, true); } catch (Exception ex) { LoggingService.LogError("Error while context menu popup.", ex); } return(true); }
public override void Dispose() { CancelQuickFixTimer(); RefactoringPreviewTooltipWindow.HidePreviewTooltip(); Editor.CaretPositionChanged -= HandleCaretPositionChanged; DocumentContext.DocumentParsed -= HandleDocumentDocumentParsed; Editor.TextChanged -= Editor_TextChanged; Editor.BeginAtomicUndoOperation -= Editor_BeginAtomicUndoOperation; Editor.EndAtomicUndoOperation -= Editor_EndAtomicUndoOperation; RemoveWidget(); base.Dispose(); }
static CodeFixMenuEntry CreateFixMenuEntry(Ide.Editor.TextEditor editor, CodeAction fix, ref int mnemonic) { var label = mnemonic < 0 ? fix.Title : CreateLabel(fix.Title, ref mnemonic); var item = new CodeFixMenuEntry(label, async delegate { await new ContextActionRunner(editor, fix).Run(); }); item.ShowPreviewTooltip = delegate(Xwt.Rectangle rect) { RefactoringPreviewTooltipWindow.ShowPreviewTooltip(editor, fix, rect); }; return(item); }
ContextMenu CreateContextMenu(CodeFixMenu entrySet) { var menu = new ContextMenu(); foreach (var item in entrySet.Items) { if (item == CodeFixMenuEntry.Separator) { menu.Items.Add(new SeparatorContextMenuItem()); continue; } var menuItem = new ContextMenuItem(item.Label); menuItem.Context = item.Action; if (item.Action == null) { if (!(item is CodeFixMenu itemAsMenu) || itemAsMenu.Items.Count <= 0) { menuItem.Sensitive = false; } } var subMenu = item as CodeFixMenu; if (subMenu != null) { menuItem.SubMenu = CreateContextMenu(subMenu); menuItem.Selected += delegate { RefactoringPreviewTooltipWindow.HidePreviewTooltip(); }; menuItem.Deselected += delegate { RefactoringPreviewTooltipWindow.HidePreviewTooltip(); }; } else { menuItem.Clicked += (sender, e) => ((System.Action)((ContextMenuItem)sender).Context)(); menuItem.Selected += (sender, e) => { RefactoringPreviewTooltipWindow.HidePreviewTooltip(); if (item.ShowPreviewTooltip != null) { item.ShowPreviewTooltip(e); } }; menuItem.Deselected += delegate { RefactoringPreviewTooltipWindow.HidePreviewTooltip(); }; } menu.Items.Add(menuItem); } menu.Closed += delegate { RefactoringPreviewTooltipWindow.HidePreviewTooltip(); }; return(menu); }
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); }); thisInstanceMenuItem.ShowPreviewTooltip = delegate(Xwt.Rectangle rect) { HidePreviewTooltip(); currentPreviewWindow = new RefactoringPreviewTooltipWindow(this.Editor, this.DocumentContext, fix.CodeAction); currentPreviewWindow.RequestPopup(rect); }; 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); }); thisInstanceMenuItem.ShowPreviewTooltip = delegate(Xwt.Rectangle rect) { HidePreviewTooltip(); currentPreviewWindow = new RefactoringPreviewTooltipWindow(this.Editor, this.DocumentContext, fix.CodeAction); currentPreviewWindow.RequestPopup(rect); }; 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 \u2018{0}\u2019", 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)); } } ); menuItem.ShowPreviewTooltip = async delegate(Xwt.Rectangle rect) { var fixes = await CSharpSuppressionFixProvider.Instance.GetSuppressionsAsync(DocumentContext.AnalysisDocument, new TextSpan (Editor.CaretOffset, 0), new [] { warning }, default(CancellationToken)).ConfigureAwait(false); HidePreviewTooltip(); var fix = fixes.FirstOrDefault(); if (fix == null) { return; } currentPreviewWindow = new RefactoringPreviewTooltipWindow(this.Editor, this.DocumentContext, fix.Action); currentPreviewWindow.RequestPopup(rect); }; subMenu.Add(menuItem); menu.Add(subMenu); items++; } foreach (var fix_ in GetCurrentFixes().DiagnosticsAtCaret) { var fix = fix_; var label = GettextCatalog.GetString("_Options for \u2018{0}\u2019", 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); }); menuItem.ShowPreviewTooltip = async delegate(Xwt.Rectangle rect) { var fixes = await CSharpSuppressionFixProvider.Instance.GetSuppressionsAsync(DocumentContext.AnalysisDocument, new TextSpan (Editor.CaretOffset, 0), new [] { fix }, default(CancellationToken)).ConfigureAwait(false); HidePreviewTooltip(); var fix2 = fixes.FirstOrDefault(); if (fix2 == null) { return; } currentPreviewWindow = new RefactoringPreviewTooltipWindow(this.Editor, this.DocumentContext, fix2.Action); currentPreviewWindow.RequestPopup(rect); }; 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++; } }